testsuite.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* testsuite.c
  2. *
  3. * Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
  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/openssl/ssl.h>
  25. #include <cyassl/test.h>
  26. #include <cyassl/ctaocrypt/md5.h>
  27. #ifdef SINGLE_THREADED
  28. #error testsuite needs threads to run, please run ctaocrypt/test, \
  29. and the examples/ individually
  30. #endif
  31. void wait_tcp_ready(func_args*);
  32. void ctaocrypt_test(void*);
  33. void client_test(void*);
  34. void echoclient_test(void*);
  35. THREAD_RETURN CYASSL_THREAD server_test(void*);
  36. THREAD_RETURN CYASSL_THREAD echoserver_test(void*);
  37. void file_test(char* file, byte* hash);
  38. enum {
  39. NUMARGS = 3
  40. };
  41. int main(int argc, char** argv)
  42. {
  43. func_args args;
  44. func_args server_args;
  45. tcp_ready ready;
  46. THREAD_TYPE serverThread;
  47. StartTCP();
  48. args.argc = server_args.argc = argc;
  49. args.argv = server_args.argv = argv;
  50. CyaSSL_Init();
  51. #ifdef DEBUG_CYASSL
  52. CyaSSL_Debugging_ON();
  53. #endif
  54. if (CurrentDir("testsuite"))
  55. ChangeDirBack(1);
  56. else if (CurrentDir("build")) /* Xcode->Preferences->Locations->Build */
  57. ChangeDirBack(2); /* Location "Place build product in locations
  58. specified by targets", uses build/Debug */
  59. /* CTaoCrypt test */
  60. ctaocrypt_test(&args);
  61. if (args.return_code != 0) return args.return_code;
  62. /* Simple CyaSSL client server test */
  63. InitTcpReady(&ready);
  64. server_args.signal = &ready;
  65. start_thread(server_test, &server_args, &serverThread);
  66. wait_tcp_ready(&server_args);
  67. client_test(&args);
  68. if (args.return_code != 0) return args.return_code;
  69. join_thread(serverThread);
  70. if (server_args.return_code != 0) return server_args.return_code;
  71. /* Echo input yaSSL client server test */
  72. start_thread(echoserver_test, &server_args, &serverThread);
  73. wait_tcp_ready(&server_args);
  74. {
  75. func_args echo_args;
  76. char* myArgv[NUMARGS];
  77. char argc0[32];
  78. char argc1[32];
  79. char argc2[32];
  80. myArgv[0] = argc0;
  81. myArgv[1] = argc1;
  82. myArgv[2] = argc2;
  83. echo_args.argc = NUMARGS;
  84. echo_args.argv = myArgv;
  85. strcpy(echo_args.argv[0], "echoclient");
  86. strcpy(echo_args.argv[1], "input");
  87. strcpy(echo_args.argv[2], "output");
  88. remove("output");
  89. /* make sure OK */
  90. echoclient_test(&echo_args);
  91. if (echo_args.return_code != 0) return echo_args.return_code;
  92. #ifdef CYASSL_DTLS
  93. wait_tcp_ready(&server_args);
  94. #endif
  95. /* send quit to echoserver */
  96. echo_args.argc = 2;
  97. strcpy(echo_args.argv[1], "quit");
  98. echoclient_test(&echo_args);
  99. if (echo_args.return_code != 0) return echo_args.return_code;
  100. join_thread(serverThread);
  101. if (server_args.return_code != 0) return server_args.return_code;
  102. }
  103. /* validate output equals input */
  104. {
  105. byte input[MD5_DIGEST_SIZE];
  106. byte output[MD5_DIGEST_SIZE];
  107. file_test("input", input);
  108. file_test("output", output);
  109. if (memcmp(input, output, sizeof(input)) != 0)
  110. return EXIT_FAILURE;
  111. }
  112. CyaSSL_Cleanup();
  113. FreeTcpReady(&ready);
  114. printf("\nAll tests passed!\n");
  115. return EXIT_SUCCESS;
  116. }
  117. void wait_tcp_ready(func_args* args)
  118. {
  119. #ifdef _POSIX_THREADS
  120. pthread_mutex_lock(&args->signal->mutex);
  121. if (!args->signal->ready)
  122. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  123. args->signal->ready = 0; /* reset */
  124. pthread_mutex_unlock(&args->signal->mutex);
  125. #endif
  126. }
  127. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  128. {
  129. #ifdef _POSIX_THREADS
  130. pthread_create(thread, 0, fun, args);
  131. return;
  132. #else
  133. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  134. #endif
  135. }
  136. void join_thread(THREAD_TYPE thread)
  137. {
  138. #ifdef _POSIX_THREADS
  139. pthread_join(thread, 0);
  140. #else
  141. int res = WaitForSingleObject(thread, INFINITE);
  142. assert(res == WAIT_OBJECT_0);
  143. res = CloseHandle(thread);
  144. assert(res);
  145. #endif
  146. }
  147. void InitTcpReady(tcp_ready* ready)
  148. {
  149. ready->ready = 0;
  150. #ifdef _POSIX_THREADS
  151. pthread_mutex_init(&ready->mutex, 0);
  152. pthread_cond_init(&ready->cond, 0);
  153. #endif
  154. }
  155. void FreeTcpReady(tcp_ready* ready)
  156. {
  157. #ifdef _POSIX_THREADS
  158. pthread_mutex_destroy(&ready->mutex);
  159. pthread_cond_destroy(&ready->cond);
  160. #endif
  161. }
  162. void file_test(char* file, byte* check)
  163. {
  164. FILE* f;
  165. int i = 0, j;
  166. Md5 md5;
  167. byte buf[1024];
  168. byte md5sum[MD5_DIGEST_SIZE];
  169. InitMd5(&md5);
  170. if( !( f = fopen( file, "rb" ) )) {
  171. printf("Can't open %s\n", file);
  172. return;
  173. }
  174. while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 )
  175. Md5Update(&md5, buf, i);
  176. Md5Final(&md5, md5sum);
  177. memcpy(check, md5sum, sizeof(md5sum));
  178. for(j = 0; j < MD5_DIGEST_SIZE; ++j )
  179. printf( "%02x", md5sum[j] );
  180. printf(" %s\n", file);
  181. fclose(f);
  182. }