testsuite.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* testsuite.c
  2. *
  3. * Copyright (C) 2006-2013 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/ctaocrypt/settings.h>
  25. #include <cyassl/openssl/ssl.h>
  26. #include <cyassl/test.h>
  27. #include <cyassl/ctaocrypt/sha256.h>
  28. #include "ctaocrypt/test/test.h"
  29. #ifdef SINGLE_THREADED
  30. #error testsuite needs threads to run, please run ctaocrypt/test, \
  31. and the examples/ individually
  32. #endif
  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. #include "ctaocrypt/test/test.h"
  38. void file_test(const char* file, byte* hash);
  39. void simple_test(func_args*);
  40. enum {
  41. NUMARGS = 3
  42. };
  43. int myoptind = 0;
  44. char* myoptarg = NULL;
  45. int main(int argc, char** argv)
  46. {
  47. func_args server_args;
  48. tcp_ready ready;
  49. THREAD_TYPE serverThread;
  50. #ifdef HAVE_CAVIUM
  51. int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
  52. if (ret != 0)
  53. err_sys("Cavium OpenNitroxDevice failed");
  54. #endif /* HAVE_CAVIUM */
  55. StartTCP();
  56. server_args.argc = argc;
  57. server_args.argv = argv;
  58. CyaSSL_Init();
  59. #if defined(DEBUG_CYASSL) && !defined(HAVE_VALGRIND)
  60. CyaSSL_Debugging_ON();
  61. #endif
  62. if (CurrentDir("testsuite"))
  63. ChangeDirBack(1);
  64. else if (CurrentDir("build")) /* Xcode->Preferences->Locations->Build */
  65. ChangeDirBack(2); /* Location "Place build product in locations
  66. specified by targets", uses build/Debug */
  67. server_args.signal = &ready;
  68. InitTcpReady(&ready);
  69. /* CTaoCrypt test */
  70. ctaocrypt_test(&server_args);
  71. if (server_args.return_code != 0) return server_args.return_code;
  72. /* Simple CyaSSL client server test */
  73. simple_test(&server_args);
  74. if (server_args.return_code != 0) return server_args.return_code;
  75. /* Echo input yaSSL client server test */
  76. start_thread(echoserver_test, &server_args, &serverThread);
  77. wait_tcp_ready(&server_args);
  78. {
  79. func_args echo_args;
  80. char* myArgv[NUMARGS];
  81. char argc0[32];
  82. char argc1[32];
  83. char argc2[32];
  84. myArgv[0] = argc0;
  85. myArgv[1] = argc1;
  86. myArgv[2] = argc2;
  87. echo_args.argc = NUMARGS;
  88. echo_args.argv = myArgv;
  89. strcpy(echo_args.argv[0], "echoclient");
  90. strcpy(echo_args.argv[1], "input");
  91. strcpy(echo_args.argv[2], "output");
  92. remove("output");
  93. /* Share the signal, it has the new port number in it. */
  94. echo_args.signal = server_args.signal;
  95. /* make sure OK */
  96. echoclient_test(&echo_args);
  97. if (echo_args.return_code != 0) return echo_args.return_code;
  98. #ifdef CYASSL_DTLS
  99. wait_tcp_ready(&server_args);
  100. #endif
  101. /* send quit to echoserver */
  102. echo_args.argc = 2;
  103. strcpy(echo_args.argv[1], "quit");
  104. echoclient_test(&echo_args);
  105. if (echo_args.return_code != 0) return echo_args.return_code;
  106. join_thread(serverThread);
  107. if (server_args.return_code != 0) return server_args.return_code;
  108. }
  109. /* validate output equals input */
  110. {
  111. byte input[SHA256_DIGEST_SIZE];
  112. byte output[SHA256_DIGEST_SIZE];
  113. file_test("input", input);
  114. file_test("output", output);
  115. if (memcmp(input, output, sizeof(input)) != 0)
  116. return EXIT_FAILURE;
  117. }
  118. CyaSSL_Cleanup();
  119. FreeTcpReady(&ready);
  120. #ifdef HAVE_CAVIUM
  121. CspShutdown(CAVIUM_DEV_ID);
  122. #endif
  123. printf("\nAll tests passed!\n");
  124. return EXIT_SUCCESS;
  125. }
  126. void simple_test(func_args* args)
  127. {
  128. THREAD_TYPE serverThread;
  129. func_args svrArgs;
  130. char *svrArgv[NUMARGS];
  131. char argc0s[32];
  132. char argc1s[32];
  133. char argc2s[32];
  134. func_args cliArgs;
  135. char *cliArgv[NUMARGS];
  136. char argc0c[32];
  137. char argc1c[32];
  138. char argc2c[32];
  139. svrArgv[0] = argc0s;
  140. svrArgv[1] = argc1s;
  141. svrArgv[2] = argc2s;
  142. cliArgv[0] = argc0c;
  143. cliArgv[1] = argc1c;
  144. cliArgv[2] = argc2c;
  145. svrArgs.argc = 1;
  146. svrArgs.argv = svrArgv;
  147. svrArgs.return_code = 0;
  148. cliArgs.argc = 1;
  149. cliArgs.argv = cliArgv;
  150. cliArgs.return_code = 0;
  151. strcpy(svrArgs.argv[0], "SimpleServer");
  152. #if !defined(USE_WINDOWS_API) && !defined(CYASSL_SNIFFER)
  153. svrArgs.argc = NUMARGS;
  154. strcpy(svrArgs.argv[1], "-p");
  155. strcpy(svrArgs.argv[2], "0");
  156. #endif
  157. /* Set the last arg later, when it is known. */
  158. args->return_code = 0;
  159. svrArgs.signal = args->signal;
  160. start_thread(server_test, &svrArgs, &serverThread);
  161. wait_tcp_ready(&svrArgs);
  162. /* Setting the actual port number. */
  163. strcpy(cliArgs.argv[0], "SimpleClient");
  164. #ifndef USE_WINDOWS_API
  165. cliArgs.argc = NUMARGS;
  166. strcpy(cliArgs.argv[1], "-p");
  167. snprintf(cliArgs.argv[2], sizeof(argc2c), "%d", svrArgs.signal->port);
  168. #endif
  169. client_test(&cliArgs);
  170. if (cliArgs.return_code != 0) {
  171. args->return_code = cliArgs.return_code;
  172. return;
  173. }
  174. join_thread(serverThread);
  175. if (svrArgs.return_code != 0) args->return_code = svrArgs.return_code;
  176. }
  177. void wait_tcp_ready(func_args* args)
  178. {
  179. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  180. pthread_mutex_lock(&args->signal->mutex);
  181. if (!args->signal->ready)
  182. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  183. args->signal->ready = 0; /* reset */
  184. pthread_mutex_unlock(&args->signal->mutex);
  185. #else
  186. (void)args;
  187. #endif
  188. }
  189. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  190. {
  191. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  192. pthread_create(thread, 0, fun, args);
  193. return;
  194. #else
  195. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  196. #endif
  197. }
  198. void join_thread(THREAD_TYPE thread)
  199. {
  200. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  201. pthread_join(thread, 0);
  202. #else
  203. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  204. assert(res == WAIT_OBJECT_0);
  205. res = CloseHandle((HANDLE)thread);
  206. assert(res);
  207. #endif
  208. }
  209. void InitTcpReady(tcp_ready* ready)
  210. {
  211. ready->ready = 0;
  212. ready->port = 0;
  213. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  214. pthread_mutex_init(&ready->mutex, 0);
  215. pthread_cond_init(&ready->cond, 0);
  216. #endif
  217. }
  218. void FreeTcpReady(tcp_ready* ready)
  219. {
  220. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  221. pthread_mutex_destroy(&ready->mutex);
  222. pthread_cond_destroy(&ready->cond);
  223. #else
  224. (void)ready;
  225. #endif
  226. }
  227. void file_test(const char* file, byte* check)
  228. {
  229. FILE* f;
  230. int i = 0, j;
  231. Sha256 sha256;
  232. byte buf[1024];
  233. byte shasum[SHA256_DIGEST_SIZE];
  234. InitSha256(&sha256);
  235. if( !( f = fopen( file, "rb" ) )) {
  236. printf("Can't open %s\n", file);
  237. return;
  238. }
  239. while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 )
  240. Sha256Update(&sha256, buf, i);
  241. Sha256Final(&sha256, shasum);
  242. memcpy(check, shasum, sizeof(shasum));
  243. for(j = 0; j < SHA256_DIGEST_SIZE; ++j )
  244. printf( "%02x", shasum[j] );
  245. printf(" %s\n", file);
  246. fclose(f);
  247. }