PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/suites.c

https://github.com/andersmalm/cyassl
C | 432 lines | 329 code | 54 blank | 49 comment | 76 complexity | 2ff1a764e2d48c3b56a072002eaf0b0c MD5 | raw file
Possible License(s): GPL-2.0
  1. /* suites.c
  2. *
  3. * Copyright (C) 2006-2012 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 <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <cyassl/ssl.h>
  28. #include <tests/unit.h>
  29. #define MAX_ARGS 40
  30. #define MAX_COMMAND_SZ 240
  31. #include "examples/client/client.h"
  32. #include "examples/server/server.h"
  33. static void execute_test_case(int svr_argc, char** svr_argv,
  34. int cli_argc, char** cli_argv)
  35. {
  36. func_args cliArgs = {cli_argc, cli_argv, 0, NULL};
  37. func_args svrArgs = {svr_argc, svr_argv, 0, NULL};
  38. tcp_ready ready;
  39. THREAD_TYPE serverThread;
  40. char commandLine[MAX_COMMAND_SZ];
  41. int i;
  42. static int tests = 1;
  43. commandLine[0] = '\0';
  44. for (i = 0; i < svr_argc; i++) {
  45. strcat(commandLine, svr_argv[i]);
  46. strcat(commandLine, " ");
  47. }
  48. printf("trying server command line[%d]: %s\n", tests, commandLine);
  49. commandLine[0] = '\0';
  50. for (i = 0; i < cli_argc; i++) {
  51. strcat(commandLine, cli_argv[i]);
  52. strcat(commandLine, " ");
  53. }
  54. printf("trying client command line[%d]: %s\n", tests++, commandLine);
  55. InitTcpReady(&ready);
  56. /* start server */
  57. svrArgs.signal = &ready;
  58. start_thread(server_test, &svrArgs, &serverThread);
  59. wait_tcp_ready(&svrArgs);
  60. /* start client */
  61. client_test(&cliArgs);
  62. /* verify results */
  63. if (cliArgs.return_code != 0) {
  64. printf("client_test failed\n");
  65. exit(EXIT_FAILURE);
  66. }
  67. join_thread(serverThread);
  68. if (svrArgs.return_code != 0) {
  69. printf("server_test failed\n");
  70. exit(EXIT_FAILURE);
  71. }
  72. FreeTcpReady(&ready);
  73. }
  74. static void test_harness(void* vargs)
  75. {
  76. func_args* args = (func_args*)vargs;
  77. char* script;
  78. long sz, len;
  79. int cliMode = 0; /* server or client command flag, server first */
  80. FILE* file;
  81. char* svrArgs[MAX_ARGS];
  82. int svrArgsSz;
  83. char* cliArgs[MAX_ARGS];
  84. int cliArgsSz;
  85. char* cursor;
  86. char* comment;
  87. const char* fname = "tests/test.conf";
  88. if (args->argc == 1) {
  89. printf("notice: using default file %s\n", fname);
  90. }
  91. else if(args->argc != 2) {
  92. printf("usage: harness [FILE]\n");
  93. args->return_code = 1;
  94. return;
  95. }
  96. else {
  97. fname = args->argv[1];
  98. }
  99. file = fopen(fname, "r");
  100. if (file == NULL) {
  101. fprintf(stderr, "unable to open %s\n", fname);
  102. args->return_code = 1;
  103. return;
  104. }
  105. fseek(file, 0, SEEK_END);
  106. sz = ftell(file);
  107. rewind(file);
  108. if (sz == 0) {
  109. fprintf(stderr, "%s is empty\n", fname);
  110. fclose(file);
  111. args->return_code = 1;
  112. return;
  113. }
  114. script = (char*)malloc(sz+1);
  115. if (script == 0) {
  116. fprintf(stderr, "unable to allocte script buffer\n");
  117. fclose(file);
  118. args->return_code = 1;
  119. return;
  120. }
  121. len = fread(script, 1, sz, file);
  122. if (len != sz) {
  123. fprintf(stderr, "read error\n");
  124. fclose(file);
  125. args->return_code = 1;
  126. return;
  127. }
  128. fclose(file);
  129. script[sz] = 0;
  130. cursor = script;
  131. svrArgsSz = 1;
  132. svrArgs[0] = args->argv[0];
  133. cliArgsSz = 1;
  134. cliArgs[0] = args->argv[0];
  135. while (*cursor != 0) {
  136. int do_it = 0;
  137. switch (*cursor) {
  138. case '\n':
  139. /* A blank line triggers test case execution or switches
  140. to client mode if we don't have the client command yet */
  141. if (cliMode == 0)
  142. cliMode = 1; /* switch to client mode processing */
  143. else
  144. do_it = 1; /* Do It, we have server and client */
  145. cursor++;
  146. break;
  147. case '#':
  148. /* Ignore lines that start with a #. */
  149. comment = strsep(&cursor, "\n");
  150. printf("%s\n", comment);
  151. break;
  152. case '-':
  153. /* Parameters start with a -. They end in either a newline
  154. * or a space. Capture until either, save in Args list. */
  155. if (cliMode)
  156. cliArgs[cliArgsSz++] = strsep(&cursor, " \n");
  157. else
  158. svrArgs[svrArgsSz++] = strsep(&cursor, " \n");
  159. break;
  160. default:
  161. /* Anything from cursor until end of line that isn't the above
  162. * is data for a paramter. Just up until the next newline in
  163. * the Args list. */
  164. if (cliMode)
  165. cliArgs[cliArgsSz++] = strsep(&cursor, "\n");
  166. else
  167. svrArgs[svrArgsSz++] = strsep(&cursor, "\n");
  168. if (*cursor == 0) /* eof */
  169. do_it = 1;
  170. }
  171. if (svrArgsSz == MAX_ARGS || cliArgsSz == MAX_ARGS) {
  172. fprintf(stderr, "too many arguments, forcing test run\n");
  173. do_it = 1;
  174. }
  175. if (do_it) {
  176. execute_test_case(svrArgsSz, svrArgs, cliArgsSz, cliArgs);
  177. svrArgsSz = 1;
  178. cliArgsSz = 1;
  179. cliMode = 0;
  180. }
  181. }
  182. free(script);
  183. args->return_code = 0;
  184. }
  185. int SuiteTest(void)
  186. {
  187. func_args args;
  188. char argv0[2][80];
  189. char* myArgv[2];
  190. printf(" Begin Cipher Suite Tests\n");
  191. /* setup */
  192. myArgv[0] = argv0[0];
  193. myArgv[1] = argv0[1];
  194. args.argv = myArgv;
  195. strcpy(argv0[0], "SuiteTest");
  196. #if !defined(NO_RSA)
  197. /* default case */
  198. args.argc = 1;
  199. printf("starting default cipher suite tests\n");
  200. test_harness(&args);
  201. if (args.return_code != 0) {
  202. printf("error from script %d\n", args.return_code);
  203. exit(EXIT_FAILURE);
  204. }
  205. #endif
  206. /* any extra cases will need another argument */
  207. args.argc = 2;
  208. #ifdef OPENSSL_EXTRA
  209. /* add openssl extra suites */
  210. strcpy(argv0[1], "tests/test-openssl.conf");
  211. printf("starting openssl extra cipher suite tests\n");
  212. test_harness(&args);
  213. if (args.return_code != 0) {
  214. printf("error from script %d\n", args.return_code);
  215. exit(EXIT_FAILURE);
  216. }
  217. #endif
  218. #if !defined(NO_RSA) && defined(HAVE_NULL_CIPHER)
  219. /* add rsa null cipher suites */
  220. strcpy(argv0[1], "tests/test-null.conf");
  221. printf("starting null cipher suite tests\n");
  222. test_harness(&args);
  223. if (args.return_code != 0) {
  224. printf("error from script %d\n", args.return_code);
  225. exit(EXIT_FAILURE);
  226. }
  227. #endif
  228. #ifdef HAVE_HC128
  229. /* add hc128 extra suites */
  230. strcpy(argv0[1], "tests/test-hc128.conf");
  231. printf("starting hc128 extra cipher suite tests\n");
  232. test_harness(&args);
  233. if (args.return_code != 0) {
  234. printf("error from script %d\n", args.return_code);
  235. exit(EXIT_FAILURE);
  236. }
  237. #endif
  238. #ifdef HAVE_RABBIT
  239. /* add rabbit extra suites */
  240. strcpy(argv0[1], "tests/test-rabbit.conf");
  241. printf("starting rabbit extra cipher suite tests\n");
  242. test_harness(&args);
  243. if (args.return_code != 0) {
  244. printf("error from script %d\n", args.return_code);
  245. exit(EXIT_FAILURE);
  246. }
  247. #endif
  248. #if !defined(NO_PSK) && !defined(NO_AES)
  249. /* add psk extra suites */
  250. strcpy(argv0[1], "tests/test-psk.conf");
  251. printf("starting psk extra cipher suite tests\n");
  252. test_harness(&args);
  253. if (args.return_code != 0) {
  254. printf("error from script %d\n", args.return_code);
  255. exit(EXIT_FAILURE);
  256. }
  257. #endif
  258. #if !defined(NO_PSK) && defined(HAVE_NULL_CIPHER) && !defined(NO_OLD_TLS)
  259. strcpy(argv0[1], "tests/test-psk-null.conf");
  260. printf("starting psk extra null cipher suite tests\n");
  261. test_harness(&args);
  262. if (args.return_code != 0) {
  263. printf("error from script %d\n", args.return_code);
  264. exit(EXIT_FAILURE);
  265. }
  266. #endif
  267. #ifdef CYASSL_LEANPSK
  268. strcpy(argv0[1], "tests/test-leanpsk.conf");
  269. printf("starting lean-psk cipher suite tests\n");
  270. test_harness(&args);
  271. if (args.return_code != 0) {
  272. printf("error from script %d\n", args.return_code);
  273. exit(EXIT_FAILURE);
  274. }
  275. #endif
  276. #ifdef HAVE_NTRU
  277. /* add ntru extra suites */
  278. strcpy(argv0[1], "tests/test-ntru.conf");
  279. printf("starting ntru extra cipher suite tests\n");
  280. test_harness(&args);
  281. if (args.return_code != 0) {
  282. printf("error from script %d\n", args.return_code);
  283. exit(EXIT_FAILURE);
  284. }
  285. #endif
  286. #ifdef HAVE_ECC
  287. /* add ecc extra suites */
  288. strcpy(argv0[1], "tests/test-ecc.conf");
  289. printf("starting ecc extra cipher suite tests\n");
  290. test_harness(&args);
  291. if (args.return_code != 0) {
  292. printf("error from script %d\n", args.return_code);
  293. exit(EXIT_FAILURE);
  294. }
  295. #endif
  296. #ifdef HAVE_AESGCM
  297. /* add aesgcm extra suites */
  298. strcpy(argv0[1], "tests/test-aesgcm.conf");
  299. printf("starting aesgcm extra cipher suite tests\n");
  300. test_harness(&args);
  301. if (args.return_code != 0) {
  302. printf("error from script %d\n", args.return_code);
  303. exit(EXIT_FAILURE);
  304. }
  305. #endif
  306. #if defined(HAVE_AESGCM) && defined(OPENSSL_EXTRA)
  307. /* add aesgcm openssl extra suites */
  308. strcpy(argv0[1], "tests/test-aesgcm-openssl.conf");
  309. printf("starting aesgcm openssl extra cipher suite tests\n");
  310. test_harness(&args);
  311. if (args.return_code != 0) {
  312. printf("error from script %d\n", args.return_code);
  313. exit(EXIT_FAILURE);
  314. }
  315. #endif
  316. #if defined(HAVE_AESGCM) && defined(HAVE_ECC)
  317. /* add aesgcm ecc extra suites */
  318. strcpy(argv0[1], "tests/test-aesgcm-ecc.conf");
  319. printf("starting aesgcm ecc extra cipher suite tests\n");
  320. test_harness(&args);
  321. if (args.return_code != 0) {
  322. printf("error from script %d\n", args.return_code);
  323. exit(EXIT_FAILURE);
  324. }
  325. #endif
  326. #if defined(HAVE_AESCCM)
  327. /* add aesccm extra suites */
  328. strcpy(argv0[1], "tests/test-aesccm.conf");
  329. printf("starting aesccm cipher suite tests\n");
  330. test_harness(&args);
  331. if (args.return_code != 0) {
  332. printf("error from script %d\n", args.return_code);
  333. exit(EXIT_FAILURE);
  334. }
  335. #ifdef HAVE_ECC
  336. /* add aesccm ecc extra suites */
  337. strcpy(argv0[1], "tests/test-aesccm-ecc.conf");
  338. printf("starting aesccm ecc cipher suite tests\n");
  339. test_harness(&args);
  340. if (args.return_code != 0) {
  341. printf("error from script %d\n", args.return_code);
  342. exit(EXIT_FAILURE);
  343. }
  344. #endif
  345. #endif
  346. #ifdef HAVE_CAMELLIA
  347. /* add camellia suites */
  348. strcpy(argv0[1], "tests/test-camellia.conf");
  349. printf("starting camellia suite tests\n");
  350. test_harness(&args);
  351. if (args.return_code != 0) {
  352. printf("error from script %d\n", args.return_code);
  353. exit(EXIT_FAILURE);
  354. }
  355. #ifdef OPENSSL_EXTRA
  356. /* add camellia openssl extra suites */
  357. strcpy(argv0[1], "tests/test-camellia-openssl.conf");
  358. printf("starting camellia openssl extra suite tests\n");
  359. test_harness(&args);
  360. if (args.return_code != 0) {
  361. printf("error from script %d\n", args.return_code);
  362. exit(EXIT_FAILURE);
  363. }
  364. #endif
  365. #endif
  366. #ifdef CYASSL_DTLS
  367. /* add dtls extra suites */
  368. strcpy(argv0[1], "tests/test-dtls.conf");
  369. printf("starting dtls extra cipher suite tests\n");
  370. test_harness(&args);
  371. if (args.return_code != 0) {
  372. printf("error from script %d\n", args.return_code);
  373. exit(EXIT_FAILURE);
  374. }
  375. #endif
  376. printf(" End Cipher Suite Tests\n");
  377. return args.return_code;
  378. }