Bitcoin Core  26.1.0
P2P Digital Currency
i2p_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 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 <logging.h>
8 #include <netaddress.h>
9 #include <test/util/logging.h>
10 #include <test/util/net.h>
11 #include <test/util/setup_common.h>
12 #include <util/readwritefile.h>
13 #include <util/threadinterrupt.h>
14 
15 #include <boost/test/unit_test.hpp>
16 
17 #include <memory>
18 #include <string>
19 
22 {
23 public:
24  explicit EnvTestingSetup(const ChainType chainType = ChainType::MAIN,
25  const std::vector<const char*>& extra_args = {})
26  : BasicTestingSetup{chainType, extra_args},
29  {
31  }
32 
34  {
37  }
38 
39 private:
41  const std::function<std::unique_ptr<Sock>(const CService&)> m_create_sock_orig;
42 };
43 
44 BOOST_FIXTURE_TEST_SUITE(i2p_tests, EnvTestingSetup)
45 
46 BOOST_AUTO_TEST_CASE(unlimited_recv)
47 {
48  // Mock CreateSock() to create MockSock.
49  CreateSock = [](const CService&) {
50  return std::make_unique<StaticContentsSock>(std::string(i2p::sam::MAX_MSG_SIZE + 1, 'a'));
51  };
52 
53  CThreadInterrupt interrupt;
54  i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key", CService{}, &interrupt);
55 
56  {
57  ASSERT_DEBUG_LOG("Creating persistent SAM session");
58  ASSERT_DEBUG_LOG("too many bytes without a terminator");
59 
60  i2p::Connection conn;
61  bool proxy_error;
62  BOOST_REQUIRE(!session.Connect(CService{}, conn, proxy_error));
63  }
64 }
65 
66 BOOST_AUTO_TEST_CASE(listen_ok_accept_fail)
67 {
68  size_t num_sockets{0};
69  CreateSock = [&num_sockets](const CService&) {
70  // clang-format off
71  ++num_sockets;
72  // First socket is the control socket for creating the session.
73  if (num_sockets == 1) {
74  return std::make_unique<StaticContentsSock>(
75  // reply to HELLO
76  "HELLO REPLY RESULT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fw89rBQAEAAcAAOvuCIKTyv5f~1QgGq7XQl-IqBULTB5WzB3gw5yGPtd1p0AeoADrq1ccZggLPQ4ZLUsGK-HVw373rcTfvxrcuwenqVjiN4tbbYLWtP7xXGWj6fM6HyORhU63GphrjEePpMUHDHXd3o7pWGM-ieVVQSK~1MzF9P93pQWI3Do52EeNAayz4HbpPjNhVBzG1hUEFwznfPmUZBPuaOR4-uBm1NEWEuONlNOCctE4-U0Ukh94z-Qb55U5vXjR5G4apmBblr68t6Wm1TKlzpgFHzSqLryh3stWqrOKY1H0z9eZ2z1EkHFOpD5LyF6nf51e-lV7HLMl44TYzoEHK8RRVodtLcW9lacVdBpv~tOzlZERIiDziZODPETENZMz5oy9DQ7UUw==\n"
79  // reply to SESSION CREATE
80  "SESSION STATUS RESULT=OK\n"
81  // dummy to avoid reporting EOF on the socket
82  "a"
83  );
84  }
85  // Subsequent sockets are for recreating the session or for listening and accepting incoming connections.
86  if (num_sockets % 2 == 0) {
87  // Replies to Listen() and Accept()
88  return std::make_unique<StaticContentsSock>(
89  // reply to HELLO
90  "HELLO REPLY RESULT=OK VERSION=3.1\n"
91  // reply to STREAM ACCEPT
92  "STREAM STATUS RESULT=OK\n"
93  // continued reply to STREAM ACCEPT, violating the protocol described at
94  // https://geti2p.net/en/docs/api/samv3#Accept%20Response
95  // should be base64, something like
96  // "IchV608baDoXbqzQKSqFDmTXPVgoDbPAhZJvNRXXxi4hyFXrTxtoOhdurNApKoUOZNc9WCgNs8CFkm81FdfGLiHIVetPG2g6F26s0CkqhQ5k1z1YKA2zwIWSbzUV18YuIchV608baDoXbqzQKSqFDmTXPVgoDbPAhZJvNRXXxi4hyFXrTxtoOhdurNApKoUOZNc9WCgNs8CFkm81FdfGLiHIVetPG2g6F26s0CkqhQ5k1z1YKA2zwIWSbzUV18YuIchV608baDoXbqzQKSqFDmTXPVgoDbPAhZJvNRXXxi4hyFXrTxtoOhdurNApKoUOZNc9WCgNs8CFkm81FdfGLiHIVetPG2g6F26s0CkqhQ5k1z1YKA2zwIWSbzUV18YuIchV608baDoXbqzQKSqFDmTXPVgoDbPAhZJvNRXXxi4hyFXrTxtoOhdurNApKoUOZNc9WCgNs8CFkm81FdfGLlSreVaCuCS5sdb-8ToWULWP7kt~lRPDeUNxQMq3cRSBBQAEAAcAAA==\n"
97  "STREAM STATUS RESULT=I2P_ERROR MESSAGE=\"Session was closed\"\n"
98  );
99  } else {
100  // Another control socket, but without creating a destination (it is cached in the session).
101  return std::make_unique<StaticContentsSock>(
102  // reply to HELLO
103  "HELLO REPLY RESULT=OK VERSION=3.1\n"
104  // reply to SESSION CREATE
105  "SESSION STATUS RESULT=OK\n"
106  // dummy to avoid reporting EOF on the socket
107  "a"
108  );
109  }
110  // clang-format on
111  };
112 
113  CThreadInterrupt interrupt;
114  i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key",
115  CService{in6_addr(IN6ADDR_LOOPBACK_INIT), /*port=*/7656},
116  &interrupt);
117 
118  i2p::Connection conn;
119  for (size_t i = 0; i < 5; ++i) {
120  ASSERT_DEBUG_LOG("Creating persistent SAM session");
121  ASSERT_DEBUG_LOG("Persistent SAM session" /* ... created */);
122  ASSERT_DEBUG_LOG("Error accepting");
123  ASSERT_DEBUG_LOG("Destroying SAM session");
124  BOOST_REQUIRE(session.Listen(conn));
125  BOOST_REQUIRE(!session.Accept(conn));
126  }
127 }
128 
129 BOOST_AUTO_TEST_CASE(damaged_private_key)
130 {
131  const auto CreateSockOrig = CreateSock;
132 
133  CreateSock = [](const CService&) {
134  return std::make_unique<StaticContentsSock>("HELLO REPLY RESULT=OK VERSION=3.1\n"
135  "SESSION STATUS RESULT=OK DESTINATION=\n");
136  };
137 
138  const auto i2p_private_key_file = m_args.GetDataDirNet() / "test_i2p_private_key_damaged";
139 
140  for (const auto& [file_contents, expected_error] : std::vector<std::tuple<std::string, std::string>>{
141  {"", "The private key is too short (0 < 387)"},
142 
143  {"abcd", "The private key is too short (4 < 387)"},
144 
145  {std::string(386, '\0'), "The private key is too short (386 < 387)"},
146 
147  {std::string(385, '\0') + '\0' + '\1',
148  "Certificate length (1) designates that the private key should be 388 bytes, but it is only "
149  "387 bytes"},
150 
151  {std::string(385, '\0') + '\0' + '\5' + "abcd",
152  "Certificate length (5) designates that the private key should be 392 bytes, but it is only "
153  "391 bytes"}}) {
154  BOOST_REQUIRE(WriteBinaryFile(i2p_private_key_file, file_contents));
155 
156  CThreadInterrupt interrupt;
157  i2p::sam::Session session(i2p_private_key_file, CService{}, &interrupt);
158 
159  {
160  ASSERT_DEBUG_LOG("Creating persistent SAM session");
161  ASSERT_DEBUG_LOG(expected_error);
162 
163  i2p::Connection conn;
164  bool proxy_error;
165  BOOST_CHECK(!session.Connect(CService{}, conn, proxy_error));
166  }
167  }
168 
169  CreateSock = CreateSockOrig;
170 }
171 
BCLog::Logger & LogInstance()
Definition: logging.cpp:20
Save the log level and the value of CreateSock and restore them when the test ends.
Definition: i2p_tests.cpp:21
Level LogLevel() const
Definition: logging.h:171
const BCLog::Level m_prev_log_level
Definition: i2p_tests.cpp:40
I2P SAM session.
Definition: i2p.h:55
An established connection with another peer.
Definition: i2p.h:31
bool WriteBinaryFile(const fs::path &filename, const std::string &data)
Write contents of std::string to a file.
Basic testing setup.
Definition: setup_common.h:49
EnvTestingSetup(const ChainType chainType=ChainType::MAIN, const std::vector< const char *> &extra_args={})
Definition: i2p_tests.cpp:24
ChainType
Definition: chaintype.h:11
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:534
BOOST_AUTO_TEST_SUITE_END()
void SetLogLevel(Level level)
Definition: logging.h:172
#define ASSERT_DEBUG_LOG(message)
Definition: logging.h:39
A helper class for interruptible sleeps.
Level
Definition: logging.h:74
static constexpr size_t MAX_MSG_SIZE
The maximum size of an incoming message from the I2P SAM proxy (in bytes).
Definition: i2p.h:50
ArgsManager gArgs
Definition: args.cpp:42
BOOST_AUTO_TEST_CASE(unlimited_recv)
Definition: i2p_tests.cpp:46
const std::function< std::unique_ptr< Sock >const CService &)> m_create_sock_orig
Definition: i2p_tests.cpp:41
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:502
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:231
#define BOOST_CHECK(expr)
Definition: object.cpp:17