diff options
| author | Jesper Jensen <jesper@slashwin.dk> | 2021-09-03 22:32:42 +0200 |
|---|---|---|
| committer | Jesper Jensen <jesper@slashwin.dk> | 2021-09-03 22:32:42 +0200 |
| commit | 9b4973e7d96a3605583c3f9a80567945c8d4be71 (patch) | |
| tree | 3f17876a557a15ffe2a90591e157d1065a444a11 /test | |
| parent | 5471d80c5350f764ec53733bae9d32c0a1a339e0 (diff) | |
Add ping support
Diffstat (limited to 'test')
| -rw-r--r-- | test/benc.c | 35 | ||||
| -rw-r--r-- | test/query.c | 133 |
2 files changed, 165 insertions, 3 deletions
diff --git a/test/benc.c b/test/benc.c index d734907..a9d6ddb 100644 --- a/test/benc.c +++ b/test/benc.c @@ -26,7 +26,7 @@ void test_unfinished_string() { int depth = 0; int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 1); - TEST_ASSERT_EQUAL(0, len); + TEST_ASSERT_EQUAL(-BENC_EBADP, 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); @@ -70,7 +70,7 @@ void test_unfinished_int() { int depth = 0; int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 1); - TEST_ASSERT_EQUAL(0, len); + TEST_ASSERT_EQUAL(-BENC_EBADP, len); TEST_ASSERT_EQUAL(BNT_INT, stream[0].type); TEST_ASSERT_EQUAL_PTR(packet + 1, stream[0].loc); } @@ -98,7 +98,7 @@ void test_incorrect_int_char() { 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(-BENC_EBADP, len); TEST_ASSERT_EQUAL(BNT_INT, stream[0].type); TEST_ASSERT_EQUAL_PTR(packet + 1, stream[0].loc); } @@ -149,6 +149,17 @@ void test_dict() { nodeCursor++; } +void test_dict_without_end() { + struct benc_node stream[4]; + char* packet = "d"; + + const char* cursor = packet; + int depth = 0; + int64_t len = benc_decode(&cursor, cursor + strlen(packet), &depth, stream, 4); + + TEST_ASSERT_EQUAL(-1, len); +} + void test_nested_list() { struct benc_node stream[5]; char* packet = "lli1eee"; @@ -343,3 +354,21 @@ void test_stop_at_end() { TEST_ASSERT_EQUAL_MESSAGE(-1, found, "Found something"); TEST_ASSERT_EQUAL_PTR_MESSAGE(stream+4, bcursor.readhead, "Didn't stop at dict end"); } + +void test_find_key_with_small_token_buffer() { + struct benc_node stream[1]; + char* packet = "d1:a1:a1:b1:b1:c1:ce"; + + struct bcursor bcursor; + bcur_open(&bcursor, packet, packet+strlen(packet), stream, 1); + bcur_next(&bcursor, 1); // Skip the dict token + + ssize_t found = bcur_find_key(&bcursor, (const enum benc_nodetype[]){BNT_STRING}, (const char*[]){"c"}, (const size_t[]){1}, 1); + + TEST_ASSERT_EQUAL(0, found); + TEST_ASSERT_EQUAL(BNT_STRING, bcursor.readhead->type); + TEST_ASSERT_EQUAL(1, bcursor.readhead->size); + TEST_ASSERT_EQUAL_STRING_LEN("c", bcursor.readhead->loc, 1); + bcur_next(&bcursor, 2); + TEST_ASSERT_EQUAL(BNT_END, bcursor.readhead->type); +} diff --git a/test/query.c b/test/query.c new file mode 100644 index 0000000..cccb639 --- /dev/null +++ b/test/query.c @@ -0,0 +1,133 @@ +#include <unity.h> + +#include "query.h" + +#include <string.h> + +void test_malformed_empty() { + struct nodeid self = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}}; + char* packet = ""; + size_t packet_len = 0; + + char response[1024]; + char* response_cursor = response; + char* response_end = response + sizeof(response); + + int rc = handle_request(&self, "ping", packet, packet_len, &response_cursor, response_end-response_cursor); + + TEST_ASSERT_EQUAL(QUERY_EBADQ, rc); +} + +void test_malformed_only_dict_start() { + struct nodeid self = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}}; + char* packet = "d"; + size_t packet_len = 1; + + char response[1024]; + char* response_cursor = response; + char* response_end = response + sizeof(response); + + int rc = handle_request(&self, "ping", packet, packet_len, &response_cursor, response_end-response_cursor); + + TEST_ASSERT_EQUAL(QUERY_EBADQ, rc); +} + +void test_malformed_empty_args_key() { + struct nodeid self = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}}; + char* packet = "d1:ae"; + size_t packet_len = strlen(packet); + + char response[1024]; + char* response_cursor = response; + char* response_end = response + sizeof(response); + + int rc = handle_request(&self, "ping", packet, packet_len, &response_cursor, response_end-response_cursor); + + TEST_ASSERT_EQUAL(QUERY_EBADQ, rc); +} + +void test_malformed_wrong_args_type() { + struct nodeid self = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}}; + char* packet = "d1:a1:re"; + size_t packet_len = strlen(packet); + + char response[1024]; + char* response_cursor = response; + char* response_end = response + sizeof(response); + + int rc = handle_request(&self, "ping", packet, packet_len, &response_cursor, response_end-response_cursor); + + TEST_ASSERT_EQUAL(QUERY_EBADQ, rc); +} + +void test_malformed_empty_args() { + struct nodeid self = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}}; + char* packet = "d1:adee"; + size_t packet_len = strlen(packet); + + char response[1024]; + char* response_cursor = response; + char* response_end = response + sizeof(response); + + int rc = handle_request(&self, "ping", packet, packet_len, &response_cursor, response_end-response_cursor); + + TEST_ASSERT_EQUAL(QUERY_EBADQ, rc); +} + +void test_malformed_wrong_id_arg_type() { + struct nodeid self = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}}; + char* packet = "d1:ad2:idi1eee"; + size_t packet_len = strlen(packet); + + char response[1024]; + char* response_cursor = response; + char* response_end = response + sizeof(response); + + int rc = handle_request(&self, "ping", packet, packet_len, &response_cursor, response_end-response_cursor); + + TEST_ASSERT_EQUAL(QUERY_EBADQ, rc); +} + +void test_malformed_wrong_id_length() { + struct nodeid self = {.inner={0x0034048f, 0x08000020, 0x00888880, 0x02008460, 0x0ab00521}}; + char* packet = "d1:ad2:id19:aaaaaaaaaaaaaaaaaaaee"; + size_t packet_len = strlen(packet); + + char response[1024]; + char* response_cursor = response; + char* response_end = response + sizeof(response); + + int rc = handle_request(&self, "ping", packet, packet_len, &response_cursor, response_end-response_cursor); + + TEST_ASSERT_EQUAL(QUERY_EBADQ, rc); +} + +void test_ping() { + struct nodeid self = {.inner_b={"aaaaaaaaaaaaaaaaaaab"}}; + char* packet = "d1:ad2:id20:aaaaaaaaaaaaaaaaaaaaee"; + size_t packet_len = strlen(packet); + + char response[1024]; + char* response_cursor = response; + char* response_end = response + sizeof(response); + + int rc = handle_request(&self, "ping", packet, packet_len, &response_cursor, response_end-response_cursor); + + TEST_ASSERT_EQUAL(0, rc); + TEST_ASSERT_EQUAL(30, response_cursor - response); + TEST_ASSERT_EQUAL_CHAR_ARRAY("d2:id20:aaaaaaaaaaaaaaaaaaabee", response, 30); +} + +void test_bad_method() { + struct nodeid self = {.inner_b={"aaaaaaaaaaaaaaaaaaab"}}; + char* packet = "de"; + size_t packet_len = strlen(packet); + + char response[1024]; + char* response_cursor = response; + char* response_end = response + sizeof(response); + + int rc = handle_request(&self, "someWrongMethod", packet, packet_len, &response_cursor, response_end-response_cursor); + + TEST_ASSERT_EQUAL(QUERY_EUNK, rc); +} |
