diff options
author | irungentoo <irungentoo@gmail.com> | 2014-01-05 19:22:38 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-01-05 19:22:38 -0500 |
commit | 6cd1e7fb707e20681e066b0a7e452e15c227c280 (patch) | |
tree | d36892dd4affaedbd47e90f5066b2dc6cda630da /auto_tests | |
parent | 7e2d21271a549347575a38e5c3859d7374d496ed (diff) |
Tests added and some fixes for the onion part.
Diffstat (limited to 'auto_tests')
-rw-r--r-- | auto_tests/onion_test.c | 101 |
1 files changed, 98 insertions, 3 deletions
diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 684b7a4f..57ac71d8 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c | |||
@@ -12,6 +12,13 @@ | |||
12 | #include "../toxcore/onion.h" | 12 | #include "../toxcore/onion.h" |
13 | #include "../toxcore/onion_announce.h" | 13 | #include "../toxcore/onion_announce.h" |
14 | 14 | ||
15 | #ifdef __WIN32__ | ||
16 | #define c_sleep(x) Sleep(1*x) | ||
17 | #else | ||
18 | #include <unistd.h> | ||
19 | #define c_sleep(x) usleep(1000*x) | ||
20 | #endif | ||
21 | |||
15 | void do_onion(Onion *onion) | 22 | void do_onion(Onion *onion) |
16 | { | 23 | { |
17 | networking_poll(onion->net); | 24 | networking_poll(onion->net); |
@@ -46,6 +53,38 @@ static int handle_test_2(void *object, IP_Port source, uint8_t *packet, uint32_t | |||
46 | handled_test_2 = 1; | 53 | handled_test_2 = 1; |
47 | return 0; | 54 | return 0; |
48 | } | 55 | } |
56 | /* | ||
57 | void print_client_id(uint8_t *client_id, uint32_t length) | ||
58 | { | ||
59 | uint32_t j; | ||
60 | |||
61 | for (j = 0; j < length; j++) { | ||
62 | printf("%02hhX", client_id[j]); | ||
63 | } | ||
64 | printf("\n"); | ||
65 | } | ||
66 | */ | ||
67 | static int handled_test_3; | ||
68 | uint8_t test_3_pub_key[crypto_box_PUBLICKEYBYTES]; | ||
69 | uint8_t test_3_ping_id[crypto_hash_sha256_BYTES]; | ||
70 | static int handle_test_3(void *object, IP_Port source, uint8_t *packet, uint32_t length) | ||
71 | { | ||
72 | Onion *onion = object; | ||
73 | |||
74 | if (length != (1 + crypto_box_NONCEBYTES + crypto_hash_sha256_BYTES + crypto_box_MACBYTES)) | ||
75 | return 1; | ||
76 | |||
77 | //print_client_id(packet, length); | ||
78 | int len = decrypt_data(test_3_pub_key, onion->dht->c->self_secret_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES, | ||
79 | crypto_hash_sha256_BYTES + crypto_box_MACBYTES, test_3_ping_id); | ||
80 | |||
81 | if (len == -1) | ||
82 | return 1; | ||
83 | |||
84 | //print_client_id(test_3_ping_id, sizeof(test_3_ping_id)); | ||
85 | handled_test_3 = 1; | ||
86 | return 0; | ||
87 | } | ||
49 | 88 | ||
50 | START_TEST(test_basic) | 89 | START_TEST(test_basic) |
51 | { | 90 | { |
@@ -72,7 +111,7 @@ START_TEST(test_basic) | |||
72 | nodes[1] = n2; | 111 | nodes[1] = n2; |
73 | nodes[2] = n1; | 112 | nodes[2] = n1; |
74 | nodes[3] = n2; | 113 | nodes[3] = n2; |
75 | int ret = send_onion_packet(onion1, nodes, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); | 114 | int ret = send_onion_packet(onion1->dht, nodes, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); |
76 | ck_assert_msg(ret == 0, "Failed to create/send onion packet."); | 115 | ck_assert_msg(ret == 0, "Failed to create/send onion packet."); |
77 | 116 | ||
78 | handled_test_1 = 0; | 117 | handled_test_1 = 0; |
@@ -89,13 +128,69 @@ START_TEST(test_basic) | |||
89 | do_onion(onion1); | 128 | do_onion(onion1); |
90 | do_onion(onion2); | 129 | do_onion(onion2); |
91 | } | 130 | } |
131 | |||
132 | Onion_Announce *onion1_a = new_onion_announce(onion1->dht); | ||
133 | Onion_Announce *onion2_a = new_onion_announce(onion2->dht); | ||
134 | networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1); | ||
135 | ck_assert_msg((onion1_a != NULL) && (onion2_a != NULL), "Onion_Announce failed initializing."); | ||
136 | uint8_t zeroes[64] = {0}; | ||
137 | memcpy(test_3_pub_key, nodes[3].client_id, crypto_box_PUBLICKEYBYTES); | ||
138 | ret = send_announce_request(onion1->dht, nodes, onion1->dht->c->self_public_key, onion1->dht->c->self_secret_key, | ||
139 | zeroes, onion1->dht->c->self_public_key); | ||
140 | ck_assert_msg(ret == 0, "Failed to create/send onion announce_request packet."); | ||
141 | handled_test_3 = 0; | ||
142 | |||
143 | while (handled_test_3 == 0) { | ||
144 | do_onion(onion1); | ||
145 | do_onion(onion2); | ||
146 | } | ||
147 | |||
148 | send_announce_request(onion1->dht, nodes, onion1->dht->c->self_public_key, onion1->dht->c->self_secret_key, | ||
149 | test_3_ping_id, onion1->dht->c->self_public_key); | ||
150 | |||
151 | while (memcmp(onion2_a->entries[ONION_ANNOUNCE_MAX_ENTRIES - 1].public_key, onion1->dht->c->self_public_key, | ||
152 | crypto_box_PUBLICKEYBYTES) != 0) { | ||
153 | do_onion(onion1); | ||
154 | do_onion(onion2); | ||
155 | c_sleep(50); | ||
156 | } | ||
92 | } | 157 | } |
93 | END_TEST | 158 | END_TEST |
94 | 159 | ||
160 | typedef struct { | ||
161 | Onion *onion; | ||
162 | Onion_Announce *onion_a; | ||
163 | } Onions; | ||
164 | |||
165 | Onions *new_onions(uint16_t port) | ||
166 | { | ||
167 | IP ip; | ||
168 | ip_init(&ip, 1); | ||
169 | ip.ip6.uint8[15] = 1; | ||
170 | Onions *on = malloc(sizeof(Onions)); | ||
171 | DHT *dht = new_DHT(new_net_crypto(new_networking(ip, port))); | ||
172 | on->onion = new_onion(dht); | ||
173 | on->onion_a = new_onion_announce(dht); | ||
174 | |||
175 | if (on->onion && on->onion_a) | ||
176 | return on; | ||
177 | |||
178 | return NULL; | ||
179 | } | ||
180 | |||
181 | #define NUM_ONIONS 64 | ||
182 | |||
95 | START_TEST(test_announce) | 183 | START_TEST(test_announce) |
96 | { | 184 | { |
97 | 185 | uint32_t i; | |
98 | 186 | Onions *onions[NUM_ONIONS]; | |
187 | |||
188 | for (i = 0; i < NUM_ONIONS; ++i) { | ||
189 | onions[i] = new_onions(i + 34655); | ||
190 | ck_assert_msg(onions[i] != 0, "Failed to create onions. %u"); | ||
191 | } | ||
192 | |||
193 | |||
99 | } | 194 | } |
100 | END_TEST | 195 | END_TEST |
101 | 196 | ||