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