@@ -57,6 +57,53 @@ std::optional<service_node> node_for_destination(network_destination destination
57
57
return std::nullopt;
58
58
}
59
59
60
+ std::shared_ptr<TestServer> create_test_server (uint16_t port) {
61
+ oxen::quic::opt::inbound_alpns server_alpns{" oxenstorage" };
62
+ auto server_key_pair = session::ed25519::ed25519_key_pair (to_span (fmt::format (" {:032}" , port)));
63
+ auto server_x25519_pubkey = session::curve25519::to_curve25519_pubkey (
64
+ {server_key_pair.first .data (), server_key_pair.first .size ()});
65
+ auto server_x25519_seckey = session::curve25519::to_curve25519_seckey (
66
+ {server_key_pair.second .data (), server_key_pair.second .size ()});
67
+ auto creds =
68
+ oxen::quic::GNUTLSCreds::make_from_ed_seckey (to_string_view (server_key_pair.second ));
69
+ oxen::quic::Address server_local{port};
70
+ session::onionreq::HopEncryption decryptor{
71
+ x25519_seckey::from_bytes (to_span (server_x25519_seckey)),
72
+ x25519_pubkey::from_bytes (to_span (server_x25519_pubkey)),
73
+ true };
74
+
75
+ auto server_cb = [&](oxen::quic::message m) {
76
+ nlohmann::json response{{" hf" , {1 , 0 , 0 }}, {" t" , 1234567890 }, {" version" , {2 , 8 , 0 }}};
77
+ m.respond (response.dump (), false );
78
+ };
79
+
80
+ auto onion_cb = [&](oxen::quic::message m) {
81
+ nlohmann::json response{{" hf" , {2 , 0 , 0 }}, {" t" , 1234567890 }, {" version" , {2 , 8 , 0 }}};
82
+ m.respond (response.dump (), false );
83
+ };
84
+
85
+ oxen::quic::stream_constructor_callback server_constructor =
86
+ [&](oxen::quic::Connection& c, oxen::quic::Endpoint& e, std::optional<int64_t >) {
87
+ auto s = e.loop .make_shared <oxen::quic::BTRequestStream>(c, e);
88
+ s->register_handler (" info" , server_cb);
89
+ s->register_handler (" onion_req" , onion_cb);
90
+ return s;
91
+ };
92
+
93
+ auto loop = std::make_shared<oxen::quic::Loop>();
94
+ auto endpoint = oxen::quic::Endpoint::endpoint (*loop, server_local, server_alpns);
95
+ endpoint->listen (creds, server_constructor);
96
+
97
+ auto node = service_node{
98
+ to_string_view (server_key_pair.first ),
99
+ {2 , 8 , 0 },
100
+ INVALID_SWARM_ID,
101
+ " 127.0.0.1" s,
102
+ endpoint->local ().port ()};
103
+
104
+ return std::make_shared<TestServer>(loop, endpoint, node);
105
+ }
106
+
60
107
} // namespace
61
108
62
109
namespace session ::network {
@@ -193,61 +240,13 @@ class TestNetwork : public Network {
193
240
std::optional<std::string>) {});
194
241
}
195
242
196
- std::shared_ptr<TestServer> create_test_node (uint16_t port) {
197
- oxen::quic::opt::inbound_alpns server_alpns{" oxenstorage" };
198
- auto server_key_pair =
199
- session::ed25519::ed25519_key_pair (to_span (fmt::format (" {:032}" , port)));
200
- auto server_x25519_pubkey = session::curve25519::to_curve25519_pubkey (
201
- {server_key_pair.first .data (), server_key_pair.first .size ()});
202
- auto server_x25519_seckey = session::curve25519::to_curve25519_seckey (
203
- {server_key_pair.second .data (), server_key_pair.second .size ()});
204
- auto creds = oxen::quic::GNUTLSCreds::make_from_ed_seckey (
205
- to_string_view (server_key_pair.second ));
206
- oxen::quic::Address server_local{port};
207
- session::onionreq::HopEncryption decryptor{
208
- x25519_seckey::from_bytes (to_span (server_x25519_seckey)),
209
- x25519_pubkey::from_bytes (to_span (server_x25519_pubkey)),
210
- true };
211
-
212
- auto server_cb = [&](oxen::quic::message m) {
213
- nlohmann::json response{{" hf" , {1 , 0 , 0 }}, {" t" , 1234567890 }, {" version" , {2 , 8 , 0 }}};
214
- m.respond (response.dump (), false );
215
- };
216
-
217
- auto onion_cb = [&](oxen::quic::message m) {
218
- nlohmann::json response{{" hf" , {2 , 0 , 0 }}, {" t" , 1234567890 }, {" version" , {2 , 8 , 0 }}};
219
- m.respond (response.dump (), false );
220
- };
221
-
222
- oxen::quic::stream_constructor_callback server_constructor =
223
- [&](oxen::quic::Connection& c, oxen::quic::Endpoint& e, std::optional<int64_t >) {
224
- auto s = e.loop .make_shared <oxen::quic::BTRequestStream>(c, e);
225
- s->register_handler (" info" , server_cb);
226
- s->register_handler (" onion_req" , onion_cb);
227
- return s;
228
- };
229
-
230
- auto loop = std::make_shared<oxen::quic::Loop>();
231
- auto endpoint = oxen::quic::Endpoint::endpoint (*loop, server_local, server_alpns);
232
- endpoint->listen (creds, server_constructor);
233
-
234
- auto node = service_node{
235
- to_string_view (server_key_pair.first ),
236
- {2 , 8 , 0 },
237
- INVALID_SWARM_ID,
238
- " 127.0.0.1" s,
239
- endpoint->local ().port ()};
240
-
241
- return std::make_shared<TestServer>(loop, endpoint, node);
242
- }
243
-
244
243
std::pair<std::vector<std::shared_ptr<TestServer>>, onion_path> create_test_path () {
245
244
std::vector<std::shared_ptr<TestServer>> path_servers;
246
245
std::vector<service_node> path_nodes;
247
246
path_nodes.reserve (3 );
248
247
249
248
for (auto i = 0 ; i < 3 ; ++i) {
250
- path_servers.emplace_back (create_test_node (static_cast <uint16_t >(1000 + i)));
249
+ path_servers.emplace_back (create_test_server (static_cast <uint16_t >(1000 + i)));
251
250
path_nodes.emplace_back (path_servers[i]->node );
252
251
}
253
252
@@ -1073,10 +1072,11 @@ TEST_CASE("Network", "[network][build_path]") {
1073
1072
1074
1073
TEST_CASE (" Network" , " [network][find_valid_path]" ) {
1075
1074
auto ed_pk = " 4cb76fdc6d32278e3f83dbf608360ecc6b65727934b85d2fb86862ff98c46ab7" _hexbytes;
1075
+ auto test_server = create_test_server (500 );
1076
1076
auto target = test_node (ed_pk, 1 );
1077
- auto network = TestNetwork (std::nullopt, true , false , false );
1078
- auto test_server = network.create_test_node (500 );
1079
1077
auto info = request_info::make (target, std::nullopt, std::nullopt, 0ms);
1078
+
1079
+ auto network = TestNetwork (std::nullopt, true , false , false );
1080
1080
auto invalid_path = onion_path{
1081
1081
" Test" ,
1082
1082
{test_server->node , nullptr , nullptr , nullptr },
@@ -1125,7 +1125,7 @@ TEST_CASE("Network", "[network][find_valid_path]") {
1125
1125
TEST_CASE (" Network" , " [network][build_path_if_needed]" ) {
1126
1126
auto ed_pk = " 4cb76fdc6d32278e3f83dbf608360ecc6b65727934b85d2fb86862ff98c46ab7" _hexbytes;
1127
1127
auto target = test_node (ed_pk, 0 );
1128
- ;
1128
+
1129
1129
std::optional<TestNetwork> network;
1130
1130
auto invalid_path = onion_path{
1131
1131
" Test" , connection_info{target, nullptr , nullptr , nullptr }, {target}, uint8_t {0 }};
@@ -1221,8 +1221,8 @@ TEST_CASE("Network", "[network][build_path_if_needed]") {
1221
1221
}
1222
1222
1223
1223
TEST_CASE (" Network" , " [network][establish_connection]" ) {
1224
+ auto test_server = create_test_server (500 );
1224
1225
auto network = TestNetwork (std::nullopt, true , true , false );
1225
- auto test_server = network.create_test_node (500 );
1226
1226
std::promise<std::pair<connection_info, std::optional<std::string>>> prom;
1227
1227
1228
1228
network.establish_connection (
@@ -1248,7 +1248,7 @@ TEST_CASE("Network", "[network][check_request_queue_timeouts]") {
1248
1248
// Test that it doesn't start checking for timeouts when the request doesn't have
1249
1249
// a build paths timeout
1250
1250
network.emplace (std::nullopt, true , true , false );
1251
- test_server.emplace (network-> create_test_node (501 ));
1251
+ test_server.emplace (create_test_server (501 ));
1252
1252
network->send_onion_request (
1253
1253
(*test_server)->node ,
1254
1254
to_vector (" {\" method\" :\" info\" ,\" params\" :{}}" ),
@@ -1265,7 +1265,7 @@ TEST_CASE("Network", "[network][check_request_queue_timeouts]") {
1265
1265
// Test that it does start checking for timeouts when the request has a
1266
1266
// paths build timeout
1267
1267
network.emplace (std::nullopt, true , true , false );
1268
- test_server.emplace (network-> create_test_node (502 ));
1268
+ test_server.emplace (create_test_server (502 ));
1269
1269
network->ignore_calls_to (" build_path" );
1270
1270
network->send_onion_request (
1271
1271
(*test_server)->node ,
@@ -1283,7 +1283,7 @@ TEST_CASE("Network", "[network][check_request_queue_timeouts]") {
1283
1283
// Test that it fails the request with a timeout if it has a build path timeout
1284
1284
// and the path build takes too long
1285
1285
network.emplace (std::nullopt, true , true , false );
1286
- test_server.emplace (network-> create_test_node (503 ));
1286
+ test_server.emplace (create_test_server (503 ));
1287
1287
network->ignore_calls_to (" build_path" );
1288
1288
network->send_onion_request (
1289
1289
(*test_server)->node ,
@@ -1307,8 +1307,8 @@ TEST_CASE("Network", "[network][check_request_queue_timeouts]") {
1307
1307
}
1308
1308
1309
1309
TEST_CASE (" Network" , " [network][send_request]" ) {
1310
+ auto test_server = create_test_server (500 );
1310
1311
auto network = TestNetwork (std::nullopt, true , true , false );
1311
- auto test_server = network.create_test_node (500 );
1312
1312
std::promise<Result> prom;
1313
1313
1314
1314
network.establish_connection (
@@ -1360,8 +1360,8 @@ TEST_CASE("Network", "[network][send_request]") {
1360
1360
}
1361
1361
1362
1362
TEST_CASE (" Network" , " [network][send_onion_request]" ) {
1363
+ auto test_server = create_test_server (500 );
1363
1364
auto network = TestNetwork (std::nullopt, true , true , false );
1364
- auto test_server = network.create_test_node (500 );
1365
1365
auto [test_path_servers, test_path] = network.create_test_path ();
1366
1366
network.handle_onion_requests_as_plaintext = true ;
1367
1367
network.set_paths (PathType::standard, {test_path});
@@ -1402,8 +1402,8 @@ TEST_CASE("Network", "[network][send_onion_request]") {
1402
1402
}
1403
1403
1404
1404
TEST_CASE (" Network" , " [network][c][network_send_onion_request]" ) {
1405
+ auto test_server_cpp = create_test_server (500 );
1405
1406
auto test_network = std::make_unique<TestNetwork>(std::nullopt, true , true , false );
1406
- auto test_server_cpp = test_network->create_test_node (500 );
1407
1407
std::optional<std::pair<std::vector<std::shared_ptr<TestServer>>, onion_path>> test_path_data;
1408
1408
test_path_data.emplace (test_network->create_test_path ());
1409
1409
test_network->handle_onion_requests_as_plaintext = true ;
0 commit comments