1 | #include <math.h> |
---|
2 | #include <stdio.h> /* fprintf */ |
---|
3 | #include <string.h> /* strcmp */ |
---|
4 | |
---|
5 | #include "transmission.h" |
---|
6 | #include "bitfield.h" |
---|
7 | #include "ConvertUTF.h" /* tr_utf8_validate*/ |
---|
8 | #include "platform.h" |
---|
9 | #include "crypto.h" |
---|
10 | #include "utils.h" |
---|
11 | #include "web.h" |
---|
12 | |
---|
13 | /* #define VERBOSE */ |
---|
14 | #undef VERBOSE |
---|
15 | #define NUM_LOOPS 1 |
---|
16 | #define SPEED_TEST 0 |
---|
17 | |
---|
18 | #if SPEED_TEST |
---|
19 | #define VERBOSE |
---|
20 | #undef NUM_LOOPS |
---|
21 | #define NUM_LOOPS 200 |
---|
22 | #endif |
---|
23 | |
---|
24 | static int test = 0; |
---|
25 | |
---|
26 | #ifdef VERBOSE |
---|
27 | #define check( A ) \ |
---|
28 | { \ |
---|
29 | ++test; \ |
---|
30 | if( A ){ \ |
---|
31 | fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ |
---|
32 | } else { \ |
---|
33 | fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ |
---|
34 | return test; \ |
---|
35 | } \ |
---|
36 | } |
---|
37 | #else |
---|
38 | #define check( A ) \ |
---|
39 | { \ |
---|
40 | ++test; \ |
---|
41 | if( !( A ) ){ \ |
---|
42 | fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ |
---|
43 | return test; \ |
---|
44 | } \ |
---|
45 | } |
---|
46 | #endif |
---|
47 | |
---|
48 | static int |
---|
49 | test_bitfields( void ) |
---|
50 | { |
---|
51 | unsigned int i; |
---|
52 | unsigned int bitcount = 5000000; |
---|
53 | tr_bitfield * field = tr_bitfieldNew( bitcount ); |
---|
54 | |
---|
55 | /* test tr_bitfieldAdd */ |
---|
56 | for( i = 0; i < bitcount; ++i ) |
---|
57 | if( !( i % 7 ) ) |
---|
58 | tr_bitfieldAdd( field, i ); |
---|
59 | for( i = 0; i < bitcount; ++i ) |
---|
60 | check( tr_bitfieldHas( field, i ) == ( !( i % 7 ) ) ); |
---|
61 | |
---|
62 | /* test tr_bitfieldAddRange */ |
---|
63 | tr_bitfieldAddRange( field, 0, bitcount ); |
---|
64 | for( i = 0; i < bitcount; ++i ) |
---|
65 | check( tr_bitfieldHas( field, i ) ); |
---|
66 | |
---|
67 | /* test tr_bitfieldRemRange in the middle of a boundary */ |
---|
68 | tr_bitfieldRemRange( field, 4, 21 ); |
---|
69 | for( i = 0; i < 64; ++i ) |
---|
70 | check( tr_bitfieldHas( field, i ) == ( ( i < 4 ) || ( i >= 21 ) ) ); |
---|
71 | |
---|
72 | /* test tr_bitfieldRemRange on the boundaries */ |
---|
73 | tr_bitfieldAddRange( field, 0, 64 ); |
---|
74 | tr_bitfieldRemRange( field, 8, 24 ); |
---|
75 | for( i = 0; i < 64; ++i ) |
---|
76 | check( tr_bitfieldHas( field, i ) == ( ( i < 8 ) || ( i >= 24 ) ) ); |
---|
77 | |
---|
78 | /* test tr_bitfieldRemRange when begin & end is on the same word */ |
---|
79 | tr_bitfieldAddRange( field, 0, 64 ); |
---|
80 | tr_bitfieldRemRange( field, 4, 5 ); |
---|
81 | for( i = 0; i < 64; ++i ) |
---|
82 | check( tr_bitfieldHas( field, i ) == ( ( i < 4 ) || ( i >= 5 ) ) ); |
---|
83 | |
---|
84 | /* test tr_bitfieldAddRange */ |
---|
85 | tr_bitfieldRemRange( field, 0, 64 ); |
---|
86 | tr_bitfieldAddRange( field, 4, 21 ); |
---|
87 | for( i = 0; i < 64; ++i ) |
---|
88 | check( tr_bitfieldHas( field, i ) == ( ( 4 <= i ) && ( i < 21 ) ) ); |
---|
89 | |
---|
90 | /* test tr_bitfieldAddRange on the boundaries */ |
---|
91 | tr_bitfieldRemRange( field, 0, 64 ); |
---|
92 | tr_bitfieldAddRange( field, 8, 24 ); |
---|
93 | for( i = 0; i < 64; ++i ) |
---|
94 | check( tr_bitfieldHas( field, i ) == ( ( 8 <= i ) && ( i < 24 ) ) ); |
---|
95 | |
---|
96 | /* test tr_bitfieldAddRange when begin & end is on the same word */ |
---|
97 | tr_bitfieldRemRange( field, 0, 64 ); |
---|
98 | tr_bitfieldAddRange( field, 4, 5 ); |
---|
99 | for( i = 0; i < 64; ++i ) |
---|
100 | check( tr_bitfieldHas( field, i ) == ( ( 4 <= i ) && ( i < 5 ) ) ); |
---|
101 | |
---|
102 | tr_bitfieldFree( field ); |
---|
103 | return 0; |
---|
104 | } |
---|
105 | |
---|
106 | static int |
---|
107 | test_strip_positional_args( void ) |
---|
108 | { |
---|
109 | const char * in; |
---|
110 | const char * out; |
---|
111 | const char * expected; |
---|
112 | |
---|
113 | in = "Hello %1$s foo %2$.*f"; |
---|
114 | expected = "Hello %s foo %.*f"; |
---|
115 | out = tr_strip_positional_args( in ); |
---|
116 | check( out != NULL ) |
---|
117 | check( !strcmp( out, expected ) ) |
---|
118 | |
---|
119 | in = "Hello %1$'d foo %2$'f"; |
---|
120 | expected = "Hello %d foo %f"; |
---|
121 | out = tr_strip_positional_args( in ); |
---|
122 | check( out != NULL ) |
---|
123 | check( !strcmp( out, expected ) ) |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|
128 | static int |
---|
129 | test_strstrip( void ) |
---|
130 | { |
---|
131 | char *in, *out; |
---|
132 | |
---|
133 | /* strstrip */ |
---|
134 | in = tr_strdup( " test " ); |
---|
135 | out = tr_strstrip( in ); |
---|
136 | check( in == out ); |
---|
137 | check( !strcmp( in, "test" ) ); |
---|
138 | tr_free( in ); |
---|
139 | |
---|
140 | /* strstrip */ |
---|
141 | in = tr_strdup( " test test " ); |
---|
142 | out = tr_strstrip( in ); |
---|
143 | check( in == out ); |
---|
144 | check( !strcmp( in, "test test" ) ); |
---|
145 | tr_free( in ); |
---|
146 | |
---|
147 | /* strstrip */ |
---|
148 | in = tr_strdup( "test" ); |
---|
149 | out = tr_strstrip( in ); |
---|
150 | check( in == out ); |
---|
151 | check( !strcmp( in, "test" ) ); |
---|
152 | tr_free( in ); |
---|
153 | |
---|
154 | return 0; |
---|
155 | } |
---|
156 | |
---|
157 | static int |
---|
158 | test_buildpath( void ) |
---|
159 | { |
---|
160 | char * out; |
---|
161 | |
---|
162 | out = tr_buildPath( "foo", "bar", NULL ); |
---|
163 | check( !strcmp( out, "foo" TR_PATH_DELIMITER_STR "bar" ) ); |
---|
164 | tr_free( out ); |
---|
165 | |
---|
166 | out = tr_buildPath( "", "foo", "bar", NULL ); |
---|
167 | check( !strcmp( out, TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar" ) ); |
---|
168 | tr_free( out ); |
---|
169 | |
---|
170 | return 0; |
---|
171 | } |
---|
172 | |
---|
173 | static int |
---|
174 | test_utf8( void ) |
---|
175 | { |
---|
176 | const char * in; |
---|
177 | char * out; |
---|
178 | |
---|
179 | in = "hello world"; |
---|
180 | out = tr_utf8clean( in, -1 ); |
---|
181 | check( out != NULL ) |
---|
182 | check( !strcmp( out, in ) ) |
---|
183 | tr_free( out ); |
---|
184 | |
---|
185 | in = "hello world"; |
---|
186 | out = tr_utf8clean( in, 5 ); |
---|
187 | check( out != NULL ) |
---|
188 | check( !strcmp( out, "hello" ) ) |
---|
189 | tr_free( out ); |
---|
190 | |
---|
191 | /* this version is not utf-8 */ |
---|
192 | in = "Òðóäíî áûòü Áîãîì"; |
---|
193 | out = tr_utf8clean( in, 17 ); |
---|
194 | check( out != NULL ) |
---|
195 | check( ( strlen( out ) == 17 ) || ( strlen( out ) == 32 ) ) |
---|
196 | check( tr_utf8_validate( out, -1, NULL ) ) |
---|
197 | tr_free( out ); |
---|
198 | |
---|
199 | /* same string, but utf-8 clean */ |
---|
200 | in = "ÃðóÀÃî áûòÌ Ãîãîì"; |
---|
201 | out = tr_utf8clean( in, -1 ); |
---|
202 | check( out != NULL ) |
---|
203 | check( tr_utf8_validate( out, -1, NULL ) ) |
---|
204 | check ( !strcmp( in, out ) ) |
---|
205 | tr_free( out ); |
---|
206 | |
---|
207 | return 0; |
---|
208 | } |
---|
209 | |
---|
210 | static int |
---|
211 | test_numbers( void ) |
---|
212 | { |
---|
213 | int i; |
---|
214 | int count; |
---|
215 | int * numbers; |
---|
216 | |
---|
217 | numbers = tr_parseNumberRange( "1-10,13,16-19", -1, &count ); |
---|
218 | check( count == 15 ); |
---|
219 | check( numbers != NULL ); |
---|
220 | check( numbers[0] == 1 ); |
---|
221 | check( numbers[5] == 6 ); |
---|
222 | check( numbers[9] == 10 ); |
---|
223 | check( numbers[10] == 13 ); |
---|
224 | check( numbers[11] == 16 ); |
---|
225 | check( numbers[14] == 19 ); |
---|
226 | tr_free( numbers ); |
---|
227 | |
---|
228 | numbers = tr_parseNumberRange( "1-5,3-7,2-6", -1, &count ); |
---|
229 | check( count == 7 ); |
---|
230 | check( numbers != NULL ); |
---|
231 | for( i=0; i<count; ++i ) |
---|
232 | check( numbers[i] == i+1 ); |
---|
233 | tr_free( numbers ); |
---|
234 | |
---|
235 | numbers = tr_parseNumberRange( "1-Hello", -1, &count ); |
---|
236 | check( count == 0 ); |
---|
237 | check( numbers == NULL ); |
---|
238 | |
---|
239 | numbers = tr_parseNumberRange( "1-", -1, &count ); |
---|
240 | check( count == 0 ); |
---|
241 | check( numbers == NULL ); |
---|
242 | |
---|
243 | numbers = tr_parseNumberRange( "Hello", -1, &count ); |
---|
244 | check( count == 0 ); |
---|
245 | check( numbers == NULL ); |
---|
246 | |
---|
247 | return 0; |
---|
248 | } |
---|
249 | |
---|
250 | static int |
---|
251 | compareInts( const void * va, const void * vb ) |
---|
252 | { |
---|
253 | const int a = *(const int *)va; |
---|
254 | const int b = *(const int*)vb; |
---|
255 | return a - b; |
---|
256 | } |
---|
257 | |
---|
258 | static int |
---|
259 | test_lowerbound( void ) |
---|
260 | { |
---|
261 | int i; |
---|
262 | const int A[] = { 1, 2, 3, 3, 3, 5, 8 }; |
---|
263 | const int expected_pos[] = { 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 }; |
---|
264 | const int expected_exact[] = { TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE }; |
---|
265 | const int N = sizeof(A) / sizeof(A[0]); |
---|
266 | |
---|
267 | for( i=1; i<=10; ++i ) |
---|
268 | { |
---|
269 | tr_bool exact; |
---|
270 | const int pos = tr_lowerBound( &i, A, N, sizeof(int), compareInts, &exact ); |
---|
271 | |
---|
272 | #if 0 |
---|
273 | fprintf( stderr, "searching for %d. ", i ); |
---|
274 | fprintf( stderr, "result: index = %d, ", pos ); |
---|
275 | if( pos != N ) |
---|
276 | fprintf( stderr, "A[%d] == %d\n", pos, A[pos] ); |
---|
277 | else |
---|
278 | fprintf( stderr, "which is off the end.\n" ); |
---|
279 | #endif |
---|
280 | check( pos == expected_pos[i-1] ) |
---|
281 | check( exact == expected_exact[i-1] ) |
---|
282 | } |
---|
283 | |
---|
284 | return 0; |
---|
285 | } |
---|
286 | |
---|
287 | static int |
---|
288 | test_memmem( void ) |
---|
289 | { |
---|
290 | char const haystack[12] = "abcabcabcabc"; |
---|
291 | char const needle[3] = "cab"; |
---|
292 | |
---|
293 | check( tr_memmem( haystack, sizeof haystack, haystack, sizeof haystack) == haystack ) |
---|
294 | check( tr_memmem( haystack, sizeof haystack, needle, sizeof needle) == haystack + 2 ) |
---|
295 | check( tr_memmem( needle, sizeof needle, haystack, sizeof haystack) == NULL ) |
---|
296 | |
---|
297 | return 0; |
---|
298 | } |
---|
299 | |
---|
300 | static int |
---|
301 | test_hex( void ) |
---|
302 | { |
---|
303 | char hex1[41]; |
---|
304 | char hex2[41]; |
---|
305 | uint8_t sha1[20]; |
---|
306 | /*uint8_t sha2[20];*/ |
---|
307 | |
---|
308 | memcpy( hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41 ); |
---|
309 | tr_hex_to_sha1( sha1, hex1 ); |
---|
310 | tr_sha1_to_hex( hex2, sha1 ); |
---|
311 | check( !strcmp( hex1, hex2 ) ) |
---|
312 | |
---|
313 | return 0; |
---|
314 | } |
---|
315 | |
---|
316 | static int |
---|
317 | test_array( void ) |
---|
318 | { |
---|
319 | int i; |
---|
320 | int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
---|
321 | int n = sizeof( array ) / sizeof( array[0] ); |
---|
322 | |
---|
323 | tr_removeElementFromArray( array, 5u, sizeof( int ), n-- ); |
---|
324 | for( i=0; i<n; ++i ) |
---|
325 | check( array[i] == ( i<5 ? i : i+1 ) ); |
---|
326 | |
---|
327 | tr_removeElementFromArray( array, 0u, sizeof( int ), n-- ); |
---|
328 | for( i=0; i<n; ++i ) |
---|
329 | check( array[i] == ( i<4 ? i+1 : i+2 ) ); |
---|
330 | |
---|
331 | tr_removeElementFromArray( array, n-1, sizeof( int ), n ); n--; |
---|
332 | for( i=0; i<n; ++i ) |
---|
333 | check( array[i] == ( i<4 ? i+1 : i+2 ) ); |
---|
334 | |
---|
335 | return 0; |
---|
336 | } |
---|
337 | |
---|
338 | static int |
---|
339 | test_url( void ) |
---|
340 | { |
---|
341 | int port; |
---|
342 | char * scheme; |
---|
343 | char * host; |
---|
344 | char * path; |
---|
345 | char * str; |
---|
346 | const char * url; |
---|
347 | |
---|
348 | url = "http://www.some-tracker.org/some/path"; |
---|
349 | check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) ) |
---|
350 | check( !strcmp( scheme, "http" ) ) |
---|
351 | check( !strcmp( host, "www.some-tracker.org" ) ) |
---|
352 | check( !strcmp( path, "/some/path" ) ) |
---|
353 | check( port == 80 ) |
---|
354 | tr_free( scheme ); |
---|
355 | tr_free( path ); |
---|
356 | tr_free( host ); |
---|
357 | |
---|
358 | url = "http://www.some-tracker.org:80/some/path"; |
---|
359 | check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) ) |
---|
360 | check( !strcmp( scheme, "http" ) ) |
---|
361 | check( !strcmp( host, "www.some-tracker.org" ) ) |
---|
362 | check( !strcmp( path, "/some/path" ) ) |
---|
363 | check( port == 80 ) |
---|
364 | tr_free( scheme ); |
---|
365 | tr_free( path ); |
---|
366 | tr_free( host ); |
---|
367 | |
---|
368 | url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2"; |
---|
369 | str = tr_http_unescape( url, strlen( url ) ); |
---|
370 | check( !strcmp( str, "http://www.example.com/~user/?test=1&test1=2" ) ) |
---|
371 | tr_free( str ); |
---|
372 | |
---|
373 | return 0; |
---|
374 | } |
---|
375 | |
---|
376 | static int |
---|
377 | test_truncd( void ) |
---|
378 | { |
---|
379 | char buf[32]; |
---|
380 | const double nan = sqrt( -1 ); |
---|
381 | |
---|
382 | tr_snprintf( buf, sizeof( buf ), "%.2f%%", 99.999 ); |
---|
383 | check( !strcmp( buf, "100.00%" ) ); |
---|
384 | |
---|
385 | tr_snprintf( buf, sizeof( buf ), "%.2f%%", tr_truncd( 99.999, 2 ) ); |
---|
386 | check( !strcmp( buf, "99.99%" ) ); |
---|
387 | |
---|
388 | tr_snprintf( buf, sizeof( buf ), "%.4f", tr_truncd( 403650.656250, 4 ) ); |
---|
389 | check( !strcmp( buf, "403650.6562" ) ); |
---|
390 | |
---|
391 | tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.15, 2 ) ); |
---|
392 | check( !strcmp( buf, "2.15" ) ); |
---|
393 | |
---|
394 | tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.05, 2 ) ); |
---|
395 | check( !strcmp( buf, "2.05" ) ); |
---|
396 | |
---|
397 | tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 3.3333, 2 ) ); |
---|
398 | check( !strcmp( buf, "3.33" ) ); |
---|
399 | |
---|
400 | tr_snprintf( buf, sizeof( buf ), "%.0f", tr_truncd( 3.3333, 0 ) ); |
---|
401 | check( !strcmp( buf, "3" ) ); |
---|
402 | |
---|
403 | tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( nan, 2 ) ); |
---|
404 | check( strstr( buf, "nan" ) != NULL ); |
---|
405 | |
---|
406 | return 0; |
---|
407 | } |
---|
408 | |
---|
409 | int |
---|
410 | main( void ) |
---|
411 | { |
---|
412 | char *in, *out; |
---|
413 | int len; |
---|
414 | int i; |
---|
415 | int l; |
---|
416 | |
---|
417 | /* base64 */ |
---|
418 | out = tr_base64_encode( "YOYO!", -1, &len ); |
---|
419 | check( out ); |
---|
420 | check( !strcmp( out, "WU9ZTyE=" ) ); |
---|
421 | check( len == 8 ); |
---|
422 | in = tr_base64_decode( out, -1, &len ); |
---|
423 | check( in ); |
---|
424 | check( !strcmp( in, "YOYO!" ) ); |
---|
425 | check( len == 5 ); |
---|
426 | tr_free( in ); |
---|
427 | tr_free( out ); |
---|
428 | out = tr_base64_encode( NULL, 0, &len ); |
---|
429 | check( out == NULL ); |
---|
430 | check( len == 0 ); |
---|
431 | |
---|
432 | if( ( i = test_hex( ) ) ) |
---|
433 | return i; |
---|
434 | if( ( i = test_lowerbound( ) ) ) |
---|
435 | return i; |
---|
436 | if( ( i = test_strip_positional_args( ) ) ) |
---|
437 | return i; |
---|
438 | if( ( i = test_strstrip( ) ) ) |
---|
439 | return i; |
---|
440 | if( ( i = test_buildpath( ) ) ) |
---|
441 | return i; |
---|
442 | if( ( i = test_utf8( ) ) ) |
---|
443 | return i; |
---|
444 | if( ( i = test_numbers( ) ) ) |
---|
445 | return i; |
---|
446 | if( ( i = test_memmem( ) ) ) |
---|
447 | return i; |
---|
448 | if( ( i = test_array( ) ) ) |
---|
449 | return i; |
---|
450 | if( ( i = test_url( ) ) ) |
---|
451 | return i; |
---|
452 | if( ( i = test_truncd( ) ) ) |
---|
453 | return i; |
---|
454 | |
---|
455 | /* test that tr_cryptoRandInt() stays in-bounds */ |
---|
456 | for( i = 0; i < 100000; ++i ) |
---|
457 | { |
---|
458 | const int val = tr_cryptoRandInt( 100 ); |
---|
459 | check( val >= 0 ); |
---|
460 | check( val < 100 ); |
---|
461 | } |
---|
462 | |
---|
463 | /* simple bitfield tests */ |
---|
464 | for( l = 0; l < NUM_LOOPS; ++l ) |
---|
465 | if( ( i = test_bitfields( ) ) ) |
---|
466 | return i; |
---|
467 | |
---|
468 | return 0; |
---|
469 | } |
---|
470 | |
---|