public_server.c 31 KB

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