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, 5) < 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 | for (i = 0 ; i < 10 ; i++)
|
---|
122 | {
|
---|
123 | for (j = 0 ; j < 10000 ; j++)
|
---|
124 | {
|
---|
125 | int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
|
---|
126 | if (result < 0)
|
---|
127 | fprintf (stderr, "failed to write log %d\n", result);
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (CF_Log_Close (i) < 0)
|
---|
131 | fprintf (stderr, "failed to close log\n");
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* mt {{{ */
|
---|
135 | if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
136 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
137 |
|
---|
138 | if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
|
---|
139 | CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
|
---|
140 |
|
---|
141 | if (CF_Mutex_Create (&globalMutex) < 0) {
|
---|
142 | // error
|
---|
143 | }
|
---|
144 |
|
---|
145 | for (i = 0 ; i < 10 ; i++)
|
---|
146 | {
|
---|
147 | if (CF_Thread_Create (&tid[i], test_log_mt, &i) < 0)
|
---|
148 | {
|
---|
149 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
|
---|
150 | return ;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | for (i = 0 ; i < 10 ; i++)
|
---|
155 | {
|
---|
156 | if (CF_Thread_Join (&tid[i]) < 0)
|
---|
157 | CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
|
---|
158 | if (CF_Thread_Release (&tid[i]) < 0)
|
---|
159 | CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (CF_Mutex_Destory (&globalMutex) < 0) {
|
---|
163 | // error
|
---|
164 | }
|
---|
165 | /* }}} mt */
|
---|
166 |
|
---|
167 | /* finalize */
|
---|
168 | CF_Log_Finalize ();
|
---|
169 | }
|
---|
170 |
|
---|
171 | void test_file (const char * message)
|
---|
172 | {
|
---|
173 | int fd = 0;
|
---|
174 | char buffer[128] = {0x00,};
|
---|
175 |
|
---|
176 | fd = CF_File_Open (file, CF_FILE_READ);
|
---|
177 | if (fd < 0)
|
---|
178 | CF_DEBUG_PRINT (stderr, "what the ... file open ?\n");
|
---|
179 |
|
---|
180 | if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
|
---|
181 | CF_DEBUG_PRINT (stderr, "what the ... file read ?\n");
|
---|
182 | else
|
---|
183 | CF_DEBUG_PRINT_BIN (stdout, (unsigned char *)buffer,
|
---|
184 | sizeof (buffer),
|
---|
185 | "-- %d bytes of %d bytes\n",
|
---|
186 | sizeof (buffer),
|
---|
187 | CF_File_GetSize (file));
|
---|
188 |
|
---|
189 | CF_File_Close (fd);
|
---|
190 | }
|
---|
191 |
|
---|
192 | void test_callstack3 (void)
|
---|
193 | {
|
---|
194 | // ...
|
---|
195 |
|
---|
196 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
197 | CF_DEBUG_PRINT_CALLSTACK (stderr);
|
---|
198 | CF_DEBUG_END_FUNCTION;
|
---|
199 |
|
---|
200 | // ...
|
---|
201 | }
|
---|
202 | void test_callstack2 (void)
|
---|
203 | {
|
---|
204 | // ...
|
---|
205 |
|
---|
206 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
207 | test_callstack3 ();
|
---|
208 | CF_DEBUG_END_FUNCTION;
|
---|
209 |
|
---|
210 | // ...
|
---|
211 | }
|
---|
212 | void test_callstack1 (void)
|
---|
213 | {
|
---|
214 | // ...
|
---|
215 |
|
---|
216 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
217 | test_callstack2 ();
|
---|
218 | CF_DEBUG_END_FUNCTION;
|
---|
219 |
|
---|
220 | // ...
|
---|
221 | }
|
---|
222 |
|
---|
223 | int socket_echo_server (void * arg)
|
---|
224 | {
|
---|
225 | int srvsock = *((int *)arg);
|
---|
226 | int clntsock = 0;
|
---|
227 | int recvd = 0;
|
---|
228 | char buf[1024] = {0x00,};
|
---|
229 | int i = 0;
|
---|
230 |
|
---|
231 | /*------------------------------------------------------------*/
|
---|
232 | clntsock = CF_Socket_Accept (srvsock);
|
---|
233 | if (clntsock < 0)
|
---|
234 | {
|
---|
235 | CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
|
---|
236 | return -1;
|
---|
237 | }
|
---|
238 | CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
|
---|
239 |
|
---|
240 | for (i = 0 ; i < 10 ; i++)
|
---|
241 | {
|
---|
242 | if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
|
---|
243 | {
|
---|
244 | CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
|
---|
245 | return -2;
|
---|
246 | }
|
---|
247 | CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
|
---|
248 | CF_DEBUG_PRINT (stderr, "[SERVER] recv {%s}\n", buf);
|
---|
249 |
|
---|
250 | if (CF_Socket_Send (clntsock, buf, recvd) < 0)
|
---|
251 | {
|
---|
252 | return -3;
|
---|
253 | }
|
---|
254 | CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
|
---|
255 | CF_DEBUG_PRINT (stderr, "[SERVER] resp {%s}\n", buf);
|
---|
256 | }
|
---|
257 |
|
---|
258 | CF_Socket_Close (clntsock);
|
---|
259 |
|
---|
260 | return 0;
|
---|
261 | /*------------------------------------------------------------*/
|
---|
262 | }
|
---|
263 |
|
---|
264 | int socket_echo_client (void * arg)
|
---|
265 | {
|
---|
266 | int sock = 0;
|
---|
267 | int recvd = 0;
|
---|
268 | char buf[1024] = {0x00,};
|
---|
269 | int i = 0;
|
---|
270 |
|
---|
271 | sprintf (buf, "...wow ? is it succeed ?");
|
---|
272 |
|
---|
273 | /*------------------------------------------------------------*/
|
---|
274 | sock = CF_Socket_Connect ("localhost", PORT);
|
---|
275 | if (sock < 0)
|
---|
276 | {
|
---|
277 | CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
|
---|
278 | return -1;
|
---|
279 | }
|
---|
280 | CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
|
---|
281 |
|
---|
282 | for (i = 0 ; i < 10 ; i++)
|
---|
283 | {
|
---|
284 | if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
|
---|
285 | {
|
---|
286 | CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
|
---|
287 | return -2;
|
---|
288 | }
|
---|
289 | CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
|
---|
290 | CF_DEBUG_PRINT (stderr, "[CLIENT] sent {%s}\n", buf);
|
---|
291 |
|
---|
292 | memset (buf, 0x00, sizeof (buf));
|
---|
293 |
|
---|
294 | if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
|
---|
295 | {
|
---|
296 | CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
|
---|
297 | return -3;
|
---|
298 | }
|
---|
299 | CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
|
---|
300 | CF_DEBUG_PRINT (stderr, "[CLIENT] recv {%s}\n", buf);
|
---|
301 | }
|
---|
302 |
|
---|
303 | CF_Socket_Close (sock);
|
---|
304 |
|
---|
305 | return 0;
|
---|
306 | /*------------------------------------------------------------*/
|
---|
307 | }
|
---|
308 |
|
---|
309 | void test_socket (void)
|
---|
310 | {
|
---|
311 | CF_Thread stid[THREAD_POOL];
|
---|
312 | CF_Thread ctid[THREAD_POOL];
|
---|
313 |
|
---|
314 | int sock = 0;
|
---|
315 | int iter = 0;
|
---|
316 |
|
---|
317 | /*------------------------------------------------------------*/
|
---|
318 | if (CF_Log_Initialize (1) < 0)
|
---|
319 | {
|
---|
320 | fprintf (stderr, "failed to init. log\n");
|
---|
321 | return ;
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (CF_Log_Open (LOG_SOCKET, "socket.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
325 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
326 |
|
---|
327 | CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
328 |
|
---|
329 | if (CF_Socket_Initialize () < 0)
|
---|
330 | {
|
---|
331 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
332 | return ;
|
---|
333 | }
|
---|
334 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
|
---|
335 |
|
---|
336 | sock = CF_Socket_Server (PORT, 5);
|
---|
337 | if (sock < 0)
|
---|
338 | {
|
---|
339 | CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
|
---|
340 | return ;
|
---|
341 | }
|
---|
342 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
|
---|
343 |
|
---|
344 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
345 | {
|
---|
346 | if (CF_Thread_Create (&stid[iter], socket_echo_server, &sock) < 0)
|
---|
347 | {
|
---|
348 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
349 | return ;
|
---|
350 | }
|
---|
351 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
|
---|
352 | }
|
---|
353 |
|
---|
354 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
355 | {
|
---|
356 | if (CF_Thread_Create (&ctid[iter], socket_echo_client, &sock) < 0)
|
---|
357 | {
|
---|
358 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
359 | return ;
|
---|
360 | }
|
---|
361 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
|
---|
362 | }
|
---|
363 |
|
---|
364 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
365 | {
|
---|
366 | CF_Thread_Join (&ctid[iter]);
|
---|
367 | CF_Thread_Release (&ctid[iter]);
|
---|
368 |
|
---|
369 | CF_Thread_Join (&stid[iter]);
|
---|
370 | CF_Thread_Release (&stid[iter]);
|
---|
371 |
|
---|
372 | CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
|
---|
373 | }
|
---|
374 |
|
---|
375 | CF_Socket_Close (sock);
|
---|
376 |
|
---|
377 | CF_Socket_Finalize ();
|
---|
378 |
|
---|
379 | CF_Log_Close (LOG_SOCKET);
|
---|
380 |
|
---|
381 | CF_Log_Finalize ();
|
---|
382 | /*------------------------------------------------------------*/
|
---|
383 | }
|
---|
384 |
|
---|
385 | void test_codec (void)
|
---|
386 | {
|
---|
387 | char data[] = "ONE OK ROCK - Nothing Helps";
|
---|
388 |
|
---|
389 | char encode[512] = {0x00,};
|
---|
390 | unsigned char bin[512] = {0x00,};
|
---|
391 | size_t length = 0;
|
---|
392 |
|
---|
393 | /* hex */
|
---|
394 | CF_DEBUG_PRINT (stderr, "------------------- codec/hex ----------------\n");
|
---|
395 | CF_DEBUG_PRINT (stderr, "data : %s\n", data);
|
---|
396 | CF_DEBUG_PRINT (stderr, "= Convert binary to hex =\n");
|
---|
397 | CF_Codec_Hex_Encode ((unsigned char *)data, strlen (data), encode);
|
---|
398 | CF_DEBUG_PRINT (stderr, "hex : %s\n", encode);
|
---|
399 | CF_DEBUG_PRINT_BIN (stderr, (unsigned char *) data, strlen (data), "data : %s\n", data);
|
---|
400 |
|
---|
401 | CF_DEBUG_PRINT (stderr, "= Convert hex to binary =\n");
|
---|
402 | if (CF_Codec_Hex_Decode (encode, bin, &length) < 0) {
|
---|
403 | // error
|
---|
404 | }
|
---|
405 | else
|
---|
406 | CF_DEBUG_PRINT_BIN (stderr, bin, length, "bin : %s\n", bin);
|
---|
407 |
|
---|
408 | memset (bin , 0x00, sizeof (bin));
|
---|
409 | memset (encode, 0x00, sizeof (encode));
|
---|
410 | length = 0;
|
---|
411 |
|
---|
412 | /* base64 */
|
---|
413 | CF_DEBUG_PRINT (stderr, "----------------- codec/base64 ---------------\n");
|
---|
414 | CF_DEBUG_PRINT (stderr, "data : %s\n", data);
|
---|
415 | CF_DEBUG_PRINT (stderr, "= Convert binary to base64 =\n");
|
---|
416 | CF_Codec_Base64_Encode ((unsigned char *)data, strlen (data), encode);
|
---|
417 | CF_DEBUG_PRINT (stderr, "base64 : %s\n", encode);
|
---|
418 |
|
---|
419 | CF_DEBUG_PRINT (stderr, "= Convert base64 to binary =\n");
|
---|
420 | if (CF_Codec_Base64_Decode (encode, bin, &length) < 0) {
|
---|
421 | // error
|
---|
422 | }
|
---|
423 | else
|
---|
424 | CF_DEBUG_PRINT_BIN (stderr, bin, length, "bin : %s\n", bin);
|
---|
425 | }
|
---|