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