1 | #include <stdio.h> /* fprintf */ |
---|
2 | #include <string.h> /* strcmp */ |
---|
3 | #include "transmission.h" |
---|
4 | #include "ConvertUTF.h" /* tr_utf8_validate*/ |
---|
5 | #include "platform.h" |
---|
6 | #include "utils.h" |
---|
7 | #include "crypto.h" |
---|
8 | |
---|
9 | #define VERBOSE 0 |
---|
10 | #define NUM_LOOPS 1 |
---|
11 | #define SPEED_TEST 0 |
---|
12 | |
---|
13 | #if SPEED_TEST |
---|
14 | #undef VERBOSE |
---|
15 | #define VERBOSE 0 |
---|
16 | #undef NUM_LOOPS |
---|
17 | #define NUM_LOOPS 200 |
---|
18 | #endif |
---|
19 | |
---|
20 | static int test = 0; |
---|
21 | |
---|
22 | #define check( A ) \ |
---|
23 | { \ |
---|
24 | ++test; \ |
---|
25 | if( A ){ \ |
---|
26 | if( VERBOSE ) \ |
---|
27 | fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__,\ |
---|
28 | __LINE__ );\ |
---|
29 | } else { \ |
---|
30 | fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__,\ |
---|
31 | __LINE__ ); \ |
---|
32 | return test; \ |
---|
33 | } \ |
---|
34 | } |
---|
35 | |
---|
36 | static int |
---|
37 | test_bitfields( void ) |
---|
38 | { |
---|
39 | unsigned int i; |
---|
40 | unsigned int bitcount = 5000000; |
---|
41 | tr_bitfield * field = tr_bitfieldNew( bitcount ); |
---|
42 | |
---|
43 | /* test tr_bitfieldAdd */ |
---|
44 | for( i = 0; i < bitcount; ++i ) |
---|
45 | if( !( i % 7 ) ) |
---|
46 | tr_bitfieldAdd( field, i ); |
---|
47 | for( i = 0; i < bitcount; ++i ) |
---|
48 | check( tr_bitfieldHas( field, i ) == ( !( i % 7 ) ) ); |
---|
49 | |
---|
50 | /* test tr_bitfieldAddRange */ |
---|
51 | tr_bitfieldAddRange( field, 0, bitcount ); |
---|
52 | for( i = 0; i < bitcount; ++i ) |
---|
53 | check( tr_bitfieldHas( field, i ) ); |
---|
54 | |
---|
55 | /* test tr_bitfieldRemRange in the middle of a boundary */ |
---|
56 | tr_bitfieldRemRange( field, 4, 21 ); |
---|
57 | for( i = 0; i < 64; ++i ) |
---|
58 | check( tr_bitfieldHas( field, i ) == ( ( i < 4 ) || ( i >= 21 ) ) ); |
---|
59 | |
---|
60 | /* test tr_bitfieldRemRange on the boundaries */ |
---|
61 | tr_bitfieldAddRange( field, 0, 64 ); |
---|
62 | tr_bitfieldRemRange( field, 8, 24 ); |
---|
63 | for( i = 0; i < 64; ++i ) |
---|
64 | check( tr_bitfieldHas( field, i ) == ( ( i < 8 ) || ( i >= 24 ) ) ); |
---|
65 | |
---|
66 | /* test tr_bitfieldRemRange when begin & end is on the same word */ |
---|
67 | tr_bitfieldAddRange( field, 0, 64 ); |
---|
68 | tr_bitfieldRemRange( field, 4, 5 ); |
---|
69 | for( i = 0; i < 64; ++i ) |
---|
70 | check( tr_bitfieldHas( field, i ) == ( ( i < 4 ) || ( i >= 5 ) ) ); |
---|
71 | |
---|
72 | /* test tr_bitfieldAddRange */ |
---|
73 | tr_bitfieldRemRange( field, 0, 64 ); |
---|
74 | tr_bitfieldAddRange( field, 4, 21 ); |
---|
75 | for( i = 0; i < 64; ++i ) |
---|
76 | check( tr_bitfieldHas( field, i ) == ( ( 4 <= i ) && ( i < 21 ) ) ); |
---|
77 | |
---|
78 | /* test tr_bitfieldAddRange on the boundaries */ |
---|
79 | tr_bitfieldRemRange( field, 0, 64 ); |
---|
80 | tr_bitfieldAddRange( field, 8, 24 ); |
---|
81 | for( i = 0; i < 64; ++i ) |
---|
82 | check( tr_bitfieldHas( field, i ) == ( ( 8 <= i ) && ( i < 24 ) ) ); |
---|
83 | |
---|
84 | /* test tr_bitfieldAddRange when begin & end is on the same word */ |
---|
85 | tr_bitfieldRemRange( field, 0, 64 ); |
---|
86 | tr_bitfieldAddRange( field, 4, 5 ); |
---|
87 | for( i = 0; i < 64; ++i ) |
---|
88 | check( tr_bitfieldHas( field, i ) == ( ( 4 <= i ) && ( i < 5 ) ) ); |
---|
89 | |
---|
90 | tr_bitfieldFree( field ); |
---|
91 | return 0; |
---|
92 | } |
---|
93 | |
---|
94 | static int |
---|
95 | test_strstrip( void ) |
---|
96 | { |
---|
97 | char *in, *out; |
---|
98 | |
---|
99 | /* strstrip */ |
---|
100 | in = tr_strdup( " test " ); |
---|
101 | out = tr_strstrip( in ); |
---|
102 | check( in == out ); |
---|
103 | check( !strcmp( in, "test" ) ); |
---|
104 | tr_free( in ); |
---|
105 | |
---|
106 | /* strstrip */ |
---|
107 | in = tr_strdup( " test test " ); |
---|
108 | out = tr_strstrip( in ); |
---|
109 | check( in == out ); |
---|
110 | check( !strcmp( in, "test test" ) ); |
---|
111 | tr_free( in ); |
---|
112 | |
---|
113 | /* strstrip */ |
---|
114 | in = tr_strdup( "test" ); |
---|
115 | out = tr_strstrip( in ); |
---|
116 | check( in == out ); |
---|
117 | check( !strcmp( in, "test" ) ); |
---|
118 | tr_free( in ); |
---|
119 | |
---|
120 | return 0; |
---|
121 | } |
---|
122 | |
---|
123 | static int |
---|
124 | test_buildpath( void ) |
---|
125 | { |
---|
126 | char * out; |
---|
127 | |
---|
128 | out = tr_buildPath( "foo", "bar", NULL ); |
---|
129 | check( !strcmp( out, "foo" TR_PATH_DELIMITER_STR "bar" ) ); |
---|
130 | tr_free( out ); |
---|
131 | |
---|
132 | out = tr_buildPath( "", "foo", "bar", NULL ); |
---|
133 | check( !strcmp( out, TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar" ) ); |
---|
134 | tr_free( out ); |
---|
135 | |
---|
136 | return 0; |
---|
137 | } |
---|
138 | |
---|
139 | static int |
---|
140 | test_utf8( void ) |
---|
141 | { |
---|
142 | const char * in; |
---|
143 | char * out; |
---|
144 | tr_bool err; |
---|
145 | |
---|
146 | in = "hello world"; |
---|
147 | out = tr_utf8clean( in, -1, &err ); |
---|
148 | check( err == FALSE ) |
---|
149 | check( out != NULL ) |
---|
150 | check( !strcmp( out, in ) ) |
---|
151 | tr_free( out ); |
---|
152 | |
---|
153 | in = "hello world"; |
---|
154 | out = tr_utf8clean( in, 5, &err ); |
---|
155 | check( err == FALSE ) |
---|
156 | check( out != NULL ) |
---|
157 | check( !strcmp( out, "hello" ) ) |
---|
158 | tr_free( out ); |
---|
159 | |
---|
160 | /* this version is not utf-8 */ |
---|
161 | in = "Òðóäíî áûòü Áîãîì"; |
---|
162 | out = tr_utf8clean( in, 17, &err ); |
---|
163 | check( out != NULL ) |
---|
164 | check( err != 0 ) |
---|
165 | check( strlen( out ) == 17 ) |
---|
166 | check( tr_utf8_validate( out, -1, NULL ) ) |
---|
167 | tr_free( out ); |
---|
168 | |
---|
169 | /* same string, but utf-8 clean */ |
---|
170 | in = "ÃðóÀÃî áûòÌ Ãîãîì"; |
---|
171 | out = tr_utf8clean( in, -1, &err ); |
---|
172 | check( out != NULL ) |
---|
173 | check( !err ); |
---|
174 | check( tr_utf8_validate( out, -1, NULL ) ) |
---|
175 | check ( !strcmp( in, out ) ) |
---|
176 | tr_free( out ); |
---|
177 | |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
181 | int |
---|
182 | main( void ) |
---|
183 | { |
---|
184 | char *in, *out; |
---|
185 | int len; |
---|
186 | int i; |
---|
187 | int l; |
---|
188 | |
---|
189 | /* base64 */ |
---|
190 | in = "YOYO!"; |
---|
191 | out = tr_base64_encode( in, -1, &len ); |
---|
192 | check( out ); |
---|
193 | check( !strcmp( out, "WU9ZTyE=\n" ) ); |
---|
194 | check( len == 9 ); |
---|
195 | in = tr_base64_decode( out, -1, &len ); |
---|
196 | check( in ); |
---|
197 | check( !strcmp( in, "YOYO!" ) ); |
---|
198 | check( len == 5 ); |
---|
199 | tr_free( in ); |
---|
200 | tr_free( out ); |
---|
201 | |
---|
202 | if( ( i = test_strstrip( ) ) ) |
---|
203 | return i; |
---|
204 | if( ( i = test_buildpath( ) ) ) |
---|
205 | return i; |
---|
206 | if( ( i = test_utf8( ) ) ) |
---|
207 | return i; |
---|
208 | |
---|
209 | /* test that tr_cryptoRandInt() stays in-bounds */ |
---|
210 | for( i = 0; i < 100000; ++i ) |
---|
211 | { |
---|
212 | const int val = tr_cryptoRandInt( 100 ); |
---|
213 | check( val >= 0 ); |
---|
214 | check( val < 100 ); |
---|
215 | } |
---|
216 | |
---|
217 | /* simple bitfield tests */ |
---|
218 | for( l = 0; l < NUM_LOOPS; ++l ) |
---|
219 | if( ( i = test_bitfields( ) ) ) |
---|
220 | return i; |
---|
221 | |
---|
222 | return 0; |
---|
223 | } |
---|
224 | |
---|