testsuite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* testsuite.c
  2. *
  3. * Copyright (C) 2006-2017 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 argc0[32];
  107. char argc1[32];
  108. char argc2[32];
  109. myArgv[0] = argc0;
  110. myArgv[1] = argc1;
  111. myArgv[2] = argc2;
  112. echo_args.argc = 3;
  113. echo_args.argv = myArgv;
  114. /* Create unique file name */
  115. outputName = mymktemp(tempName, len, num);
  116. if (outputName == NULL) {
  117. printf("Could not create unique file name");
  118. return EXIT_FAILURE;
  119. }
  120. strcpy(echo_args.argv[0], "echoclient");
  121. strcpy(echo_args.argv[1], "input");
  122. strcpy(echo_args.argv[2], outputName);
  123. /* Share the signal, it has the new port number in it. */
  124. echo_args.signal = server_args.signal;
  125. /* make sure OK */
  126. echoclient_test(&echo_args);
  127. if (echo_args.return_code != 0) return echo_args.return_code;
  128. #ifdef WOLFSSL_DTLS
  129. wait_tcp_ready(&server_args);
  130. #endif
  131. /* send quit to echoserver */
  132. echo_args.argc = 2;
  133. strcpy(echo_args.argv[1], "quit");
  134. echoclient_test(&echo_args);
  135. if (echo_args.return_code != 0) return echo_args.return_code;
  136. join_thread(serverThread);
  137. if (server_args.return_code != 0) return server_args.return_code;
  138. }
  139. /* show ciphers */
  140. {
  141. char ciphers[1024];
  142. XMEMSET(ciphers, 0, sizeof(ciphers));
  143. wolfSSL_get_ciphers(ciphers, sizeof(ciphers)-1);
  144. printf("ciphers = %s\n", ciphers);
  145. }
  146. /* validate output equals input */
  147. {
  148. #ifndef NO_SHA256
  149. byte input[WC_SHA256_DIGEST_SIZE];
  150. byte output[WC_SHA256_DIGEST_SIZE];
  151. file_test("input", input);
  152. file_test(outputName, output);
  153. #endif
  154. remove(outputName);
  155. #ifndef NO_SHA256
  156. if (memcmp(input, output, sizeof(input)) != 0)
  157. return EXIT_FAILURE;
  158. #endif
  159. }
  160. wolfSSL_Cleanup();
  161. FreeTcpReady(&ready);
  162. #ifdef WOLFSSL_TIRTOS
  163. fdCloseSession(Task_self());
  164. #endif
  165. #ifdef HAVE_WNR
  166. if (wc_FreeNetRandom() < 0)
  167. err_sys("Failed to free netRandom context");
  168. #endif /* HAVE_WNR */
  169. printf("\nAll tests passed!\n");
  170. #else
  171. (void)argc;
  172. (void)argv;
  173. #endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
  174. return EXIT_SUCCESS;
  175. }
  176. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  177. void simple_test(func_args* args)
  178. {
  179. THREAD_TYPE serverThread;
  180. func_args svrArgs;
  181. char *svrArgv[9];
  182. char argc0s[32];
  183. char argc1s[32];
  184. char argc2s[32];
  185. char argc3s[32];
  186. char argc4s[32];
  187. char argc5s[32];
  188. char argc6s[32];
  189. char argc7s[32];
  190. char argc8s[32];
  191. func_args cliArgs;
  192. char *cliArgv[NUMARGS];
  193. char argc0c[32];
  194. char argc1c[32];
  195. char argc2c[32];
  196. svrArgv[0] = argc0s;
  197. svrArgv[1] = argc1s;
  198. svrArgv[2] = argc2s;
  199. svrArgv[3] = argc3s;
  200. svrArgv[4] = argc4s;
  201. svrArgv[5] = argc5s;
  202. svrArgv[6] = argc6s;
  203. svrArgv[7] = argc7s;
  204. svrArgv[8] = argc8s;
  205. cliArgv[0] = argc0c;
  206. cliArgv[1] = argc1c;
  207. cliArgv[2] = argc2c;
  208. svrArgs.argc = 1;
  209. svrArgs.argv = svrArgv;
  210. svrArgs.return_code = 0;
  211. cliArgs.argc = 1;
  212. cliArgs.argv = cliArgv;
  213. cliArgs.return_code = 0;
  214. strcpy(svrArgs.argv[0], "SimpleServer");
  215. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_SNIFFER) && \
  216. !defined(WOLFSSL_TIRTOS)
  217. strcpy(svrArgs.argv[svrArgs.argc++], "-p");
  218. strcpy(svrArgs.argv[svrArgs.argc++], "0");
  219. #endif
  220. /* Set the last arg later, when it is known. */
  221. args->return_code = 0;
  222. svrArgs.signal = args->signal;
  223. start_thread(server_test, &svrArgs, &serverThread);
  224. wait_tcp_ready(&svrArgs);
  225. /* Setting the actual port number. */
  226. strcpy(cliArgs.argv[0], "SimpleClient");
  227. #ifndef USE_WINDOWS_API
  228. cliArgs.argc = NUMARGS;
  229. strcpy(cliArgs.argv[1], "-p");
  230. snprintf(cliArgs.argv[2], sizeof(argc2c), "%d", svrArgs.signal->port);
  231. #endif
  232. client_test(&cliArgs);
  233. if (cliArgs.return_code != 0) {
  234. args->return_code = cliArgs.return_code;
  235. return;
  236. }
  237. join_thread(serverThread);
  238. if (svrArgs.return_code != 0) args->return_code = svrArgs.return_code;
  239. }
  240. #endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
  241. void wait_tcp_ready(func_args* args)
  242. {
  243. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  244. pthread_mutex_lock(&args->signal->mutex);
  245. if (!args->signal->ready)
  246. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  247. args->signal->ready = 0; /* reset */
  248. pthread_mutex_unlock(&args->signal->mutex);
  249. #else
  250. (void)args;
  251. #endif
  252. }
  253. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  254. {
  255. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  256. pthread_create(thread, 0, fun, args);
  257. return;
  258. #elif defined(WOLFSSL_TIRTOS)
  259. /* Initialize the defaults and set the parameters. */
  260. Task_Params taskParams;
  261. Task_Params_init(&taskParams);
  262. taskParams.arg0 = (UArg)args;
  263. taskParams.stackSize = 65535;
  264. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  265. if (*thread == NULL) {
  266. printf("Failed to create new Task\n");
  267. }
  268. Task_yield();
  269. #else
  270. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  271. #endif
  272. }
  273. void join_thread(THREAD_TYPE thread)
  274. {
  275. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  276. pthread_join(thread, 0);
  277. #elif defined(WOLFSSL_TIRTOS)
  278. while(1) {
  279. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  280. Task_sleep(5);
  281. break;
  282. }
  283. Task_yield();
  284. }
  285. #else
  286. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  287. assert(res == WAIT_OBJECT_0);
  288. res = CloseHandle((HANDLE)thread);
  289. assert(res);
  290. (void)res; /* Suppress un-used variable warning */
  291. #endif
  292. }
  293. #ifndef NO_SHA256
  294. void file_test(const char* file, byte* check)
  295. {
  296. FILE* f;
  297. int i = 0, j, ret;
  298. wc_Sha256 sha256;
  299. byte buf[1024];
  300. byte shasum[WC_SHA256_DIGEST_SIZE];
  301. ret = wc_InitSha256(&sha256);
  302. if (ret != 0) {
  303. printf("Can't wc_InitSha256 %d\n", ret);
  304. return;
  305. }
  306. if( !( f = fopen( file, "rb" ) )) {
  307. printf("Can't open %s\n", file);
  308. return;
  309. }
  310. while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 ) {
  311. ret = wc_Sha256Update(&sha256, buf, i);
  312. if (ret != 0) {
  313. printf("Can't wc_Sha256Update %d\n", ret);
  314. fclose(f);
  315. return;
  316. }
  317. }
  318. ret = wc_Sha256Final(&sha256, shasum);
  319. if (ret != 0) {
  320. printf("Can't wc_Sha256Final %d\n", ret);
  321. fclose(f);
  322. return;
  323. }
  324. XMEMCPY(check, shasum, sizeof(shasum));
  325. for(j = 0; j < WC_SHA256_DIGEST_SIZE; ++j )
  326. printf( "%02x", shasum[j] );
  327. printf(" %s\n", file);
  328. fclose(f);
  329. }
  330. #endif
  331. #else /* SINGLE_THREADED */
  332. int myoptind = 0;
  333. char* myoptarg = NULL;
  334. int main(int argc, char** argv)
  335. {
  336. func_args server_args;
  337. server_args.argc = argc;
  338. server_args.argv = argv;
  339. wolfSSL_Init();
  340. ChangeToWolfRoot();
  341. wolfcrypt_test(&server_args);
  342. if (server_args.return_code != 0) return server_args.return_code;
  343. wolfSSL_Cleanup();
  344. printf("\nAll tests passed!\n");
  345. EXIT_TEST(EXIT_SUCCESS);
  346. }
  347. #endif /* SINGLE_THREADED */