wolfssh_test.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* test.h
  2. *
  3. * Copyright (C) 2014-2019 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSH.
  6. *
  7. * wolfSSH 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 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSH 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 wolfSSH. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #pragma once
  21. #ifndef _WOLFSSH_TEST_H_
  22. #define _WOLFSSH_TEST_H_
  23. #include <wolfssh/ssh.h>
  24. #include <tinystdio.h>
  25. /*#include <stdlib.h>*/
  26. #include <ctype.h>
  27. /*#include <wolfssh/error.h>*/
  28. /* Socket Handling */
  29. #ifndef WOLFSSH_SOCKET_INVALID
  30. #if defined(USE_WINDOWS_API) || defined(MICROCHIP_MPLAB_HARMONY)
  31. #define WOLFSSH_SOCKET_INVALID ((SOCKET_T)INVALID_SOCKET)
  32. #elif defined(WOLFSSH_TIRTOS)
  33. #define WOLFSSH_SOCKET_INVALID ((SOCKET_T)-1)
  34. #else
  35. #define WOLFSSH_SOCKET_INVALID (SOCKET_T)(0)
  36. #endif
  37. #endif /* WOLFSSH_SOCKET_INVALID */
  38. #ifndef WOLFSSL_SOCKET_IS_INVALID
  39. #if defined(USE_WINDOWS_API) || defined(WOLFSSL_TIRTOS)
  40. #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) == WOLFSSL_SOCKET_INVALID)
  41. #else
  42. #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) < WOLFSSL_SOCKET_INVALID)
  43. #endif
  44. #endif /* WOLFSSL_SOCKET_IS_INVALID */
  45. #if defined(__MACH__) || defined(USE_WINDOWS_API)
  46. #ifndef _SOCKLEN_T
  47. typedef int socklen_t;
  48. #endif
  49. #endif
  50. #ifdef USE_WINDOWS_API
  51. #define WCLOSESOCKET(s) closesocket(s)
  52. #define WSTARTTCP() do { WSADATA wsd; WSAStartup(0x0002, &wsd); } while(0)
  53. #elif defined(MICROCHIP_TCPIP) || defined(MICROCHIP_MPLAB_HARMONY)
  54. #ifdef MICROCHIP_MPLAB_HARMONY
  55. #define WCLOSESOCKET(s) TCPIP_TCP_Close((s))
  56. #else
  57. #define WCLOSESOCKET(s) closesocket((s))
  58. #endif
  59. #define WSTARTTCP()
  60. #elif defined(WOLFSSL_NUCLEUS)
  61. #define WCLOSESOCKET(s) NU_Close_Socket((s))
  62. #define WSTARTTCP()
  63. #else
  64. #define WCLOSESOCKET(s) close(s)
  65. #define WSTARTTCP()
  66. #endif
  67. #ifdef SINGLE_THREADED
  68. typedef unsigned int THREAD_RETURN;
  69. typedef void* THREAD_TYPE;
  70. #define WOLFSSH_THREAD
  71. #else
  72. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  73. typedef void* THREAD_RETURN;
  74. typedef pthread_t THREAD_TYPE;
  75. #define WOLFSSH_THREAD
  76. #define INFINITE -1
  77. #define WAIT_OBJECT_0 0L
  78. #elif defined(WOLFSSL_NUCLEUS)
  79. typedef unsigned int THREAD_RETURN;
  80. typedef intptr_t THREAD_TYPE;
  81. #define WOLFSSH_THREAD
  82. #else
  83. typedef unsigned int THREAD_RETURN;
  84. typedef intptr_t THREAD_TYPE;
  85. #define WOLFSSH_THREAD __stdcall
  86. #endif
  87. #endif
  88. #ifdef TEST_IPV6
  89. typedef struct sockaddr_in6 SOCKADDR_IN_T;
  90. #define AF_INET_V AF_INET6
  91. #else
  92. #ifndef WOLFSSL_NUCLEUS
  93. typedef struct sockaddr_in SOCKADDR_IN_T;
  94. #endif
  95. #define AF_INET_V AF_INET
  96. #endif
  97. #define serverKeyRsaPemFile "./keys/server-key-rsa.pem"
  98. #ifndef TEST_IPV6
  99. static const char* const wolfSshIp = "127.0.0.1";
  100. #else /* TEST_IPV6 */
  101. static const char* const wolfSshIp = "::1";
  102. #endif /* TEST_IPV6 */
  103. #ifdef __GNUC__
  104. #define WS_NORETURN __attribute__((noreturn))
  105. #else
  106. #define WS_NORETURN
  107. #endif
  108. #ifdef USE_WINDOWS_API
  109. #pragma warning(push)
  110. #pragma warning(disable:4996)
  111. /* For Windows builds, disable compiler warnings for:
  112. * - 4996: deprecated function */
  113. #endif
  114. #if defined(WOLFSSH_TEST_CLIENT) || defined(WOLFSSH_TEST_SERVER)
  115. #ifdef USE_WINDOWS_API
  116. #pragma warning(pop)
  117. #endif
  118. #endif /* WOLFSSH_TEST_CLIENT || WOLFSSH_TEST_SERVER */
  119. #ifndef XNTOHS
  120. #define XNTOHS(a) ntohs((a))
  121. #endif
  122. #if (defined(WOLFSSH_TEST_SERVER) || defined(WOLFSSH_TEST_CLIENT))
  123. #ifdef WOLFSSL_NUCLEUS
  124. #define WFD_SET_TYPE FD_SET
  125. #define WFD_SET NU_FD_Set
  126. #define WFD_ZERO NU_FD_Init
  127. #define WFD_ISSET NU_FD_Check
  128. #else
  129. #define WFD_SET_TYPE fd_set
  130. #define WFD_SET FD_SET
  131. #define WFD_ZERO FD_ZERO
  132. #define WFD_ISSET FD_ISSET
  133. #endif
  134. /* returns 1 or greater when something is ready to be read */
  135. static INLINE int wSelect(int nfds, WFD_SET_TYPE* recvfds,
  136. WFD_SET_TYPE *writefds, WFD_SET_TYPE *errfds, struct timeval* timeout)
  137. {
  138. #ifdef WOLFSSL_NUCLEUS
  139. int ret = NU_Select (nfds, recvfds, writefds, errfds,
  140. (UNSIGNED)timeout->tv_sec);
  141. if (ret == NU_SUCCESS) {
  142. return 1;
  143. }
  144. return 0;
  145. #else
  146. return select(nfds, recvfds, writefds, errfds, timeout);
  147. #endif
  148. }
  149. #endif /* WOLFSSH_TEST_SERVER || WOLFSSH_TEST_CLIENT */
  150. /* Wolf Root Directory Helper */
  151. /* KEIL-RL File System does not support relative directory */
  152. #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) \
  153. && !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS)
  154. /* Maximum depth to search for WolfSSL root */
  155. #define MAX_WOLF_ROOT_DEPTH 5
  156. static INLINE int ChangeToWolfSshRoot(void)
  157. {
  158. #if !defined(NO_FILESYSTEM)
  159. int depth, res;
  160. WFILE* file;
  161. for(depth = 0; depth <= MAX_WOLF_ROOT_DEPTH; depth++) {
  162. if (WFOPEN(&file, serverKeyRsaPemFile, "rb") == 0) {
  163. WFCLOSE(file);
  164. return depth;
  165. }
  166. #ifdef USE_WINDOWS_API
  167. res = SetCurrentDirectoryA("..\\");
  168. #else
  169. res = chdir("../");
  170. #endif
  171. if (res < 0) {
  172. printf("chdir to ../ failed!\n");
  173. break;
  174. }
  175. }
  176. err_sys("wolfSSH root not found");
  177. return -1;
  178. #else
  179. return 0;
  180. #endif
  181. }
  182. #endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOL
  183. FSSL_TIRTOS) */
  184. #ifdef WOLFSSH_SFTP
  185. typedef int (*WS_CallbackSftpCommand)(const char* in, char* out, int outSz);
  186. #endif
  187. typedef struct ssh_func_args {
  188. int argc;
  189. char** argv;
  190. int return_code;
  191. tcp_ready* signal;
  192. WS_CallbackUserAuth user_auth;
  193. #ifdef WOLFSSH_SFTP
  194. /* callback for example sftp client commands instead of WFGETS */
  195. WS_CallbackSftpCommand sftp_cb;
  196. #endif
  197. } ssh_func_args;
  198. #ifdef WOLFSSH_TEST_LOCKING
  199. static INLINE void InitTcpReady(tcp_ready* ready)
  200. {
  201. ready->ready = 0;
  202. ready->port = 0;
  203. ready->srfName = NULL;
  204. #ifdef SINGLE_THREADED
  205. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  206. pthread_mutex_init(&ready->mutex, 0);
  207. pthread_cond_init(&ready->cond, 0);
  208. #endif
  209. }
  210. static INLINE void FreeTcpReady(tcp_ready* ready)
  211. {
  212. #ifdef SINGLE_THREADED
  213. (void)ready;
  214. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  215. pthread_mutex_destroy(&ready->mutex);
  216. pthread_cond_destroy(&ready->cond);
  217. #else
  218. (void)ready;
  219. #endif
  220. }
  221. static INLINE void WaitTcpReady(ssh_func_args* args)
  222. {
  223. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  224. pthread_mutex_lock(&args->signal->mutex);
  225. if (!args->signal->ready)
  226. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  227. args->signal->ready = 0; /* reset */
  228. pthread_mutex_unlock(&args->signal->mutex);
  229. #else
  230. (void)args;
  231. #endif
  232. }
  233. #endif /* WOLFSSH_TEST_LOCKING */
  234. #ifdef WOLFSSH_TEST_THREADING
  235. typedef THREAD_RETURN WOLFSSH_THREAD THREAD_FUNC(void*);
  236. static INLINE void ThreadStart(THREAD_FUNC fun, void* args, THREAD_TYPE* thread)
  237. {
  238. #ifdef SINGLE_THREADED
  239. (void)fun;
  240. (void)args;
  241. (void)thread;
  242. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  243. pthread_create(thread, 0, fun, args);
  244. return;
  245. #elif defined(WOLFSSL_TIRTOS)
  246. /* Initialize the defaults and set the parameters. */
  247. Task_Params taskParams;
  248. Task_Params_init(&taskParams);
  249. taskParams.arg0 = (UArg)args;
  250. taskParams.stackSize = 65535;
  251. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  252. if (*thread == NULL) {
  253. printf("Failed to create new Task\n");
  254. }
  255. Task_yield();
  256. #else
  257. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  258. #endif
  259. }
  260. static INLINE void ThreadJoin(THREAD_TYPE thread)
  261. {
  262. #ifdef SINGLE_THREADED
  263. (void)thread;
  264. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  265. pthread_join(thread, 0);
  266. #elif defined(WOLFSSL_TIRTOS)
  267. while(1) {
  268. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  269. Task_sleep(5);
  270. break;
  271. }
  272. Task_yield();
  273. }
  274. #else
  275. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  276. assert(res == WAIT_OBJECT_0);
  277. res = CloseHandle((HANDLE)thread);
  278. assert(res);
  279. (void)res; /* Suppress un-used variable warning */
  280. #endif
  281. }
  282. static INLINE void ThreadDetach(THREAD_TYPE thread)
  283. {
  284. #ifdef SINGLE_THREADED
  285. (void)thread;
  286. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  287. pthread_detach(thread);
  288. #elif defined(WOLFSSL_TIRTOS)
  289. #if 0
  290. while(1) {
  291. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  292. Task_sleep(5);
  293. break;
  294. }
  295. Task_yield();
  296. }
  297. #endif
  298. #else
  299. int res = CloseHandle((HANDLE)thread);
  300. assert(res);
  301. (void)res; /* Suppress un-used variable warning */
  302. #endif
  303. }
  304. #endif /* WOLFSSH_TEST_THREADING */
  305. #endif /* _WOLFSSH_TEST_H_ */