diff options
author | hugbubby <hugbubby@protonmail.com> | 2018-06-21 08:00:25 -0700 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-06-22 19:52:30 +0000 |
commit | 36e20e7e94760b48bee4894cced30814d72c1938 (patch) | |
tree | 650aa83ed4fcca799c3d8ff2a03eb82d5a09aef9 /auto_tests | |
parent | 850d7eac38de09eb9deb48182d6032d164cc6e13 (diff) |
Clarify/Improve test_some test
Better error messages, better sleep() call placements, etc.
Did not modify large chunk of function because I couldn't explain
it. Maybe I'll come back later once I've regained lost brain cells.
Diffstat (limited to 'auto_tests')
-rw-r--r-- | auto_tests/TCP_test.c | 152 |
1 files changed, 95 insertions, 57 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 61a187d1..1f3a7f48 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c | |||
@@ -54,9 +54,9 @@ START_TEST(test_basic) | |||
54 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 54 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
55 | crypto_new_keypair(self_public_key, self_secret_key); | 55 | crypto_new_keypair(self_public_key, self_secret_key); |
56 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr); | 56 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr); |
57 | ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server"); | 57 | ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server."); |
58 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, | 58 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, |
59 | "Failed to bind a TCP relay server to all %d attempted ports", NUM_PORTS); | 59 | "Failed to bind a TCP relay server to all %d attempted ports.", NUM_PORTS); |
60 | 60 | ||
61 | Socket sock; | 61 | Socket sock; |
62 | 62 | ||
@@ -67,7 +67,7 @@ START_TEST(test_basic) | |||
67 | ip_port_loopback.ip = get_loopback(); | 67 | ip_port_loopback.ip = get_loopback(); |
68 | ip_port_loopback.port = net_htons(ports[i]); | 68 | ip_port_loopback.port = net_htons(ports[i]); |
69 | int ret = net_connect(sock, ip_port_loopback); | 69 | int ret = net_connect(sock, ip_port_loopback); |
70 | ck_assert_msg(ret == 0, "Failed to connect to created TCP relay server on port %d", ports[i]); | 70 | ck_assert_msg(ret == 0, "Failed to connect to created TCP relay server on port %d.", ports[i]); |
71 | 71 | ||
72 | // Leave open one connection for the next test. | 72 | // Leave open one connection for the next test. |
73 | if (i + 1 < NUM_PORTS) { | 73 | if (i + 1 < NUM_PORTS) { |
@@ -75,12 +75,14 @@ START_TEST(test_basic) | |||
75 | } | 75 | } |
76 | } | 76 | } |
77 | 77 | ||
78 | // Key creation. | ||
78 | uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 79 | uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
79 | uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 80 | uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
80 | uint8_t f_nonce[CRYPTO_NONCE_SIZE]; | 81 | uint8_t f_nonce[CRYPTO_NONCE_SIZE]; |
81 | crypto_new_keypair(f_public_key, f_secret_key); | 82 | crypto_new_keypair(f_public_key, f_secret_key); |
82 | random_nonce(f_nonce); | 83 | random_nonce(f_nonce); |
83 | 84 | ||
85 | // Generation of the initial handshake. | ||
84 | uint8_t t_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 86 | uint8_t t_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
85 | uint8_t handshake_plain[TCP_HANDSHAKE_PLAIN_SIZE]; | 87 | uint8_t handshake_plain[TCP_HANDSHAKE_PLAIN_SIZE]; |
86 | crypto_new_keypair(handshake_plain, t_secret_key); | 88 | crypto_new_keypair(handshake_plain, t_secret_key); |
@@ -89,34 +91,40 @@ START_TEST(test_basic) | |||
89 | memcpy(handshake, f_public_key, CRYPTO_PUBLIC_KEY_SIZE); | 91 | memcpy(handshake, f_public_key, CRYPTO_PUBLIC_KEY_SIZE); |
90 | random_nonce(handshake + CRYPTO_PUBLIC_KEY_SIZE); | 92 | random_nonce(handshake + CRYPTO_PUBLIC_KEY_SIZE); |
91 | 93 | ||
94 | // Encrypting handshake | ||
92 | int ret = encrypt_data(self_public_key, f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain, | 95 | int ret = encrypt_data(self_public_key, f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain, |
93 | TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); | 96 | TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); |
94 | ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), | 97 | ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), |
95 | "Encrypt failed."); | 98 | "encrypt_data() call failed."); |
99 | |||
100 | // Sending the handshake | ||
96 | ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, | 101 | ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, |
97 | "send Failed."); | 102 | "An attempt to send the initial handshake minus last byte failed."); |
98 | c_sleep(50); | ||
99 | do_TCP_server(tcp_s); | ||
100 | c_sleep(50); | 103 | c_sleep(50); |
101 | do_TCP_server(tcp_s); | 104 | do_TCP_server(tcp_s); |
102 | c_sleep(50); | 105 | c_sleep(50); |
103 | ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, "send Failed."); | 106 | |
104 | c_sleep(50); | 107 | ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, |
105 | do_TCP_server(tcp_s); | 108 | "The attempt to send the last byte of handshake failed."); |
109 | |||
106 | c_sleep(50); | 110 | c_sleep(50); |
107 | do_TCP_server(tcp_s); | 111 | do_TCP_server(tcp_s); |
108 | c_sleep(50); | 112 | c_sleep(50); |
113 | |||
114 | // Receiving server response and decrypting it | ||
109 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; | 115 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; |
110 | uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; | 116 | uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; |
111 | ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); | 117 | ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, |
118 | "Could/did not receive a server response to the initial handshake."); | ||
112 | ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE, | 119 | ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE, |
113 | TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); | 120 | TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); |
114 | ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed."); | 121 | ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Failed to decrypt handshake response."); |
115 | uint8_t f_nonce_r[CRYPTO_NONCE_SIZE]; | 122 | uint8_t f_nonce_r[CRYPTO_NONCE_SIZE]; |
116 | uint8_t f_shared_key[CRYPTO_SHARED_KEY_SIZE]; | 123 | uint8_t f_shared_key[CRYPTO_SHARED_KEY_SIZE]; |
117 | encrypt_precompute(response_plain, t_secret_key, f_shared_key); | 124 | encrypt_precompute(response_plain, t_secret_key, f_shared_key); |
118 | memcpy(f_nonce_r, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE); | 125 | memcpy(f_nonce_r, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE); |
119 | 126 | ||
127 | // Building a request | ||
120 | uint8_t r_req_p[1 + CRYPTO_PUBLIC_KEY_SIZE] = {0}; | 128 | uint8_t r_req_p[1 + CRYPTO_PUBLIC_KEY_SIZE] = {0}; |
121 | memcpy(r_req_p + 1, f_public_key, CRYPTO_PUBLIC_KEY_SIZE); | 129 | memcpy(r_req_p + 1, f_public_key, CRYPTO_PUBLIC_KEY_SIZE); |
122 | uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE]; | 130 | uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE]; |
@@ -125,32 +133,42 @@ START_TEST(test_basic) | |||
125 | encrypt_data_symmetric(f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2); | 133 | encrypt_data_symmetric(f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2); |
126 | increment_nonce(f_nonce); | 134 | increment_nonce(f_nonce); |
127 | memcpy(r_req, &size, 2); | 135 | memcpy(r_req, &size, 2); |
128 | uint32_t i; | ||
129 | 136 | ||
130 | for (i = 0; i < sizeof(r_req); ++i) { | 137 | // Sending the request at random intervals in random pieces. |
131 | ck_assert_msg(net_send(sock, r_req + i, 1) == 1, "send Failed."); | 138 | for (uint32_t i = 0; i < sizeof(r_req);) { |
132 | #if 0 | 139 | uint8_t msg_length = rand() % 5 + 1; // msg_length = 1 to 5 |
133 | ck_assert_msg(net_send(sock, r_req, sizeof(r_req)) == sizeof(r_req), "send Failed."); | 140 | |
134 | #endif | 141 | if (i + msg_length >= sizeof(r_req)) { |
135 | do_TCP_server(tcp_s); | 142 | msg_length = sizeof(r_req) - i; |
143 | } | ||
144 | |||
145 | ck_assert_msg(net_send(sock, r_req + i, msg_length) == msg_length, | ||
146 | "Failed to send request after completing the handshake."); | ||
147 | i += msg_length; | ||
148 | |||
136 | c_sleep(50); | 149 | c_sleep(50); |
150 | do_TCP_server(tcp_s); | ||
137 | } | 151 | } |
138 | 152 | ||
139 | do_TCP_server(tcp_s); | 153 | // Receiving the second response and verifying its validity |
140 | c_sleep(50); | ||
141 | uint8_t packet_resp[4096]; | 154 | uint8_t packet_resp[4096]; |
142 | int recv_data_len = net_recv(sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); | 155 | int recv_data_len = net_recv(sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); |
143 | ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, | 156 | ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, |
144 | "recv Failed. %u", recv_data_len); | 157 | "Failed to receive server response to request. %u", recv_data_len); |
145 | memcpy(&size, packet_resp, 2); | 158 | memcpy(&size, packet_resp, 2); |
146 | ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Wrong packet size."); | 159 | ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, |
160 | "Wrong packet size for request response."); | ||
161 | |||
147 | uint8_t packet_resp_plain[4096]; | 162 | uint8_t packet_resp_plain[4096]; |
148 | ret = decrypt_data_symmetric(f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain); | 163 | ret = decrypt_data_symmetric(f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain); |
149 | ck_assert_msg(ret != -1, "decryption failed"); | 164 | ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response."); |
150 | increment_nonce(f_nonce_r); | 165 | increment_nonce(f_nonce_r); |
151 | ck_assert_msg(packet_resp_plain[0] == 1, "wrong packet id %u", packet_resp_plain[0]); | 166 | |
152 | ck_assert_msg(packet_resp_plain[1] == 0, "connection not refused %u", packet_resp_plain[1]); | 167 | ck_assert_msg(packet_resp_plain[0] == 1, "Server sent the wrong packet id: %u", packet_resp_plain[0]); |
153 | ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "key in packet wrong"); | 168 | ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection."); |
169 | ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "Server sent the wrong public key."); | ||
170 | |||
171 | // Closing connections. | ||
154 | kill_sock(sock); | 172 | kill_sock(sock); |
155 | kill_TCP_server(tcp_s); | 173 | kill_TCP_server(tcp_s); |
156 | } | 174 | } |
@@ -174,7 +192,7 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) | |||
174 | ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); | 192 | ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); |
175 | 193 | ||
176 | int ret = net_connect(sock, ip_port_loopback); | 194 | int ret = net_connect(sock, ip_port_loopback); |
177 | ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); | 195 | ck_assert_msg(ret == 0, "Failed to connect to the test TCP relay server."); |
178 | 196 | ||
179 | uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 197 | uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
180 | crypto_new_keypair(sec_c->public_key, f_secret_key); | 198 | crypto_new_keypair(sec_c->public_key, f_secret_key); |
@@ -191,20 +209,27 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) | |||
191 | ret = encrypt_data(tcp_server_public_key(tcp_s), f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain, | 209 | ret = encrypt_data(tcp_server_public_key(tcp_s), f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain, |
192 | TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); | 210 | TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); |
193 | ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), | 211 | ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), |
194 | "Encrypt failed."); | 212 | "Failed to encrypt the outgoing handshake."); |
213 | |||
195 | ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, | 214 | ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, |
196 | "send Failed."); | 215 | "Failed to send the first portion of the handshake to the TCP relay server."); |
216 | c_sleep(50); | ||
197 | do_TCP_server(tcp_s); | 217 | do_TCP_server(tcp_s); |
198 | c_sleep(50); | 218 | c_sleep(50); |
199 | ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, "send Failed."); | 219 | |
220 | ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, | ||
221 | "Failed to send last byte of handshake."); | ||
200 | c_sleep(50); | 222 | c_sleep(50); |
201 | do_TCP_server(tcp_s); | 223 | do_TCP_server(tcp_s); |
224 | c_sleep(50); | ||
225 | |||
202 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; | 226 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; |
203 | uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; | 227 | uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; |
204 | ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); | 228 | ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, |
229 | "Failed to receive server handshake response."); | ||
205 | ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE, | 230 | ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE, |
206 | TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); | 231 | TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); |
207 | ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed."); | 232 | ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Failed to decrypt server handshake response."); |
208 | encrypt_precompute(response_plain, t_secret_key, sec_c->shared_key); | 233 | encrypt_precompute(response_plain, t_secret_key, sec_c->shared_key); |
209 | memcpy(sec_c->recv_nonce, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE); | 234 | memcpy(sec_c->recv_nonce, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE); |
210 | sec_c->sock = sock; | 235 | sec_c->sock = sock; |
@@ -231,18 +256,18 @@ static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t * | |||
231 | 256 | ||
232 | increment_nonce(con->sent_nonce); | 257 | increment_nonce(con->sent_nonce); |
233 | 258 | ||
234 | ck_assert_msg(net_send(con->sock, packet, SIZEOF_VLA(packet)) == SIZEOF_VLA(packet), "send failed"); | 259 | ck_assert_msg(net_send(con->sock, packet, SIZEOF_VLA(packet)) == SIZEOF_VLA(packet), "Failed to send a packet"); |
235 | return 0; | 260 | return 0; |
236 | } | 261 | } |
237 | 262 | ||
238 | static int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t length) | 263 | static int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t length) |
239 | { | 264 | { |
240 | int len = net_recv(con->sock, data, length); | 265 | int rlen = net_recv(con->sock, data, length); |
241 | ck_assert_msg(len == length, "wrong len %i\n", len); | 266 | ck_assert_msg(rlen == length, "Did not receive packet of correct length. Wanted %i, instead got %i", length, rlen); |
242 | len = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data); | 267 | rlen = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data); |
243 | ck_assert_msg(len != -1, "Decrypt failed"); | 268 | ck_assert_msg(rlen != -1, "Failed to decrypt a received packet from the Relay server."); |
244 | increment_nonce(con->recv_nonce); | 269 | increment_nonce(con->recv_nonce); |
245 | return len; | 270 | return rlen; |
246 | } | 271 | } |
247 | 272 | ||
248 | START_TEST(test_some) | 273 | START_TEST(test_some) |
@@ -260,49 +285,60 @@ START_TEST(test_some) | |||
260 | 285 | ||
261 | uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE]; | 286 | uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE]; |
262 | requ_p[0] = 0; | 287 | requ_p[0] = 0; |
288 | |||
289 | // Sending wrong public keys to test server response. | ||
263 | memcpy(requ_p + 1, con3->public_key, CRYPTO_PUBLIC_KEY_SIZE); | 290 | memcpy(requ_p + 1, con3->public_key, CRYPTO_PUBLIC_KEY_SIZE); |
264 | write_packet_TCP_secure_connection(con1, requ_p, sizeof(requ_p)); | 291 | write_packet_TCP_secure_connection(con1, requ_p, sizeof(requ_p)); |
265 | memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE); | 292 | memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE); |
266 | write_packet_TCP_secure_connection(con3, requ_p, sizeof(requ_p)); | 293 | write_packet_TCP_secure_connection(con3, requ_p, sizeof(requ_p)); |
294 | |||
295 | c_sleep(50); | ||
267 | do_TCP_server(tcp_s); | 296 | do_TCP_server(tcp_s); |
268 | c_sleep(50); | 297 | c_sleep(50); |
298 | |||
299 | // Testing response from connection 1 | ||
269 | uint8_t data[2048]; | 300 | uint8_t data[2048]; |
270 | int len = read_packet_sec_TCP(con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); | 301 | int len = read_packet_sec_TCP(con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); |
271 | ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "wrong len %u", len); | 302 | ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); |
272 | ck_assert_msg(data[0] == 1, "wrong packet id %u", data[0]); | 303 | ck_assert_msg(data[0] == 1, "Wrong response packet id of %d.", data[0]); |
273 | ck_assert_msg(data[1] == 16, "connection not refused %u", data[1]); | 304 | ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); |
274 | ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "key in packet wrong"); | 305 | ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "Key in response packet wrong."); |
306 | |||
307 | // Connection 3 | ||
275 | len = read_packet_sec_TCP(con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); | 308 | len = read_packet_sec_TCP(con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); |
276 | ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "wrong len %u", len); | 309 | ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); |
277 | ck_assert_msg(data[0] == 1, "wrong packet id %u", data[0]); | 310 | ck_assert_msg(data[0] == 1, "Wrong response packet id of %d.", data[0]); |
278 | ck_assert_msg(data[1] == 16, "connection not refused %u", data[1]); | 311 | ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); |
279 | ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "key in packet wrong"); | 312 | ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong."); |
313 | |||
314 | uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; //What is this packet???? | ||
280 | 315 | ||
281 | uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; | ||
282 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); | 316 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); |
283 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); | 317 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); |
284 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); | 318 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); |
319 | |||
285 | c_sleep(50); | 320 | c_sleep(50); |
286 | do_TCP_server(tcp_s); | 321 | do_TCP_server(tcp_s); |
287 | c_sleep(50); | 322 | c_sleep(50); |
323 | |||
288 | len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE); | 324 | len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE); |
289 | ck_assert_msg(len == 2, "wrong len %u", len); | 325 | ck_assert_msg(len == 2, "wrong len %d", len); |
290 | ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); | 326 | ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); |
291 | ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); | 327 | ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); |
292 | len = read_packet_sec_TCP(con3, data, 2 + 2 + CRYPTO_MAC_SIZE); | 328 | len = read_packet_sec_TCP(con3, data, 2 + 2 + CRYPTO_MAC_SIZE); |
293 | ck_assert_msg(len == 2, "wrong len %u", len); | 329 | ck_assert_msg(len == 2, "wrong len %d", len); |
294 | ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); | 330 | ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); |
295 | ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); | 331 | ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); |
296 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); | 332 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); |
297 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | 333 | ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); |
298 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | 334 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], |
299 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | 335 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); |
300 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); | 336 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); |
301 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | 337 | ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); |
302 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | 338 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], |
303 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | 339 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); |
304 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); | 340 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); |
305 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | 341 | ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); |
306 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | 342 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], |
307 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | 343 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); |
308 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); | 344 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); |
@@ -312,15 +348,15 @@ START_TEST(test_some) | |||
312 | do_TCP_server(tcp_s); | 348 | do_TCP_server(tcp_s); |
313 | c_sleep(50); | 349 | c_sleep(50); |
314 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); | 350 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); |
315 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | 351 | ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); |
316 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | 352 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], |
317 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | 353 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); |
318 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); | 354 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); |
319 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | 355 | ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); |
320 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | 356 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], |
321 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | 357 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); |
322 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); | 358 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); |
323 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | 359 | ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); |
324 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | 360 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], |
325 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | 361 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); |
326 | 362 | ||
@@ -330,9 +366,11 @@ START_TEST(test_some) | |||
330 | do_TCP_server(tcp_s); | 366 | do_TCP_server(tcp_s); |
331 | c_sleep(50); | 367 | c_sleep(50); |
332 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); | 368 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); |
333 | ck_assert_msg(len == sizeof(ping_packet), "wrong len %u", len); | 369 | ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len); |
334 | ck_assert_msg(data[0] == 5, "wrong packet id %u", data[0]); | 370 | ck_assert_msg(data[0] == 5, "wrong packet id %u", data[0]); |
335 | ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data"); | 371 | ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data"); |
372 | |||
373 | // Kill off the connections | ||
336 | kill_TCP_server(tcp_s); | 374 | kill_TCP_server(tcp_s); |
337 | kill_TCP_con(con1); | 375 | kill_TCP_con(con1); |
338 | kill_TCP_con(con2); | 376 | kill_TCP_con(con2); |