summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJesper Jensen <jesper@slashwin.dk>2021-10-05 22:48:37 +0200
committerJesper Jensen <jesper@slashwin.dk>2021-10-05 22:48:37 +0200
commit6e6829ad209550fbac0e363f9421c1fba78915b5 (patch)
treea769fdc053e5cd8dabbebe9048725bdc9b6500cc /test
parente3eb0d2eda0bb8c4decf9bc56b025157e70387c2 (diff)
Add getclient timeout
Diffstat (limited to 'test')
-rw-r--r--test/proto.c65
-rw-r--r--test/routing.c61
2 files changed, 119 insertions, 7 deletions
diff --git a/test/proto.c b/test/proto.c
index 03a4a45..600602e 100644
--- a/test/proto.c
+++ b/test/proto.c
@@ -208,6 +208,7 @@ void test_remove_from_routing_after_3_retries() {
// 1st retry
struct message* message_cursor = outbuff;
proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, now, &message_cursor, outbuff+2);
+ TEST_ASSERT_EQUAL_PTR_MESSAGE(message_cursor, outbuff+1, "Ping was not sent");
}
now += PROTO_TMOUT;
@@ -215,13 +216,7 @@ void test_remove_from_routing_after_3_retries() {
// 2nd retry
struct message* message_cursor = outbuff;
proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, now, &message_cursor, outbuff+2);
- }
-
- now += PROTO_TMOUT;
- {
- // 3rd retry
- struct message* message_cursor = outbuff;
- proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, now, &message_cursor, outbuff+2);
+ TEST_ASSERT_EQUAL_PTR_MESSAGE(message_cursor, outbuff+1, "Ping was not sent");
}
now += PROTO_TMOUT;
@@ -233,3 +228,59 @@ void test_remove_from_routing_after_3_retries() {
entry = routing_get(&other);
TEST_ASSERT_NULL(entry);
}
+
+void test_ping_node_when_uncertain() {
+ struct message outbuff[10] = {0};
+ time_t now = 0;
+
+ struct sockaddr_storage remote;
+ socklen_t remote_len;
+
+ struct dht dht;
+ dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
+ struct nodeid other = (struct nodeid){.inner={0x61616161, 0x61616161, 0x61616161, 0x61616161, 0x61616161}};
+
+ {
+ struct message* message_cursor = outbuff;
+ proto_begin(&dht, 0, &message_cursor, outbuff+2);
+ remote_len = outbuff[0].dest_len;
+ memcpy(&remote, &outbuff[0].dest, remote_len);
+ }
+ now += 10;
+
+ {
+ // The node responds
+ // We return no new nodes to stop any new pings from going out
+ char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes0:""ee";
+ struct message* message_cursor = outbuff;
+ proto_run(&dht, buff, sizeof(buff), (struct sockaddr_in*)&remote, remote_len, now, &message_cursor, outbuff+2);
+ }
+ struct entry* entry = routing_get(&other);
+ TEST_ASSERT_NOT_NULL(entry);
+
+ now += PROTO_UNCTM;
+ {
+ // The node becomes uncertain
+ struct message* message_cursor = outbuff;
+ proto_run(&dht, NULL, 0, (struct sockaddr_in*)NULL, 0, now, &message_cursor, outbuff+2);
+
+ // Which should create a ping
+ 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);
+ }
+
+ now += 5;
+ {
+ // The node responds
+ char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes0:""ee";
+ struct message* message_cursor = outbuff;
+ proto_run(&dht, buff, sizeof(buff), (struct sockaddr_in*)&remote, remote_len, now, &message_cursor, outbuff+2);
+
+ // The routing table entry should have its expiry time updated
+ struct entry* entry = routing_get(&other);
+ TEST_ASSERT_NOT_NULL(entry);
+ TEST_ASSERT_GREATER_THAN(now, entry->expire);
+ }
+}
diff --git a/test/routing.c b/test/routing.c
index eb899ba..ad6f2c3 100644
--- a/test/routing.c
+++ b/test/routing.c
@@ -219,3 +219,64 @@ void test_get_after_offer_and_remove() {
TEST_ASSERT_NULL(e);
}
+
+void test_close_to_self_load_factor() {
+ routing_flush();
+
+ // The address we are going to store
+ struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};
+
+ // Make a nodeid that is one bit different
+ struct nodeid new = self;
+
+ new.inner[4] += 1;
+ struct entry* entry;
+ TEST_ASSERT_TRUE_MESSAGE(routing_offer(&new, &entry), "Did not accept new entry");
+
+ // Set the entries
+ entry->addr = addr;
+ entry->expire = time(NULL);
+
+ int filled;
+ int total;
+ double load_factor[8] = {0};
+ routing_status(&filled, &total, load_factor, 8);
+
+ TEST_ASSERT_EQUAL(1, filled);
+ TEST_ASSERT_EQUAL(1280, total);
+ // First bucket should have none
+ TEST_ASSERT_EQUAL_DOUBLE(0.0, load_factor[0]);
+ // The final bucket should have the one node
+ TEST_ASSERT_EQUAL_DOUBLE(1.0/(1280/8), load_factor[7]);
+}
+
+void test_far_from_self_load_factor() {
+ routing_flush();
+
+ // The address we are going to store
+ struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 0};
+
+ // Make a nodeid that is one bit different
+ struct nodeid new = self;
+
+ // Flip top bit to make it very dissimilar
+ new.inner[0] ^= 0x80000000;
+ struct entry* entry;
+ TEST_ASSERT_TRUE_MESSAGE(routing_offer(&new, &entry), "Did not accept new entry");
+
+ // Set the entries
+ entry->addr = addr;
+ entry->expire = time(NULL);
+
+ int filled;
+ int total;
+ double load_factor[8] = {0};
+ routing_status(&filled, &total, load_factor, 8);
+
+ TEST_ASSERT_EQUAL(1, filled);
+ TEST_ASSERT_EQUAL(1280, total);
+ // First bucket should have the one node
+ TEST_ASSERT_EQUAL_DOUBLE(1.0/(1280/8), load_factor[0]);
+ // The final bucket should have none
+ TEST_ASSERT_EQUAL_DOUBLE(0.0, load_factor[7]);
+}