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