summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2020-05-04 02:12:57 +0100
committeriphydf <iphydf@users.noreply.github.com>2020-05-04 02:14:56 +0100
commitcc99ecd43a81cab0fba294a747658f0ea2c4bbe2 (patch)
tree5f4515465dc084cbc9448357e661239049466ccc
parent02a5bdc60c9ff8474a959027882048290571f806 (diff)
Stop hard-coding packet IDs in tests.
-rw-r--r--auto_tests/TCP_test.c20
-rw-r--r--auto_tests/onion_test.c33
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)
123 memcpy(f_nonce_r, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE); 123 memcpy(f_nonce_r, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE);
124 124
125 // Building a request 125 // Building a request
126 uint8_t r_req_p[1 + CRYPTO_PUBLIC_KEY_SIZE] = {0}; 126 uint8_t r_req_p[1 + CRYPTO_PUBLIC_KEY_SIZE];
127 r_req_p[0] = TCP_PACKET_ROUTING_REQUEST;
127 memcpy(r_req_p + 1, f_public_key, CRYPTO_PUBLIC_KEY_SIZE); 128 memcpy(r_req_p + 1, f_public_key, CRYPTO_PUBLIC_KEY_SIZE);
128 uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE]; 129 uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE];
129 uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE; 130 uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE;
@@ -163,7 +164,8 @@ START_TEST(test_basic)
163 ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response."); 164 ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response.");
164 increment_nonce(f_nonce_r); 165 increment_nonce(f_nonce_r);
165 166
166 ck_assert_msg(packet_resp_plain[0] == 1, "Server sent the wrong packet id: %u", packet_resp_plain[0]); 167 ck_assert_msg(packet_resp_plain[0] == TCP_PACKET_ROUTING_RESPONSE, "Server sent the wrong packet id: %u",
168 packet_resp_plain[0]);
167 ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection."); 169 ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection.");
168 ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "Server sent the wrong public key."); 170 ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "Server sent the wrong public key.");
169 171
@@ -288,7 +290,7 @@ START_TEST(test_some)
288 struct sec_TCP_con *con3 = new_TCP_con(tcp_s, mono_time); 290 struct sec_TCP_con *con3 = new_TCP_con(tcp_s, mono_time);
289 291
290 uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE]; 292 uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE];
291 requ_p[0] = 0; 293 requ_p[0] = TCP_PACKET_ROUTING_REQUEST;
292 294
293 // Sending wrong public keys to test server response. 295 // Sending wrong public keys to test server response.
294 memcpy(requ_p + 1, con3->public_key, CRYPTO_PUBLIC_KEY_SIZE); 296 memcpy(requ_p + 1, con3->public_key, CRYPTO_PUBLIC_KEY_SIZE);
@@ -302,14 +304,14 @@ START_TEST(test_some)
302 uint8_t data[2048]; 304 uint8_t data[2048];
303 int len = read_packet_sec_TCP(con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); 305 int len = read_packet_sec_TCP(con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
304 ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); 306 ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
305 ck_assert_msg(data[0] == 1, "Wrong response packet id of %d.", data[0]); 307 ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
306 ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); 308 ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
307 ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "Key in response packet wrong."); 309 ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "Key in response packet wrong.");
308 310
309 // Connection 3 311 // Connection 3
310 len = read_packet_sec_TCP(con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); 312 len = read_packet_sec_TCP(con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
311 ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); 313 ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
312 ck_assert_msg(data[0] == 1, "Wrong response packet id of %d.", data[0]); 314 ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
313 ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); 315 ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
314 ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong."); 316 ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong.");
315 317
@@ -323,11 +325,11 @@ START_TEST(test_some)
323 325
324 len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE); 326 len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE);
325 ck_assert_msg(len == 2, "wrong len %d", len); 327 ck_assert_msg(len == 2, "wrong len %d", len);
326 ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); 328 ck_assert_msg(data[0] == TCP_PACKET_CONNECTION_NOTIFICATION, "wrong packet id %u", data[0]);
327 ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); 329 ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]);
328 len = read_packet_sec_TCP(con3, data, 2 + 2 + CRYPTO_MAC_SIZE); 330 len = read_packet_sec_TCP(con3, data, 2 + 2 + CRYPTO_MAC_SIZE);
329 ck_assert_msg(len == 2, "wrong len %d", len); 331 ck_assert_msg(len == 2, "wrong len %d", len);
330 ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); 332 ck_assert_msg(data[0] == TCP_PACKET_CONNECTION_NOTIFICATION, "wrong packet id %u", data[0]);
331 ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); 333 ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]);
332 len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); 334 len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE);
333 ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); 335 ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
@@ -358,14 +360,14 @@ START_TEST(test_some)
358 ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], 360 ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1],
359 data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); 361 data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]);
360 362
361 uint8_t ping_packet[1 + sizeof(uint64_t)] = {4, 8, 6, 9, 67}; 363 uint8_t ping_packet[1 + sizeof(uint64_t)] = {TCP_PACKET_PING, 8, 6, 9, 67};
362 write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet)); 364 write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet));
363 365
364 do_TCP_server_delay(tcp_s, mono_time, 50); 366 do_TCP_server_delay(tcp_s, mono_time, 50);
365 367
366 len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); 368 len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE);
367 ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len); 369 ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len);
368 ck_assert_msg(data[0] == 5, "wrong packet id %u", data[0]); 370 ck_assert_msg(data[0] == TCP_PACKET_PONG, "wrong packet id %u", data[0]);
369 ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data"); 371 ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data");
370 372
371 // Kill off the connections 373 // 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
42{ 42{
43 Onion *onion = (Onion *)object; 43 Onion *onion = (Onion *)object;
44 44
45 if (memcmp(packet, "\x83 Install Gentoo", sizeof("\x83 Install Gentoo")) != 0) { 45 const char req_message[] = "Install Gentoo";
46 uint8_t req_packet[1 + sizeof(req_message)];
47 req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
48 memcpy(req_packet + 1, req_message, sizeof(req_message));
49
50 if (memcmp(packet, req_packet, sizeof(req_packet)) != 0) {
46 return 1; 51 return 1;
47 } 52 }
48 53
49 if (send_onion_response(onion->net, source, (const uint8_t *)"\x84 install gentoo", sizeof("\x84 install gentoo"), 54 const char res_message[] = "install gentoo";
50 packet + sizeof("\x84 install gentoo")) == -1) { 55 uint8_t res_packet[1 + sizeof(res_message)];
56 res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE;
57 memcpy(res_packet + 1, res_message, sizeof(res_message));
58
59 if (send_onion_response(onion->net, source, res_packet, sizeof(res_packet),
60 packet + sizeof(res_packet)) == -1) {
51 return 1; 61 return 1;
52 } 62 }
53 63
@@ -58,11 +68,16 @@ static int handle_test_1(void *object, IP_Port source, const uint8_t *packet, ui
58static int handled_test_2; 68static int handled_test_2;
59static int handle_test_2(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) 69static int handle_test_2(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
60{ 70{
61 if (length != sizeof("\x84 install gentoo")) { 71 const char res_message[] = "install gentoo";
72 uint8_t res_packet[1 + sizeof(res_message)];
73 res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE;
74 memcpy(res_packet + 1, res_message, sizeof(res_message));
75
76 if (length != sizeof(res_packet)) {
62 return 1; 77 return 1;
63 } 78 }
64 79
65 if (memcmp(packet, (const uint8_t *)"\x84 install gentoo", sizeof("\x84 install gentoo")) != 0) { 80 if (memcmp(packet, res_packet, sizeof(res_packet)) != 0) {
66 return 1; 81 return 1;
67 } 82 }
68 83
@@ -179,6 +194,11 @@ static void test_basic(void)
179 memcpy(n2.public_key, dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE); 194 memcpy(n2.public_key, dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE);
180 n2.ip_port = on2; 195 n2.ip_port = on2;
181 196
197 const char req_message[] = "Install Gentoo";
198 uint8_t req_packet[1 + sizeof(req_message)];
199 req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
200 memcpy(req_packet + 1, req_message, sizeof(req_message));
201
182 Node_format nodes[4]; 202 Node_format nodes[4];
183 nodes[0] = n1; 203 nodes[0] = n1;
184 nodes[1] = n2; 204 nodes[1] = n2;
@@ -186,8 +206,7 @@ static void test_basic(void)
186 nodes[3] = n2; 206 nodes[3] = n2;
187 Onion_Path path; 207 Onion_Path path;
188 create_onion_path(onion1->dht, &path, nodes); 208 create_onion_path(onion1->dht, &path, nodes);
189 int ret = send_onion_packet(onion1->net, &path, nodes[3].ip_port, (const uint8_t *)"\x83 Install Gentoo", 209 int ret = send_onion_packet(onion1->net, &path, nodes[3].ip_port, req_packet, sizeof(req_packet));
190 sizeof("\x83 Install Gentoo"));
191 ck_assert_msg(ret == 0, "Failed to create/send onion packet."); 210 ck_assert_msg(ret == 0, "Failed to create/send onion packet.");
192 211
193 handled_test_1 = 0; 212 handled_test_1 = 0;