public_server.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /* Copyright (c) 2015 the Civetweb developers
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #ifdef _MSC_VER
  22. #define _CRT_SECURE_NO_WARNINGS
  23. #endif
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <time.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include "public_server.h"
  30. #include <civetweb.h>
  31. #if defined(_WIN32)
  32. #include <Windows.h>
  33. #define mg_Sleep(x) (Sleep(x * 1000))
  34. #else
  35. #include <unistd.h>
  36. #define mg_Sleep(x) (sleep(x))
  37. #endif
  38. /* This unit test file uses the excellent Check unit testing library.
  39. * The API documentation is available here:
  40. * http://check.sourceforge.net/doc/check_html/index.html
  41. */
  42. const char *locate_ssl_cert(void)
  43. {
  44. return
  45. #ifdef _WIN32
  46. #ifdef LOCAL_TEST
  47. "resources\\ssl_cert.pem";
  48. #else
  49. /* Appveyor */
  50. "..\\..\\..\\resources\\ssl_cert.pem"; /* TODO: the different paths
  51. * used in the different test
  52. * system is an unsolved
  53. * problem */
  54. #endif
  55. #else
  56. #ifdef LOCAL_TEST
  57. "../resources/ssl_cert.pem";
  58. #else
  59. /* Travis */
  60. "../../resources/ssl_cert.pem"; // TODO: fix path in CI test environment
  61. #endif
  62. #endif
  63. }
  64. START_TEST(test_the_test_environment)
  65. {
  66. char wd[300];
  67. char buf[500];
  68. FILE *f;
  69. struct stat st;
  70. int ret;
  71. const char *ssl_cert = locate_ssl_cert();
  72. memset(wd, 0, sizeof(wd));
  73. memset(buf, 0, sizeof(buf));
  74. /* Get the current working directory */
  75. #ifdef _WIN32
  76. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  77. wd[sizeof(wd) - 1] = 0;
  78. #else
  79. (void)getcwd(wd, sizeof(wd));
  80. wd[sizeof(wd) - 1] = 0;
  81. #endif
  82. /* Check the pem file */
  83. #ifdef _WIN32
  84. strcpy(buf, wd);
  85. strcat(buf, "\\");
  86. strcat(buf, ssl_cert);
  87. f = fopen(buf, "rb");
  88. #else
  89. strcpy(buf, wd);
  90. strcat(buf, "/");
  91. strcat(buf, ssl_cert);
  92. f = fopen(buf, "r");
  93. #endif
  94. if (f) {
  95. fclose(f);
  96. } else {
  97. fprintf(stderr, "%s not found", buf);
  98. }
  99. /* Check the test dir */
  100. #ifdef _WIN32
  101. strcpy(buf, wd);
  102. strcat(buf, "\\test");
  103. #else
  104. strcpy(buf, wd);
  105. strcat(buf, "/test");
  106. #endif
  107. memset(&st, 0, sizeof(st));
  108. ret = stat(buf, &st);
  109. if (ret) {
  110. fprintf(stderr, "%s not found", buf);
  111. }
  112. }
  113. END_TEST
  114. static int log_msg_func(const struct mg_connection *conn, const char *message)
  115. {
  116. struct mg_context *ctx;
  117. char *ud;
  118. ck_assert(conn != NULL);
  119. ctx = mg_get_context(conn);
  120. ck_assert(ctx != NULL);
  121. ud = (char *)mg_get_user_data(ctx);
  122. strncpy(ud, message, 255);
  123. ud[255] = 0;
  124. return 1;
  125. }
  126. START_TEST(test_mg_start_stop_http_server)
  127. {
  128. struct mg_context *ctx;
  129. const char *OPTIONS[] = {
  130. #if !defined(NO_FILES)
  131. "document_root",
  132. ".",
  133. #endif
  134. "listening_ports",
  135. "8080",
  136. NULL,
  137. };
  138. size_t ports_cnt;
  139. int ports[16];
  140. int ssl[16];
  141. struct mg_callbacks callbacks;
  142. char errmsg[256];
  143. memset(ports, 0, sizeof(ports));
  144. memset(ssl, 0, sizeof(ssl));
  145. memset(&callbacks, 0, sizeof(callbacks));
  146. memset(errmsg, 0, sizeof(errmsg));
  147. callbacks.log_message = log_msg_func;
  148. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  149. mg_Sleep(1);
  150. ck_assert_str_eq(errmsg, "");
  151. ck_assert(ctx != NULL);
  152. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  153. ck_assert_uint_eq(ports_cnt, 1);
  154. ck_assert_int_eq(ports[0], 8080);
  155. ck_assert_int_eq(ssl[0], 0);
  156. ck_assert_int_eq(ports[1], 0);
  157. ck_assert_int_eq(ssl[1], 0);
  158. mg_Sleep(1);
  159. mg_stop(ctx);
  160. }
  161. END_TEST
  162. START_TEST(test_mg_start_stop_https_server)
  163. {
  164. #ifndef NO_SSL
  165. struct mg_context *ctx;
  166. size_t ports_cnt;
  167. int ports[16];
  168. int ssl[16];
  169. struct mg_callbacks callbacks;
  170. char errmsg[256];
  171. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  172. int opt_idx = 0;
  173. const char *ssl_cert = locate_ssl_cert();
  174. ck_assert(ssl_cert != NULL);
  175. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  176. #if !defined(NO_FILES)
  177. OPTIONS[opt_idx++] = "document_root";
  178. OPTIONS[opt_idx++] = ".";
  179. #endif
  180. OPTIONS[opt_idx++] = "listening_ports";
  181. OPTIONS[opt_idx++] = "8080r,8443s";
  182. OPTIONS[opt_idx++] = "ssl_certificate";
  183. OPTIONS[opt_idx++] = ssl_cert;
  184. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  185. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  186. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  187. memset(ports, 0, sizeof(ports));
  188. memset(ssl, 0, sizeof(ssl));
  189. memset(&callbacks, 0, sizeof(callbacks));
  190. memset(errmsg, 0, sizeof(errmsg));
  191. callbacks.log_message = log_msg_func;
  192. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  193. mg_Sleep(1);
  194. ck_assert_str_eq(errmsg, "");
  195. ck_assert(ctx != NULL);
  196. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  197. ck_assert_uint_eq(ports_cnt, 2);
  198. ck_assert_int_eq(ports[0], 8080);
  199. ck_assert_int_eq(ssl[0], 0);
  200. ck_assert_int_eq(ports[1], 8443);
  201. ck_assert_int_eq(ssl[1], 1);
  202. ck_assert_int_eq(ports[2], 0);
  203. ck_assert_int_eq(ssl[2], 0);
  204. mg_Sleep(1);
  205. mg_stop(ctx);
  206. #endif
  207. }
  208. END_TEST
  209. static struct mg_context *g_ctx;
  210. static int request_test_handler(struct mg_connection *conn, void *cbdata)
  211. {
  212. int i;
  213. char chunk_data[32];
  214. const struct mg_request_info *ri;
  215. struct mg_context *ctx;
  216. void *ud, *cud;
  217. ctx = mg_get_context(conn);
  218. ud = mg_get_user_data(ctx);
  219. ri = mg_get_request_info(conn);
  220. ck_assert(ri != NULL);
  221. ck_assert(ctx == g_ctx);
  222. ck_assert(ud == &g_ctx);
  223. mg_set_user_connection_data(conn, (void *)6543);
  224. cud = mg_get_user_connection_data(conn);
  225. ck_assert(cud == (void *)6543);
  226. ck_assert(cbdata == (void *)7);
  227. strcpy(chunk_data, "123456789A123456789B123456789C");
  228. mg_printf(conn,
  229. "HTTP/1.1 200 OK\r\n"
  230. "Transfer-Encoding: chunked\r\n"
  231. "Content-Type: text/plain\r\n\r\n");
  232. for (i = 1; i <= 10; i++) {
  233. mg_printf(conn, "%x\r\n", i);
  234. mg_write(conn, chunk_data, (unsigned)i);
  235. mg_printf(conn, "\r\n");
  236. }
  237. mg_printf(conn, "0\r\n\r\n");
  238. return 1;
  239. }
  240. START_TEST(test_request_handlers)
  241. {
  242. char ebuf[100];
  243. struct mg_context *ctx;
  244. struct mg_connection *conn;
  245. const struct mg_request_info *ri;
  246. char uri[64];
  247. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  248. const char *expected =
  249. "112123123412345123456123456712345678123456789123456789A";
  250. int i;
  251. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  252. #if defined(USE_IPV6) && defined(NO_SSL)
  253. const char *HTTP_PORT = "8084,[::]:8086";
  254. short ipv4_port = 8084;
  255. short ipv6_port = 8086;
  256. #elif !defined(USE_IPV6) && defined(NO_SSL)
  257. const char *HTTP_PORT = "8084";
  258. short ipv4_port = 8084;
  259. #elif defined(USE_IPV6) && !defined(NO_SSL)
  260. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  261. short ipv4_port = 8084;
  262. short ipv4s_port = 8094;
  263. short ipv4r_port = 8194;
  264. short ipv6_port = 8086;
  265. short ipv6s_port = 8096;
  266. short ipv6r_port = 8196;
  267. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  268. const char *HTTP_PORT = "8084,8194r,8094s";
  269. short ipv4_port = 8084;
  270. short ipv4s_port = 8094;
  271. short ipv4r_port = 8194;
  272. #endif
  273. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  274. const char *opt;
  275. FILE *f;
  276. int opt_idx = 0;
  277. const char *ssl_cert = locate_ssl_cert();
  278. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  279. OPTIONS[opt_idx++] = "listening_ports";
  280. OPTIONS[opt_idx++] = HTTP_PORT;
  281. #if !defined(NO_FILES)
  282. OPTIONS[opt_idx++] = "document_root";
  283. OPTIONS[opt_idx++] = ".";
  284. #endif
  285. #ifndef NO_SSL
  286. ck_assert(ssl_cert != NULL);
  287. OPTIONS[opt_idx++] = "ssl_certificate";
  288. OPTIONS[opt_idx++] = ssl_cert;
  289. #endif
  290. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  291. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  292. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  293. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  294. ck_assert(ctx != NULL);
  295. g_ctx = ctx;
  296. opt = mg_get_option(ctx, "listening_ports");
  297. ck_assert_str_eq(opt, HTTP_PORT);
  298. opt = mg_get_option(ctx, "cgi_environment");
  299. ck_assert_str_eq(opt, "");
  300. opt = mg_get_option(ctx, "unknown_option_name");
  301. ck_assert(opt == NULL);
  302. for (i = 0; i < 1000; i++) {
  303. sprintf(uri, "/U%u", i);
  304. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  305. }
  306. for (i = 500; i < 800; i++) {
  307. sprintf(uri, "/U%u", i);
  308. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  309. }
  310. for (i = 600; i >= 0; i--) {
  311. sprintf(uri, "/U%u", i);
  312. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  313. }
  314. for (i = 750; i <= 1000; i++) {
  315. sprintf(uri, "/U%u", i);
  316. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  317. }
  318. for (i = 5; i < 9; i++) {
  319. sprintf(uri, "/U%u", i);
  320. mg_set_request_handler(
  321. ctx, uri, request_test_handler, (void *)(ptrdiff_t)i);
  322. }
  323. /* Try to load non existing file */
  324. conn = mg_download("localhost",
  325. ipv4_port,
  326. 0,
  327. ebuf,
  328. sizeof(ebuf),
  329. "%s",
  330. "GET /file/not/found HTTP/1.0\r\n\r\n");
  331. ck_assert(conn != NULL);
  332. ri = mg_get_request_info(conn);
  333. ck_assert(ri != NULL);
  334. ck_assert_str_eq(ri->uri, "404");
  335. mg_close_connection(conn);
  336. /* Get data from callback */
  337. conn = mg_download(
  338. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  339. ck_assert(conn != NULL);
  340. ri = mg_get_request_info(conn);
  341. ck_assert(ri != NULL);
  342. ck_assert_str_eq(ri->uri, "200");
  343. i = mg_read(conn, buf, sizeof(buf));
  344. ck_assert_int_eq(i, (int)strlen(expected));
  345. buf[i] = 0;
  346. ck_assert_str_eq(buf, expected);
  347. mg_close_connection(conn);
  348. /* Get data from callback using http://127.0.0.1 */
  349. conn = mg_download(
  350. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  351. ck_assert(conn != NULL);
  352. ri = mg_get_request_info(conn);
  353. ck_assert(ri != NULL);
  354. ck_assert_str_eq(ri->uri, "200");
  355. i = mg_read(conn, buf, sizeof(buf));
  356. ck_assert_int_eq(i, (int)strlen(expected));
  357. buf[i] = 0;
  358. ck_assert_str_eq(buf, expected);
  359. mg_close_connection(conn);
  360. #if defined(USE_IPV6)
  361. /* Get data from callback using http://[::1] */
  362. conn =
  363. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  364. ck_assert(conn != NULL);
  365. ri = mg_get_request_info(conn);
  366. ck_assert(ri != NULL);
  367. ck_assert_str_eq(ri->uri, "200");
  368. i = mg_read(conn, buf, sizeof(buf));
  369. ck_assert_int_eq(i, (int)strlen(expected));
  370. buf[i] = 0;
  371. ck_assert_str_eq(buf, expected);
  372. mg_close_connection(conn);
  373. #endif
  374. #if !defined(NO_SSL)
  375. /* Get data from callback using https://127.0.0.1 */
  376. conn = mg_download(
  377. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  378. ck_assert(conn != NULL);
  379. ri = mg_get_request_info(conn);
  380. ck_assert(ri != NULL);
  381. ck_assert_str_eq(ri->uri, "200");
  382. i = mg_read(conn, buf, sizeof(buf));
  383. ck_assert_int_eq(i, (int)strlen(expected));
  384. buf[i] = 0;
  385. ck_assert_str_eq(buf, expected);
  386. mg_close_connection(conn);
  387. /* Get redirect from callback using http://127.0.0.1 */
  388. conn = mg_download(
  389. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  390. ck_assert(conn != NULL);
  391. ri = mg_get_request_info(conn);
  392. ck_assert(ri != NULL);
  393. ck_assert_str_eq(ri->uri, "302");
  394. i = mg_read(conn, buf, sizeof(buf));
  395. ck_assert_int_eq(i, -1);
  396. mg_close_connection(conn);
  397. #endif
  398. #if defined(USE_IPV6) && !defined(NO_SSL)
  399. /* Get data from callback using https://[::1] */
  400. conn =
  401. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  402. ck_assert(conn != NULL);
  403. ri = mg_get_request_info(conn);
  404. ck_assert(ri != NULL);
  405. ck_assert_str_eq(ri->uri, "200");
  406. i = mg_read(conn, buf, sizeof(buf));
  407. ck_assert_int_eq(i, (int)strlen(expected));
  408. buf[i] = 0;
  409. ck_assert_str_eq(buf, expected);
  410. mg_close_connection(conn);
  411. /* Get redirect from callback using http://127.0.0.1 */
  412. conn =
  413. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  414. ck_assert(conn != NULL);
  415. ri = mg_get_request_info(conn);
  416. ck_assert(ri != NULL);
  417. ck_assert_str_eq(ri->uri, "302");
  418. i = mg_read(conn, buf, sizeof(buf));
  419. ck_assert_int_eq(i, -1);
  420. mg_close_connection(conn);
  421. #endif
  422. /* It seems to be impossible to find out what the actual working
  423. * directory of the CI test environment is. Before breaking another
  424. * dozen of builds by trying blindly with different paths, just
  425. * create the file here */
  426. #ifdef _WIN32
  427. f = fopen("test.txt", "wb");
  428. #else
  429. f = fopen("test.txt", "w");
  430. #endif
  431. fwrite("simple text file\n", 17, 1, f);
  432. fclose(f);
  433. /* Get static data */
  434. conn = mg_download("localhost",
  435. ipv4_port,
  436. 0,
  437. ebuf,
  438. sizeof(ebuf),
  439. "%s",
  440. "GET /test.txt HTTP/1.0\r\n\r\n");
  441. ck_assert(conn != NULL);
  442. ri = mg_get_request_info(conn);
  443. ck_assert(ri != NULL);
  444. #if defined(NO_FILES)
  445. ck_assert_str_eq(ri->uri, "404");
  446. #else
  447. ck_assert_str_eq(ri->uri, "200");
  448. i = mg_read(conn, buf, sizeof(buf));
  449. ck_assert_int_eq(i, 17);
  450. if ((i >= 0) && (i < (int)sizeof(buf))) {
  451. buf[i] = 0;
  452. }
  453. ck_assert_str_eq(buf, "simple text file\n");
  454. #endif
  455. mg_close_connection(conn);
  456. /* Get directory listing */
  457. conn = mg_download("localhost",
  458. ipv4_port,
  459. 0,
  460. ebuf,
  461. sizeof(ebuf),
  462. "%s",
  463. "GET / HTTP/1.0\r\n\r\n");
  464. ck_assert(conn != NULL);
  465. ri = mg_get_request_info(conn);
  466. ck_assert(ri != NULL);
  467. #if defined(NO_FILES)
  468. ck_assert_str_eq(ri->uri, "404");
  469. #else
  470. ck_assert_str_eq(ri->uri, "200");
  471. i = mg_read(conn, buf, sizeof(buf));
  472. ck_assert(i > 6);
  473. buf[6] = 0;
  474. ck_assert_str_eq(buf, "<html>");
  475. #endif
  476. mg_close_connection(conn);
  477. /* POST to static file (will not work) */
  478. conn = mg_download("localhost",
  479. ipv4_port,
  480. 0,
  481. ebuf,
  482. sizeof(ebuf),
  483. "%s",
  484. "POST /test.txt HTTP/1.0\r\n\r\n");
  485. ck_assert(conn != NULL);
  486. ri = mg_get_request_info(conn);
  487. ck_assert(ri != NULL);
  488. #if defined(NO_FILES)
  489. ck_assert_str_eq(ri->uri, "404");
  490. #else
  491. ck_assert_str_eq(ri->uri, "405");
  492. i = mg_read(conn, buf, sizeof(buf));
  493. ck_assert(i >= 29);
  494. buf[29] = 0;
  495. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  496. #endif
  497. mg_close_connection(conn);
  498. /* PUT to static file (will not work) */
  499. conn = mg_download("localhost",
  500. ipv4_port,
  501. 0,
  502. ebuf,
  503. sizeof(ebuf),
  504. "%s",
  505. "PUT /test.txt HTTP/1.0\r\n\r\n");
  506. ck_assert(conn != NULL);
  507. ri = mg_get_request_info(conn);
  508. ck_assert(ri != NULL);
  509. #if defined(NO_FILES)
  510. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  511. #else
  512. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  513. #endif
  514. mg_close_connection(conn);
  515. /* TODO: Test websockets */
  516. /* Close the server */
  517. g_ctx = NULL;
  518. mg_stop(ctx);
  519. mg_Sleep(1);
  520. }
  521. END_TEST
  522. Suite *make_public_server_suite(void)
  523. {
  524. Suite *const suite = suite_create("PublicServer");
  525. TCase *const checktestenv = tcase_create("Check test environment");
  526. TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
  527. TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
  528. TCase *const serverrequests = tcase_create("Server Requests");
  529. tcase_add_test(checktestenv, test_the_test_environment);
  530. suite_add_tcase(suite, checktestenv);
  531. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  532. suite_add_tcase(suite, startstophttp);
  533. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  534. suite_add_tcase(suite, startstophttps);
  535. tcase_add_test(serverrequests, test_request_handlers);
  536. suite_add_tcase(suite, serverrequests);
  537. return suite;
  538. }
  539. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  540. /* Used to debug test cases without using the check framework */
  541. static int chk_ok = 0;
  542. static int chk_failed = 0;
  543. void main(void)
  544. {
  545. test_the_test_environment(0);
  546. test_mg_start_stop_http_server(0);
  547. test_mg_start_stop_https_server(0);
  548. test_request_handlers(0);
  549. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  550. }
  551. void _ck_assert_failed(const char *file, int line, const char *expr, ...)
  552. {
  553. va_list va;
  554. va_start(va, expr);
  555. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  556. vfprintf(stderr, expr, va);
  557. va_end(va);
  558. chk_failed++;
  559. }
  560. void _mark_point(const char *file, int line) { chk_ok++; }
  561. void tcase_fn_start(const char *fname, const char *file, int line) {}
  562. void suite_add_tcase(Suite *s, TCase *tc){};
  563. void _tcase_add_test(TCase *tc,
  564. TFun tf,
  565. const char *fname,
  566. int _signal,
  567. int allowed_exit_value,
  568. int start,
  569. int end){};
  570. TCase *tcase_create(const char *name) { return NULL; };
  571. Suite *suite_create(const char *name) { return NULL; };
  572. #endif