1 | /* |
---|
2 | * This file Copyright (C) 2013-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: utils-test.c 14480 2015-03-19 06:08:06Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <limits.h> /* INT_MAX */ |
---|
11 | #include <math.h> /* sqrt () */ |
---|
12 | #include <string.h> /* strlen () */ |
---|
13 | #include <stdlib.h> /* setenv (), unsetenv () */ |
---|
14 | |
---|
15 | #ifdef _WIN32 |
---|
16 | #include <windows.h> |
---|
17 | #define setenv(key, value, unused) SetEnvironmentVariableA (key, value) |
---|
18 | #define unsetenv(key) SetEnvironmentVariableA (key, NULL) |
---|
19 | #endif |
---|
20 | |
---|
21 | #include "transmission.h" |
---|
22 | #include "ConvertUTF.h" /* tr_utf8_validate*/ |
---|
23 | #include "platform.h" |
---|
24 | #include "crypto-utils.h" /* tr_rand_int_weak */ |
---|
25 | #include "utils.h" |
---|
26 | #include "web.h" |
---|
27 | |
---|
28 | #define NUM_LOOPS 1 |
---|
29 | #define SPEED_TEST 0 |
---|
30 | |
---|
31 | #if SPEED_TEST |
---|
32 | #define VERBOSE |
---|
33 | #undef NUM_LOOPS |
---|
34 | #define NUM_LOOPS 200 |
---|
35 | #endif |
---|
36 | |
---|
37 | #include "libtransmission-test.h" |
---|
38 | |
---|
39 | static int |
---|
40 | test_strip_positional_args (void) |
---|
41 | { |
---|
42 | const char * in; |
---|
43 | const char * out; |
---|
44 | const char * expected; |
---|
45 | |
---|
46 | in = "Hello %1$s foo %2$.*f"; |
---|
47 | expected = "Hello %s foo %.*f"; |
---|
48 | out = tr_strip_positional_args (in); |
---|
49 | check_streq (expected, out); |
---|
50 | |
---|
51 | in = "Hello %1$'d foo %2$'f"; |
---|
52 | expected = "Hello %d foo %f"; |
---|
53 | out = tr_strip_positional_args (in); |
---|
54 | check_streq (expected, out); |
---|
55 | |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | static int |
---|
60 | test_strstrip (void) |
---|
61 | { |
---|
62 | char *in, *out; |
---|
63 | |
---|
64 | /* strstrip */ |
---|
65 | in = tr_strdup (" test "); |
---|
66 | out = tr_strstrip (in); |
---|
67 | check (in == out); |
---|
68 | check_streq ("test", out); |
---|
69 | tr_free (in); |
---|
70 | |
---|
71 | /* strstrip */ |
---|
72 | in = tr_strdup (" test test "); |
---|
73 | out = tr_strstrip (in); |
---|
74 | check (in == out); |
---|
75 | check_streq ("test test", out); |
---|
76 | tr_free (in); |
---|
77 | |
---|
78 | /* strstrip */ |
---|
79 | in = tr_strdup ("test"); |
---|
80 | out = tr_strstrip (in); |
---|
81 | check (in == out); |
---|
82 | check_streq ("test", out); |
---|
83 | tr_free (in); |
---|
84 | |
---|
85 | return 0; |
---|
86 | } |
---|
87 | |
---|
88 | static int |
---|
89 | test_buildpath (void) |
---|
90 | { |
---|
91 | char * out; |
---|
92 | |
---|
93 | out = tr_buildPath ("foo", "bar", NULL); |
---|
94 | check_streq ("foo" TR_PATH_DELIMITER_STR "bar", out); |
---|
95 | tr_free (out); |
---|
96 | |
---|
97 | out = tr_buildPath ("", "foo", "bar", NULL); |
---|
98 | check_streq (TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar", out); |
---|
99 | tr_free (out); |
---|
100 | |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | static int |
---|
105 | test_utf8 (void) |
---|
106 | { |
---|
107 | const char * in; |
---|
108 | char * out; |
---|
109 | |
---|
110 | in = "hello world"; |
---|
111 | out = tr_utf8clean (in, -1); |
---|
112 | check_streq (in, out); |
---|
113 | tr_free (out); |
---|
114 | |
---|
115 | in = "hello world"; |
---|
116 | out = tr_utf8clean (in, 5); |
---|
117 | check_streq ("hello", out); |
---|
118 | tr_free (out); |
---|
119 | |
---|
120 | /* this version is not utf-8 (but cp866) */ |
---|
121 | in = "\x92\xE0\xE3\xA4\xAD\xAE \xA1\xEB\xE2\xEC \x81\xAE\xA3\xAE\xAC"; |
---|
122 | out = tr_utf8clean (in, 17); |
---|
123 | check (out != NULL); |
---|
124 | check ((strlen (out) == 17) || (strlen (out) == 33)); |
---|
125 | check (tr_utf8_validate (out, -1, NULL)); |
---|
126 | tr_free (out); |
---|
127 | |
---|
128 | /* same string, but utf-8 clean */ |
---|
129 | in = "ТÑÑЎМП бÑÑÑ ÐПгПЌ"; |
---|
130 | out = tr_utf8clean (in, -1); |
---|
131 | check (out != NULL); |
---|
132 | check (tr_utf8_validate (out, -1, NULL)); |
---|
133 | check_streq (in, out); |
---|
134 | tr_free (out); |
---|
135 | |
---|
136 | return 0; |
---|
137 | } |
---|
138 | |
---|
139 | static int |
---|
140 | test_numbers (void) |
---|
141 | { |
---|
142 | int i; |
---|
143 | int count; |
---|
144 | int * numbers; |
---|
145 | |
---|
146 | numbers = tr_parseNumberRange ("1-10,13,16-19", -1, &count); |
---|
147 | check_int_eq (15, count); |
---|
148 | check_int_eq (1, numbers[0]); |
---|
149 | check_int_eq (6, numbers[5]); |
---|
150 | check_int_eq (10, numbers[9]); |
---|
151 | check_int_eq (13, numbers[10]); |
---|
152 | check_int_eq (16, numbers[11]); |
---|
153 | check_int_eq (19, numbers[14]); |
---|
154 | tr_free (numbers); |
---|
155 | |
---|
156 | numbers = tr_parseNumberRange ("1-5,3-7,2-6", -1, &count); |
---|
157 | check (count == 7); |
---|
158 | check (numbers != NULL); |
---|
159 | for (i=0; i<count; ++i) |
---|
160 | check_int_eq (i+1, numbers[i]); |
---|
161 | tr_free (numbers); |
---|
162 | |
---|
163 | numbers = tr_parseNumberRange ("1-Hello", -1, &count); |
---|
164 | check_int_eq (0, count); |
---|
165 | check (numbers == NULL); |
---|
166 | |
---|
167 | numbers = tr_parseNumberRange ("1-", -1, &count); |
---|
168 | check_int_eq (0, count); |
---|
169 | check (numbers == NULL); |
---|
170 | |
---|
171 | numbers = tr_parseNumberRange ("Hello", -1, &count); |
---|
172 | check_int_eq (0, count); |
---|
173 | check (numbers == NULL); |
---|
174 | |
---|
175 | return 0; |
---|
176 | } |
---|
177 | |
---|
178 | static int |
---|
179 | compareInts (const void * va, const void * vb) |
---|
180 | { |
---|
181 | const int a = *(const int *)va; |
---|
182 | const int b = *(const int *)vb; |
---|
183 | return a - b; |
---|
184 | } |
---|
185 | |
---|
186 | static int |
---|
187 | test_lowerbound (void) |
---|
188 | { |
---|
189 | int i; |
---|
190 | const int A[] = { 1, 2, 3, 3, 3, 5, 8 }; |
---|
191 | const int expected_pos[] = { 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 }; |
---|
192 | const bool expected_exact[] = { true, true, true, false, true, false, false, true, false, false }; |
---|
193 | const int N = sizeof (A) / sizeof (A[0]); |
---|
194 | |
---|
195 | for (i=1; i<=10; i++) |
---|
196 | { |
---|
197 | bool exact; |
---|
198 | const int pos = tr_lowerBound (&i, A, N, sizeof (int), compareInts, &exact); |
---|
199 | |
---|
200 | #if 0 |
---|
201 | fprintf (stderr, "searching for %d. ", i); |
---|
202 | fprintf (stderr, "result: index = %d, ", pos); |
---|
203 | if (pos != N) |
---|
204 | fprintf (stderr, "A[%d] == %d\n", pos, A[pos]); |
---|
205 | else |
---|
206 | fprintf (stderr, "which is off the end.\n"); |
---|
207 | #endif |
---|
208 | check_int_eq (expected_pos[i-1], pos); |
---|
209 | check_int_eq (expected_exact[i-1], exact); |
---|
210 | } |
---|
211 | |
---|
212 | return 0; |
---|
213 | } |
---|
214 | |
---|
215 | static int |
---|
216 | test_quickFindFirst_Iteration (const size_t k, const size_t n, int * buf, int range) |
---|
217 | { |
---|
218 | size_t i; |
---|
219 | int highest_low; |
---|
220 | int lowest_high; |
---|
221 | |
---|
222 | /* populate buf with random ints */ |
---|
223 | for (i=0; i<n; ++i) |
---|
224 | buf[i] = tr_rand_int_weak (range); |
---|
225 | |
---|
226 | /* find the best k */ |
---|
227 | tr_quickfindFirstK (buf, n, sizeof(int), compareInts, k); |
---|
228 | |
---|
229 | /* confirm that the smallest K ints are in the first slots K slots in buf */ |
---|
230 | |
---|
231 | highest_low = INT_MIN; |
---|
232 | for (i=0; i<k; ++i) |
---|
233 | if (highest_low < buf[i]) |
---|
234 | highest_low = buf[i]; |
---|
235 | |
---|
236 | lowest_high = INT_MAX; |
---|
237 | for (i=k; i<n; ++i) |
---|
238 | if (lowest_high > buf[i]) |
---|
239 | lowest_high = buf[i]; |
---|
240 | |
---|
241 | check (highest_low <= lowest_high); |
---|
242 | |
---|
243 | return 0; |
---|
244 | } |
---|
245 | |
---|
246 | static int |
---|
247 | test_quickfindFirst (void) |
---|
248 | { |
---|
249 | size_t i; |
---|
250 | const size_t k = 10; |
---|
251 | const size_t n = 100; |
---|
252 | const size_t n_trials = 1000; |
---|
253 | int * buf = tr_new (int, n); |
---|
254 | |
---|
255 | for (i=0; i<n_trials; ++i) |
---|
256 | check_int_eq (0, test_quickFindFirst_Iteration (k, n, buf, 100)); |
---|
257 | |
---|
258 | tr_free (buf); |
---|
259 | return 0; |
---|
260 | } |
---|
261 | |
---|
262 | static int |
---|
263 | test_memmem (void) |
---|
264 | { |
---|
265 | char const haystack[12] = "abcabcabcabc"; |
---|
266 | char const needle[3] = "cab"; |
---|
267 | |
---|
268 | check (tr_memmem (haystack, sizeof haystack, haystack, sizeof haystack) == haystack); |
---|
269 | check (tr_memmem (haystack, sizeof haystack, needle, sizeof needle) == haystack + 2); |
---|
270 | check (tr_memmem (needle, sizeof needle, haystack, sizeof haystack) == NULL); |
---|
271 | |
---|
272 | return 0; |
---|
273 | } |
---|
274 | |
---|
275 | static int |
---|
276 | test_hex (void) |
---|
277 | { |
---|
278 | char hex1[41]; |
---|
279 | char hex2[41]; |
---|
280 | uint8_t binary[20]; |
---|
281 | |
---|
282 | memcpy (hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41); |
---|
283 | tr_hex_to_binary (hex1, binary, 20); |
---|
284 | tr_binary_to_hex (binary, hex2, 20); |
---|
285 | check_streq (hex1, hex2); |
---|
286 | |
---|
287 | return 0; |
---|
288 | } |
---|
289 | |
---|
290 | static int |
---|
291 | test_array (void) |
---|
292 | { |
---|
293 | int i; |
---|
294 | int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
---|
295 | int n = sizeof (array) / sizeof (array[0]); |
---|
296 | |
---|
297 | tr_removeElementFromArray (array, 5u, sizeof (int), n--); |
---|
298 | for (i=0; i<n; ++i) |
---|
299 | check_int_eq ((i<5 ? i : i+1), array[i]); |
---|
300 | |
---|
301 | tr_removeElementFromArray (array, 0u, sizeof (int), n--); |
---|
302 | for (i=0; i<n; ++i) |
---|
303 | check_int_eq ((i<4 ? i+1 : i+2), array[i]); |
---|
304 | |
---|
305 | tr_removeElementFromArray (array, n-1, sizeof (int), n); n--; |
---|
306 | for (i=0; i<n; ++i) |
---|
307 | check_int_eq ((i<4 ? i+1 : i+2), array[i]); |
---|
308 | |
---|
309 | return 0; |
---|
310 | } |
---|
311 | |
---|
312 | static int |
---|
313 | test_url (void) |
---|
314 | { |
---|
315 | int port; |
---|
316 | char * scheme; |
---|
317 | char * host; |
---|
318 | char * path; |
---|
319 | char * str; |
---|
320 | const char * url; |
---|
321 | |
---|
322 | url = "http://1"; |
---|
323 | check (!tr_urlParse (url, -1, &scheme, &host, &port, &path)); |
---|
324 | check_streq ("http", scheme); |
---|
325 | check_streq ("1", host); |
---|
326 | check_streq ("/", path); |
---|
327 | check_int_eq (80, port); |
---|
328 | tr_free (scheme); |
---|
329 | tr_free (path); |
---|
330 | tr_free (host); |
---|
331 | |
---|
332 | url = "http://www.some-tracker.org/some/path"; |
---|
333 | check (!tr_urlParse (url, -1, &scheme, &host, &port, &path)); |
---|
334 | check_streq ("http", scheme); |
---|
335 | check_streq ("www.some-tracker.org", host); |
---|
336 | check_streq ("/some/path", path); |
---|
337 | check_int_eq (80, port); |
---|
338 | tr_free (scheme); |
---|
339 | tr_free (path); |
---|
340 | tr_free (host); |
---|
341 | |
---|
342 | url = "http://www.some-tracker.org:80/some/path"; |
---|
343 | check (!tr_urlParse (url, -1, &scheme, &host, &port, &path)); |
---|
344 | check_streq ("http", scheme); |
---|
345 | check_streq ("www.some-tracker.org", host); |
---|
346 | check_streq ("/some/path", path); |
---|
347 | check_int_eq (80, port); |
---|
348 | tr_free (scheme); |
---|
349 | tr_free (path); |
---|
350 | tr_free (host); |
---|
351 | |
---|
352 | url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2"; |
---|
353 | str = tr_http_unescape (url, strlen (url)); |
---|
354 | check_streq ("http://www.example.com/~user/?test=1&test1=2", str); |
---|
355 | tr_free (str); |
---|
356 | |
---|
357 | return 0; |
---|
358 | } |
---|
359 | |
---|
360 | static int |
---|
361 | test_truncd (void) |
---|
362 | { |
---|
363 | char buf[32]; |
---|
364 | const double nan = sqrt (-1); |
---|
365 | |
---|
366 | tr_snprintf (buf, sizeof (buf), "%.2f%%", 99.999); |
---|
367 | check_streq ("100.00%", buf); |
---|
368 | |
---|
369 | tr_snprintf (buf, sizeof (buf), "%.2f%%", tr_truncd (99.999, 2)); |
---|
370 | check_streq ("99.99%", buf); |
---|
371 | |
---|
372 | tr_snprintf (buf, sizeof (buf), "%.4f", tr_truncd (403650.656250, 4)); |
---|
373 | check_streq ("403650.6562", buf); |
---|
374 | |
---|
375 | tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.15, 2)); |
---|
376 | check_streq ("2.15", buf); |
---|
377 | |
---|
378 | tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.05, 2)); |
---|
379 | check_streq ("2.05", buf); |
---|
380 | |
---|
381 | tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (3.3333, 2)); |
---|
382 | check_streq ("3.33", buf); |
---|
383 | |
---|
384 | tr_snprintf (buf, sizeof (buf), "%.0f", tr_truncd (3.3333, 0)); |
---|
385 | check_streq ("3", buf); |
---|
386 | |
---|
387 | #if !(defined (_MSC_VER) || (defined (__MINGW32__) && defined (__MSVCRT__))) |
---|
388 | /* FIXME: MSCVRT behaves differently in case of nan */ |
---|
389 | tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (nan, 2)); |
---|
390 | check (strstr (buf, "nan") != NULL); |
---|
391 | #else |
---|
392 | (void) nan; |
---|
393 | #endif |
---|
394 | |
---|
395 | return 0; |
---|
396 | } |
---|
397 | |
---|
398 | static char * |
---|
399 | test_strdup_printf_valist (const char * fmt, ...) |
---|
400 | { |
---|
401 | va_list args; |
---|
402 | char * ret; |
---|
403 | |
---|
404 | va_start (args, fmt); |
---|
405 | ret = tr_strdup_vprintf (fmt, args); |
---|
406 | va_end (args); |
---|
407 | |
---|
408 | return ret; |
---|
409 | } |
---|
410 | |
---|
411 | static int |
---|
412 | test_strdup_printf (void) |
---|
413 | { |
---|
414 | char * s, * s2, * s3; |
---|
415 | |
---|
416 | s = tr_strdup_printf ("%s", "test"); |
---|
417 | check_streq ("test", s); |
---|
418 | tr_free (s); |
---|
419 | |
---|
420 | s = tr_strdup_printf ("%d %s %c %u", -1, "0", '1', 2); |
---|
421 | check_streq ("-1 0 1 2", s); |
---|
422 | tr_free (s); |
---|
423 | |
---|
424 | s3 = tr_malloc0 (4098); |
---|
425 | memset (s3, '-', 4097); |
---|
426 | s3[2047] = 't'; |
---|
427 | s3[2048] = 'e'; |
---|
428 | s3[2049] = 's'; |
---|
429 | s3[2050] = 't'; |
---|
430 | |
---|
431 | s2 = tr_malloc0 (4096); |
---|
432 | memset (s2, '-', 4095); |
---|
433 | s2[2047] = '%'; |
---|
434 | s2[2048] = 's'; |
---|
435 | |
---|
436 | s = tr_strdup_printf (s2, "test"); |
---|
437 | check_streq (s3, s); |
---|
438 | tr_free (s); |
---|
439 | |
---|
440 | tr_free (s2); |
---|
441 | |
---|
442 | s = tr_strdup_printf ("%s", s3); |
---|
443 | check_streq (s3, s); |
---|
444 | tr_free (s); |
---|
445 | |
---|
446 | tr_free (s3); |
---|
447 | |
---|
448 | s = test_strdup_printf_valist ("\n-%s-%s-%s-\n", "\r", "\t", "\b"); |
---|
449 | check_streq ("\n-\r-\t-\b-\n", s); |
---|
450 | tr_free (s); |
---|
451 | |
---|
452 | return 0; |
---|
453 | } |
---|
454 | |
---|
455 | static int |
---|
456 | test_env (void) |
---|
457 | { |
---|
458 | const char * test_key = "TR_TEST_ENV"; |
---|
459 | int x; |
---|
460 | char * s; |
---|
461 | |
---|
462 | unsetenv (test_key); |
---|
463 | |
---|
464 | check (!tr_env_key_exists (test_key)); |
---|
465 | x = tr_env_get_int (test_key, 123); |
---|
466 | check_int_eq (123, x); |
---|
467 | s = tr_env_get_string (test_key, NULL); |
---|
468 | check (s == NULL); |
---|
469 | s = tr_env_get_string (test_key, "a"); |
---|
470 | check_streq ("a", s); |
---|
471 | tr_free (s); |
---|
472 | |
---|
473 | setenv (test_key, "", 1); |
---|
474 | |
---|
475 | check (tr_env_key_exists (test_key)); |
---|
476 | x = tr_env_get_int (test_key, 456); |
---|
477 | check_int_eq (456, x); |
---|
478 | s = tr_env_get_string (test_key, NULL); |
---|
479 | check_streq ("", s); |
---|
480 | tr_free (s); |
---|
481 | s = tr_env_get_string (test_key, "b"); |
---|
482 | check_streq ("", s); |
---|
483 | tr_free (s); |
---|
484 | |
---|
485 | setenv (test_key, "135", 1); |
---|
486 | |
---|
487 | check (tr_env_key_exists (test_key)); |
---|
488 | x = tr_env_get_int (test_key, 789); |
---|
489 | check_int_eq (135, x); |
---|
490 | s = tr_env_get_string (test_key, NULL); |
---|
491 | check_streq ("135", s); |
---|
492 | tr_free (s); |
---|
493 | s = tr_env_get_string (test_key, "c"); |
---|
494 | check_streq ("135", s); |
---|
495 | tr_free (s); |
---|
496 | |
---|
497 | return 0; |
---|
498 | } |
---|
499 | |
---|
500 | int |
---|
501 | main (void) |
---|
502 | { |
---|
503 | const testFunc tests[] = { test_array, |
---|
504 | test_buildpath, |
---|
505 | test_hex, |
---|
506 | test_lowerbound, |
---|
507 | test_quickfindFirst, |
---|
508 | test_memmem, |
---|
509 | test_numbers, |
---|
510 | test_strip_positional_args, |
---|
511 | test_strdup_printf, |
---|
512 | test_strstrip, |
---|
513 | test_truncd, |
---|
514 | test_url, |
---|
515 | test_utf8, |
---|
516 | test_env }; |
---|
517 | |
---|
518 | return runTests (tests, NUM_TESTS (tests)); |
---|
519 | } |
---|
520 | |
---|