testsuite.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* testsuite.c
  2. *
  3. * Copyright (C) 2006-2020 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/ssl.h>
  26. #include <wolfssl/test.h>
  27. #include <wolfcrypt/test/test.h>
  28. #ifndef SINGLE_THREADED
  29. #ifdef OPENSSL_EXTRA
  30. #include <wolfssl/openssl/ssl.h>
  31. #endif
  32. #include <wolfssl/wolfcrypt/sha256.h>
  33. #include <examples/echoclient/echoclient.h>
  34. #include <examples/echoserver/echoserver.h>
  35. #include <examples/server/server.h>
  36. #include <examples/client/client.h>
  37. #ifndef NO_SHA256
  38. void file_test(const char* file, byte* hash);
  39. #endif
  40. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  41. void simple_test(func_args*);
  42. enum {
  43. NUMARGS = 3
  44. };
  45. static const char *outputName;
  46. #endif
  47. int myoptind = 0;
  48. char* myoptarg = NULL;
  49. #ifndef NO_TESTSUITE_MAIN_DRIVER
  50. static int testsuite_test(int argc, char** argv);
  51. int main(int argc, char** argv)
  52. {
  53. return testsuite_test(argc, argv);
  54. }
  55. #endif /* NO_TESTSUITE_MAIN_DRIVER */
  56. int testsuite_test(int argc, char** argv)
  57. {
  58. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  59. func_args server_args;
  60. tcp_ready ready;
  61. THREAD_TYPE serverThread;
  62. #ifndef USE_WINDOWS_API
  63. char tempName[] = "/tmp/output-XXXXXX";
  64. int len = 18;
  65. int num = 6;
  66. #else
  67. char tempName[] = "fnXXXXXX";
  68. int len = 8;
  69. int num = 6;
  70. #endif
  71. #ifdef HAVE_WNR
  72. if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0) {
  73. err_sys("Whitewood netRandom global config failed");
  74. return -1237;
  75. }
  76. #endif /* HAVE_WNR */
  77. StartTCP();
  78. server_args.argc = argc;
  79. server_args.argv = argv;
  80. wolfSSL_Init();
  81. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  82. wolfSSL_Debugging_ON();
  83. #endif
  84. #if !defined(WOLFSSL_TIRTOS)
  85. ChangeToWolfRoot();
  86. #endif
  87. #ifdef WOLFSSL_TIRTOS
  88. fdOpenSession(Task_self());
  89. #endif
  90. server_args.signal = &ready;
  91. InitTcpReady(&ready);
  92. #ifndef NO_CRYPT_TEST
  93. /* wc_ test */
  94. wolfcrypt_test(&server_args);
  95. if (server_args.return_code != 0) return server_args.return_code;
  96. #endif
  97. /* Simple wolfSSL client server test */
  98. simple_test(&server_args);
  99. if (server_args.return_code != 0) return server_args.return_code;
  100. /* Echo input wolfSSL client server test */
  101. start_thread(echoserver_test, &server_args, &serverThread);
  102. wait_tcp_ready(&server_args);
  103. {
  104. func_args echo_args;
  105. char* myArgv[NUMARGS];
  106. char arg[3][32];
  107. myArgv[0] = arg[0];
  108. myArgv[1] = arg[1];
  109. myArgv[2] = arg[2];
  110. echo_args.argc = 3;
  111. echo_args.argv = myArgv;
  112. /* Create unique file name */
  113. outputName = mymktemp(tempName, len, num);
  114. if (outputName == NULL) {
  115. printf("Could not create unique file name");
  116. return EXIT_FAILURE;
  117. }
  118. strcpy(arg[0], "echoclient");
  119. strcpy(arg[1], "input");
  120. strcpy(arg[2], outputName);
  121. /* Share the signal, it has the new port number in it. */
  122. echo_args.signal = server_args.signal;
  123. /* make sure OK */
  124. echoclient_test(&echo_args);
  125. if (echo_args.return_code != 0) return echo_args.return_code;
  126. #ifdef WOLFSSL_DTLS
  127. wait_tcp_ready(&server_args);
  128. #endif
  129. /* send quit to echoserver */
  130. echo_args.argc = 2;
  131. strcpy(echo_args.argv[1], "quit");
  132. echoclient_test(&echo_args);
  133. if (echo_args.return_code != 0) return echo_args.return_code;
  134. join_thread(serverThread);
  135. if (server_args.return_code != 0) return server_args.return_code;
  136. }
  137. /* show ciphers */
  138. {
  139. char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
  140. XMEMSET(ciphers, 0, sizeof(ciphers));
  141. wolfSSL_get_ciphers(ciphers, sizeof(ciphers)-1);
  142. printf("ciphers = %s\n", ciphers);
  143. }
  144. /* validate output equals input */
  145. {
  146. #ifndef NO_SHA256
  147. byte input[WC_SHA256_DIGEST_SIZE];
  148. byte output[WC_SHA256_DIGEST_SIZE];
  149. file_test("input", input);
  150. file_test(outputName, output);
  151. #endif
  152. remove(outputName);
  153. #ifndef NO_SHA256
  154. if (memcmp(input, output, sizeof(input)) != 0)
  155. return EXIT_FAILURE;
  156. #endif
  157. }
  158. wolfSSL_Cleanup();
  159. FreeTcpReady(&ready);
  160. #ifdef WOLFSSL_TIRTOS
  161. fdCloseSession(Task_self());
  162. #endif
  163. #ifdef HAVE_WNR
  164. if (wc_FreeNetRandom() < 0)
  165. err_sys("Failed to free netRandom context");
  166. #endif /* HAVE_WNR */
  167. printf("\nAll tests passed!\n");
  168. #else
  169. (void)argc;
  170. (void)argv;
  171. #endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
  172. return EXIT_SUCCESS;
  173. }
  174. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  175. void simple_test(func_args* args)
  176. {
  177. THREAD_TYPE serverThread;
  178. int i;
  179. func_args svrArgs;
  180. char *svrArgv[9];
  181. char argvs[9][32];
  182. func_args cliArgs;
  183. char *cliArgv[NUMARGS];
  184. char argvc[3][32];
  185. for (i = 0; i < 9; i++)
  186. svrArgv[i] = argvs[i];
  187. for (i = 0; i < 3; i++)
  188. cliArgv[i] = argvc[i];
  189. strcpy(argvs[0], "SimpleServer");
  190. svrArgs.argc = 1;
  191. svrArgs.argv = svrArgv;
  192. svrArgs.return_code = 0;
  193. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_SNIFFER) && \
  194. !defined(WOLFSSL_TIRTOS)
  195. strcpy(argvs[svrArgs.argc++], "-p");
  196. strcpy(argvs[svrArgs.argc++], "0");
  197. #endif
  198. /* Set the last arg later, when it is known. */
  199. args->return_code = 0;
  200. svrArgs.signal = args->signal;
  201. start_thread(server_test, &svrArgs, &serverThread);
  202. wait_tcp_ready(&svrArgs);
  203. /* Setting the actual port number. */
  204. strcpy(argvc[0], "SimpleClient");
  205. cliArgs.argv = cliArgv;
  206. cliArgs.return_code = 0;
  207. #ifndef USE_WINDOWS_API
  208. cliArgs.argc = NUMARGS;
  209. strcpy(argvc[1], "-p");
  210. snprintf(argvc[2], sizeof(argvc[2]), "%d", svrArgs.signal->port);
  211. #else
  212. cliArgs.argc = 1;
  213. #endif
  214. client_test(&cliArgs);
  215. if (cliArgs.return_code != 0) {
  216. args->return_code = cliArgs.return_code;
  217. return;
  218. }
  219. join_thread(serverThread);
  220. if (svrArgs.return_code != 0) args->return_code = svrArgs.return_code;
  221. }
  222. #endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
  223. void wait_tcp_ready(func_args* args)
  224. {
  225. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  226. pthread_mutex_lock(&args->signal->mutex);
  227. if (!args->signal->ready)
  228. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  229. args->signal->ready = 0; /* reset */
  230. pthread_mutex_unlock(&args->signal->mutex);
  231. #else
  232. (void)args;
  233. #endif
  234. }
  235. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  236. {
  237. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  238. pthread_create(thread, 0, fun, args);
  239. return;
  240. #elif defined(WOLFSSL_TIRTOS)
  241. /* Initialize the defaults and set the parameters. */
  242. Task_Params taskParams;
  243. Task_Params_init(&taskParams);
  244. taskParams.arg0 = (UArg)args;
  245. taskParams.stackSize = 65535;
  246. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  247. if (*thread == NULL) {
  248. printf("Failed to create new Task\n");
  249. }
  250. Task_yield();
  251. #else
  252. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  253. #endif
  254. }
  255. void join_thread(THREAD_TYPE thread)
  256. {
  257. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  258. pthread_join(thread, 0);
  259. #elif defined(WOLFSSL_TIRTOS)
  260. while(1) {
  261. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  262. Task_sleep(5);
  263. break;
  264. }
  265. Task_yield();
  266. }
  267. #else
  268. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  269. assert(res == WAIT_OBJECT_0);
  270. res = CloseHandle((HANDLE)thread);
  271. assert(res);
  272. (void)res; /* Suppress un-used variable warning */
  273. #endif
  274. }
  275. #ifndef NO_SHA256
  276. void file_test(const char* file, byte* check)
  277. {
  278. FILE* f;
  279. int i = 0, j, ret;
  280. wc_Sha256 sha256;
  281. byte buf[1024];
  282. byte shasum[WC_SHA256_DIGEST_SIZE];
  283. ret = wc_InitSha256(&sha256);
  284. if (ret != 0) {
  285. printf("Can't wc_InitSha256 %d\n", ret);
  286. return;
  287. }
  288. if( !( f = fopen( file, "rb" ) )) {
  289. printf("Can't open %s\n", file);
  290. return;
  291. }
  292. while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 ) {
  293. ret = wc_Sha256Update(&sha256, buf, i);
  294. if (ret != 0) {
  295. printf("Can't wc_Sha256Update %d\n", ret);
  296. fclose(f);
  297. return;
  298. }
  299. }
  300. ret = wc_Sha256Final(&sha256, shasum);
  301. if (ret != 0) {
  302. printf("Can't wc_Sha256Final %d\n", ret);
  303. fclose(f);
  304. return;
  305. }
  306. XMEMCPY(check, shasum, sizeof(shasum));
  307. for(j = 0; j < WC_SHA256_DIGEST_SIZE; ++j )
  308. printf( "%02x", shasum[j] );
  309. printf(" %s\n", file);
  310. fclose(f);
  311. }
  312. #endif
  313. #else /* SINGLE_THREADED */
  314. int myoptind = 0;
  315. char* myoptarg = NULL;
  316. int main(int argc, char** argv)
  317. {
  318. func_args server_args;
  319. server_args.argc = argc;
  320. server_args.argv = argv;
  321. wolfSSL_Init();
  322. ChangeToWolfRoot();
  323. wolfcrypt_test(&server_args);
  324. if (server_args.return_code != 0) return server_args.return_code;
  325. wolfSSL_Cleanup();
  326. printf("\nAll tests passed!\n");
  327. EXIT_TEST(EXIT_SUCCESS);
  328. }
  329. #endif /* SINGLE_THREADED */