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_strstrip( void ) |
---|
107 | { |
---|
108 | char *in, *out; |
---|
109 | |
---|
110 | /* strstrip */ |
---|
111 | in = tr_strdup( " test " ); |
---|
112 | out = tr_strstrip( in ); |
---|
113 | check( in == out ); |
---|
114 | check( !strcmp( in, "test" ) ); |
---|
115 | tr_free( in ); |
---|
116 | |
---|
117 | /* strstrip */ |
---|
118 | in = tr_strdup( " test test " ); |
---|
119 | out = tr_strstrip( in ); |
---|
120 | check( in == out ); |
---|
121 | check( !strcmp( in, "test test" ) ); |
---|
122 | tr_free( in ); |
---|
123 | |
---|
124 | /* strstrip */ |
---|
125 | in = tr_strdup( "test" ); |
---|
126 | out = tr_strstrip( in ); |
---|
127 | check( in == out ); |
---|
128 | check( !strcmp( in, "test" ) ); |
---|
129 | tr_free( in ); |
---|
130 | |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | static int |
---|
135 | test_buildpath( void ) |
---|
136 | { |
---|
137 | char * out; |
---|
138 | |
---|
139 | out = tr_buildPath( "foo", "bar", NULL ); |
---|
140 | check( !strcmp( out, "foo" TR_PATH_DELIMITER_STR "bar" ) ); |
---|
141 | tr_free( out ); |
---|
142 | |
---|
143 | out = tr_buildPath( "", "foo", "bar", NULL ); |
---|
144 | check( !strcmp( out, TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar" ) ); |
---|
145 | tr_free( out ); |
---|
146 | |
---|
147 | return 0; |
---|
148 | } |
---|
149 | |
---|
150 | static int |
---|
151 | test_utf8( void ) |
---|
152 | { |
---|
153 | const char * in; |
---|
154 | char * out; |
---|
155 | |
---|
156 | in = "hello world"; |
---|
157 | out = tr_utf8clean( in, -1 ); |
---|
158 | check( out != NULL ) |
---|
159 | check( !strcmp( out, in ) ) |
---|
160 | tr_free( out ); |
---|
161 | |
---|
162 | in = "hello world"; |
---|
163 | out = tr_utf8clean( in, 5 ); |
---|
164 | check( out != NULL ) |
---|
165 | check( !strcmp( out, "hello" ) ) |
---|
166 | tr_free( out ); |
---|
167 | |
---|
168 | /* this version is not utf-8 */ |
---|
169 | in = "Òðóäíî áûòü Áîãîì"; |
---|
170 | out = tr_utf8clean( in, 17 ); |
---|
171 | check( out != NULL ) |
---|
172 | check( ( strlen( out ) == 17 ) || ( strlen( out ) == 32 ) ) |
---|
173 | check( tr_utf8_validate( out, -1, NULL ) ) |
---|
174 | tr_free( out ); |
---|
175 | |
---|
176 | /* same string, but utf-8 clean */ |
---|
177 | in = "ÃðóÀÃî áûòÌ Ãîãîì"; |
---|
178 | out = tr_utf8clean( in, -1 ); |
---|
179 | check( out != NULL ) |
---|
180 | check( tr_utf8_validate( out, -1, NULL ) ) |
---|
181 | check ( !strcmp( in, out ) ) |
---|
182 | tr_free( out ); |
---|
183 | |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | static int |
---|
188 | test_numbers( void ) |
---|
189 | { |
---|
190 | int i; |
---|
191 | int count; |
---|
192 | int * numbers; |
---|
193 | |
---|
194 | numbers = tr_parseNumberRange( "1-10,13,16-19", -1, &count ); |
---|
195 | check( count == 15 ); |
---|
196 | check( numbers != NULL ); |
---|
197 | check( numbers[0] == 1 ); |
---|
198 | check( numbers[5] == 6 ); |
---|
199 | check( numbers[9] == 10 ); |
---|
200 | check( numbers[10] == 13 ); |
---|
201 | check( numbers[11] == 16 ); |
---|
202 | check( numbers[14] == 19 ); |
---|
203 | tr_free( numbers ); |
---|
204 | |
---|
205 | numbers = tr_parseNumberRange( "1-5,3-7,2-6", -1, &count ); |
---|
206 | check( count == 7 ); |
---|
207 | check( numbers != NULL ); |
---|
208 | for( i=0; i<count; ++i ) |
---|
209 | check( numbers[i] == i+1 ); |
---|
210 | tr_free( numbers ); |
---|
211 | |
---|
212 | numbers = tr_parseNumberRange( "1-Hello", -1, &count ); |
---|
213 | check( count == 0 ); |
---|
214 | check( numbers == NULL ); |
---|
215 | |
---|
216 | numbers = tr_parseNumberRange( "1-", -1, &count ); |
---|
217 | check( count == 0 ); |
---|
218 | check( numbers == NULL ); |
---|
219 | |
---|
220 | numbers = tr_parseNumberRange( "Hello", -1, &count ); |
---|
221 | check( count == 0 ); |
---|
222 | check( numbers == NULL ); |
---|
223 | |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | static int |
---|
228 | compareInts( const void * va, const void * vb ) |
---|
229 | { |
---|
230 | const int a = *(const int *)va; |
---|
231 | const int b = *(const int*)vb; |
---|
232 | return a - b; |
---|
233 | } |
---|
234 | |
---|
235 | static int |
---|
236 | test_lowerbound( void ) |
---|
237 | { |
---|
238 | int i; |
---|
239 | const int A[] = { 1, 2, 3, 3, 3, 5, 8 }; |
---|
240 | const int expected_pos[] = { 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 }; |
---|
241 | const int expected_exact[] = { TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE }; |
---|
242 | const int N = sizeof(A) / sizeof(A[0]); |
---|
243 | |
---|
244 | for( i=1; i<=10; ++i ) |
---|
245 | { |
---|
246 | tr_bool exact; |
---|
247 | const int pos = tr_lowerBound( &i, A, N, sizeof(int), compareInts, &exact ); |
---|
248 | |
---|
249 | #if 0 |
---|
250 | fprintf( stderr, "searching for %d. ", i ); |
---|
251 | fprintf( stderr, "result: index = %d, ", pos ); |
---|
252 | if( pos != N ) |
---|
253 | fprintf( stderr, "A[%d] == %d\n", pos, A[pos] ); |
---|
254 | else |
---|
255 | fprintf( stderr, "which is off the end.\n" ); |
---|
256 | #endif |
---|
257 | check( pos == expected_pos[i-1] ) |
---|
258 | check( exact == expected_exact[i-1] ) |
---|
259 | } |
---|
260 | |
---|
261 | return 0; |
---|
262 | } |
---|
263 | |
---|
264 | static int |
---|
265 | test_memmem( void ) |
---|
266 | { |
---|
267 | char const haystack[12] = "abcabcabcabc"; |
---|
268 | char const needle[3] = "cab"; |
---|
269 | |
---|
270 | check( tr_memmem( haystack, sizeof haystack, haystack, sizeof haystack) == haystack ) |
---|
271 | check( tr_memmem( haystack, sizeof haystack, needle, sizeof needle) == haystack + 2 ) |
---|
272 | check( tr_memmem( needle, sizeof needle, haystack, sizeof haystack) == NULL ) |
---|
273 | check( tr_memmem( haystack, sizeof haystack, "", 0) == haystack ) |
---|
274 | check( tr_memmem( haystack, sizeof haystack, NULL, 0) == haystack ) |
---|
275 | check( tr_memmem( haystack, 0, "", 0) == haystack ) |
---|
276 | |
---|
277 | return 0; |
---|
278 | } |
---|
279 | |
---|
280 | static int |
---|
281 | test_hex( void ) |
---|
282 | { |
---|
283 | char hex1[41]; |
---|
284 | char hex2[41]; |
---|
285 | uint8_t sha1[20]; |
---|
286 | /*uint8_t sha2[20];*/ |
---|
287 | |
---|
288 | memcpy( hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41 ); |
---|
289 | tr_hex_to_sha1( sha1, hex1 ); |
---|
290 | tr_sha1_to_hex( hex2, sha1 ); |
---|
291 | check( !strcmp( hex1, hex2 ) ) |
---|
292 | |
---|
293 | return 0; |
---|
294 | } |
---|
295 | |
---|
296 | static int |
---|
297 | test_array( void ) |
---|
298 | { |
---|
299 | int i; |
---|
300 | int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
---|
301 | int n = sizeof( array ) / sizeof( array[0] ); |
---|
302 | |
---|
303 | tr_removeElementFromArray( array, 5, sizeof( int ), n-- ); |
---|
304 | for( i=0; i<n; ++i ) |
---|
305 | check( array[i] == ( i<5 ? i : i+1 ) ); |
---|
306 | |
---|
307 | tr_removeElementFromArray( array, 0, sizeof( int ), n-- ); |
---|
308 | for( i=0; i<n; ++i ) |
---|
309 | check( array[i] == ( i<4 ? i+1 : i+2 ) ); |
---|
310 | |
---|
311 | tr_removeElementFromArray( array, n-1, sizeof( int ), n ); n--; |
---|
312 | for( i=0; i<n; ++i ) |
---|
313 | check( array[i] == ( i<4 ? i+1 : i+2 ) ); |
---|
314 | |
---|
315 | return 0; |
---|
316 | } |
---|
317 | |
---|
318 | static int |
---|
319 | test_url( void ) |
---|
320 | { |
---|
321 | int port; |
---|
322 | char * scheme; |
---|
323 | char * host; |
---|
324 | char * path; |
---|
325 | char * str; |
---|
326 | const char * url; |
---|
327 | |
---|
328 | url = "http://www.some-tracker.org/some/path"; |
---|
329 | check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) ) |
---|
330 | check( !strcmp( scheme, "http" ) ) |
---|
331 | check( !strcmp( host, "www.some-tracker.org" ) ) |
---|
332 | check( !strcmp( path, "/some/path" ) ) |
---|
333 | check( port == 80 ) |
---|
334 | tr_free( scheme ); |
---|
335 | tr_free( path ); |
---|
336 | tr_free( host ); |
---|
337 | |
---|
338 | url = "http://www.some-tracker.org:80/some/path"; |
---|
339 | check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) ) |
---|
340 | check( !strcmp( scheme, "http" ) ) |
---|
341 | check( !strcmp( host, "www.some-tracker.org" ) ) |
---|
342 | check( !strcmp( path, "/some/path" ) ) |
---|
343 | check( port == 80 ) |
---|
344 | tr_free( scheme ); |
---|
345 | tr_free( path ); |
---|
346 | tr_free( host ); |
---|
347 | |
---|
348 | url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2"; |
---|
349 | str = tr_http_unescape( url, strlen( url ) ); |
---|
350 | check( !strcmp( str, "http://www.example.com/~user/?test=1&test1=2" ) ) |
---|
351 | tr_free( str ); |
---|
352 | |
---|
353 | return 0; |
---|
354 | } |
---|
355 | |
---|
356 | int |
---|
357 | main( void ) |
---|
358 | { |
---|
359 | char buf[32]; |
---|
360 | char *in, *out; |
---|
361 | int len; |
---|
362 | int i; |
---|
363 | int l; |
---|
364 | |
---|
365 | /* tr_truncd */ |
---|
366 | tr_snprintf( buf, sizeof( buf ), "%.2f%%", 99.999 ); |
---|
367 | check( !strcmp( buf, "100.00%" ) ); |
---|
368 | tr_snprintf( buf, sizeof( buf ), "%.2f%%", tr_truncd( 99.999, 2 ) ); |
---|
369 | check( !strcmp( buf, "99.99%" ) ); |
---|
370 | |
---|
371 | /* base64 */ |
---|
372 | out = tr_base64_encode( "YOYO!", -1, &len ); |
---|
373 | check( out ); |
---|
374 | check( !strcmp( out, "WU9ZTyE=\n" ) ); |
---|
375 | check( len == 9 ); |
---|
376 | in = tr_base64_decode( out, -1, &len ); |
---|
377 | check( in ); |
---|
378 | check( !strcmp( in, "YOYO!" ) ); |
---|
379 | check( len == 5 ); |
---|
380 | tr_free( in ); |
---|
381 | tr_free( out ); |
---|
382 | out = tr_base64_encode( NULL, 0, &len ); |
---|
383 | check( out == NULL ); |
---|
384 | check( len == 0 ); |
---|
385 | |
---|
386 | if( ( i = test_hex( ) ) ) |
---|
387 | return i; |
---|
388 | if( ( i = test_lowerbound( ) ) ) |
---|
389 | return i; |
---|
390 | if( ( i = test_strstrip( ) ) ) |
---|
391 | return i; |
---|
392 | if( ( i = test_buildpath( ) ) ) |
---|
393 | return i; |
---|
394 | if( ( i = test_utf8( ) ) ) |
---|
395 | return i; |
---|
396 | if( ( i = test_numbers( ) ) ) |
---|
397 | return i; |
---|
398 | if( ( i = test_memmem( ) ) ) |
---|
399 | return i; |
---|
400 | if( ( i = test_array( ) ) ) |
---|
401 | return i; |
---|
402 | if( ( i = test_url( ) ) ) |
---|
403 | return i; |
---|
404 | |
---|
405 | /* test that tr_cryptoRandInt() stays in-bounds */ |
---|
406 | for( i = 0; i < 100000; ++i ) |
---|
407 | { |
---|
408 | const int val = tr_cryptoRandInt( 100 ); |
---|
409 | check( val >= 0 ); |
---|
410 | check( val < 100 ); |
---|
411 | } |
---|
412 | |
---|
413 | /* simple bitfield tests */ |
---|
414 | for( l = 0; l < NUM_LOOPS; ++l ) |
---|
415 | if( ( i = test_bitfields( ) ) ) |
---|
416 | return i; |
---|
417 | |
---|
418 | return 0; |
---|
419 | } |
---|
420 | |
---|