1 | /**
|
---|
2 | * @file test.c
|
---|
3 | * @author myusgun <myusgun@gmail.com>
|
---|
4 | */
|
---|
5 | #include "cf_file.h"
|
---|
6 | #include "cf_log.h"
|
---|
7 | #include "cf_socket.h"
|
---|
8 | #include "cf_thread.h"
|
---|
9 | #include "cf_codec.h"
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * before including "cf_debug.h",
|
---|
13 | * if _DEBUG is defined, the debugging code will be act.
|
---|
14 | */
|
---|
15 | #include "cf_debug.h"
|
---|
16 |
|
---|
17 | #include <stdlib.h>
|
---|
18 | #include <string.h>
|
---|
19 | #include <errno.h>
|
---|
20 |
|
---|
21 | #define THREAD_POOL 5
|
---|
22 | #define PORT 1234
|
---|
23 |
|
---|
24 | CF_Mutex globalMutex;
|
---|
25 |
|
---|
26 | #define LOG_SOCKET 0
|
---|
27 | #define LOG_MT 1
|
---|
28 |
|
---|
29 | const char * file = "./log.txt";
|
---|
30 |
|
---|
31 | int test_log_mt (void * arg);
|
---|
32 | void test_log (const char * message);
|
---|
33 | void test_file (const char * message);
|
---|
34 | void test_callstack3 (void);
|
---|
35 | void test_callstack2 (void);
|
---|
36 | void test_callstack1 (void);
|
---|
37 | int socket_echo_server (void * arg);
|
---|
38 | int socket_echo_client (void * arg);
|
---|
39 | void test_socket (void);
|
---|
40 | void test_codec (void);
|
---|
41 |
|
---|
42 | int main (int argc, char ** argv)
|
---|
43 | {
|
---|
44 | CF_DEBUG_INITIALIZE;
|
---|
45 |
|
---|
46 | // 1
|
---|
47 | CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
|
---|
48 | test_log ("LOG_TEST");
|
---|
49 |
|
---|
50 | // 2
|
---|
51 | CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
|
---|
52 | test_file ("FILE_READ_TEST");
|
---|
53 |
|
---|
54 | // 3
|
---|
55 | CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
|
---|
56 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
57 | test_callstack1 ();
|
---|
58 | CF_DEBUG_END_FUNCTION;
|
---|
59 |
|
---|
60 | // 4
|
---|
61 | CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n");
|
---|
62 | test_socket ();
|
---|
63 |
|
---|
64 | // 5
|
---|
65 | CF_DEBUG_PRINT (stderr, " == CODEC TEST ==\n");
|
---|
66 | test_codec ();
|
---|
67 |
|
---|
68 | CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
|
---|
69 |
|
---|
70 | CF_DEBUG_FINALIZE;
|
---|
71 |
|
---|
72 | return 0;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int test_log_mt (void * arg)
|
---|
76 | {
|
---|
77 | static int cnt = 0;
|
---|
78 | int i = 0;
|
---|
79 | int th = 0;
|
---|
80 |
|
---|
81 | if (CF_Mutex_Lock (&globalMutex) < 0) { // for critical section
|
---|
82 | // error
|
---|
83 | }
|
---|
84 | th = cnt++;
|
---|
85 | if (CF_Mutex_Unlock (&globalMutex) < 0) { // for critical section
|
---|
86 | // error
|
---|
87 | }
|
---|
88 |
|
---|
89 | CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
|
---|
90 |
|
---|
91 | for (i = 0 ; i < 100000 ; i++)
|
---|
92 | {
|
---|
93 | CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
|
---|
94 | }
|
---|
95 |
|
---|
96 | CF_DEBUG_PRINT (stderr, "end %dth thread\n", th);
|
---|
97 |
|
---|
98 | return 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void test_log (const char * message)
|
---|
102 | {
|
---|
103 | int i, j;
|
---|
104 | char idname[16] = {0x00,};
|
---|
105 | CF_Thread tid[10];
|
---|
106 |
|
---|
107 | /* initialize */
|
---|
108 | if (CF_Log_Initialize (10) < 0)
|
---|
109 | {
|
---|
110 | fprintf (stderr, "failed to init. log\n");
|
---|
111 | return ;
|
---|
112 | }
|
---|
113 |
|
---|
114 | for (i = 0 ; i < 10 ; i++)
|
---|
115 | {
|
---|
116 | sprintf (idname, "logid%d.txt", i);
|
---|
117 | if (CF_Log_Open (i, idname, CF_LOG_BUFFER_NO) < 0)
|
---|
118 | fprintf (stderr, "failed to open log\n");
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (CF_Log_SetLevel (5) < 0)
|
---|
122 | fprintf (stderr, "failed to set log level\n");
|
---|
123 |
|
---|
124 | for (i = 0 ; i < 10 ; i++)
|
---|
125 | {
|
---|
126 | for (j = 0 ; j < 10000 ; j++)
|
---|
127 | {
|
---|
128 | int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
|
---|
129 | if (result < 0)
|
---|
130 | fprintf (stderr, "failed to write log %d\n", result);
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (CF_Log_Close (i) < 0)
|
---|
134 | fprintf (stderr, "failed to close log\n");
|
---|
135 | }
|
---|
136 |
|
---|
137 | /* mt {{{ */
|
---|
138 | if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
139 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
140 |
|
---|
141 | if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
|
---|
142 | CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
|
---|
143 |
|
---|
144 | if (CF_Mutex_Create (&globalMutex) < 0) {
|
---|
145 | // error
|
---|
146 | }
|
---|
147 |
|
---|
148 | for (i = 0 ; i < 10 ; i++)
|
---|
149 | {
|
---|
150 | if (CF_Thread_Create (&tid[i], test_log_mt, &i) < 0)
|
---|
151 | {
|
---|
152 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
|
---|
153 | return ;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | for (i = 0 ; i < 10 ; i++)
|
---|
158 | {
|
---|
159 | if (CF_Thread_Join (&tid[i]) < 0)
|
---|
160 | CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
|
---|
161 | if (CF_Thread_Release (&tid[i]) < 0)
|
---|
162 | CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (CF_Mutex_Destory (&globalMutex) < 0) {
|
---|
166 | // error
|
---|
167 | }
|
---|
168 | /* }}} mt */
|
---|
169 |
|
---|
170 | /* finalize */
|
---|
171 | CF_Log_Finalize ();
|
---|
172 | }
|
---|
173 |
|
---|
174 | void test_file (const char * message)
|
---|
175 | {
|
---|
176 | int fd = 0;
|
---|
177 | char buffer[128] = {0x00,};
|
---|
178 |
|
---|
179 | fd = CF_File_Open (file, CF_FILE_READ);
|
---|
180 | if (fd < 0)
|
---|
181 | CF_DEBUG_PRINT (stderr, "what the ... file open ?\n");
|
---|
182 |
|
---|
183 | if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
|
---|
184 | CF_DEBUG_PRINT (stderr, "what the ... file read ?\n");
|
---|
185 | else
|
---|
186 | CF_DEBUG_PRINT_BIN (stdout, (unsigned char *)buffer,
|
---|
187 | sizeof (buffer),
|
---|
188 | "-- %d bytes of %d bytes\n",
|
---|
189 | sizeof (buffer),
|
---|
190 | CF_File_GetSize (file));
|
---|
191 |
|
---|
192 | CF_File_Close (fd);
|
---|
193 | }
|
---|
194 |
|
---|
195 | void test_callstack3 (void)
|
---|
196 | {
|
---|
197 | // ...
|
---|
198 |
|
---|
199 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
200 | CF_DEBUG_PRINT_CALLSTACK (stderr);
|
---|
201 | CF_DEBUG_END_FUNCTION;
|
---|
202 |
|
---|
203 | // ...
|
---|
204 | }
|
---|
205 | void test_callstack2 (void)
|
---|
206 | {
|
---|
207 | // ...
|
---|
208 |
|
---|
209 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
210 | test_callstack3 ();
|
---|
211 | CF_DEBUG_END_FUNCTION;
|
---|
212 |
|
---|
213 | // ...
|
---|
214 | }
|
---|
215 | void test_callstack1 (void)
|
---|
216 | {
|
---|
217 | // ...
|
---|
218 |
|
---|
219 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
220 | test_callstack2 ();
|
---|
221 | CF_DEBUG_END_FUNCTION;
|
---|
222 |
|
---|
223 | // ...
|
---|
224 | }
|
---|
225 |
|
---|
226 | int socket_echo_server (void * arg)
|
---|
227 | {
|
---|
228 | int srvsock = *((int *)arg);
|
---|
229 | int clntsock = 0;
|
---|
230 | int recvd = 0;
|
---|
231 | char buf[1024] = {0x00,};
|
---|
232 | int i = 0;
|
---|
233 |
|
---|
234 | /*------------------------------------------------------------*/
|
---|
235 | clntsock = CF_Socket_Accept (srvsock);
|
---|
236 | if (clntsock < 0)
|
---|
237 | {
|
---|
238 | CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
|
---|
239 | return -1;
|
---|
240 | }
|
---|
241 | CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
|
---|
242 |
|
---|
243 | for (i = 0 ; i < 10 ; i++)
|
---|
244 | {
|
---|
245 | if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
|
---|
246 | {
|
---|
247 | CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
|
---|
248 | return -2;
|
---|
249 | }
|
---|
250 | CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
|
---|
251 | CF_DEBUG_PRINT (stderr, "[SERVER] recv {%s}\n", buf);
|
---|
252 |
|
---|
253 | if (CF_Socket_Send (clntsock, buf, recvd) < 0)
|
---|
254 | {
|
---|
255 | return -3;
|
---|
256 | }
|
---|
257 | CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
|
---|
258 | CF_DEBUG_PRINT (stderr, "[SERVER] resp {%s}\n", buf);
|
---|
259 | }
|
---|
260 |
|
---|
261 | CF_Socket_Close (clntsock);
|
---|
262 |
|
---|
263 | return 0;
|
---|
264 | /*------------------------------------------------------------*/
|
---|
265 | }
|
---|
266 |
|
---|
267 | int socket_echo_client (void * arg)
|
---|
268 | {
|
---|
269 | int sock = 0;
|
---|
270 | int recvd = 0;
|
---|
271 | char buf[1024] = {0x00,};
|
---|
272 | int i = 0;
|
---|
273 |
|
---|
274 | sprintf (buf, "...wow ? is it succeed ?");
|
---|
275 |
|
---|
276 | /*------------------------------------------------------------*/
|
---|
277 | sock = CF_Socket_Connect ("localhost", PORT);
|
---|
278 | if (sock < 0)
|
---|
279 | {
|
---|
280 | CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
|
---|
281 | return -1;
|
---|
282 | }
|
---|
283 | CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
|
---|
284 |
|
---|
285 | for (i = 0 ; i < 10 ; i++)
|
---|
286 | {
|
---|
287 | if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
|
---|
288 | {
|
---|
289 | CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
|
---|
290 | return -2;
|
---|
291 | }
|
---|
292 | CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
|
---|
293 | CF_DEBUG_PRINT (stderr, "[CLIENT] sent {%s}\n", buf);
|
---|
294 |
|
---|
295 | memset (buf, 0x00, sizeof (buf));
|
---|
296 |
|
---|
297 | if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
|
---|
298 | {
|
---|
299 | CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
|
---|
300 | return -3;
|
---|
301 | }
|
---|
302 | CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
|
---|
303 | CF_DEBUG_PRINT (stderr, "[CLIENT] recv {%s}\n", buf);
|
---|
304 | }
|
---|
305 |
|
---|
306 | CF_Socket_Close (sock);
|
---|
307 |
|
---|
308 | return 0;
|
---|
309 | /*------------------------------------------------------------*/
|
---|
310 | }
|
---|
311 |
|
---|
312 | void test_socket (void)
|
---|
313 | {
|
---|
314 | CF_Thread stid[THREAD_POOL];
|
---|
315 | CF_Thread ctid[THREAD_POOL];
|
---|
316 |
|
---|
317 | int sock = 0;
|
---|
318 | int iter = 0;
|
---|
319 |
|
---|
320 | /*------------------------------------------------------------*/
|
---|
321 | if (CF_Log_Initialize (1) < 0)
|
---|
322 | {
|
---|
323 | fprintf (stderr, "failed to init. log\n");
|
---|
324 | return ;
|
---|
325 | }
|
---|
326 |
|
---|
327 | if (CF_Log_Open (LOG_SOCKET, "socket.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
328 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
329 |
|
---|
330 | CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
331 |
|
---|
332 | if (CF_Socket_Initialize () < 0)
|
---|
333 | {
|
---|
334 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
335 | return ;
|
---|
336 | }
|
---|
337 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
|
---|
338 |
|
---|
339 | sock = CF_Socket_Server (PORT, 5);
|
---|
340 | if (sock < 0)
|
---|
341 | {
|
---|
342 | CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
|
---|
343 | return ;
|
---|
344 | }
|
---|
345 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
|
---|
346 |
|
---|
347 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
348 | {
|
---|
349 | if (CF_Thread_Create (&stid[iter], socket_echo_server, &sock) < 0)
|
---|
350 | {
|
---|
351 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
352 | return ;
|
---|
353 | }
|
---|
354 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
|
---|
355 | }
|
---|
356 |
|
---|
357 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
358 | {
|
---|
359 | if (CF_Thread_Create (&ctid[iter], socket_echo_client, &sock) < 0)
|
---|
360 | {
|
---|
361 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
362 | return ;
|
---|
363 | }
|
---|
364 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
|
---|
365 | }
|
---|
366 |
|
---|
367 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
368 | {
|
---|
369 | CF_Thread_Join (&ctid[iter]);
|
---|
370 | CF_Thread_Release (&ctid[iter]);
|
---|
371 |
|
---|
372 | CF_Thread_Join (&stid[iter]);
|
---|
373 | CF_Thread_Release (&stid[iter]);
|
---|
374 |
|
---|
375 | CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
|
---|
376 | }
|
---|
377 |
|
---|
378 | CF_Socket_Close (sock);
|
---|
379 |
|
---|
380 | CF_Socket_Finalize ();
|
---|
381 |
|
---|
382 | CF_Log_Close (LOG_SOCKET);
|
---|
383 |
|
---|
384 | CF_Log_Finalize ();
|
---|
385 | /*------------------------------------------------------------*/
|
---|
386 | }
|
---|
387 |
|
---|
388 | void test_codec (void)
|
---|
389 | {
|
---|
390 | char data[] = "ONE OK ROCK - Nothing Helps";
|
---|
391 |
|
---|
392 | char encode[512] = {0x00,};
|
---|
393 | unsigned char bin[512] = {0x00,};
|
---|
394 | size_t length = 0;
|
---|
395 |
|
---|
396 | /* hex */
|
---|
397 | CF_DEBUG_PRINT (stderr, "------------------- codec/hex ----------------\n");
|
---|
398 | CF_DEBUG_PRINT (stderr, "data : %s\n", data);
|
---|
399 | CF_DEBUG_PRINT (stderr, "= Convert binary to hex =\n");
|
---|
400 | CF_Codec_Hex_Encode ((unsigned char *)data, strlen (data), encode);
|
---|
401 | CF_DEBUG_PRINT (stderr, "hex : %s\n", encode);
|
---|
402 | CF_DEBUG_PRINT_BIN (stderr, (unsigned char *) data, strlen (data), "data : %s\n", data);
|
---|
403 |
|
---|
404 | CF_DEBUG_PRINT (stderr, "= Convert hex to binary =\n");
|
---|
405 | if (CF_Codec_Hex_Decode (encode, bin, &length) < 0) {
|
---|
406 | // error
|
---|
407 | }
|
---|
408 | else
|
---|
409 | CF_DEBUG_PRINT_BIN (stderr, bin, length, "bin : %s\n", bin);
|
---|
410 |
|
---|
411 | memset (bin , 0x00, sizeof (bin));
|
---|
412 | memset (encode, 0x00, sizeof (encode));
|
---|
413 | length = 0;
|
---|
414 |
|
---|
415 | /* base64 */
|
---|
416 | CF_DEBUG_PRINT (stderr, "----------------- codec/base64 ---------------\n");
|
---|
417 | CF_DEBUG_PRINT (stderr, "data : %s\n", data);
|
---|
418 | CF_DEBUG_PRINT (stderr, "= Convert binary to base64 =\n");
|
---|
419 | CF_Codec_Base64_Encode ((unsigned char *)data, strlen (data), encode);
|
---|
420 | CF_DEBUG_PRINT (stderr, "base64 : %s\n", encode);
|
---|
421 |
|
---|
422 | CF_DEBUG_PRINT (stderr, "= Convert base64 to binary =\n");
|
---|
423 | if (CF_Codec_Base64_Decode (encode, bin, &length) < 0) {
|
---|
424 | // error
|
---|
425 | }
|
---|
426 | else
|
---|
427 | CF_DEBUG_PRINT_BIN (stderr, bin, length, "bin : %s\n", bin);
|
---|
428 | }
|
---|