public_server.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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 test_sleep(x) (Sleep(x * 1000))
  34. #else
  35. #include <unistd.h>
  36. #define test_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. static 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. static int wait_not_null(void *volatile *data)
  65. {
  66. int i;
  67. for (i = 0; i < 100; i++) {
  68. test_sleep(1);
  69. if (*data != NULL) {
  70. return 1;
  71. }
  72. }
  73. return 0;
  74. }
  75. START_TEST(test_the_test_environment)
  76. {
  77. char wd[300];
  78. char buf[500];
  79. FILE *f;
  80. struct stat st;
  81. int ret;
  82. const char *ssl_cert = locate_ssl_cert();
  83. memset(wd, 0, sizeof(wd));
  84. memset(buf, 0, sizeof(buf));
  85. /* Get the current working directory */
  86. #ifdef _WIN32
  87. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  88. wd[sizeof(wd) - 1] = 0;
  89. #else
  90. (void)getcwd(wd, sizeof(wd));
  91. wd[sizeof(wd) - 1] = 0;
  92. #endif
  93. /* Check the pem file */
  94. #ifdef _WIN32
  95. strcpy(buf, wd);
  96. strcat(buf, "\\");
  97. strcat(buf, ssl_cert);
  98. f = fopen(buf, "rb");
  99. #else
  100. strcpy(buf, wd);
  101. strcat(buf, "/");
  102. strcat(buf, ssl_cert);
  103. f = fopen(buf, "r");
  104. #endif
  105. if (f) {
  106. fclose(f);
  107. } else {
  108. fprintf(stderr, "%s not found", buf);
  109. }
  110. /* Check the test dir */
  111. #ifdef _WIN32
  112. strcpy(buf, wd);
  113. strcat(buf, "\\test");
  114. #else
  115. strcpy(buf, wd);
  116. strcat(buf, "/test");
  117. #endif
  118. memset(&st, 0, sizeof(st));
  119. ret = stat(buf, &st);
  120. if (ret) {
  121. fprintf(stderr, "%s not found", buf);
  122. }
  123. }
  124. END_TEST
  125. static int log_msg_func(const struct mg_connection *conn, const char *message)
  126. {
  127. struct mg_context *ctx;
  128. char *ud;
  129. ck_assert(conn != NULL);
  130. ctx = mg_get_context(conn);
  131. ck_assert(ctx != NULL);
  132. ud = (char *)mg_get_user_data(ctx);
  133. strncpy(ud, message, 255);
  134. ud[255] = 0;
  135. return 1;
  136. }
  137. START_TEST(test_mg_start_stop_http_server)
  138. {
  139. struct mg_context *ctx;
  140. const char *OPTIONS[] = {
  141. #if !defined(NO_FILES)
  142. "document_root",
  143. ".",
  144. #endif
  145. "listening_ports",
  146. "8080",
  147. NULL,
  148. };
  149. size_t ports_cnt;
  150. int ports[16];
  151. int ssl[16];
  152. struct mg_callbacks callbacks;
  153. char errmsg[256];
  154. memset(ports, 0, sizeof(ports));
  155. memset(ssl, 0, sizeof(ssl));
  156. memset(&callbacks, 0, sizeof(callbacks));
  157. memset(errmsg, 0, sizeof(errmsg));
  158. callbacks.log_message = log_msg_func;
  159. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  160. test_sleep(1);
  161. ck_assert_str_eq(errmsg, "");
  162. ck_assert(ctx != NULL);
  163. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  164. ck_assert_uint_eq(ports_cnt, 1);
  165. ck_assert_int_eq(ports[0], 8080);
  166. ck_assert_int_eq(ssl[0], 0);
  167. ck_assert_int_eq(ports[1], 0);
  168. ck_assert_int_eq(ssl[1], 0);
  169. test_sleep(1);
  170. mg_stop(ctx);
  171. }
  172. END_TEST
  173. START_TEST(test_mg_start_stop_https_server)
  174. {
  175. #ifndef NO_SSL
  176. struct mg_context *ctx;
  177. size_t ports_cnt;
  178. int ports[16];
  179. int ssl[16];
  180. struct mg_callbacks callbacks;
  181. char errmsg[256];
  182. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  183. int opt_idx = 0;
  184. const char *ssl_cert = locate_ssl_cert();
  185. ck_assert(ssl_cert != NULL);
  186. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  187. #if !defined(NO_FILES)
  188. OPTIONS[opt_idx++] = "document_root";
  189. OPTIONS[opt_idx++] = ".";
  190. #endif
  191. OPTIONS[opt_idx++] = "listening_ports";
  192. OPTIONS[opt_idx++] = "8080r,8443s";
  193. OPTIONS[opt_idx++] = "ssl_certificate";
  194. OPTIONS[opt_idx++] = ssl_cert;
  195. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  196. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  197. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  198. memset(ports, 0, sizeof(ports));
  199. memset(ssl, 0, sizeof(ssl));
  200. memset(&callbacks, 0, sizeof(callbacks));
  201. memset(errmsg, 0, sizeof(errmsg));
  202. callbacks.log_message = log_msg_func;
  203. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  204. test_sleep(1);
  205. ck_assert_str_eq(errmsg, "");
  206. ck_assert(ctx != NULL);
  207. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  208. ck_assert_uint_eq(ports_cnt, 2);
  209. ck_assert_int_eq(ports[0], 8080);
  210. ck_assert_int_eq(ssl[0], 0);
  211. ck_assert_int_eq(ports[1], 8443);
  212. ck_assert_int_eq(ssl[1], 1);
  213. ck_assert_int_eq(ports[2], 0);
  214. ck_assert_int_eq(ssl[2], 0);
  215. test_sleep(1);
  216. mg_stop(ctx);
  217. #endif
  218. }
  219. END_TEST
  220. static struct mg_context *g_ctx;
  221. static int request_test_handler(struct mg_connection *conn, void *cbdata)
  222. {
  223. int i;
  224. char chunk_data[32];
  225. const struct mg_request_info *ri;
  226. struct mg_context *ctx;
  227. void *ud, *cud;
  228. ctx = mg_get_context(conn);
  229. ud = mg_get_user_data(ctx);
  230. ri = mg_get_request_info(conn);
  231. ck_assert(ri != NULL);
  232. ck_assert(ctx == g_ctx);
  233. ck_assert(ud == &g_ctx);
  234. mg_set_user_connection_data(conn, (void *)6543);
  235. cud = mg_get_user_connection_data(conn);
  236. ck_assert_ptr_eq((void *)cud, (void *)6543);
  237. ck_assert_ptr_eq((void *)cbdata, (void *)7);
  238. strcpy(chunk_data, "123456789A123456789B123456789C");
  239. mg_printf(conn,
  240. "HTTP/1.1 200 OK\r\n"
  241. "Transfer-Encoding: chunked\r\n"
  242. "Content-Type: text/plain\r\n\r\n");
  243. for (i = 1; i <= 10; i++) {
  244. mg_printf(conn, "%x\r\n", i);
  245. mg_write(conn, chunk_data, (unsigned)i);
  246. mg_printf(conn, "\r\n");
  247. }
  248. mg_printf(conn, "0\r\n\r\n");
  249. return 1;
  250. }
  251. #ifdef USE_WEBSOCKET
  252. /****************************************************************************/
  253. /* WEBSOCKET SERVER */
  254. /****************************************************************************/
  255. static const char *websocket_welcome_msg = "websocket welcome\n";
  256. static const size_t websocket_welcome_msg_len =
  257. 18 /* strlen(websocket_welcome_msg) */;
  258. static const char *websocket_acknowledge_msg = "websocket msg ok\n";
  259. static const size_t websocket_acknowledge_msg_len =
  260. 17 /* strlen(websocket_acknowledge_msg) */;
  261. static const char *websocket_goodbye_msg = "websocket bye\n";
  262. static const size_t websocket_goodbye_msg_len =
  263. 14 /* strlen(websocket_goodbye_msg) */;
  264. static int websock_server_connect(const struct mg_connection *conn, void *udata)
  265. {
  266. (void)conn;
  267. ck_assert_ptr_eq((void *)udata, (void *)7531);
  268. printf("Server: Websocket connected\n");
  269. return 0; /* return 0 to accept every connection */
  270. }
  271. static void websock_server_ready(struct mg_connection *conn, void *udata)
  272. {
  273. ck_assert_ptr_eq((void *)udata, (void *)7531);
  274. printf("Server: Websocket ready\n");
  275. /* Send websocket welcome message */
  276. mg_lock_connection(conn);
  277. mg_websocket_write(conn,
  278. WEBSOCKET_OPCODE_TEXT,
  279. websocket_welcome_msg,
  280. websocket_welcome_msg_len);
  281. mg_unlock_connection(conn);
  282. }
  283. static int websock_server_data(struct mg_connection *conn,
  284. int bits,
  285. char *data,
  286. size_t data_len,
  287. void *udata)
  288. {
  289. (void)bits;
  290. ck_assert_ptr_eq((void *)udata, (void *)7531);
  291. printf("Server: Got %u bytes from the client\n", (unsigned)data_len);
  292. if (data_len < 3 || 0 != memcmp(data, "bye", 3)) {
  293. /* Send websocket acknowledge message */
  294. mg_lock_connection(conn);
  295. mg_websocket_write(conn,
  296. WEBSOCKET_OPCODE_TEXT,
  297. websocket_acknowledge_msg,
  298. websocket_acknowledge_msg_len);
  299. mg_unlock_connection(conn);
  300. } else {
  301. /* Send websocket acknowledge message */
  302. mg_lock_connection(conn);
  303. mg_websocket_write(conn,
  304. WEBSOCKET_OPCODE_TEXT,
  305. websocket_goodbye_msg,
  306. websocket_goodbye_msg_len);
  307. mg_unlock_connection(conn);
  308. }
  309. return 1; /* return 1 to keep the connetion open */
  310. }
  311. static void websock_server_close(const struct mg_connection *conn, void *udata)
  312. {
  313. (void)conn;
  314. ck_assert_ptr_eq((void *)udata, (void *)7531);
  315. printf("Server: Close connection\n");
  316. /* Can not send a websocket goodbye message here - the connection is already
  317. * closed */
  318. }
  319. /****************************************************************************/
  320. /* WEBSOCKET CLIENT */
  321. /****************************************************************************/
  322. struct tclient_data {
  323. void *data;
  324. size_t len;
  325. int closed;
  326. };
  327. static int websocket_client_data_handler(struct mg_connection *conn,
  328. int flags,
  329. char *data,
  330. size_t data_len,
  331. void *user_data)
  332. {
  333. struct mg_context *ctx = mg_get_context(conn);
  334. struct tclient_data *pclient_data =
  335. (struct tclient_data *)mg_get_user_data(ctx);
  336. (void)user_data; /* TODO: check this */
  337. ck_assert(pclient_data != NULL);
  338. ck_assert_int_eq(flags, (int)(128 | 1));
  339. printf("Client received data from server: ");
  340. fwrite(data, 1, data_len, stdout);
  341. printf("\n");
  342. pclient_data->data = malloc(data_len);
  343. ck_assert(pclient_data->data != NULL);
  344. memcpy(pclient_data->data, data, data_len);
  345. pclient_data->len = data_len;
  346. return 1;
  347. }
  348. static void websocket_client_close_handler(const struct mg_connection *conn,
  349. void *user_data)
  350. {
  351. struct mg_context *ctx = mg_get_context(conn);
  352. struct tclient_data *pclient_data =
  353. (struct tclient_data *)mg_get_user_data(ctx);
  354. (void)user_data; /* TODO: check this */
  355. ck_assert(pclient_data != NULL);
  356. printf("Client: Close handler\n");
  357. pclient_data->closed++;
  358. }
  359. #endif
  360. START_TEST(test_request_handlers)
  361. {
  362. char ebuf[100];
  363. struct mg_context *ctx;
  364. struct mg_connection *client_conn;
  365. const struct mg_request_info *ri;
  366. char uri[64];
  367. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  368. const char *expected =
  369. "112123123412345123456123456712345678123456789123456789A";
  370. int i;
  371. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  372. #if defined(USE_IPV6) && defined(NO_SSL)
  373. const char *HTTP_PORT = "8084,[::]:8086";
  374. short ipv4_port = 8084;
  375. short ipv6_port = 8086;
  376. #elif !defined(USE_IPV6) && defined(NO_SSL)
  377. const char *HTTP_PORT = "8084";
  378. short ipv4_port = 8084;
  379. #elif defined(USE_IPV6) && !defined(NO_SSL)
  380. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  381. short ipv4_port = 8084;
  382. short ipv4s_port = 8094;
  383. short ipv4r_port = 8194;
  384. short ipv6_port = 8086;
  385. short ipv6s_port = 8096;
  386. short ipv6r_port = 8196;
  387. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  388. const char *HTTP_PORT = "8084,8194r,8094s";
  389. short ipv4_port = 8084;
  390. short ipv4s_port = 8094;
  391. short ipv4r_port = 8194;
  392. #endif
  393. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  394. const char *opt;
  395. FILE *f;
  396. int opt_idx = 0;
  397. const char *ssl_cert = locate_ssl_cert();
  398. #ifdef USE_WEBSOCKET
  399. struct tclient_data ws_client1_data = {NULL, 0, 0};
  400. struct tclient_data ws_client2_data = {NULL, 0, 0};
  401. struct tclient_data ws_client3_data = {NULL, 0, 0};
  402. struct mg_connection *ws_client1_conn = NULL;
  403. struct mg_connection *ws_client2_conn = NULL;
  404. struct mg_connection *ws_client3_conn = NULL;
  405. #endif
  406. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  407. OPTIONS[opt_idx++] = "listening_ports";
  408. OPTIONS[opt_idx++] = HTTP_PORT;
  409. #if !defined(NO_FILES)
  410. OPTIONS[opt_idx++] = "document_root";
  411. OPTIONS[opt_idx++] = ".";
  412. #endif
  413. #ifndef NO_SSL
  414. ck_assert(ssl_cert != NULL);
  415. OPTIONS[opt_idx++] = "ssl_certificate";
  416. OPTIONS[opt_idx++] = ssl_cert;
  417. #endif
  418. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  419. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  420. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  421. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  422. ck_assert(ctx != NULL);
  423. g_ctx = ctx;
  424. opt = mg_get_option(ctx, "listening_ports");
  425. ck_assert_str_eq(opt, HTTP_PORT);
  426. opt = mg_get_option(ctx, "cgi_environment");
  427. ck_assert_str_eq(opt, "");
  428. opt = mg_get_option(ctx, "unknown_option_name");
  429. ck_assert(opt == NULL);
  430. for (i = 0; i < 1000; i++) {
  431. sprintf(uri, "/U%u", i);
  432. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  433. }
  434. for (i = 500; i < 800; i++) {
  435. sprintf(uri, "/U%u", i);
  436. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  437. }
  438. for (i = 600; i >= 0; i--) {
  439. sprintf(uri, "/U%u", i);
  440. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  441. }
  442. for (i = 750; i <= 1000; i++) {
  443. sprintf(uri, "/U%u", i);
  444. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  445. }
  446. for (i = 5; i < 9; i++) {
  447. sprintf(uri, "/U%u", i);
  448. mg_set_request_handler(
  449. ctx, uri, request_test_handler, (void *)(ptrdiff_t)i);
  450. }
  451. #ifdef USE_WEBSOCKET
  452. mg_set_websocket_handler(ctx,
  453. "/websocket",
  454. websock_server_connect,
  455. websock_server_ready,
  456. websock_server_data,
  457. websock_server_close,
  458. (void *)7531);
  459. #endif
  460. /* Try to load non existing file */
  461. client_conn = mg_download("localhost",
  462. ipv4_port,
  463. 0,
  464. ebuf,
  465. sizeof(ebuf),
  466. "%s",
  467. "GET /file/not/found HTTP/1.0\r\n\r\n");
  468. ck_assert(client_conn != NULL);
  469. ri = mg_get_request_info(client_conn);
  470. ck_assert(ri != NULL);
  471. ck_assert_str_eq(ri->uri, "404");
  472. mg_close_connection(client_conn);
  473. /* Get data from callback */
  474. client_conn = mg_download(
  475. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  476. ck_assert(client_conn != NULL);
  477. ri = mg_get_request_info(client_conn);
  478. ck_assert(ri != NULL);
  479. ck_assert_str_eq(ri->uri, "200");
  480. i = mg_read(client_conn, buf, sizeof(buf));
  481. ck_assert_int_eq(i, (int)strlen(expected));
  482. buf[i] = 0;
  483. ck_assert_str_eq(buf, expected);
  484. mg_close_connection(client_conn);
  485. /* Get data from callback using http://127.0.0.1 */
  486. client_conn = mg_download(
  487. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  488. ck_assert(client_conn != NULL);
  489. ri = mg_get_request_info(client_conn);
  490. ck_assert(ri != NULL);
  491. ck_assert_str_eq(ri->uri, "200");
  492. i = mg_read(client_conn, buf, sizeof(buf));
  493. ck_assert_int_eq(i, (int)strlen(expected));
  494. buf[i] = 0;
  495. ck_assert_str_eq(buf, expected);
  496. mg_close_connection(client_conn);
  497. #if defined(USE_IPV6)
  498. /* Get data from callback using http://[::1] */
  499. client_conn =
  500. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  501. ck_assert(client_conn != NULL);
  502. ri = mg_get_request_info(client_conn);
  503. ck_assert(ri != NULL);
  504. ck_assert_str_eq(ri->uri, "200");
  505. i = mg_read(client_conn, buf, sizeof(buf));
  506. ck_assert_int_eq(i, (int)strlen(expected));
  507. buf[i] = 0;
  508. ck_assert_str_eq(buf, expected);
  509. mg_close_connection(client_conn);
  510. #endif
  511. #if !defined(NO_SSL)
  512. /* Get data from callback using https://127.0.0.1 */
  513. client_conn = mg_download(
  514. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  515. ck_assert(client_conn != NULL);
  516. ri = mg_get_request_info(client_conn);
  517. ck_assert(ri != NULL);
  518. ck_assert_str_eq(ri->uri, "200");
  519. i = mg_read(client_conn, buf, sizeof(buf));
  520. ck_assert_int_eq(i, (int)strlen(expected));
  521. buf[i] = 0;
  522. ck_assert_str_eq(buf, expected);
  523. mg_close_connection(client_conn);
  524. /* Get redirect from callback using http://127.0.0.1 */
  525. client_conn = mg_download(
  526. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  527. ck_assert(client_conn != NULL);
  528. ri = mg_get_request_info(client_conn);
  529. ck_assert(ri != NULL);
  530. ck_assert_str_eq(ri->uri, "302");
  531. i = mg_read(client_conn, buf, sizeof(buf));
  532. ck_assert_int_eq(i, -1);
  533. mg_close_connection(client_conn);
  534. #endif
  535. #if defined(USE_IPV6) && !defined(NO_SSL)
  536. /* Get data from callback using https://[::1] */
  537. client_conn =
  538. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  539. ck_assert(client_conn != NULL);
  540. ri = mg_get_request_info(client_conn);
  541. ck_assert(ri != NULL);
  542. ck_assert_str_eq(ri->uri, "200");
  543. i = mg_read(client_conn, buf, sizeof(buf));
  544. ck_assert_int_eq(i, (int)strlen(expected));
  545. buf[i] = 0;
  546. ck_assert_str_eq(buf, expected);
  547. mg_close_connection(client_conn);
  548. /* Get redirect from callback using http://127.0.0.1 */
  549. client_conn =
  550. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  551. ck_assert(client_conn != NULL);
  552. ri = mg_get_request_info(client_conn);
  553. ck_assert(ri != NULL);
  554. ck_assert_str_eq(ri->uri, "302");
  555. i = mg_read(client_conn, buf, sizeof(buf));
  556. ck_assert_int_eq(i, -1);
  557. mg_close_connection(client_conn);
  558. #endif
  559. /* It seems to be impossible to find out what the actual working
  560. * directory of the CI test environment is. Before breaking another
  561. * dozen of builds by trying blindly with different paths, just
  562. * create the file here */
  563. #ifdef _WIN32
  564. f = fopen("test.txt", "wb");
  565. #else
  566. f = fopen("test.txt", "w");
  567. #endif
  568. fwrite("simple text file\n", 17, 1, f);
  569. fclose(f);
  570. /* Get static data */
  571. client_conn = mg_download("localhost",
  572. ipv4_port,
  573. 0,
  574. ebuf,
  575. sizeof(ebuf),
  576. "%s",
  577. "GET /test.txt HTTP/1.0\r\n\r\n");
  578. ck_assert(client_conn != NULL);
  579. ri = mg_get_request_info(client_conn);
  580. ck_assert(ri != NULL);
  581. #if defined(NO_FILES)
  582. ck_assert_str_eq(ri->uri, "404");
  583. #else
  584. ck_assert_str_eq(ri->uri, "200");
  585. i = mg_read(client_conn, buf, sizeof(buf));
  586. ck_assert_int_eq(i, 17);
  587. if ((i >= 0) && (i < (int)sizeof(buf))) {
  588. buf[i] = 0;
  589. }
  590. ck_assert_str_eq(buf, "simple text file\n");
  591. #endif
  592. mg_close_connection(client_conn);
  593. /* Get directory listing */
  594. client_conn = mg_download("localhost",
  595. ipv4_port,
  596. 0,
  597. ebuf,
  598. sizeof(ebuf),
  599. "%s",
  600. "GET / HTTP/1.0\r\n\r\n");
  601. ck_assert(client_conn != NULL);
  602. ri = mg_get_request_info(client_conn);
  603. ck_assert(ri != NULL);
  604. #if defined(NO_FILES)
  605. ck_assert_str_eq(ri->uri, "404");
  606. #else
  607. ck_assert_str_eq(ri->uri, "200");
  608. i = mg_read(client_conn, buf, sizeof(buf));
  609. ck_assert(i > 6);
  610. buf[6] = 0;
  611. ck_assert_str_eq(buf, "<html>");
  612. #endif
  613. mg_close_connection(client_conn);
  614. /* POST to static file (will not work) */
  615. client_conn = mg_download("localhost",
  616. ipv4_port,
  617. 0,
  618. ebuf,
  619. sizeof(ebuf),
  620. "%s",
  621. "POST /test.txt HTTP/1.0\r\n\r\n");
  622. ck_assert(client_conn != NULL);
  623. ri = mg_get_request_info(client_conn);
  624. ck_assert(ri != NULL);
  625. #if defined(NO_FILES)
  626. ck_assert_str_eq(ri->uri, "404");
  627. #else
  628. ck_assert_str_eq(ri->uri, "405");
  629. i = mg_read(client_conn, buf, sizeof(buf));
  630. ck_assert(i >= 29);
  631. buf[29] = 0;
  632. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  633. #endif
  634. mg_close_connection(client_conn);
  635. /* PUT to static file (will not work) */
  636. client_conn = mg_download("localhost",
  637. ipv4_port,
  638. 0,
  639. ebuf,
  640. sizeof(ebuf),
  641. "%s",
  642. "PUT /test.txt HTTP/1.0\r\n\r\n");
  643. ck_assert(client_conn != NULL);
  644. ri = mg_get_request_info(client_conn);
  645. ck_assert(ri != NULL);
  646. #if defined(NO_FILES)
  647. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  648. #else
  649. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  650. #endif
  651. mg_close_connection(client_conn);
  652. /* Websocket test */
  653. #ifdef USE_WEBSOCKET
  654. /* Then connect a first client */
  655. ws_client1_conn =
  656. mg_connect_websocket_client("localhost",
  657. ipv4_port,
  658. 0,
  659. ebuf,
  660. sizeof(ebuf),
  661. "/websocket",
  662. NULL,
  663. websocket_client_data_handler,
  664. websocket_client_close_handler,
  665. &ws_client1_data);
  666. ck_assert(ws_client1_conn != NULL);
  667. wait_not_null(
  668. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  669. ck_assert_int_eq(ws_client1_data.closed, 0);
  670. ck_assert_int_eq(ws_client2_data.closed, 0);
  671. ck_assert_int_eq(ws_client3_data.closed, 0);
  672. ck_assert(ws_client2_data.data == NULL);
  673. ck_assert_uint_eq(ws_client2_data.len, 0);
  674. ck_assert(ws_client1_data.data != NULL);
  675. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  676. ck_assert(!memcmp(ws_client1_data.data,
  677. websocket_welcome_msg,
  678. websocket_welcome_msg_len));
  679. free(ws_client1_data.data);
  680. ws_client1_data.data = NULL;
  681. ws_client1_data.len = 0;
  682. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data1", 5);
  683. wait_not_null(
  684. &(ws_client1_data
  685. .data)); /* Wait for the websocket acknowledge message */
  686. ck_assert_int_eq(ws_client1_data.closed, 0);
  687. ck_assert_int_eq(ws_client2_data.closed, 0);
  688. ck_assert(ws_client2_data.data == NULL);
  689. ck_assert_uint_eq(ws_client2_data.len, 0);
  690. ck_assert(ws_client1_data.data != NULL);
  691. ck_assert_uint_eq(ws_client1_data.len, websocket_acknowledge_msg_len);
  692. ck_assert(!memcmp(ws_client1_data.data,
  693. websocket_acknowledge_msg,
  694. websocket_acknowledge_msg_len));
  695. free(ws_client1_data.data);
  696. ws_client1_data.data = NULL;
  697. ws_client1_data.len = 0;
  698. /* Now connect a second client */
  699. #ifdef USE_IPV6
  700. ws_client2_conn =
  701. mg_connect_websocket_client("[::1]",
  702. ipv6_port,
  703. 0,
  704. ebuf,
  705. sizeof(ebuf),
  706. "/websocket",
  707. NULL,
  708. websocket_client_data_handler,
  709. websocket_client_close_handler,
  710. &ws_client2_data);
  711. #else
  712. ws_client2_conn =
  713. mg_connect_websocket_client("127.0.0.1",
  714. ipv4_port,
  715. 0,
  716. ebuf,
  717. sizeof(ebuf),
  718. "/websocket",
  719. NULL,
  720. websocket_client_data_handler,
  721. websocket_client_close_handler,
  722. &ws_client2_data);
  723. #endif
  724. ck_assert(ws_client2_conn != NULL);
  725. wait_not_null(
  726. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  727. ck_assert(ws_client1_data.closed == 0);
  728. ck_assert(ws_client2_data.closed == 0);
  729. ck_assert(ws_client1_data.data == NULL);
  730. ck_assert(ws_client1_data.len == 0);
  731. ck_assert(ws_client2_data.data != NULL);
  732. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  733. ck_assert(!memcmp(ws_client2_data.data,
  734. websocket_welcome_msg,
  735. websocket_welcome_msg_len));
  736. free(ws_client2_data.data);
  737. ws_client2_data.data = NULL;
  738. ws_client2_data.len = 0;
  739. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data2", 5);
  740. wait_not_null(
  741. &(ws_client1_data
  742. .data)); /* Wait for the websocket acknowledge message */
  743. ck_assert(ws_client1_data.closed == 0);
  744. ck_assert(ws_client2_data.closed == 0);
  745. ck_assert(ws_client2_data.data == NULL);
  746. ck_assert(ws_client2_data.len == 0);
  747. ck_assert(ws_client1_data.data != NULL);
  748. ck_assert(ws_client1_data.len == websocket_acknowledge_msg_len);
  749. ck_assert(!memcmp(ws_client1_data.data,
  750. websocket_acknowledge_msg,
  751. websocket_acknowledge_msg_len));
  752. free(ws_client1_data.data);
  753. ws_client1_data.data = NULL;
  754. ws_client1_data.len = 0;
  755. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  756. wait_not_null(
  757. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  758. ck_assert(ws_client1_data.closed == 0);
  759. ck_assert(ws_client2_data.closed == 0);
  760. ck_assert(ws_client2_data.data == NULL);
  761. ck_assert(ws_client2_data.len == 0);
  762. ck_assert(ws_client1_data.data != NULL);
  763. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  764. ck_assert(!memcmp(ws_client1_data.data,
  765. websocket_goodbye_msg,
  766. websocket_goodbye_msg_len));
  767. free(ws_client1_data.data);
  768. ws_client1_data.data = NULL;
  769. ws_client1_data.len = 0;
  770. mg_close_connection(ws_client1_conn);
  771. test_sleep(3); /* Won't get any message */
  772. ck_assert(ws_client1_data.closed == 1);
  773. ck_assert(ws_client2_data.closed == 0);
  774. ck_assert(ws_client1_data.data == NULL);
  775. ck_assert(ws_client1_data.len == 0);
  776. ck_assert(ws_client2_data.data == NULL);
  777. ck_assert(ws_client2_data.len == 0);
  778. mg_websocket_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  779. wait_not_null(
  780. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  781. ck_assert(ws_client1_data.closed == 1);
  782. ck_assert(ws_client2_data.closed == 0);
  783. ck_assert(ws_client1_data.data == NULL);
  784. ck_assert(ws_client1_data.len == 0);
  785. ck_assert(ws_client2_data.data != NULL);
  786. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  787. ck_assert(!memcmp(ws_client2_data.data,
  788. websocket_goodbye_msg,
  789. websocket_goodbye_msg_len));
  790. free(ws_client2_data.data);
  791. ws_client2_data.data = NULL;
  792. ws_client2_data.len = 0;
  793. mg_close_connection(ws_client2_conn);
  794. test_sleep(3); /* Won't get any message */
  795. ck_assert(ws_client1_data.closed == 1);
  796. ck_assert(ws_client2_data.closed == 1);
  797. ck_assert(ws_client1_data.data == NULL);
  798. ck_assert(ws_client1_data.len == 0);
  799. ck_assert(ws_client2_data.data == NULL);
  800. ck_assert(ws_client2_data.len == 0);
  801. /* Connect client 3 */
  802. ws_client3_conn =
  803. mg_connect_websocket_client("localhost",
  804. ipv4_port,
  805. 0,
  806. ebuf,
  807. sizeof(ebuf),
  808. "/websocket",
  809. NULL,
  810. websocket_client_data_handler,
  811. websocket_client_close_handler,
  812. &ws_client3_data);
  813. ck_assert(ws_client3_conn != NULL);
  814. wait_not_null(
  815. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  816. ck_assert(ws_client1_data.closed == 1);
  817. ck_assert(ws_client2_data.closed == 1);
  818. ck_assert(ws_client3_data.closed == 0);
  819. ck_assert(ws_client1_data.data == NULL);
  820. ck_assert(ws_client1_data.len == 0);
  821. ck_assert(ws_client2_data.data == NULL);
  822. ck_assert(ws_client2_data.len == 0);
  823. ck_assert(ws_client3_data.data != NULL);
  824. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  825. ck_assert(!memcmp(ws_client3_data.data,
  826. websocket_welcome_msg,
  827. websocket_welcome_msg_len));
  828. free(ws_client3_data.data);
  829. ws_client3_data.data = NULL;
  830. ws_client3_data.len = 0;
  831. #endif
  832. /* Close the server */
  833. g_ctx = NULL;
  834. mg_stop(ctx);
  835. for (i = 0; i < 100; i++) {
  836. test_sleep(1);
  837. if (ws_client3_data.closed != 0) {
  838. break;
  839. }
  840. }
  841. ck_assert_int_eq(ws_client3_data.closed, 1);
  842. }
  843. END_TEST
  844. Suite *make_public_server_suite(void)
  845. {
  846. Suite *const suite = suite_create("PublicServer");
  847. TCase *const checktestenv = tcase_create("Check test environment");
  848. TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
  849. TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
  850. TCase *const serverrequests = tcase_create("Server Requests");
  851. tcase_add_test(checktestenv, test_the_test_environment);
  852. suite_add_tcase(suite, checktestenv);
  853. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  854. suite_add_tcase(suite, startstophttp);
  855. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  856. suite_add_tcase(suite, startstophttps);
  857. tcase_add_test(serverrequests, test_request_handlers);
  858. suite_add_tcase(suite, serverrequests);
  859. return suite;
  860. }
  861. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  862. /* Used to debug test cases without using the check framework */
  863. static int chk_ok = 0;
  864. static int chk_failed = 0;
  865. void main(void)
  866. {
  867. test_the_test_environment(0);
  868. test_mg_start_stop_http_server(0);
  869. test_mg_start_stop_https_server(0);
  870. test_request_handlers(0);
  871. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  872. }
  873. void _ck_assert_failed(const char *file, int line, const char *expr, ...)
  874. {
  875. va_list va;
  876. va_start(va, expr);
  877. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  878. vfprintf(stderr, expr, va);
  879. fprintf(stderr, "\n\n");
  880. va_end(va);
  881. chk_failed++;
  882. }
  883. void _mark_point(const char *file, int line) { chk_ok++; }
  884. void tcase_fn_start(const char *fname, const char *file, int line) {}
  885. void suite_add_tcase(Suite *s, TCase *tc){};
  886. void _tcase_add_test(TCase *tc,
  887. TFun tf,
  888. const char *fname,
  889. int _signal,
  890. int allowed_exit_value,
  891. int start,
  892. int end){};
  893. TCase *tcase_create(const char *name) { return NULL; };
  894. Suite *suite_create(const char *name) { return NULL; };
  895. #endif