testsuite.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* testsuite.c
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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-1301, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/ctaocrypt/settings.h>
  25. #include <cyassl/test.h>
  26. #include "ctaocrypt/test/test.h"
  27. #ifndef SINGLE_THREADED
  28. #include <cyassl/openssl/ssl.h>
  29. #include <cyassl/ctaocrypt/sha256.h>
  30. #include "examples/echoclient/echoclient.h"
  31. #include "examples/echoserver/echoserver.h"
  32. #include "examples/server/server.h"
  33. #include "examples/client/client.h"
  34. void file_test(const char* file, byte* hash);
  35. void simple_test(func_args*);
  36. enum {
  37. NUMARGS = 3
  38. };
  39. #ifndef USE_WINDOWS_API
  40. static const char outputName[] = "/tmp/output";
  41. #else
  42. static const char outputName[] = "output";
  43. #endif
  44. int myoptind = 0;
  45. char* myoptarg = NULL;
  46. #ifndef NO_TESTSUITE_MAIN_DRIVER
  47. static int testsuite_test(int argc, char** argv);
  48. int main(int argc, char** argv)
  49. {
  50. return testsuite_test(argc, argv);
  51. }
  52. #endif /* NO_TESTSUITE_MAIN_DRIVER */
  53. int testsuite_test(int argc, char** argv)
  54. {
  55. func_args server_args;
  56. tcp_ready ready;
  57. THREAD_TYPE serverThread;
  58. #ifdef HAVE_CAVIUM
  59. int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
  60. if (ret != 0)
  61. err_sys("Cavium OpenNitroxDevice failed");
  62. #endif /* HAVE_CAVIUM */
  63. StartTCP();
  64. server_args.argc = argc;
  65. server_args.argv = argv;
  66. CyaSSL_Init();
  67. #if defined(DEBUG_CYASSL) && !defined(HAVE_VALGRIND)
  68. CyaSSL_Debugging_ON();
  69. #endif
  70. #if !defined(CYASSL_TIRTOS)
  71. if (CurrentDir("testsuite") || CurrentDir("_build"))
  72. ChangeDirBack(1);
  73. else if (CurrentDir("Debug") || CurrentDir("Release"))
  74. ChangeDirBack(3); /* Xcode->Preferences->Locations->Locations*/
  75. /* Derived Data Advanced -> Custom */
  76. /* Relative to Workspace, Build/Products */
  77. /* Debug or Release */
  78. #endif
  79. #ifdef CYASSL_TIRTOS
  80. fdOpenSession(Task_self());
  81. #endif
  82. server_args.signal = &ready;
  83. InitTcpReady(&ready);
  84. /* CTaoCrypt test */
  85. ctaocrypt_test(&server_args);
  86. if (server_args.return_code != 0) return server_args.return_code;
  87. /* Simple CyaSSL client server test */
  88. simple_test(&server_args);
  89. if (server_args.return_code != 0) return server_args.return_code;
  90. /* Echo input yaSSL client server test */
  91. start_thread(echoserver_test, &server_args, &serverThread);
  92. wait_tcp_ready(&server_args);
  93. {
  94. func_args echo_args;
  95. char* myArgv[NUMARGS];
  96. char argc0[32];
  97. char argc1[32];
  98. char argc2[32];
  99. myArgv[0] = argc0;
  100. myArgv[1] = argc1;
  101. myArgv[2] = argc2;
  102. echo_args.argc = 3;
  103. echo_args.argv = myArgv;
  104. strcpy(echo_args.argv[0], "echoclient");
  105. strcpy(echo_args.argv[1], "input");
  106. strcpy(echo_args.argv[2], outputName);
  107. remove(outputName);
  108. /* Share the signal, it has the new port number in it. */
  109. echo_args.signal = server_args.signal;
  110. /* make sure OK */
  111. echoclient_test(&echo_args);
  112. if (echo_args.return_code != 0) return echo_args.return_code;
  113. #ifdef CYASSL_DTLS
  114. wait_tcp_ready(&server_args);
  115. #endif
  116. /* send quit to echoserver */
  117. echo_args.argc = 2;
  118. strcpy(echo_args.argv[1], "quit");
  119. echoclient_test(&echo_args);
  120. if (echo_args.return_code != 0) return echo_args.return_code;
  121. join_thread(serverThread);
  122. if (server_args.return_code != 0) return server_args.return_code;
  123. }
  124. /* show ciphers */
  125. {
  126. char ciphers[1024];
  127. XMEMSET(ciphers, 0, sizeof(ciphers));
  128. CyaSSL_get_ciphers(ciphers, sizeof(ciphers)-1);
  129. printf("ciphers = %s\n", ciphers);
  130. }
  131. /* validate output equals input */
  132. {
  133. byte input[SHA256_DIGEST_SIZE];
  134. byte output[SHA256_DIGEST_SIZE];
  135. file_test("input", input);
  136. file_test(outputName, output);
  137. if (memcmp(input, output, sizeof(input)) != 0)
  138. return EXIT_FAILURE;
  139. }
  140. CyaSSL_Cleanup();
  141. FreeTcpReady(&ready);
  142. #ifdef CYASSL_TIRTOS
  143. fdCloseSession(Task_self());
  144. #endif
  145. #ifdef HAVE_CAVIUM
  146. CspShutdown(CAVIUM_DEV_ID);
  147. #endif
  148. printf("\nAll tests passed!\n");
  149. return EXIT_SUCCESS;
  150. }
  151. void simple_test(func_args* args)
  152. {
  153. THREAD_TYPE serverThread;
  154. func_args svrArgs;
  155. char *svrArgv[9];
  156. char argc0s[32];
  157. char argc1s[32];
  158. char argc2s[32];
  159. char argc3s[32];
  160. char argc4s[32];
  161. char argc5s[32];
  162. char argc6s[32];
  163. char argc7s[32];
  164. char argc8s[32];
  165. func_args cliArgs;
  166. char *cliArgv[NUMARGS];
  167. char argc0c[32];
  168. char argc1c[32];
  169. char argc2c[32];
  170. svrArgv[0] = argc0s;
  171. svrArgv[1] = argc1s;
  172. svrArgv[2] = argc2s;
  173. svrArgv[3] = argc3s;
  174. svrArgv[4] = argc4s;
  175. svrArgv[5] = argc5s;
  176. svrArgv[6] = argc6s;
  177. svrArgv[7] = argc7s;
  178. svrArgv[8] = argc8s;
  179. cliArgv[0] = argc0c;
  180. cliArgv[1] = argc1c;
  181. cliArgv[2] = argc2c;
  182. svrArgs.argc = 1;
  183. svrArgs.argv = svrArgv;
  184. svrArgs.return_code = 0;
  185. cliArgs.argc = 1;
  186. cliArgs.argv = cliArgv;
  187. cliArgs.return_code = 0;
  188. strcpy(svrArgs.argv[0], "SimpleServer");
  189. #if !defined(USE_WINDOWS_API) && !defined(CYASSL_SNIFFER) && \
  190. !defined(CYASSL_TIRTOS)
  191. strcpy(svrArgs.argv[svrArgs.argc++], "-p");
  192. strcpy(svrArgs.argv[svrArgs.argc++], "0");
  193. #endif
  194. #ifdef HAVE_NTRU
  195. strcpy(svrArgs.argv[svrArgs.argc++], "-d");
  196. strcpy(svrArgs.argv[svrArgs.argc++], "-n");
  197. strcpy(svrArgs.argv[svrArgs.argc++], "-c");
  198. strcpy(svrArgs.argv[svrArgs.argc++], "./certs/ntru-cert.pem");
  199. strcpy(svrArgs.argv[svrArgs.argc++], "-k");
  200. strcpy(svrArgs.argv[svrArgs.argc++], "./certs/ntru-key.raw");
  201. #endif
  202. /* Set the last arg later, when it is known. */
  203. args->return_code = 0;
  204. svrArgs.signal = args->signal;
  205. start_thread(server_test, &svrArgs, &serverThread);
  206. wait_tcp_ready(&svrArgs);
  207. /* Setting the actual port number. */
  208. strcpy(cliArgs.argv[0], "SimpleClient");
  209. #ifndef USE_WINDOWS_API
  210. cliArgs.argc = NUMARGS;
  211. strcpy(cliArgs.argv[1], "-p");
  212. snprintf(cliArgs.argv[2], sizeof(argc2c), "%d", svrArgs.signal->port);
  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. void wait_tcp_ready(func_args* args)
  223. {
  224. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  225. pthread_mutex_lock(&args->signal->mutex);
  226. if (!args->signal->ready)
  227. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  228. args->signal->ready = 0; /* reset */
  229. pthread_mutex_unlock(&args->signal->mutex);
  230. #else
  231. (void)args;
  232. #endif
  233. }
  234. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  235. {
  236. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  237. pthread_create(thread, 0, fun, args);
  238. return;
  239. #elif defined(CYASSL_TIRTOS)
  240. /* Initialize the defaults and set the parameters. */
  241. Task_Params taskParams;
  242. Task_Params_init(&taskParams);
  243. taskParams.arg0 = (UArg)args;
  244. taskParams.stackSize = 65535;
  245. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  246. if (*thread == NULL) {
  247. printf("Failed to create new Task\n");
  248. }
  249. Task_yield();
  250. #else
  251. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  252. #endif
  253. }
  254. void join_thread(THREAD_TYPE thread)
  255. {
  256. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  257. pthread_join(thread, 0);
  258. #elif defined(CYASSL_TIRTOS)
  259. while(1) {
  260. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  261. Task_sleep(5);
  262. break;
  263. }
  264. Task_yield();
  265. }
  266. #else
  267. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  268. assert(res == WAIT_OBJECT_0);
  269. res = CloseHandle((HANDLE)thread);
  270. assert(res);
  271. #endif
  272. }
  273. void InitTcpReady(tcp_ready* ready)
  274. {
  275. ready->ready = 0;
  276. ready->port = 0;
  277. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  278. pthread_mutex_init(&ready->mutex, 0);
  279. pthread_cond_init(&ready->cond, 0);
  280. #endif
  281. }
  282. void FreeTcpReady(tcp_ready* ready)
  283. {
  284. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  285. pthread_mutex_destroy(&ready->mutex);
  286. pthread_cond_destroy(&ready->cond);
  287. #else
  288. (void)ready;
  289. #endif
  290. }
  291. void file_test(const char* file, byte* check)
  292. {
  293. FILE* f;
  294. int i = 0, j, ret;
  295. Sha256 sha256;
  296. byte buf[1024];
  297. byte shasum[SHA256_DIGEST_SIZE];
  298. ret = InitSha256(&sha256);
  299. if (ret != 0) {
  300. printf("Can't InitSha256 %d\n", ret);
  301. return;
  302. }
  303. if( !( f = fopen( file, "rb" ) )) {
  304. printf("Can't open %s\n", file);
  305. return;
  306. }
  307. while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 ) {
  308. ret = Sha256Update(&sha256, buf, i);
  309. if (ret != 0) {
  310. printf("Can't Sha256Update %d\n", ret);
  311. return;
  312. }
  313. }
  314. ret = Sha256Final(&sha256, shasum);
  315. if (ret != 0) {
  316. printf("Can't Sha256Final %d\n", ret);
  317. return;
  318. }
  319. memcpy(check, shasum, sizeof(shasum));
  320. for(j = 0; j < SHA256_DIGEST_SIZE; ++j )
  321. printf( "%02x", shasum[j] );
  322. printf(" %s\n", file);
  323. fclose(f);
  324. }
  325. #else /* SINGLE_THREADED */
  326. int myoptind = 0;
  327. char* myoptarg = NULL;
  328. int main(int argc, char** argv)
  329. {
  330. func_args server_args;
  331. server_args.argc = argc;
  332. server_args.argv = argv;
  333. if (CurrentDir("testsuite") || CurrentDir("_build"))
  334. ChangeDirBack(1);
  335. else if (CurrentDir("Debug") || CurrentDir("Release"))
  336. ChangeDirBack(3); /* Xcode->Preferences->Locations->Locations*/
  337. /* Derived Data Advanced -> Custom */
  338. /* Relative to Workspace, Build/Products */
  339. /* Debug or Release */
  340. ctaocrypt_test(&server_args);
  341. if (server_args.return_code != 0) return server_args.return_code;
  342. printf("\nAll tests passed!\n");
  343. return EXIT_SUCCESS;
  344. }
  345. #endif /* SINGLE_THREADED */