summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJesper Jensen <jesper@slashwin.dk>2021-09-19 11:46:27 +0200
committerJesper Jensen <jesper@slashwin.dk>2021-09-19 11:46:27 +0200
commitb5127f1c7624f40437159f164ac8eabc50b65f74 (patch)
treeadd3888f067e550b542ad9653fe9890ce72a8944 /test
parent4c2f2c7e4c36989463e2a44c60f240cf9c0e1a48 (diff)
Add general tests
Diffstat (limited to 'test')
-rw-r--r--test/proto.c111
-rw-r--r--test/routing.c33
2 files changed, 143 insertions, 1 deletions
diff --git a/test/proto.c b/test/proto.c
new file mode 100644
index 0000000..3becc98
--- /dev/null
+++ b/test/proto.c
@@ -0,0 +1,111 @@
+#include "unity.h"
+#include "proto.h"
+
+#include "log.h"
+
+#include <string.h>
+
+void test_begin_pings_bootstrap_node() {
+ struct message outbuff[10] = {0};
+
+ struct dht dht;
+ dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
+
+ struct message* message_cursor = outbuff;
+ proto_begin(&dht, &message_cursor, outbuff+10);
+
+ TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1);
+ TEST_ASSERT_EQUAL(91, outbuff[0].payload_len);
+ TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:ad2:id20:BBBBBBBBBBBBBBBBBBBB6:target20:", outbuff[0].payload, 43);
+ TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:q9:find_node1:t1:01:y1:qe", outbuff[0].payload+63, 28);
+}
+
+void test_response_from_initial_probe() {
+ struct message outbuff[2] = {0};
+
+ struct sockaddr_storage remote;
+ socklen_t remote_len;
+
+ struct dht dht;
+ dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
+ {
+ struct message* message_cursor = outbuff;
+ proto_begin(&dht, &message_cursor, outbuff+2);
+ remote_len = outbuff[0].dest_len;
+ memcpy(&remote, &outbuff[0].dest, remote_len);
+ }
+
+ char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes26:bbbbbbbbbbbbbbbbbbbb\xFF\xFF\xFF\xFF\x00\x00""ee";
+ struct message* message_cursor = outbuff;
+ proto_run(&dht, buff, sizeof(buff), (struct sockaddr_in*)&remote, remote_len, &message_cursor, outbuff+2);
+
+ TEST_ASSERT_EQUAL_PTR_MESSAGE(message_cursor, outbuff+1, "Sends one packet");
+ TEST_ASSERT_EQUAL(91, outbuff[0].payload_len);
+ TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:ad2:id20:BBBBBBBBBBBBBBBBBBBB6:target20:", outbuff[0].payload, 43);
+ TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:q9:find_node1:t1:11:y1:qe", outbuff[0].payload+63, 28);
+
+ // The queried node gets added to the routing table
+ struct nodeid other = (struct nodeid){.inner={0x61616161, 0x61616161, 0x61616161, 0x61616161, 0x61616161}};
+ struct entry* entry = routing_get(&other);
+ TEST_ASSERT_NOT_NULL(entry);
+ TEST_ASSERT_EQUAL(((struct sockaddr_in*)&remote)->sin_addr.s_addr, entry->addr.ip);
+ TEST_ASSERT_EQUAL(((struct sockaddr_in*)&remote)->sin_port, entry->addr.port);
+}
+
+void test_reponse_from_wrong_ip() {
+ struct message outbuff[2] = {0};
+
+ struct dht dht;
+ dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
+ {
+ struct message* message_cursor = outbuff;
+ proto_begin(&dht, &message_cursor, outbuff+2);
+ }
+
+ // This is fragile, since the ip of the bootstrap node could change, and we
+ // look it up from DNS. Although it pretty unlikely that it would change to
+ // this ip
+ struct sockaddr_in remote;
+ remote.sin_family = AF_INET;
+ inet_pton(AF_INET, "255.255.255.255", &remote.sin_addr.s_addr);
+ remote.sin_port = htons(6881);
+
+ char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes26:bbbbbbbbbbbbbbbbbbbb\xFF\xFF\xFF\xFF\x00\x00""ee";
+ struct message* message_cursor = outbuff;
+ proto_run(&dht, buff, sizeof(buff), &remote, sizeof(remote), &message_cursor, outbuff+2);
+
+ // We shouldn't send any packets, since the response is rejected
+ TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff);
+
+ // Since the ip was wrong we should not have accepted the node into the
+ // rounting table
+ struct nodeid other = (struct nodeid){.inner={0x61616161, 0x61616161, 0x61616161, 0x61616161, 0x61616161}};
+ struct entry* entry = routing_get(&other);
+ TEST_ASSERT_NULL(entry);
+}
+
+void test_ping() {
+ struct message outbuff[2] = {0};
+
+ struct dht dht;
+ dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
+ {
+ struct message* message_cursor = outbuff;
+ proto_begin(&dht, &message_cursor, outbuff+2);
+ }
+
+ struct sockaddr_in remote;
+ remote.sin_family = AF_INET;
+ inet_pton(AF_INET, "255.255.255.255", &remote.sin_addr.s_addr);
+ remote.sin_port = htons(6881);
+
+ char buff[] = "d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe";
+ struct message* message_cursor = outbuff;
+ proto_run(&dht, buff, sizeof(buff), &remote, sizeof(remote), &message_cursor, outbuff+2);
+
+ // We should have sent a response
+ TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1);
+
+ TEST_ASSERT_EQUAL(48, outbuff[0].payload_len);
+ TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:r1:rd2:id20:BBBBBBBBBBBBBBBBBBBBee", outbuff[0].payload, 47);
+}
diff --git a/test/routing.c b/test/routing.c
index 60a7f03..7905bc0 100644
--- a/test/routing.c
+++ b/test/routing.c
@@ -6,7 +6,7 @@
struct nodeid self;
void setUp() {
- self = (struct nodeid){{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }};
+ self = (struct nodeid){.inner={ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }};
routing_init(&self);
}
@@ -142,3 +142,34 @@ void test_not_interested_when_bucket_is_full() {
TEST_ASSERT_FALSE_MESSAGE(routing_interested(&new), "Still interested when bucket was full");
}
+
+void test_lowest_ts_is_oldest() {
+ routing_flush();
+
+ // The address we are going to store
+ struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};
+
+ struct nodeid new = self;
+ // Flip the top bit of the id to go into the low resolution bucket
+ new.inner_b[0] ^= 0x80;
+
+ // Fill up the bucket with entries
+ for(uint8_t i = 0; i < 2; i++) {
+ struct entry* entry;
+ TEST_ASSERT_TRUE_MESSAGE(routing_offer(&new, &entry), "Did not accept new entry");
+
+ // Set the entries
+ entry->id = new;
+ entry->addr = addr;
+ // Invert the timestamps to make the last one have lowest timestamp
+ entry->last = 2-i;
+
+ new.inner_b[19] += 1;
+ }
+
+ struct entry* dest = NULL;
+ routing_oldest(&dest);
+
+ TEST_ASSERT_NOT_NULL(dest);
+ TEST_ASSERT_EQUAL(1, dest->id.inner_b[19]);
+}