stun tests: Fix a bunch of issues found by more aggressive GCC warnings
Olivier CrĂȘte
10 years ago
69 | 69 | struct sockaddr_in ip4; |
70 | 70 | uint8_t req_buf[STUN_MAX_MESSAGE_SIZE]; |
71 | 71 | uint8_t resp_buf[STUN_MAX_MESSAGE_SIZE]; |
72 | const uint64_t tie = 0x8000000000000000LL; | |
72 | const const uint64_t tie = 0x8000000000000000LL; | |
73 | 73 | StunMessageReturn val; |
74 | 74 | StunUsageIceReturn val2; |
75 | 75 | size_t len; |
76 | 76 | size_t rlen; |
77 | static char username[] = "L:R", ufrag[] = "L", pass[] = "secret"; | |
77 | static char username[] = "L:R"; | |
78 | static uint8_t ufrag[] = "L", pass[] = "secret"; | |
79 | size_t ufrag_len = strlen ((char*) ufrag); | |
80 | size_t pass_len = strlen ((char*) pass); | |
78 | 81 | int code; |
79 | uint16_t alen; | |
80 | 82 | bool control = false; |
81 | 83 | StunAgent agent; |
82 | 84 | StunMessage req; |
83 | 85 | StunMessage resp; |
84 | 86 | StunDefaultValidaterData validater_data[] = { |
85 | {ufrag, strlen (ufrag), pass, strlen (pass)}, | |
86 | {username, strlen (username), pass, strlen (pass)}, | |
87 | {ufrag, ufrag_len, pass, pass_len}, | |
88 | {(uint8_t *) username, strlen (username), pass, pass_len}, | |
87 | 89 | {NULL, 0, NULL, 0}}; |
88 | 90 | StunValidationStatus valid; |
89 | 91 | |
118 | 120 | assert (stun_agent_init_request (&agent, &req, req_buf, sizeof(req_buf), 0x666)); |
119 | 121 | val = stun_message_append_string (&req, STUN_ATTRIBUTE_USERNAME, username); |
120 | 122 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
121 | rlen = stun_agent_finish_message (&agent, &req, pass, strlen (pass)); | |
123 | rlen = stun_agent_finish_message (&agent, &req, pass, pass_len); | |
122 | 124 | assert (rlen > 0); |
123 | 125 | |
124 | 126 | len = sizeof (resp_buf); |
134 | 136 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
135 | 137 | val = stun_message_append_string (&req, STUN_ATTRIBUTE_USERNAME, username); |
136 | 138 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
137 | rlen = stun_agent_finish_message (&agent, &req, pass, strlen (pass)); | |
139 | rlen = stun_agent_finish_message (&agent, &req, pass, pass_len); | |
138 | 140 | assert (rlen > 0); |
139 | 141 | |
140 | 142 | valid = stun_agent_validate (&agent, &req, req_buf, rlen, |
154 | 156 | |
155 | 157 | /* No username */ |
156 | 158 | assert (stun_agent_init_request (&agent, &req, req_buf, sizeof(req_buf), STUN_BINDING)); |
157 | rlen = stun_agent_finish_message (&agent, &req, pass, strlen (pass)); | |
159 | rlen = stun_agent_finish_message (&agent, &req, pass, pass_len); | |
158 | 160 | assert (rlen > 0); |
159 | 161 | |
160 | 162 | valid = stun_agent_validate (&agent, &req, req_buf, rlen, |
170 | 172 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
171 | 173 | val = stun_message_append_flag (&req, STUN_ATTRIBUTE_USE_CANDIDATE); |
172 | 174 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
173 | val = stun_message_append_string (&req, STUN_ATTRIBUTE_USERNAME, ufrag); | |
174 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); | |
175 | rlen = stun_agent_finish_message (&agent, &req, pass, strlen (pass)); | |
176 | assert (rlen > 0); | |
177 | ||
178 | len = sizeof (resp_buf); | |
179 | val2 = stun_usage_ice_conncheck_create_reply (&agent, &req, | |
180 | &resp, resp_buf, &len, (struct sockaddr *)&ip4, | |
181 | sizeof (ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245); | |
182 | assert (val == STUN_USAGE_ICE_RETURN_SUCCESS); | |
175 | val = stun_message_append_string (&req, STUN_ATTRIBUTE_USERNAME, | |
176 | (char*) ufrag); | |
177 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); | |
178 | rlen = stun_agent_finish_message (&agent, &req, pass, pass_len); | |
179 | assert (rlen > 0); | |
180 | ||
181 | len = sizeof (resp_buf); | |
182 | val2 = stun_usage_ice_conncheck_create_reply (&agent, &req, | |
183 | &resp, resp_buf, &len, (struct sockaddr *)&ip4, | |
184 | sizeof (ip4), &control, tie, STUN_USAGE_ICE_COMPATIBILITY_RFC5245); | |
185 | assert (val2 == STUN_USAGE_ICE_RETURN_SUCCESS); | |
183 | 186 | assert (len > 0); |
184 | 187 | assert (stun_agent_validate (&agent, &resp, resp_buf, len, |
185 | 188 | stun_agent_default_validater, validater_data) == STUN_VALIDATION_SUCCESS); |
191 | 194 | assert (stun_agent_init_request (&agent, &req, req_buf, sizeof(req_buf), STUN_BINDING)); |
192 | 195 | val = stun_message_append_string (&req, STUN_ATTRIBUTE_USERNAME, ufrag); |
193 | 196 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
194 | rlen = stun_agent_finish_message (&agent, &req, pass, strlen (pass)); | |
197 | rlen = stun_agent_finish_message (&agent, &req, pass, pass_len); | |
195 | 198 | assert (rlen > 0); |
196 | 199 | |
197 | 200 | ip4.sin_family = AF_UNSPEC; |
210 | 213 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
211 | 214 | val = stun_message_append_string (&req, STUN_ATTRIBUTE_USERNAME, ufrag); |
212 | 215 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
213 | rlen = stun_agent_finish_message (&agent, &req, pass, strlen (pass)); | |
216 | rlen = stun_agent_finish_message (&agent, &req, pass, pass_len); | |
214 | 217 | assert (rlen > 0); |
215 | 218 | |
216 | 219 | |
232 | 235 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
233 | 236 | val = stun_message_append_string (&req, STUN_ATTRIBUTE_USERNAME, ufrag); |
234 | 237 | assert (val == STUN_MESSAGE_RETURN_SUCCESS); |
235 | rlen = stun_agent_finish_message (&agent, &req, pass, strlen (pass)); | |
238 | rlen = stun_agent_finish_message (&agent, &req, pass, pass_len); | |
236 | 239 | assert (rlen > 0); |
237 | 240 | |
238 | 241 | len = sizeof (resp_buf); |
65 | 65 | exit (1); |
66 | 66 | } |
67 | 67 | |
68 | static const char usr[] = "admin"; | |
69 | static const char pwd[] = "s3kr3t"; | |
70 | ||
71 | bool dynamic_check_validater (StunAgent *agent, | |
68 | static const uint8_t usr[] = "admin"; | |
69 | static const uint8_t pwd[] = "s3kr3t"; | |
70 | ||
71 | static bool dynamic_check_validater (StunAgent *agent, | |
72 | 72 | StunMessage *message, uint8_t *username, uint16_t username_len, |
73 | 73 | uint8_t **password, size_t *password_len, void *user_data) |
74 | 74 | { |
75 | 75 | |
76 | if (username_len != strlen (usr) || | |
77 | memcmp (username, usr, strlen (usr)) != 0) | |
76 | if (username_len != strlen ((char *) usr) || | |
77 | memcmp (username, usr, strlen ((char *) usr)) != 0) | |
78 | 78 | fatal ("vector test : Validater received wrong username!"); |
79 | 79 | |
80 | 80 | *password = (uint8_t *) pwd; |
81 | *password_len = strlen (pwd); | |
81 | *password_len = strlen ((char *) pwd); | |
82 | 82 | |
83 | 83 | |
84 | 84 | return true; |
116 | 116 | if (stun_message_find (&msg2, STUN_ATTRIBUTE_MESSAGE_INTEGRITY, &plen) != NULL) |
117 | 117 | fatal ("Missing HMAC test failed"); |
118 | 118 | |
119 | stun_message_append_string (&msg2, STUN_ATTRIBUTE_USERNAME, usr); | |
120 | ||
121 | len = stun_agent_finish_message (agent, &msg2, pwd, strlen (pwd)); | |
119 | stun_message_append_string (&msg2, STUN_ATTRIBUTE_USERNAME, (char *) usr); | |
120 | ||
121 | len = stun_agent_finish_message (agent, &msg2, pwd, strlen ((char *) pwd)); | |
122 | 122 | |
123 | 123 | if (len <= 0) |
124 | 124 | fatal ("Cannot finish message with short-term creds"); |
249 | 249 | len = sizeof (msg); |
250 | 250 | if (stun_agent_finish_message (&agent, &msg, NULL, 0) != 0) |
251 | 251 | fatal ("Fingerprint overflow test failed"); |
252 | if (stun_agent_finish_message (&agent, &msg, pwd, strlen (pwd)) != 0) | |
252 | if (stun_agent_finish_message (&agent, &msg, pwd, strlen ((char *) pwd)) != 0) | |
253 | 253 | fatal ("Message integrity overflow test failed"); |
254 | 254 | |
255 | 255 | /* Address attributes tests */ |
44 | 44 | #include <stdlib.h> |
45 | 45 | #include <assert.h> |
46 | 46 | |
47 | void print_bytes (uint8_t *bytes, int len) | |
47 | static void print_bytes (const uint8_t *bytes, int len) | |
48 | 48 | { |
49 | 49 | int i; |
50 | 50 | |
54 | 54 | printf ("\n"); |
55 | 55 | } |
56 | 56 | |
57 | void test_sha1 (uint8_t *str, uint8_t *expected) { | |
57 | static void test_sha1 (const uint8_t *str, const uint8_t *expected) { | |
58 | 58 | SHA1_CTX ctx; |
59 | 59 | uint8_t sha1[20]; |
60 | 60 | |
61 | 61 | SHA1Init(&ctx); |
62 | SHA1Update(&ctx, str, strlen (str)); | |
62 | SHA1Update(&ctx, str, strlen ((char *) str)); | |
63 | 63 | SHA1Final(sha1, &ctx); |
64 | 64 | |
65 | 65 | printf ("SHA1 of '%s' : ", str); |
72 | 72 | |
73 | 73 | } |
74 | 74 | |
75 | void test_hmac (uint8_t *key, uint8_t *str, uint8_t *expected) { | |
75 | static void test_hmac (const uint8_t *key, const uint8_t *str, | |
76 | const uint8_t *expected) { | |
76 | 77 | uint8_t hmac[20]; |
77 | 78 | |
78 | hmac_sha1(key, strlen (key), str, strlen (str), hmac); | |
79 | hmac_sha1(key, strlen ((char *) key), str, strlen ((char *) str), hmac); | |
79 | 80 | printf ("HMAC of '%s' with key '%s' is : ", str, key); |
80 | 81 | print_bytes (hmac, SHA1_MAC_LEN); |
81 | 82 | printf ("Expected : "); |
85 | 86 | exit (1); |
86 | 87 | } |
87 | 88 | |
88 | void test_md5 (uint8_t *str, uint8_t *expected) { | |
89 | static void test_md5 (const uint8_t *str, const uint8_t *expected) { | |
89 | 90 | MD5_CTX ctx; |
90 | 91 | uint8_t md5[20]; |
91 | int i; | |
92 | 92 | |
93 | 93 | MD5Init(&ctx); |
94 | MD5Update(&ctx, str, strlen (str)); | |
94 | MD5Update(&ctx, str, strlen ((char *) str)); | |
95 | 95 | MD5Final(md5, &ctx); |
96 | 96 | |
97 | 97 | printf ("MD5 of '%s' : 0x", str); |
127 | 127 | 0xaa, 0xe1, 0x16, 0xd3, |
128 | 128 | 0x87, 0x6c, 0x66, 0x4a}; |
129 | 129 | |
130 | test_hmac ("hello", "world", hello_world_hmac); | |
130 | test_hmac ((const uint8_t *) "hello", (const uint8_t*) "world", | |
131 | hello_world_hmac); | |
131 | 132 | |
132 | test_sha1 ("abc", abc_sha1); | |
133 | test_md5 ("abc", abc_md5); | |
133 | test_sha1 ((const uint8_t *) "abc", abc_sha1); | |
134 | test_md5 ((const uint8_t *) "abc", abc_md5); | |
134 | 135 | |
135 | test_sha1 ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | |
136 | abcd_etc_sha1); | |
137 | test_md5 ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | |
138 | abcd_etc_md5); | |
136 | test_sha1 ((const uint8_t *) | |
137 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", abcd_etc_sha1); | |
138 | test_md5 ((const uint8_t *) | |
139 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", abcd_etc_md5); | |
139 | 140 | |
140 | 141 | return 0; |
141 | 142 | } |
284 | 284 | } |
285 | 285 | |
286 | 286 | |
287 | bool test_attribute_validater (StunAgent *agent, | |
287 | static bool test_attribute_validater (StunAgent *agent, | |
288 | 288 | StunMessage *message, uint8_t *username, uint16_t username_len, |
289 | 289 | uint8_t **password, size_t *password_len, void *user_data) |
290 | 290 | { |
291 | char *pwd = (char *) user_data; | |
291 | uint8_t *pwd = user_data; | |
292 | 292 | |
293 | 293 | if (username_len != 4 || |
294 | 294 | memcmp (username, "ABCD", 4) != 0) |
295 | 295 | return false; |
296 | 296 | |
297 | 297 | *password = pwd; |
298 | *password_len = strlen (pwd); | |
298 | *password_len = strlen ((char *) pwd); | |
299 | 299 | |
300 | 300 | return true; |
301 | 301 | } |
367 | 367 | StunMessage msg; |
368 | 368 | uint16_t known_attributes[] = {STUN_ATTRIBUTE_MESSAGE_INTEGRITY, STUN_ATTRIBUTE_USERNAME, 0}; |
369 | 369 | |
370 | printf ("Attribute test message length: %lu\n", sizeof (acme)); | |
370 | printf ("Attribute test message length: %zd\n", sizeof (acme)); | |
371 | 371 | |
372 | 372 | stun_agent_init (&agent, known_attributes, |
373 | 373 | STUN_COMPATIBILITY_RFC5389, STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS); |
377 | 377 | fatal ("Unauthorized validation failed"); |
378 | 378 | |
379 | 379 | if (stun_agent_validate (&agent, &msg, acme, sizeof(acme), |
380 | test_attribute_validater, "bad__guy") != STUN_VALIDATION_UNAUTHORIZED) | |
380 | test_attribute_validater, (void *) "bad__guy") != STUN_VALIDATION_UNAUTHORIZED) | |
381 | 381 | fatal ("invalid password validation failed"); |
382 | 382 | |
383 | 383 | if (stun_agent_validate (&agent, &msg, acme, sizeof(acme), |
384 | test_attribute_validater, "good_guy") != STUN_VALIDATION_SUCCESS) | |
384 | test_attribute_validater, (void *) "good_guy") != STUN_VALIDATION_SUCCESS) | |
385 | 385 | fatal ("good password validation failed"); |
386 | 386 | |
387 | 387 | if (stun_message_has_attribute (&msg, 0xff00)) |
450 | 450 | } |
451 | 451 | |
452 | 452 | static const char vector_username[] = "evtj:h6vY"; |
453 | static const char vector_password[] = "VOkJxbRl1RmTxUk/WvJxBt"; | |
454 | ||
455 | bool test_vector_validater (StunAgent *agent, | |
453 | static uint8_t vector_password[] = "VOkJxbRl1RmTxUk/WvJxBt"; | |
454 | ||
455 | static bool test_vector_validater (StunAgent *agent, | |
456 | 456 | StunMessage *message, uint8_t *username, uint16_t username_len, |
457 | 457 | uint8_t **password, size_t *password_len, void *user_data) |
458 | 458 | { |
465 | 465 | memcmp (username, vector_username, strlen (vector_username)) != 0) |
466 | 466 | fatal ("vector test : Validater received wrong username!"); |
467 | 467 | |
468 | *password = (uint8_t *) vector_password; | |
469 | *password_len = strlen (vector_password); | |
468 | *password = vector_password; | |
469 | *password_len = strlen ((char *) vector_password); | |
470 | 470 | |
471 | 471 | |
472 | 472 | return true; |
634 | 634 | if (stun_message_length (&msg) != sizeof(req) - 32) |
635 | 635 | fatal ("vector test: removing attributes failed"); |
636 | 636 | |
637 | stun_agent_finish_message (&agent, &msg, vector_password, strlen (vector_password)); | |
637 | stun_agent_finish_message (&agent, &msg, vector_password, | |
638 | strlen ((char *) vector_password)); | |
638 | 639 | |
639 | 640 | if (stun_message_length (&msg) != stun_message_length (&msg2) || |
640 | 641 | memcmp (req, req2, sizeof(req)) != 0) |
709 | 710 | puts ("Testing long term credentials hash algorithm..."); |
710 | 711 | |
711 | 712 | |
712 | stun_hash_creds ("realm", strlen ("realm"), | |
713 | "user", strlen ("user"), | |
714 | "pass", strlen ("pass"), md5); | |
713 | stun_hash_creds ((uint8_t *) "realm", strlen ("realm"), | |
714 | (uint8_t *) "user", strlen ("user"), | |
715 | (uint8_t *) "pass", strlen ("pass"), md5); | |
715 | 716 | |
716 | 717 | stun_debug ("key for user:realm:pass is : "); |
717 | 718 | stun_debug_bytes (md5, 16); |