Skip to content

Commit

Permalink
Add NAT test.
Browse files Browse the repository at this point in the history
  • Loading branch information
huitema committed Jan 3, 2024
1 parent f9d8235 commit cab14ec
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
8 changes: 8 additions & 0 deletions UnitTest1/unittest1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ namespace UnitTest1

Assert::AreEqual(ret, 0);
}

TEST_METHOD(sockloop_nat)
{
int ret = sockloop_nat_test();

Assert::AreEqual(ret, 0);
}

TEST_METHOD(splay)
{
int ret = splay_test();
Expand Down
23 changes: 22 additions & 1 deletion picoquic/sockloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ int picoquic_packet_loop_v2(picoquic_quic_t* quic,
picoquic_connection_id_t log_cid;
picoquic_socket_ctx_t s_ctx[4];
int nb_sockets = 0;
int nb_sockets_available = 0;
#if 0
int testing_migration = 0; /* Hook for the migration test */
uint16_t next_port = 0; /* Data for the migration test */
Expand Down Expand Up @@ -440,6 +441,8 @@ int picoquic_packet_loop_v2(picoquic_quic_t* quic,
}

if (ret == 0) {
nb_sockets_available = nb_sockets;

if (udp_gso_available && !param->do_not_use_gso) {
send_buffer_size = 0xFFFF;
send_msg_ptr = &send_msg_size;
Expand Down Expand Up @@ -473,7 +476,7 @@ int picoquic_packet_loop_v2(picoquic_quic_t* quic,
}
}
loop_immediate = 0;
bytes_recv = picoquic_packet_loop_select(s_ctx, nb_sockets,
bytes_recv = picoquic_packet_loop_select(s_ctx, nb_sockets_available,
&addr_from,
&addr_to, &if_index_to, &received_ecn,
buffer, sizeof(buffer),
Expand Down Expand Up @@ -504,6 +507,24 @@ int picoquic_packet_loop_v2(picoquic_quic_t* quic,
next_send_time = current_time + PICOQUIC_PACKET_LOOP_SEND_DELAY_MAX;
}
}

if (ret == PICOQUIC_NO_ERROR_SIMULATE_NAT) {
if (param->extra_socket_required) {
/* Stop using the extra socket.
* This will simulate a NAT:
* - on the receive side, packets arriving to the old address will be ignored.
* - on the send side, client packets will be sent through the main socket,
* and appear to come from that port instead of the extra port.
* - since the CID does not change, the server will execute the NAT behavior.
* The client will have to update its path -- but that can be avoided if the
* test code overrides the value of the "local" address that the client
* memorized for that path.
*/
nb_sockets_available = nb_sockets / 2;
}
ret = 0;
}

if (ret != PICOQUIC_NO_ERROR_SIMULATE_NAT && ret != PICOQUIC_NO_ERROR_SIMULATE_MIGRATION) {
size_t bytes_sent = 0;

Expand Down
1 change: 1 addition & 0 deletions picoquic_t/picoquic_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static const picoquic_test_def_t test_table[] = {
{ "sockloop_errsock", sockloop_errsock_test },
{ "sockloop_ipv4", sockloop_ipv4_test },
{ "sockloop_migration", sockloop_migration_test },
{ "sockloop_nat", sockloop_nat_test },
{ "splay", splay_test },
{ "cnxcreation", cnxcreation_test },
{ "parseheader", parseheadertest },
Expand Down
1 change: 1 addition & 0 deletions picoquictest/picoquictest.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ int sockloop_eio_test();
int sockloop_errsock_test();
int sockloop_ipv4_test();
int sockloop_migration_test();
int sockloop_nat_test();
int splay_test();
int TlsStreamFrameTest();
int draft17_vector_test();
Expand Down
54 changes: 36 additions & 18 deletions picoquictest/sockloop_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,22 @@ int sockloop_test_cb(picoquic_quic_t* quic, picoquic_packet_loop_cb_enum cb_mode
if (sockloop_test_verify_extra_socket(cnx_client, (struct sockaddr*)&cb_ctx->server_address) != 0) {
ret = -1;
}
else {
else if (cb_ctx->force_migration == 3){
cb_ctx->migration_started = 1;
ret = picoquic_probe_new_path(cnx_client, (struct sockaddr*)&cb_ctx->server_address,
(struct sockaddr*)&cb_ctx->server_address,
picoquic_get_quic_time(cb_ctx->test_ctx->qserver));
}
else if (cb_ctx->force_migration == 1) {
cb_ctx->migration_started = 1;
if (cnx_client->path[0]->local_addr.ss_family == AF_INET6) {
((struct sockaddr_in6*)&cnx_client->path[0]->local_addr)->sin6_port = htons(cb_ctx->param->local_port);
}
else {
((struct sockaddr_in*)&cnx_client->path[0]->local_addr)->sin_port = htons(cb_ctx->param->local_port);
}
ret = PICOQUIC_NO_ERROR_SIMULATE_NAT;
}
}
else if (cb_ctx->migration_started && !cb_ctx->address_updated) {
if (picoquic_compare_addr((struct sockaddr*)&cnx_client->path[0]->local_addr, (struct sockaddr*)&cb_ctx->server_address) == 0) {
Expand Down Expand Up @@ -384,11 +394,6 @@ int sockloop_test_one(sockloop_test_spec_t *spec)
* in that case, only start the client connection after
* the thread is verified and started, e.g., using an
* active loop until the thread is marked ready.
* TODO: consider testing at least one migration scenario,
* for code coverage.
* TODO: consider changing the API to add a second port number, to be
* used in migration and multipath tests.
* TODO: document port number.
*/
if (ret == 0) {
loop_cb.test_ctx = test_ctx;
Expand Down Expand Up @@ -426,6 +431,9 @@ int sockloop_test_one(sockloop_test_spec_t *spec)
if (spec->double_bind) {
ret = -1;
}
else if (spec->force_migration != 0 && loop_cb.address_updated == 0) {
ret = -1;
}
else {
ret = tls_api_one_scenario_verify(test_ctx);
}
Expand Down Expand Up @@ -474,21 +482,17 @@ int sockloop_basic_test()
return(sockloop_test_one(&spec));
}

static test_api_stream_desc_t sockloop_test_scenario_5M[] = {
{ 4, 0, 257, 1000000 },
{ 8, 4, 257, 1000000 },
{ 12, 8, 257, 1000000 },
{ 16, 12, 257, 1000000 },
{ 20, 16, 257, 1000000 },
static test_api_stream_desc_t sockloop_test_scenario_1M[] = {
{ 4, 0, 257, 1000000 }
};

int sockloop_eio_test()
{
sockloop_test_spec_t spec;
sockloop_test_set_spec(&spec, 2);
spec.socket_buffer_size = 0xffff;
spec.scenario = sockloop_test_scenario_5M;
spec.scenario_size = sizeof(sockloop_test_scenario_5M);
spec.scenario = sockloop_test_scenario_1M;
spec.scenario_size = sizeof(sockloop_test_scenario_1M);
spec.simulate_eio = 1;

return(sockloop_test_one(&spec));
Expand All @@ -509,8 +513,8 @@ int sockloop_ipv4_test()
sockloop_test_set_spec(&spec, 4);
spec.af = AF_INET;
spec.socket_buffer_size = 0xffff;
spec.scenario = sockloop_test_scenario_5M;
spec.scenario_size = sizeof(sockloop_test_scenario_5M);
spec.scenario = sockloop_test_scenario_1M;
spec.scenario_size = sizeof(sockloop_test_scenario_1M);

return(sockloop_test_one(&spec));
}
Expand All @@ -519,10 +523,24 @@ int sockloop_migration_test()
{
sockloop_test_spec_t spec;
sockloop_test_set_spec(&spec, 5);
spec.af = AF_INET6;
spec.socket_buffer_size = 0xffff;
spec.scenario = sockloop_test_scenario_1M;
spec.scenario_size = sizeof(sockloop_test_scenario_1M);
spec.extra_socket_required = 1;
spec.force_migration = 3;

return(sockloop_test_one(&spec));
}

int sockloop_nat_test()
{
sockloop_test_spec_t spec;
sockloop_test_set_spec(&spec, 6);
spec.af = AF_INET;
spec.socket_buffer_size = 0xffff;
spec.scenario = sockloop_test_scenario_5M;
spec.scenario_size = sizeof(sockloop_test_scenario_5M);
spec.scenario = sockloop_test_scenario_1M;
spec.scenario_size = sizeof(sockloop_test_scenario_1M);
spec.extra_socket_required = 1;
spec.force_migration = 1;

Expand Down

0 comments on commit cab14ec

Please sign in to comment.