diff options
| author | Jesper Jensen <jesper@slashwin.dk> | 2021-08-28 22:27:24 +0200 |
|---|---|---|
| committer | Jesper Jensen <jesper@slashwin.dk> | 2021-08-28 22:27:24 +0200 |
| commit | e00efb4381c5123699e4ef9807b67eba54ea06a0 (patch) | |
| tree | df2d7567a20e2313d576fb00aab61be8ee3383fa /test | |
| parent | de3bec63c63b0c30a5a4e1d9b4a5478984b888e9 (diff) | |
Initial fanout implemented
Diffstat (limited to 'test')
| -rw-r--r-- | test/benc.c | 366 | ||||
| -rw-r--r-- | test/routing.c | 51 |
2 files changed, 417 insertions, 0 deletions
diff --git a/test/benc.c b/test/benc.c new file mode 100644 index 0000000..a60e8f0 --- /dev/null +++ b/test/benc.c @@ -0,0 +1,366 @@ +#include "unity.h" +#include "benc.h" + +#include <string.h> + +void test_string() { + struct benc_node stream[1]; + char* packet = "3:abc"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 1); + + TEST_ASSERT_EQUAL(1, len); + TEST_ASSERT_EQUAL(BNT_STRING, stream[0].type); + TEST_ASSERT_EQUAL(3, stream[0].size); + TEST_ASSERT_EQUAL_PTR(packet + 2, stream[0].loc); +} + +void test_unfinished_string() { + struct benc_node stream[1]; + // There only 3 chars available + char* packet = "4:abc"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 1); + + TEST_ASSERT_EQUAL(0, len); + TEST_ASSERT_EQUAL(BNT_STRING, stream[0].type); + TEST_ASSERT_EQUAL(4, stream[0].size); + TEST_ASSERT_EQUAL_PTR(packet + 2, stream[0].loc); +} + +void test_positive_int() { + struct benc_node stream[1]; + char* packet = "i3e"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 1); + + TEST_ASSERT_EQUAL(1, len); + TEST_ASSERT_EQUAL(BNT_INT, stream[0].type); + TEST_ASSERT_EQUAL(1, stream[0].size); + TEST_ASSERT_EQUAL_PTR(packet + 1, stream[0].loc); +} + +void test_multiple_int() { + struct benc_node stream[2]; + char* packet = "i3ei3e"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 2); + + // Stop after the first int + TEST_ASSERT_EQUAL(packet + 3, cursor); + TEST_ASSERT_EQUAL(1, len); + TEST_ASSERT_EQUAL(BNT_INT, stream[0].type); + TEST_ASSERT_EQUAL(1, stream[0].size); + TEST_ASSERT_EQUAL_PTR(packet + 1, stream[0].loc); +} + +void test_unfinished_int() { + struct benc_node stream[1]; + char* packet = "i3"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 1); + + TEST_ASSERT_EQUAL(0, len); + TEST_ASSERT_EQUAL(BNT_INT, stream[0].type); + TEST_ASSERT_EQUAL_PTR(packet + 1, stream[0].loc); +} + +void test_negative_int() { + struct benc_node stream[1]; + char* packet = "i-3e"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 1); + + TEST_ASSERT_EQUAL(1, len); + TEST_ASSERT_EQUAL(BNT_INT, stream[0].type); + TEST_ASSERT_EQUAL(2, stream[0].size); + TEST_ASSERT_EQUAL_PTR(packet + 1, stream[0].loc); +} + +void test_incorrect_int_char() { + struct benc_node stream[1]; + char* packet = "i-a3e"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 1); + + TEST_ASSERT_EQUAL(packet + 2, cursor); + TEST_ASSERT_EQUAL(0, len); + TEST_ASSERT_EQUAL(BNT_INT, stream[0].type); + TEST_ASSERT_EQUAL_PTR(packet + 1, stream[0].loc); +} + +void test_list() { + struct benc_node stream[3]; + char* packet = "li1ee"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 3); + + TEST_ASSERT_EQUAL(3, len); + size_t nodeCursor = 0; + TEST_ASSERT_EQUAL(BNT_LIST, stream[nodeCursor].type); + TEST_ASSERT_EQUAL(0, stream[nodeCursor].depth); + TEST_ASSERT_EQUAL_PTR(packet + 0, stream[nodeCursor].loc); + nodeCursor++; + TEST_ASSERT_EQUAL(BNT_INT, stream[nodeCursor].type); + nodeCursor++; + TEST_ASSERT_EQUAL(BNT_END, stream[nodeCursor].type); + TEST_ASSERT_EQUAL(0, stream[nodeCursor].depth); + TEST_ASSERT_EQUAL_PTR(packet + 4, stream[nodeCursor].loc); + nodeCursor++; +} + +void test_dict() { + struct benc_node stream[4]; + char* packet = "d1:a1:be"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 4); + + TEST_ASSERT_EQUAL(4, len); + size_t nodeCursor = 0; + TEST_ASSERT_EQUAL(BNT_DICT, stream[nodeCursor].type); + TEST_ASSERT_EQUAL(0, stream[nodeCursor].depth); + TEST_ASSERT_EQUAL_PTR(packet + 0, stream[nodeCursor].loc); + nodeCursor++; + TEST_ASSERT_EQUAL(BNT_STRING, stream[nodeCursor].type); + nodeCursor++; + TEST_ASSERT_EQUAL(BNT_STRING, stream[nodeCursor].type); + nodeCursor++; + TEST_ASSERT_EQUAL(BNT_END, stream[nodeCursor].type); + TEST_ASSERT_EQUAL(0, stream[nodeCursor].depth); + TEST_ASSERT_EQUAL_PTR(packet + 7, stream[nodeCursor].loc); + nodeCursor++; +} + +void test_nested_list() { + struct benc_node stream[5]; + char* packet = "lli1eee"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 5); + + TEST_ASSERT_EQUAL(5, len); + size_t nodeCursor = 0; + TEST_ASSERT_EQUAL(BNT_LIST, stream[nodeCursor].type); + TEST_ASSERT_EQUAL(0, stream[nodeCursor].depth); + TEST_ASSERT_EQUAL_PTR(packet + 0, stream[nodeCursor].loc); + nodeCursor++; + TEST_ASSERT_EQUAL(BNT_LIST, stream[nodeCursor].type); + TEST_ASSERT_EQUAL(1, stream[nodeCursor].depth); + TEST_ASSERT_EQUAL_PTR(packet + 1, stream[nodeCursor].loc); + nodeCursor++; + TEST_ASSERT_EQUAL(BNT_INT, stream[nodeCursor].type); + nodeCursor++; + TEST_ASSERT_EQUAL(BNT_END, stream[nodeCursor].type); + TEST_ASSERT_EQUAL(1, stream[nodeCursor].depth); + TEST_ASSERT_EQUAL_PTR(packet + 5, stream[nodeCursor].loc); + nodeCursor++; + TEST_ASSERT_EQUAL(BNT_END, stream[nodeCursor].type); + TEST_ASSERT_EQUAL(0, stream[nodeCursor].depth); + TEST_ASSERT_EQUAL_PTR(packet + 6, stream[nodeCursor].loc); + nodeCursor++; +} + +void test_parse_int() { + char* packet = "100"; + + const char* cursor = packet; + int64_t val; + bool rc = readint(&cursor, &val); + + TEST_ASSERT_EQUAL(true, rc); + TEST_ASSERT_EQUAL(100, val); + TEST_ASSERT_EQUAL_PTR(packet + 3, cursor); +} + +void test_parse_negative_int() { + char* packet = "-100"; + + const char* cursor = packet; + int64_t val; + bool rc = readint(&cursor, &val); + + TEST_ASSERT_EQUAL(true, rc); + TEST_ASSERT_EQUAL(-100, val); + TEST_ASSERT_EQUAL_PTR(packet + 4, cursor); +} + +void test_stop_at_non_digit() { + char* packet = "10a1"; + + const char* cursor = packet; + int64_t val; + bool rc = readint(&cursor, &val); + + TEST_ASSERT_EQUAL(true, rc); + TEST_ASSERT_EQUAL(10, val); + TEST_ASSERT_EQUAL_PTR(packet + 2, cursor); +} + +void test_stop_embedded_minus() { + char* packet = "10-1"; + + const char* cursor = packet; + int64_t val; + bool rc = readint(&cursor, &val); + + TEST_ASSERT_EQUAL(true, rc); + TEST_ASSERT_EQUAL(10, val); + TEST_ASSERT_EQUAL_PTR(packet + 2, cursor); +} + +void test_find_key_under_cursor() { + struct benc_node stream[4]; + char* packet = "d1:ri1ee"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 4); + + + const struct benc_node* stream_cursor = stream; + stream_cursor++; // Skip the dict + ssize_t found = skip_to_key(&stream_cursor, stream+len, (const enum benc_nodetype[]){BNT_STRING}, (const char*[]){"r"}, (const size_t[]){1}, 1); + + TEST_ASSERT_EQUAL(0, found); + TEST_ASSERT_EQUAL_PTR(stream+1, stream_cursor); +} + +void test_key_not_found() { + struct benc_node stream[4]; + char* packet = "d1:ri1ee"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 4); + + + const struct benc_node* stream_cursor = stream; + stream_cursor++; // Skip the dict + ssize_t found = skip_to_key(&stream_cursor, stream+len, (const enum benc_nodetype[]){BNT_STRING}, (const char*[]){"a"}, (const size_t[]){1}, 1); + + TEST_ASSERT_EQUAL(-1, found); + TEST_ASSERT_EQUAL_PTR(stream+3, stream_cursor); +} + +void test_skip_nested_list_value() { + struct benc_node stream[8]; + char* packet = "d1:al1:re1:ri1ee"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 8); + + + const struct benc_node* stream_cursor = stream; + stream_cursor++; // We know the first is a dict + ssize_t found = skip_to_key(&stream_cursor, stream+len, (const enum benc_nodetype[]){BNT_STRING}, (const char*[]){"r"}, (const size_t[]){1}, 1); + + TEST_ASSERT_EQUAL(0, found); + TEST_ASSERT_EQUAL_PTR(stream+5, stream_cursor); +} + +void test_skip_nested_dict_value() { + struct benc_node stream[9]; + char* packet = "d1:adi2e1:re1:ri1ee"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 9); + + const struct benc_node* stream_cursor = stream; + stream_cursor++; // We know the first is a dict + ssize_t found = skip_to_key(&stream_cursor, stream+len, (const enum benc_nodetype[]){BNT_STRING}, (const char*[]){"r"}, (const size_t[]){1}, 1); + + TEST_ASSERT_EQUAL(0, found); + TEST_ASSERT_EQUAL_PTR(stream+6, stream_cursor); +} + +void test_skip_nested_dict_key() { + struct benc_node stream[9]; + char* packet = "d1:ad1:ri2ee1:ri1ee"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 9); + + const struct benc_node* stream_cursor = stream; + stream_cursor++; // We know the first is a dict + ssize_t found = skip_to_key(&stream_cursor, stream+len, (const enum benc_nodetype[]){BNT_STRING}, (const char*[]){"r"}, (const size_t[]){1}, 1); + + TEST_ASSERT_EQUAL(0, found); + TEST_ASSERT_EQUAL_PTR(stream+6, stream_cursor); +} + +void test_skip_multilevel_list() { + struct benc_node stream[9]; + char* packet = "d1:all1:ree1:ri1ee"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 9); + + const struct benc_node* stream_cursor = stream; + stream_cursor++; // We know the first is a dict + ssize_t found = skip_to_key(&stream_cursor, stream+len, (const enum benc_nodetype[]){BNT_STRING}, (const char*[]){"r"}, (const size_t[]){1}, 1); + + TEST_ASSERT_EQUAL(0, found); + TEST_ASSERT_EQUAL_PTR(stream+7, stream_cursor); +} + +void test_find_any_semantics() { + struct benc_node stream[9]; + char* packet = "d1:a1:a1:b1:be"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 9); + + const struct benc_node* stream_cursor = stream; + stream_cursor++; // We know the first is a dict + ssize_t found = skip_to_key(&stream_cursor, stream+len, (const enum benc_nodetype[]){BNT_STRING, BNT_STRING}, (const char*[]){"b", "a"}, (const size_t[]){1, 1}, 2); + + TEST_ASSERT_EQUAL(1, found); + TEST_ASSERT_EQUAL_PTR(stream+1, stream_cursor); + + stream_cursor+=2; // Skip the key and value + found = skip_to_key(&stream_cursor, stream+len, (const enum benc_nodetype[]){BNT_STRING, BNT_STRING}, (const char*[]){"b", "a"}, (const size_t[]){1, 1}, 2); + TEST_ASSERT_EQUAL(0, found); + TEST_ASSERT_EQUAL_PTR(stream+3, stream_cursor); +} + +void test_stop_at_end() { + struct benc_node stream[9]; + char* packet = "ld1:a1:aee"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 9); + + const struct benc_node* stream_cursor = stream; + stream_cursor++; // We know the first is a list + stream_cursor++; // We know the second is a dict + // X does not exist in the packet + ssize_t found = skip_to_key(&stream_cursor, stream+len, (const enum benc_nodetype[]){BNT_STRING}, (const char*[]){"x"}, (const size_t[]){1}, 1); + + TEST_ASSERT_EQUAL_MESSAGE(-1, found, "Found something"); + TEST_ASSERT_EQUAL_PTR_MESSAGE(stream+4, stream_cursor, "Didn't stop at dict end"); +} diff --git a/test/routing.c b/test/routing.c index c4b7730..60a7f03 100644 --- a/test/routing.c +++ b/test/routing.c @@ -91,3 +91,54 @@ void test_discard_offer_when_nodeid_added_twice() { bool accept = routing_offer(&new, &entry); TEST_ASSERT_FALSE_MESSAGE(accept, "Accepted entry when bucket was full"); } + +void test_interested_when_space_in_bucket() { + routing_flush(); + + struct nodeid new = self; + new.inner[4] ^= 0x00000001; + + bool interest = routing_interested(&new); + + TEST_ASSERT_TRUE_MESSAGE(interest, "Was not interested in node"); +} + +void test_not_interested_when_nodeid_in_table() { + routing_flush(); + + struct nodeid new = self; + new.inner[4] ^= 0x00000001; + + struct entry* entry; + TEST_ASSERT_TRUE(routing_offer(&new, &entry)); + entry->addr = (struct addr){.ip = IP(128,0,0,1), .port = 0}; + entry->last = time(NULL); + + bool interest = routing_interested(&new); + TEST_ASSERT_FALSE_MESSAGE(interest, "Was interested in node"); +} + +void test_not_interested_when_bucket_is_full() { + 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[0] ^= 0x80000000; + + // Fill up the bucket with entries + for(uint8_t i = 0; i < 8; i++) { + struct entry* entry; + TEST_ASSERT_TRUE_MESSAGE(routing_offer(&new, &entry), "Did not accept new entry"); + + // Set the entries + entry->addr = addr; + entry->last = time(NULL); + + new.inner[4] += 1; + } + + TEST_ASSERT_FALSE_MESSAGE(routing_interested(&new), "Still interested when bucket was full"); +} |
