From cc99ecd43a81cab0fba294a747658f0ea2c4bbe2 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 4 May 2020 02:12:57 +0100 Subject: Stop hard-coding packet IDs in tests. --- auto_tests/TCP_test.c | 20 +++++++++++--------- auto_tests/onion_test.c | 33 ++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index c09e336c..b4a5392a 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -123,7 +123,8 @@ START_TEST(test_basic) memcpy(f_nonce_r, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE); // Building a request - uint8_t r_req_p[1 + CRYPTO_PUBLIC_KEY_SIZE] = {0}; + uint8_t r_req_p[1 + CRYPTO_PUBLIC_KEY_SIZE]; + r_req_p[0] = TCP_PACKET_ROUTING_REQUEST; memcpy(r_req_p + 1, f_public_key, CRYPTO_PUBLIC_KEY_SIZE); uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE]; uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE; @@ -163,7 +164,8 @@ START_TEST(test_basic) ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response."); increment_nonce(f_nonce_r); - ck_assert_msg(packet_resp_plain[0] == 1, "Server sent the wrong packet id: %u", packet_resp_plain[0]); + ck_assert_msg(packet_resp_plain[0] == TCP_PACKET_ROUTING_RESPONSE, "Server sent the wrong packet id: %u", + packet_resp_plain[0]); ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection."); ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "Server sent the wrong public key."); @@ -288,7 +290,7 @@ START_TEST(test_some) struct sec_TCP_con *con3 = new_TCP_con(tcp_s, mono_time); uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE]; - requ_p[0] = 0; + requ_p[0] = TCP_PACKET_ROUTING_REQUEST; // Sending wrong public keys to test server response. memcpy(requ_p + 1, con3->public_key, CRYPTO_PUBLIC_KEY_SIZE); @@ -302,14 +304,14 @@ START_TEST(test_some) uint8_t data[2048]; int len = read_packet_sec_TCP(con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); - ck_assert_msg(data[0] == 1, "Wrong response packet id of %d.", data[0]); + ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]); ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "Key in response packet wrong."); // Connection 3 len = read_packet_sec_TCP(con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); - ck_assert_msg(data[0] == 1, "Wrong response packet id of %d.", data[0]); + ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]); ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong."); @@ -323,11 +325,11 @@ START_TEST(test_some) len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE); ck_assert_msg(len == 2, "wrong len %d", len); - ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); + ck_assert_msg(data[0] == TCP_PACKET_CONNECTION_NOTIFICATION, "wrong packet id %u", data[0]); ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); len = read_packet_sec_TCP(con3, data, 2 + 2 + CRYPTO_MAC_SIZE); ck_assert_msg(len == 2, "wrong len %d", len); - ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); + ck_assert_msg(data[0] == TCP_PACKET_CONNECTION_NOTIFICATION, "wrong packet id %u", data[0]); ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); @@ -358,14 +360,14 @@ START_TEST(test_some) ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); - uint8_t ping_packet[1 + sizeof(uint64_t)] = {4, 8, 6, 9, 67}; + uint8_t ping_packet[1 + sizeof(uint64_t)] = {TCP_PACKET_PING, 8, 6, 9, 67}; write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet)); do_TCP_server_delay(tcp_s, mono_time, 50); len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len); - ck_assert_msg(data[0] == 5, "wrong packet id %u", data[0]); + ck_assert_msg(data[0] == TCP_PACKET_PONG, "wrong packet id %u", data[0]); ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data"); // Kill off the connections diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 61ba07fa..865a6d33 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -42,12 +42,22 @@ static int handle_test_1(void *object, IP_Port source, const uint8_t *packet, ui { Onion *onion = (Onion *)object; - if (memcmp(packet, "\x83 Install Gentoo", sizeof("\x83 Install Gentoo")) != 0) { + const char req_message[] = "Install Gentoo"; + uint8_t req_packet[1 + sizeof(req_message)]; + req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST; + memcpy(req_packet + 1, req_message, sizeof(req_message)); + + if (memcmp(packet, req_packet, sizeof(req_packet)) != 0) { return 1; } - if (send_onion_response(onion->net, source, (const uint8_t *)"\x84 install gentoo", sizeof("\x84 install gentoo"), - packet + sizeof("\x84 install gentoo")) == -1) { + const char res_message[] = "install gentoo"; + uint8_t res_packet[1 + sizeof(res_message)]; + res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE; + memcpy(res_packet + 1, res_message, sizeof(res_message)); + + if (send_onion_response(onion->net, source, res_packet, sizeof(res_packet), + packet + sizeof(res_packet)) == -1) { return 1; } @@ -58,11 +68,16 @@ static int handle_test_1(void *object, IP_Port source, const uint8_t *packet, ui static int handled_test_2; static int handle_test_2(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) { - if (length != sizeof("\x84 install gentoo")) { + const char res_message[] = "install gentoo"; + uint8_t res_packet[1 + sizeof(res_message)]; + res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE; + memcpy(res_packet + 1, res_message, sizeof(res_message)); + + if (length != sizeof(res_packet)) { return 1; } - if (memcmp(packet, (const uint8_t *)"\x84 install gentoo", sizeof("\x84 install gentoo")) != 0) { + if (memcmp(packet, res_packet, sizeof(res_packet)) != 0) { return 1; } @@ -179,6 +194,11 @@ static void test_basic(void) memcpy(n2.public_key, dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE); n2.ip_port = on2; + const char req_message[] = "Install Gentoo"; + uint8_t req_packet[1 + sizeof(req_message)]; + req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST; + memcpy(req_packet + 1, req_message, sizeof(req_message)); + Node_format nodes[4]; nodes[0] = n1; nodes[1] = n2; @@ -186,8 +206,7 @@ static void test_basic(void) nodes[3] = n2; Onion_Path path; create_onion_path(onion1->dht, &path, nodes); - int ret = send_onion_packet(onion1->net, &path, nodes[3].ip_port, (const uint8_t *)"\x83 Install Gentoo", - sizeof("\x83 Install Gentoo")); + int ret = send_onion_packet(onion1->net, &path, nodes[3].ip_port, req_packet, sizeof(req_packet)); ck_assert_msg(ret == 0, "Failed to create/send onion packet."); handled_test_1 = 0; -- cgit v1.2.3