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