utils.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* utils.h
  2. *
  3. * Copyright (C) 2006-2024 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 <tests/unit.h>
  26. #ifndef NO_FILESYSTEM
  27. #ifdef _MSC_VER
  28. #include <direct.h>
  29. #endif
  30. #define TMP_DIR_PREFIX "tmpDir-"
  31. /* len is length of tmpDir name, assuming
  32. * len does not include null terminating character */
  33. char* create_tmp_dir(char *tmpDir, int len)
  34. {
  35. if (len < (int)XSTR_SIZEOF(TMP_DIR_PREFIX))
  36. return NULL;
  37. XMEMCPY(tmpDir, TMP_DIR_PREFIX, XSTR_SIZEOF(TMP_DIR_PREFIX));
  38. if (mymktemp(tmpDir, len, len - (int)XSTR_SIZEOF(TMP_DIR_PREFIX)) == NULL)
  39. return NULL;
  40. #ifdef _MSC_VER
  41. if (_mkdir(tmpDir) != 0)
  42. return NULL;
  43. #elif defined(__MINGW32__)
  44. if (mkdir(tmpDir) != 0)
  45. return NULL;
  46. #else
  47. if (mkdir(tmpDir, 0700) != 0)
  48. return NULL;
  49. #endif
  50. return tmpDir;
  51. }
  52. int rem_dir(const char* dirName)
  53. {
  54. #ifdef _MSC_VER
  55. if (_rmdir(dirName) != 0)
  56. return -1;
  57. #else
  58. if (rmdir(dirName) != 0)
  59. return -1;
  60. #endif
  61. return 0;
  62. }
  63. int rem_file(const char* fileName)
  64. {
  65. #ifdef _MSC_VER
  66. if (_unlink(fileName) != 0)
  67. return -1;
  68. #else
  69. if (unlink(fileName) != 0)
  70. return -1;
  71. #endif
  72. return 0;
  73. }
  74. int copy_file(const char* in, const char* out)
  75. {
  76. byte buf[100];
  77. XFILE inFile = XBADFILE;
  78. XFILE outFile = XBADFILE;
  79. size_t sz;
  80. int ret = -1;
  81. inFile = XFOPEN(in, "rb");
  82. if (inFile == XBADFILE)
  83. goto cleanup;
  84. outFile = XFOPEN(out, "wb");
  85. if (outFile == XBADFILE)
  86. goto cleanup;
  87. while ((sz = XFREAD(buf, 1, sizeof(buf), inFile)) != 0) {
  88. if (XFERROR(inFile))
  89. goto cleanup;
  90. if (XFWRITE(buf, 1, sz, outFile) != sz)
  91. goto cleanup;
  92. if (XFEOF(inFile))
  93. break;
  94. }
  95. ret = 0;
  96. cleanup:
  97. if (inFile != XBADFILE)
  98. XFCLOSE(inFile);
  99. if (outFile != XBADFILE)
  100. XFCLOSE(outFile);
  101. return ret;
  102. }
  103. #if defined(__MACH__) || defined(__FreeBSD__)
  104. int link_file(const char* in, const char* out)
  105. {
  106. return link(in, out);
  107. }
  108. #endif
  109. #endif /* !NO_FILESYSTEM */
  110. #define TEST_MEMIO_BUF_SZ (64 * 1024)
  111. struct test_memio_ctx
  112. {
  113. byte c_buff[TEST_MEMIO_BUF_SZ];
  114. int c_len;
  115. const char* c_ciphers;
  116. byte s_buff[TEST_MEMIO_BUF_SZ];
  117. int s_len;
  118. const char* s_ciphers;
  119. };
  120. int test_memio_do_handshake(WOLFSSL *ssl_c, WOLFSSL *ssl_s,
  121. int max_rounds, int *rounds);
  122. int test_memio_setup(struct test_memio_ctx *ctx,
  123. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  124. method_provider method_c, method_provider method_s);
  125. int test_memio_setup_ex(struct test_memio_ctx *ctx,
  126. WOLFSSL_CTX **ctx_c, WOLFSSL_CTX **ctx_s, WOLFSSL **ssl_c, WOLFSSL **ssl_s,
  127. method_provider method_c, method_provider method_s,
  128. byte *caCert, int caCertSz, byte *serverCert, int serverCertSz,
  129. byte *serverKey, int serverKeySz);
  130. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
  131. void signal_ready(tcp_ready* ready)
  132. {
  133. THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
  134. ready->ready = 1;
  135. THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
  136. THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
  137. }
  138. #endif
  139. void wait_tcp_ready(func_args* args)
  140. {
  141. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
  142. tcp_ready* ready = args->signal;
  143. THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
  144. if (!ready->ready) {
  145. THREAD_CHECK_RET(wolfSSL_CondWait(&ready->cond));
  146. }
  147. ready->ready = 0; /* reset */
  148. THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
  149. #else
  150. /* no threading wait or single threaded */
  151. (void)args;
  152. #endif
  153. }
  154. #ifndef SINGLE_THREADED
  155. /* Start a thread.
  156. *
  157. * @param [in] fun Function to execute in thread.
  158. * @param [in] args Object to send to function in thread.
  159. * @param [out] thread Handle to thread.
  160. */
  161. void start_thread(THREAD_CB fun, func_args* args, THREAD_TYPE* thread)
  162. {
  163. THREAD_CHECK_RET(wolfSSL_NewThread(thread, fun, args));
  164. }
  165. /* Join thread to wait for completion.
  166. *
  167. * @param [in] thread Handle to thread.
  168. */
  169. void join_thread(THREAD_TYPE thread)
  170. {
  171. THREAD_CHECK_RET(wolfSSL_JoinThread(thread));
  172. }
  173. #endif /* SINGLE_THREADED */
  174. /* These correspond to WOLFSSL_SSLV3...WOLFSSL_DTLSV1_3 */
  175. const char* tls_desc[] = {
  176. "SSLv3", "TLSv1.0", "TLSv1.1", "TLSv1.2", "TLSv1.3",
  177. "DTLSv1.0", "DTLSv1.2", "DTLSv1.3"
  178. };