diff options
Diffstat (limited to 'test/proto.c')
| -rw-r--r-- | test/proto.c | 113 |
1 files changed, 95 insertions, 18 deletions
diff --git a/test/proto.c b/test/proto.c index 49a77b9..c69a293 100644 --- a/test/proto.c +++ b/test/proto.c @@ -8,6 +8,56 @@ #define IP(a, b, c, d) htonl(a << 24 | b << 16 | c << 8 | d) +void test_create_first_ticket() { + time_t now = TOKEN_VTMO; + struct tokens tokens = {0}; + char hash[SHA256_BLOCK_SIZE] = {0}; + struct addr addr = (struct addr){.ip = IP(128,0,0,1), .port = 6881}; + struct addr addr2 = (struct addr){.ip = IP(128,0,0,2), .port = 6881}; + int rc; + + // First token create should create a new ticket + token_create(&tokens, now, &addr, hash); + + // It should be valid for the address it was issued for + rc = token_validate(&tokens, now, &addr, hash); + TEST_ASSERT_EQUAL(TOK_VALI, rc); + + // But not for some other addr + rc = token_validate(&tokens, now, &addr2, hash); + TEST_ASSERT_EQUAL(TOK_INVA, rc); + + // Time passes and our ticket becomes invalid for issuance + now += TOKEN_ITMO; + + char hash2[SHA256_BLOCK_SIZE]; + token_create(&tokens, now, &addr, hash2); + + // We should get a different token, since we have a new ticket + if(memcmp(hash, hash2, SHA256_BLOCK_SIZE) == 0) { + TEST_FAIL(); + } + + // Which should be valid + rc = token_validate(&tokens, now, &addr, hash2); + TEST_ASSERT_EQUAL(TOK_VALI, rc); + + // And so should the old one (since it's not timed out for validation) + rc = token_validate(&tokens, now, &addr, hash); + TEST_ASSERT_EQUAL(TOK_VALI, rc); + + // More time passes and the original token becomes invalid for validation + now += TOKEN_VTMO - TOKEN_ITMO; + + // Old token should now no longer be validated + rc = token_validate(&tokens, now, &addr, hash); + TEST_ASSERT_EQUAL(TOK_INVA, rc); + + // New token should + rc = token_validate(&tokens, now, &addr, hash2); + TEST_ASSERT_EQUAL(TOK_VALI, rc); +} + void test_begin_pings_bootstrap_node() { struct message outbuff[10] = {0}; @@ -48,6 +98,11 @@ void test_response_from_initial_probe() { 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); + + struct nodeid target; + memcpy(&target, outbuff[0].payload+43, sizeof(struct nodeid)); + TEST_ASSERT_EQUAL_MEMORY_MESSAGE(&dht.self, &target, sizeof(struct nodeid), "Target should be our own id"); + 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 @@ -116,7 +171,7 @@ void test_ping() { TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1); TEST_ASSERT_EQUAL(47, outbuff[0].payload_len); - TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:r1:rd2:id20:BBBBBBBBBBBBBBBBBBBBee", outbuff[0].payload, 47); + TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:rd2:id20:BBBBBBBBBBBBBBBBBBBBe1:t2:aa1:y1:re", outbuff[0].payload, 47); } void test_unknown_method() { @@ -143,7 +198,7 @@ void test_unknown_method() { TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1); TEST_ASSERT_EQUAL(42, outbuff[0].payload_len); - TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:e1:eli204e14:Unknown Methodee", outbuff[0].payload, 42); + TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:eli204e14:Unknown Methode1:t2:aa1:y1:ee", outbuff[0].payload, 42); } void test_note_times_out() { @@ -164,7 +219,7 @@ void test_note_times_out() { // The node responds to the ping at t=5 { - char buff[] = "d1:y1:r1:t1:01:rd2:id20:aaaaaaaaaaaaaaaaaaaa5:nodes26:bbbbbbbbbbbbbbbbbbbb\xFF\xFF\xFF\xFF\x00\x00""ee"; + char buff[] = "d1:y1:r1:t1:01:rd2:id20:BB\x5F""aaaaaaaaaaaaaaaaa5: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, 5, &message_cursor, outbuff+2); } @@ -177,6 +232,12 @@ void test_note_times_out() { TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+2); TEST_ASSERT_EQUAL(91, outbuff[1].payload_len); TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:ad2:id20:BBBBBBBBBBBBBBBBBBBB6:target20:", outbuff[1].payload, 43); + + // The target should be in the same bucket as the queried node + struct nodeid target; + memcpy(&target, outbuff[1].payload+43, sizeof(struct nodeid)); + TEST_ASSERT_GREATER_THAN(19, prefix(&dht.self, &target)); + TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:q9:find_node1:t1:01:y1:qe", outbuff[1].payload+63, 28); } @@ -199,6 +260,11 @@ void test_response_after_retry() { 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); + + struct nodeid target; + memcpy(&target, outbuff[0].payload+43, sizeof(struct nodeid)); + TEST_ASSERT_EQUAL_MEMORY_MESSAGE(&dht.self, &target, sizeof(struct nodeid), "Target should be our own id"); + TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:q9:find_node1:t1:01:y1:qe", outbuff[0].payload+63, 28); } @@ -367,10 +433,10 @@ void test_query_find_node() { TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1); TEST_ASSERT_EQUAL(83, outbuff[0].payload_len); - TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:r1:rd2:id20:BBBBBBBBBBBBBBBBBBBB5:nodes26:aaaaaaaaaaaaaaaaaaaa", outbuff[0].payload, 75); - TEST_ASSERT_EQUAL_MEMORY(&((struct sockaddr_in*)&remote)->sin_addr.s_addr, outbuff[0].payload+75, 4); - TEST_ASSERT_EQUAL_MEMORY(&((struct sockaddr_in*)&remote)->sin_port, outbuff[0].payload+79, 2); - TEST_ASSERT_EQUAL_CHAR_ARRAY("ee", outbuff[0].payload+81, 2); + TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:rd2:id20:BBBBBBBBBBBBBBBBBBBB5:nodes26:aaaaaaaaaaaaaaaaaaaa", outbuff[0].payload, 62); + TEST_ASSERT_EQUAL_MEMORY(&((struct sockaddr_in*)&remote)->sin_addr.s_addr, outbuff[0].payload+62, 4); + TEST_ASSERT_EQUAL_MEMORY(&((struct sockaddr_in*)&remote)->sin_port, outbuff[0].payload+66, 2); + TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:t2:aa1:y1:re", outbuff[0].payload+68, 15); } } @@ -383,7 +449,7 @@ void test_query_get_peers_have_one() { allocate_hashtable(); - struct dht dht; + struct dht dht = {0}; dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}}; routing_init(&dht.self); @@ -405,6 +471,7 @@ void test_query_get_peers_have_one() { now += 1; // Some other node then asks for peers + char token[SHA256_BLOCK_SIZE]; { struct sockaddr_in other; other.sin_family = AF_INET; @@ -419,16 +486,20 @@ void test_query_get_peers_have_one() { // We should have sent a response TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1); - TEST_ASSERT_EQUAL(93, outbuff[0].payload_len); + TEST_ASSERT_EQUAL(125, outbuff[0].payload_len); char* cursor = outbuff[0].payload; - TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:r1:rd2:id20:BBBBBBBBBBBBBBBBBBBB5:token1:t5:nodes26:aaaaaaaaaaaaaaaaaaaa", cursor, 85); - cursor+=85; + TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:rd2:id20:BBBBBBBBBBBBBBBBBBBB5:nodes26:aaaaaaaaaaaaaaaaaaaa", cursor, 62); + cursor+=62; TEST_ASSERT_EQUAL_MEMORY(&((struct sockaddr_in*)&remote)->sin_addr.s_addr, cursor, 4); cursor+=4; TEST_ASSERT_EQUAL_MEMORY(&((struct sockaddr_in*)&remote)->sin_port, cursor, 2); cursor+=2; - TEST_ASSERT_EQUAL_CHAR_ARRAY("ee", cursor, 2); - cursor+=2; + TEST_ASSERT_EQUAL_CHAR_ARRAY("5:token32:", cursor, 10); + cursor+=10; + memcpy(token, cursor, SHA256_BLOCK_SIZE); + cursor+=SHA256_BLOCK_SIZE; + TEST_ASSERT_EQUAL_CHAR_ARRAY("e1:t2:aa1:y1:re", cursor, 15); + cursor+=15; } now += 1; @@ -440,14 +511,16 @@ void test_query_get_peers_have_one() { other.sin_port = htons(9090); // Node announces that it's a peer for that torrent - char buff[] = "d1:ad2:id20:abcdefghij012345678912:implied_porti1e9:info_hash20:aaaaaaaaaaaaaaaaaaaa4:porti1337e5:token1:te1:q13:announce_peer1:t2:aa1:y1:qe"; + char buff[] = "d1:ad2:id20:abcdefghij012345678912:implied_porti1e9:info_hash20:aaaaaaaaaaaaaaaaaaaa4:porti1337e5:token32: e1:q13:announce_peer1:t2:aa1:y1:qe"; + memcpy(buff+106, token, SHA256_BLOCK_SIZE); + dbg("PKT %s", buff); struct message* message_cursor = outbuff; proto_run(&dht, buff, sizeof(buff), (struct sockaddr_in*)&other, sizeof(other), now, &message_cursor, outbuff+2); TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1); TEST_ASSERT_EQUAL(47, outbuff[0].payload_len); char* cursor = outbuff[0].payload; - TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:r1:rd2:id20:BBBBBBBBBBBBBBBBBBBBee", cursor, 47); + TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:rd2:id20:BBBBBBBBBBBBBBBBBBBBe1:t2:aa1:y1:re", cursor, 47); cursor+=47; } now += 1; @@ -466,9 +539,13 @@ void test_query_get_peers_have_one() { // We should have sent a response TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1); - TEST_ASSERT_EQUAL(75, outbuff[0].payload_len); + TEST_ASSERT_EQUAL(107, outbuff[0].payload_len); char* cursor = outbuff[0].payload; - TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:r1:rd2:id20:BBBBBBBBBBBBBBBBBBBB5:token1:t6:valuesl6:\x80\x00\x00\x01\x23\x82""eee", cursor, 75); - cursor+=75; + TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:rd2:id20:BBBBBBBBBBBBBBBBBBBB5:token32:", cursor, 42); + cursor+=42; + // Don't care what the token is + cursor+=32; + TEST_ASSERT_EQUAL_CHAR_ARRAY("6:valuesl6:\x80\x00\x00\x01\x23\x82""ee1:t2:aa1:y1:re", cursor, 33); + cursor+=33; } } |
