wolfssh_test.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 WOLFSSL_NUCLEUS
  104. /* port 8080 was open with QEMU */
  105. static const word16 wolfSshPort = 8080;
  106. #else
  107. static const word16 wolfSshPort = 22222;
  108. #endif
  109. #ifdef __GNUC__
  110. #define WS_NORETURN __attribute__((noreturn))
  111. #else
  112. #define WS_NORETURN
  113. #endif
  114. #ifdef USE_WINDOWS_API
  115. #pragma warning(push)
  116. #pragma warning(disable:4996)
  117. /* For Windows builds, disable compiler warnings for:
  118. * - 4996: deprecated function */
  119. #endif
  120. #if defined(WOLFSSH_TEST_CLIENT) || defined(WOLFSSH_TEST_SERVER)
  121. #ifdef USE_WINDOWS_API
  122. #pragma warning(pop)
  123. #endif
  124. #endif /* WOLFSSH_TEST_CLIENT || WOLFSSH_TEST_SERVER */
  125. #ifndef XNTOHS
  126. #define XNTOHS(a) ntohs((a))
  127. #endif
  128. #if (defined(WOLFSSH_TEST_SERVER) || defined(WOLFSSH_TEST_CLIENT))
  129. #ifdef WOLFSSL_NUCLEUS
  130. #define WFD_SET_TYPE FD_SET
  131. #define WFD_SET NU_FD_Set
  132. #define WFD_ZERO NU_FD_Init
  133. #define WFD_ISSET NU_FD_Check
  134. #else
  135. #define WFD_SET_TYPE fd_set
  136. #define WFD_SET FD_SET
  137. #define WFD_ZERO FD_ZERO
  138. #define WFD_ISSET FD_ISSET
  139. #endif
  140. /* returns 1 or greater when something is ready to be read */
  141. static INLINE int wSelect(int nfds, WFD_SET_TYPE* recvfds,
  142. WFD_SET_TYPE *writefds, WFD_SET_TYPE *errfds, struct timeval* timeout)
  143. {
  144. #ifdef WOLFSSL_NUCLEUS
  145. int ret = NU_Select (nfds, recvfds, writefds, errfds,
  146. (UNSIGNED)timeout->tv_sec);
  147. if (ret == NU_SUCCESS) {
  148. return 1;
  149. }
  150. return 0;
  151. #else
  152. return select(nfds, recvfds, writefds, errfds, timeout);
  153. #endif
  154. }
  155. #endif /* WOLFSSH_TEST_SERVER || WOLFSSH_TEST_CLIENT */
  156. /* Wolf Root Directory Helper */
  157. /* KEIL-RL File System does not support relative directory */
  158. #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) \
  159. && !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS)
  160. /* Maximum depth to search for WolfSSL root */
  161. #define MAX_WOLF_ROOT_DEPTH 5
  162. static INLINE int ChangeToWolfSshRoot(void)
  163. {
  164. #if !defined(NO_FILESYSTEM)
  165. int depth, res;
  166. WFILE* file;
  167. for(depth = 0; depth <= MAX_WOLF_ROOT_DEPTH; depth++) {
  168. if (WFOPEN(&file, serverKeyRsaPemFile, "rb") == 0) {
  169. WFCLOSE(file);
  170. return depth;
  171. }
  172. #ifdef USE_WINDOWS_API
  173. res = SetCurrentDirectoryA("..\\");
  174. #else
  175. res = chdir("../");
  176. #endif
  177. if (res < 0) {
  178. printf("chdir to ../ failed!\n");
  179. break;
  180. }
  181. }
  182. err_sys("wolfSSH root not found");
  183. return -1;
  184. #else
  185. return 0;
  186. #endif
  187. }
  188. #endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOL
  189. FSSL_TIRTOS) */
  190. #ifdef WOLFSSH_SFTP
  191. typedef int (*WS_CallbackSftpCommand)(const char* in, char* out, int outSz);
  192. #endif
  193. typedef struct ssh_func_args {
  194. int argc;
  195. char** argv;
  196. int return_code;
  197. tcp_ready* signal;
  198. WS_CallbackUserAuth user_auth;
  199. #ifdef WOLFSSH_SFTP
  200. /* callback for example sftp client commands instead of WFGETS */
  201. WS_CallbackSftpCommand sftp_cb;
  202. #endif
  203. } ssh_func_args;
  204. #ifdef WOLFSSH_TEST_LOCKING
  205. static INLINE void InitTcpReady(tcp_ready* ready)
  206. {
  207. ready->ready = 0;
  208. ready->port = 0;
  209. ready->srfName = NULL;
  210. #ifdef SINGLE_THREADED
  211. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  212. pthread_mutex_init(&ready->mutex, 0);
  213. pthread_cond_init(&ready->cond, 0);
  214. #endif
  215. }
  216. static INLINE void FreeTcpReady(tcp_ready* ready)
  217. {
  218. #ifdef SINGLE_THREADED
  219. (void)ready;
  220. #elif 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. static INLINE void WaitTcpReady(ssh_func_args* args)
  228. {
  229. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  230. pthread_mutex_lock(&args->signal->mutex);
  231. if (!args->signal->ready)
  232. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  233. args->signal->ready = 0; /* reset */
  234. pthread_mutex_unlock(&args->signal->mutex);
  235. #else
  236. (void)args;
  237. #endif
  238. }
  239. #endif /* WOLFSSH_TEST_LOCKING */
  240. #ifdef WOLFSSH_TEST_THREADING
  241. typedef THREAD_RETURN WOLFSSH_THREAD THREAD_FUNC(void*);
  242. static INLINE void ThreadStart(THREAD_FUNC fun, void* args, THREAD_TYPE* thread)
  243. {
  244. #ifdef SINGLE_THREADED
  245. (void)fun;
  246. (void)args;
  247. (void)thread;
  248. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  249. pthread_create(thread, 0, fun, args);
  250. return;
  251. #elif defined(WOLFSSL_TIRTOS)
  252. /* Initialize the defaults and set the parameters. */
  253. Task_Params taskParams;
  254. Task_Params_init(&taskParams);
  255. taskParams.arg0 = (UArg)args;
  256. taskParams.stackSize = 65535;
  257. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  258. if (*thread == NULL) {
  259. printf("Failed to create new Task\n");
  260. }
  261. Task_yield();
  262. #else
  263. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  264. #endif
  265. }
  266. static INLINE void ThreadJoin(THREAD_TYPE thread)
  267. {
  268. #ifdef SINGLE_THREADED
  269. (void)thread;
  270. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  271. pthread_join(thread, 0);
  272. #elif defined(WOLFSSL_TIRTOS)
  273. while(1) {
  274. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  275. Task_sleep(5);
  276. break;
  277. }
  278. Task_yield();
  279. }
  280. #else
  281. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  282. assert(res == WAIT_OBJECT_0);
  283. res = CloseHandle((HANDLE)thread);
  284. assert(res);
  285. (void)res; /* Suppress un-used variable warning */
  286. #endif
  287. }
  288. static INLINE void ThreadDetach(THREAD_TYPE thread)
  289. {
  290. #ifdef SINGLE_THREADED
  291. (void)thread;
  292. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  293. pthread_detach(thread);
  294. #elif defined(WOLFSSL_TIRTOS)
  295. #if 0
  296. while(1) {
  297. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  298. Task_sleep(5);
  299. break;
  300. }
  301. Task_yield();
  302. }
  303. #endif
  304. #else
  305. int res = CloseHandle((HANDLE)thread);
  306. assert(res);
  307. (void)res; /* Suppress un-used variable warning */
  308. #endif
  309. }
  310. #endif /* WOLFSSH_TEST_THREADING */
  311. #endif /* _WOLFSSH_TEST_H_ */