Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <common/args.h>
6 : : #include <i2p.h>
7 : : #include <netaddress.h>
8 : : #include <netbase.h>
9 : : #include <test/fuzz/FuzzedDataProvider.h>
10 : : #include <test/fuzz/fuzz.h>
11 : : #include <test/fuzz/util.h>
12 : : #include <test/fuzz/util/net.h>
13 : : #include <test/util/setup_common.h>
14 : : #include <util/fs_helpers.h>
15 : : #include <util/threadinterrupt.h>
16 : :
17 : 1 : void initialize_i2p()
18 : : {
19 [ + - + - ]: 2 : static const auto testing_setup = MakeNoLogFileContext<>();
20 [ + - ]: 2 : }
21 : :
22 [ + - ]: 1113 : FUZZ_TARGET(i2p, .init = initialize_i2p)
23 : : {
24 : 681 : SeedRandomStateForTest(SeedRand::ZEROS);
25 : 681 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
26 : :
27 : 681 : SetMockTime(ConsumeTime(fuzzed_data_provider));
28 : :
29 : : // Mock CreateSock() to create FuzzedSock.
30 : 681 : auto CreateSockOrig = CreateSock;
31 : 2365 : CreateSock = [&fuzzed_data_provider](int, int, int) {
32 : 1684 : return std::make_unique<FuzzedSock>(fuzzed_data_provider);
33 : 681 : };
34 : :
35 [ + - + - ]: 1362 : const fs::path private_key_path = gArgs.GetDataDirNet() / "fuzzed_i2p_private_key";
36 [ + - ]: 681 : const CService addr{in6_addr(IN6ADDR_LOOPBACK_INIT), 7656};
37 : 681 : const Proxy sam_proxy{addr, false};
38 [ + - ]: 681 : CThreadInterrupt interrupt;
39 : :
40 [ + - ]: 681 : i2p::sam::Session session{private_key_path, sam_proxy, &interrupt};
41 [ + - ]: 681 : i2p::Connection conn;
42 : :
43 [ + - + + ]: 681 : if (session.Listen(conn)) {
44 [ + - + + ]: 121 : if (session.Accept(conn)) {
45 : 37 : try {
46 [ + + ]: 69 : (void)conn.sock->RecvUntilTerminator('\n', 10ms, interrupt, i2p::sam::MAX_MSG_SIZE);
47 [ - + ]: 5 : } catch (const std::runtime_error&) {
48 : 5 : }
49 : : }
50 : : }
51 : :
52 : 681 : bool proxy_error;
53 : :
54 [ + - + - : 681 : if (session.Connect(CService{}, conn, proxy_error)) {
+ + ]
55 : 19 : try {
56 [ + + ]: 19 : conn.sock->SendComplete("verack\n", 10ms, interrupt);
57 [ - + ]: 9 : } catch (const std::runtime_error&) {
58 : 9 : }
59 : : }
60 : :
61 [ + - ]: 681 : fs::remove_all(private_key_path);
62 : :
63 [ + - ]: 681 : CreateSock = CreateSockOrig;
64 : 2043 : }
|