public_server.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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_int_eq((int)cud, (int)6543);
  226. ck_assert_int_eq((int)cbdata, (int)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. #ifdef USE_WEBSOCKET
  241. /****************************************************************************/
  242. /* WEBSOCKET SERVER */
  243. /****************************************************************************/
  244. const char *websocket_welcome_msg = "websocket welcome\n";
  245. const size_t websocket_welcome_msg_len = 18 /* strlen(websocket_welcome_msg) */;
  246. const char *websocket_acknowledge_msg = "websocket msg ok\n";
  247. const size_t websocket_acknowledge_msg_len =
  248. 17 /* strlen(websocket_acknowledge_msg) */;
  249. const char *websocket_goodbye_msg = "websocket bye\n";
  250. const size_t websocket_goodbye_msg_len = 14 /* strlen(websocket_goodbye_msg) */;
  251. int websock_server_connect(const struct mg_connection *conn, void *udata)
  252. {
  253. ck_assert_int_eq((int)udata, 7531);
  254. printf("Server: Websocket connected\n");
  255. return 0; /* return 0 to accept every connection */
  256. }
  257. void websock_server_ready(struct mg_connection *conn, void *udata)
  258. {
  259. ck_assert_int_eq((int)udata, 7531);
  260. printf("Server: Websocket ready\n");
  261. /* Send websocket welcome message */
  262. mg_lock_connection(conn);
  263. mg_websocket_write(conn,
  264. WEBSOCKET_OPCODE_TEXT,
  265. websocket_welcome_msg,
  266. websocket_welcome_msg_len);
  267. mg_unlock_connection(conn);
  268. }
  269. int websock_server_data(struct mg_connection *conn,
  270. int bits,
  271. char *data,
  272. size_t data_len,
  273. void *udata)
  274. {
  275. ck_assert_int_eq((int)udata, 7531);
  276. printf("Server: Got %u bytes from the client\n", data_len);
  277. if (data_len < 3 || 0 != memcmp(data, "bye", 3)) {
  278. /* Send websocket acknowledge message */
  279. mg_lock_connection(conn);
  280. mg_websocket_write(conn,
  281. WEBSOCKET_OPCODE_TEXT,
  282. websocket_acknowledge_msg,
  283. websocket_acknowledge_msg_len);
  284. mg_unlock_connection(conn);
  285. } else {
  286. /* Send websocket acknowledge message */
  287. mg_lock_connection(conn);
  288. mg_websocket_write(conn,
  289. WEBSOCKET_OPCODE_TEXT,
  290. websocket_goodbye_msg,
  291. websocket_goodbye_msg_len);
  292. mg_unlock_connection(conn);
  293. }
  294. return 1; /* return 1 to keep the connetion open */
  295. }
  296. void websock_server_close(const struct mg_connection *conn, void *udata)
  297. {
  298. ck_assert_int_eq((int)udata, 7531);
  299. printf("Server: Close connection\n");
  300. /* Can not send a websocket goodbye message here - the connection is already
  301. * closed */
  302. }
  303. /****************************************************************************/
  304. /* WEBSOCKET CLIENT */
  305. /****************************************************************************/
  306. struct tclient_data {
  307. void *data;
  308. size_t len;
  309. int closed;
  310. };
  311. static int websocket_client_data_handler(struct mg_connection *conn,
  312. int flags,
  313. char *data,
  314. size_t data_len,
  315. void *user_data)
  316. {
  317. struct mg_context *ctx = mg_get_context(conn);
  318. struct tclient_data *pclient_data =
  319. (struct tclient_data *)mg_get_user_data(ctx);
  320. ck_assert(pclient_data != NULL);
  321. printf("Client received data from server: ");
  322. fwrite(data, 1, data_len, stdout);
  323. printf("\n");
  324. pclient_data->data = malloc(data_len);
  325. ck_assert(pclient_data->data != NULL);
  326. memcpy(pclient_data->data, data, data_len);
  327. pclient_data->len = data_len;
  328. return 1;
  329. }
  330. static void websocket_client_close_handler(const struct mg_connection *conn,
  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. ck_assert(pclient_data != NULL);
  337. printf("Client: Close handler\n");
  338. pclient_data->closed++;
  339. }
  340. #endif
  341. START_TEST(test_request_handlers)
  342. {
  343. char ebuf[100];
  344. struct mg_context *ctx;
  345. struct mg_connection *client_conn;
  346. const struct mg_request_info *ri;
  347. char uri[64];
  348. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  349. const char *expected =
  350. "112123123412345123456123456712345678123456789123456789A";
  351. int i;
  352. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  353. #if defined(USE_IPV6) && defined(NO_SSL)
  354. const char *HTTP_PORT = "8084,[::]:8086";
  355. short ipv4_port = 8084;
  356. short ipv6_port = 8086;
  357. #elif !defined(USE_IPV6) && defined(NO_SSL)
  358. const char *HTTP_PORT = "8084";
  359. short ipv4_port = 8084;
  360. #elif defined(USE_IPV6) && !defined(NO_SSL)
  361. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  362. short ipv4_port = 8084;
  363. short ipv4s_port = 8094;
  364. short ipv4r_port = 8194;
  365. short ipv6_port = 8086;
  366. short ipv6s_port = 8096;
  367. short ipv6r_port = 8196;
  368. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  369. const char *HTTP_PORT = "8084,8194r,8094s";
  370. short ipv4_port = 8084;
  371. short ipv4s_port = 8094;
  372. short ipv4r_port = 8194;
  373. #endif
  374. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  375. const char *opt;
  376. FILE *f;
  377. int opt_idx = 0;
  378. const char *ssl_cert = locate_ssl_cert();
  379. #ifdef USE_WEBSOCKET
  380. struct tclient_data ws_client1_data = {NULL, 0, 0};
  381. struct tclient_data ws_client2_data = {NULL, 0, 0};
  382. struct tclient_data ws_client3_data = {NULL, 0, 0};
  383. struct mg_connection *ws_client1_conn = NULL;
  384. struct mg_connection *ws_client2_conn = NULL;
  385. struct mg_connection *ws_client3_conn = NULL;
  386. #endif
  387. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  388. OPTIONS[opt_idx++] = "listening_ports";
  389. OPTIONS[opt_idx++] = HTTP_PORT;
  390. #if !defined(NO_FILES)
  391. OPTIONS[opt_idx++] = "document_root";
  392. OPTIONS[opt_idx++] = ".";
  393. #endif
  394. #ifndef NO_SSL
  395. ck_assert(ssl_cert != NULL);
  396. OPTIONS[opt_idx++] = "ssl_certificate";
  397. OPTIONS[opt_idx++] = ssl_cert;
  398. #endif
  399. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  400. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  401. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  402. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  403. ck_assert(ctx != NULL);
  404. g_ctx = ctx;
  405. opt = mg_get_option(ctx, "listening_ports");
  406. ck_assert_str_eq(opt, HTTP_PORT);
  407. opt = mg_get_option(ctx, "cgi_environment");
  408. ck_assert_str_eq(opt, "");
  409. opt = mg_get_option(ctx, "unknown_option_name");
  410. ck_assert(opt == NULL);
  411. for (i = 0; i < 1000; i++) {
  412. sprintf(uri, "/U%u", i);
  413. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  414. }
  415. for (i = 500; i < 800; i++) {
  416. sprintf(uri, "/U%u", i);
  417. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  418. }
  419. for (i = 600; i >= 0; i--) {
  420. sprintf(uri, "/U%u", i);
  421. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  422. }
  423. for (i = 750; i <= 1000; i++) {
  424. sprintf(uri, "/U%u", i);
  425. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  426. }
  427. for (i = 5; i < 9; i++) {
  428. sprintf(uri, "/U%u", i);
  429. mg_set_request_handler(
  430. ctx, uri, request_test_handler, (void *)(ptrdiff_t)i);
  431. }
  432. #ifdef USE_WEBSOCKET
  433. mg_set_websocket_handler(ctx,
  434. "/websocket",
  435. websock_server_connect,
  436. websock_server_ready,
  437. websock_server_data,
  438. websock_server_close,
  439. (void *)7531);
  440. #endif
  441. /* Try to load non existing file */
  442. client_conn = mg_download("localhost",
  443. ipv4_port,
  444. 0,
  445. ebuf,
  446. sizeof(ebuf),
  447. "%s",
  448. "GET /file/not/found HTTP/1.0\r\n\r\n");
  449. ck_assert(client_conn != NULL);
  450. ri = mg_get_request_info(client_conn);
  451. ck_assert(ri != NULL);
  452. ck_assert_str_eq(ri->uri, "404");
  453. mg_close_connection(client_conn);
  454. /* Get data from callback */
  455. client_conn = mg_download(
  456. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  457. ck_assert(client_conn != NULL);
  458. ri = mg_get_request_info(client_conn);
  459. ck_assert(ri != NULL);
  460. ck_assert_str_eq(ri->uri, "200");
  461. i = mg_read(client_conn, buf, sizeof(buf));
  462. ck_assert_int_eq(i, (int)strlen(expected));
  463. buf[i] = 0;
  464. ck_assert_str_eq(buf, expected);
  465. mg_close_connection(client_conn);
  466. /* Get data from callback using http://127.0.0.1 */
  467. client_conn = mg_download(
  468. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  469. ck_assert(client_conn != NULL);
  470. ri = mg_get_request_info(client_conn);
  471. ck_assert(ri != NULL);
  472. ck_assert_str_eq(ri->uri, "200");
  473. i = mg_read(client_conn, buf, sizeof(buf));
  474. ck_assert_int_eq(i, (int)strlen(expected));
  475. buf[i] = 0;
  476. ck_assert_str_eq(buf, expected);
  477. mg_close_connection(client_conn);
  478. #if defined(USE_IPV6)
  479. /* Get data from callback using http://[::1] */
  480. client_conn =
  481. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  482. ck_assert(client_conn != NULL);
  483. ri = mg_get_request_info(client_conn);
  484. ck_assert(ri != NULL);
  485. ck_assert_str_eq(ri->uri, "200");
  486. i = mg_read(client_conn, buf, sizeof(buf));
  487. ck_assert_int_eq(i, (int)strlen(expected));
  488. buf[i] = 0;
  489. ck_assert_str_eq(buf, expected);
  490. mg_close_connection(client_conn);
  491. #endif
  492. #if !defined(NO_SSL)
  493. /* Get data from callback using https://127.0.0.1 */
  494. client_conn = mg_download(
  495. "127.0.0.1", ipv4s_port, 1, 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. /* Get redirect from callback using http://127.0.0.1 */
  506. client_conn = mg_download(
  507. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  508. ck_assert(client_conn != NULL);
  509. ri = mg_get_request_info(client_conn);
  510. ck_assert(ri != NULL);
  511. ck_assert_str_eq(ri->uri, "302");
  512. i = mg_read(client_conn, buf, sizeof(buf));
  513. ck_assert_int_eq(i, -1);
  514. mg_close_connection(client_conn);
  515. #endif
  516. #if defined(USE_IPV6) && !defined(NO_SSL)
  517. /* Get data from callback using https://[::1] */
  518. client_conn =
  519. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  520. ck_assert(client_conn != NULL);
  521. ri = mg_get_request_info(client_conn);
  522. ck_assert(ri != NULL);
  523. ck_assert_str_eq(ri->uri, "200");
  524. i = mg_read(client_conn, buf, sizeof(buf));
  525. ck_assert_int_eq(i, (int)strlen(expected));
  526. buf[i] = 0;
  527. ck_assert_str_eq(buf, expected);
  528. mg_close_connection(client_conn);
  529. /* Get redirect from callback using http://127.0.0.1 */
  530. client_conn =
  531. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  532. ck_assert(client_conn != NULL);
  533. ri = mg_get_request_info(client_conn);
  534. ck_assert(ri != NULL);
  535. ck_assert_str_eq(ri->uri, "302");
  536. i = mg_read(client_conn, buf, sizeof(buf));
  537. ck_assert_int_eq(i, -1);
  538. mg_close_connection(client_conn);
  539. #endif
  540. /* It seems to be impossible to find out what the actual working
  541. * directory of the CI test environment is. Before breaking another
  542. * dozen of builds by trying blindly with different paths, just
  543. * create the file here */
  544. #ifdef _WIN32
  545. f = fopen("test.txt", "wb");
  546. #else
  547. f = fopen("test.txt", "w");
  548. #endif
  549. fwrite("simple text file\n", 17, 1, f);
  550. fclose(f);
  551. /* Get static data */
  552. client_conn = mg_download("localhost",
  553. ipv4_port,
  554. 0,
  555. ebuf,
  556. sizeof(ebuf),
  557. "%s",
  558. "GET /test.txt HTTP/1.0\r\n\r\n");
  559. ck_assert(client_conn != NULL);
  560. ri = mg_get_request_info(client_conn);
  561. ck_assert(ri != NULL);
  562. #if defined(NO_FILES)
  563. ck_assert_str_eq(ri->uri, "404");
  564. #else
  565. ck_assert_str_eq(ri->uri, "200");
  566. i = mg_read(client_conn, buf, sizeof(buf));
  567. ck_assert_int_eq(i, 17);
  568. if ((i >= 0) && (i < (int)sizeof(buf))) {
  569. buf[i] = 0;
  570. }
  571. ck_assert_str_eq(buf, "simple text file\n");
  572. #endif
  573. mg_close_connection(client_conn);
  574. /* Get directory listing */
  575. client_conn = mg_download("localhost",
  576. ipv4_port,
  577. 0,
  578. ebuf,
  579. sizeof(ebuf),
  580. "%s",
  581. "GET / HTTP/1.0\r\n\r\n");
  582. ck_assert(client_conn != NULL);
  583. ri = mg_get_request_info(client_conn);
  584. ck_assert(ri != NULL);
  585. #if defined(NO_FILES)
  586. ck_assert_str_eq(ri->uri, "404");
  587. #else
  588. ck_assert_str_eq(ri->uri, "200");
  589. i = mg_read(client_conn, buf, sizeof(buf));
  590. ck_assert(i > 6);
  591. buf[6] = 0;
  592. ck_assert_str_eq(buf, "<html>");
  593. #endif
  594. mg_close_connection(client_conn);
  595. /* POST to static file (will not work) */
  596. client_conn = mg_download("localhost",
  597. ipv4_port,
  598. 0,
  599. ebuf,
  600. sizeof(ebuf),
  601. "%s",
  602. "POST /test.txt HTTP/1.0\r\n\r\n");
  603. ck_assert(client_conn != NULL);
  604. ri = mg_get_request_info(client_conn);
  605. ck_assert(ri != NULL);
  606. #if defined(NO_FILES)
  607. ck_assert_str_eq(ri->uri, "404");
  608. #else
  609. ck_assert_str_eq(ri->uri, "405");
  610. i = mg_read(client_conn, buf, sizeof(buf));
  611. ck_assert(i >= 29);
  612. buf[29] = 0;
  613. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  614. #endif
  615. mg_close_connection(client_conn);
  616. /* PUT to static file (will not work) */
  617. client_conn = mg_download("localhost",
  618. ipv4_port,
  619. 0,
  620. ebuf,
  621. sizeof(ebuf),
  622. "%s",
  623. "PUT /test.txt HTTP/1.0\r\n\r\n");
  624. ck_assert(client_conn != NULL);
  625. ri = mg_get_request_info(client_conn);
  626. ck_assert(ri != NULL);
  627. #if defined(NO_FILES)
  628. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  629. #else
  630. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  631. #endif
  632. mg_close_connection(client_conn);
  633. /* Websocket test */
  634. #ifdef USE_WEBSOCKET
  635. /* Then connect a first client */
  636. ws_client1_conn =
  637. mg_connect_websocket_client("localhost",
  638. ipv4_port,
  639. 0,
  640. ebuf,
  641. sizeof(ebuf),
  642. "/websocket",
  643. NULL,
  644. websocket_client_data_handler,
  645. websocket_client_close_handler,
  646. &ws_client1_data);
  647. ck_assert(ws_client1_conn != NULL);
  648. mg_Sleep(3); /* Should get the websocket welcome message */
  649. ck_assert_int_eq(ws_client1_data.closed, 0);
  650. ck_assert_int_eq(ws_client2_data.closed, 0);
  651. ck_assert_int_eq(ws_client3_data.closed, 0);
  652. ck_assert(ws_client2_data.data == NULL);
  653. ck_assert_uint_eq(ws_client2_data.len, 0);
  654. ck_assert(ws_client1_data.data != NULL);
  655. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  656. ck_assert(!memcmp(ws_client1_data.data,
  657. websocket_welcome_msg,
  658. websocket_welcome_msg_len));
  659. free(ws_client1_data.data);
  660. ws_client1_data.data = NULL;
  661. ws_client1_data.len = 0;
  662. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data1", 5);
  663. mg_Sleep(3); /* Should get the acknowledge message */
  664. ck_assert_int_eq(ws_client1_data.closed, 0);
  665. ck_assert_int_eq(ws_client2_data.closed, 0);
  666. ck_assert(ws_client2_data.data == NULL);
  667. ck_assert_uint_eq(ws_client2_data.len, 0);
  668. ck_assert(ws_client1_data.data != NULL);
  669. ck_assert_uint_eq(ws_client1_data.len, websocket_acknowledge_msg_len);
  670. ck_assert(!memcmp(ws_client1_data.data,
  671. websocket_acknowledge_msg,
  672. websocket_acknowledge_msg_len));
  673. free(ws_client1_data.data);
  674. ws_client1_data.data = NULL;
  675. ws_client1_data.len = 0;
  676. /* Now connect a second client */
  677. #ifdef USE_IPV6
  678. ws_client2_conn =
  679. mg_connect_websocket_client("[::1]",
  680. ipv6_port,
  681. 0,
  682. ebuf,
  683. sizeof(ebuf),
  684. "/websocket",
  685. NULL,
  686. websocket_client_data_handler,
  687. websocket_client_close_handler,
  688. &ws_client2_data);
  689. #else
  690. ws_client2_conn =
  691. mg_connect_websocket_client("127.0.0.1",
  692. ipv4_port,
  693. 0,
  694. ebuf,
  695. sizeof(ebuf),
  696. "/websocket",
  697. NULL,
  698. websocket_client_data_handler,
  699. websocket_client_close_handler,
  700. &ws_client2_data);
  701. #endif
  702. ck_assert(ws_client2_conn != NULL);
  703. mg_Sleep(3); /* Client 2 should get the websocket welcome message */
  704. ck_assert(ws_client1_data.closed == 0);
  705. ck_assert(ws_client2_data.closed == 0);
  706. ck_assert(ws_client1_data.data == NULL);
  707. ck_assert(ws_client1_data.len == 0);
  708. ck_assert(ws_client2_data.data != NULL);
  709. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  710. ck_assert(!memcmp(ws_client2_data.data,
  711. websocket_welcome_msg,
  712. websocket_welcome_msg_len));
  713. free(ws_client2_data.data);
  714. ws_client2_data.data = NULL;
  715. ws_client2_data.len = 0;
  716. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data2", 5);
  717. mg_Sleep(3); /* Should get the acknowledge message */
  718. ck_assert(ws_client1_data.closed == 0);
  719. ck_assert(ws_client2_data.closed == 0);
  720. ck_assert(ws_client2_data.data == NULL);
  721. ck_assert(ws_client2_data.len == 0);
  722. ck_assert(ws_client1_data.data != NULL);
  723. ck_assert(ws_client1_data.len == websocket_acknowledge_msg_len);
  724. ck_assert(!memcmp(ws_client1_data.data,
  725. websocket_acknowledge_msg,
  726. websocket_acknowledge_msg_len));
  727. free(ws_client1_data.data);
  728. ws_client1_data.data = NULL;
  729. ws_client1_data.len = 0;
  730. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  731. mg_Sleep(3); /* Should get the goodbye message */
  732. ck_assert(ws_client1_data.closed == 0);
  733. ck_assert(ws_client2_data.closed == 0);
  734. ck_assert(ws_client2_data.data == NULL);
  735. ck_assert(ws_client2_data.len == 0);
  736. ck_assert(ws_client1_data.data != NULL);
  737. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  738. ck_assert(!memcmp(ws_client1_data.data,
  739. websocket_goodbye_msg,
  740. websocket_goodbye_msg_len));
  741. free(ws_client1_data.data);
  742. ws_client1_data.data = NULL;
  743. ws_client1_data.len = 0;
  744. mg_close_connection(ws_client1_conn);
  745. mg_Sleep(3); /* Won't get any message */
  746. ck_assert(ws_client1_data.closed == 1);
  747. ck_assert(ws_client2_data.closed == 0);
  748. ck_assert(ws_client1_data.data == NULL);
  749. ck_assert(ws_client1_data.len == 0);
  750. ck_assert(ws_client2_data.data == NULL);
  751. ck_assert(ws_client2_data.len == 0);
  752. mg_websocket_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  753. mg_Sleep(3); /* Should get the goodbye message */
  754. ck_assert(ws_client1_data.closed == 1);
  755. ck_assert(ws_client2_data.closed == 0);
  756. ck_assert(ws_client1_data.data == NULL);
  757. ck_assert(ws_client1_data.len == 0);
  758. ck_assert(ws_client2_data.data != NULL);
  759. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  760. ck_assert(!memcmp(ws_client2_data.data,
  761. websocket_goodbye_msg,
  762. websocket_goodbye_msg_len));
  763. free(ws_client2_data.data);
  764. ws_client2_data.data = NULL;
  765. ws_client2_data.len = 0;
  766. mg_close_connection(ws_client2_conn);
  767. mg_Sleep(3); /* Won't get any message */
  768. ck_assert(ws_client1_data.closed == 1);
  769. ck_assert(ws_client2_data.closed == 1);
  770. ck_assert(ws_client1_data.data == NULL);
  771. ck_assert(ws_client1_data.len == 0);
  772. ck_assert(ws_client2_data.data == NULL);
  773. ck_assert(ws_client2_data.len == 0);
  774. /* Connect client 3 */
  775. ws_client3_conn =
  776. mg_connect_websocket_client("localhost",
  777. ipv4_port,
  778. 0,
  779. ebuf,
  780. sizeof(ebuf),
  781. "/websocket",
  782. NULL,
  783. websocket_client_data_handler,
  784. websocket_client_close_handler,
  785. &ws_client3_data);
  786. mg_Sleep(3); /* Client 3 should get the websocket welcome message */
  787. ck_assert(ws_client1_data.closed == 1);
  788. ck_assert(ws_client2_data.closed == 1);
  789. ck_assert(ws_client3_data.closed == 0);
  790. ck_assert(ws_client1_data.data == NULL);
  791. ck_assert(ws_client1_data.len == 0);
  792. ck_assert(ws_client2_data.data == NULL);
  793. ck_assert(ws_client2_data.len == 0);
  794. ck_assert(ws_client3_data.data != NULL);
  795. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  796. ck_assert(!memcmp(ws_client3_data.data,
  797. websocket_welcome_msg,
  798. websocket_welcome_msg_len));
  799. free(ws_client3_data.data);
  800. ws_client3_data.data = NULL;
  801. ws_client3_data.len = 0;
  802. #endif
  803. /* Close the server */
  804. g_ctx = NULL;
  805. mg_stop(ctx);
  806. mg_Sleep(30);
  807. ck_assert_int_eq(ws_client3_data.closed, 1);
  808. }
  809. END_TEST
  810. Suite *make_public_server_suite(void)
  811. {
  812. Suite *const suite = suite_create("PublicServer");
  813. TCase *const checktestenv = tcase_create("Check test environment");
  814. TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
  815. TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
  816. TCase *const serverrequests = tcase_create("Server Requests");
  817. tcase_add_test(checktestenv, test_the_test_environment);
  818. suite_add_tcase(suite, checktestenv);
  819. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  820. suite_add_tcase(suite, startstophttp);
  821. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  822. suite_add_tcase(suite, startstophttps);
  823. tcase_add_test(serverrequests, test_request_handlers);
  824. suite_add_tcase(suite, serverrequests);
  825. return suite;
  826. }
  827. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  828. /* Used to debug test cases without using the check framework */
  829. static int chk_ok = 0;
  830. static int chk_failed = 0;
  831. void main(void)
  832. {
  833. test_the_test_environment(0);
  834. test_mg_start_stop_http_server(0);
  835. test_mg_start_stop_https_server(0);
  836. test_request_handlers(0);
  837. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  838. }
  839. void _ck_assert_failed(const char *file, int line, const char *expr, ...)
  840. {
  841. va_list va;
  842. va_start(va, expr);
  843. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  844. vfprintf(stderr, expr, va);
  845. fprintf(stderr, "\n\n");
  846. va_end(va);
  847. chk_failed++;
  848. }
  849. void _mark_point(const char *file, int line) { chk_ok++; }
  850. void tcase_fn_start(const char *fname, const char *file, int line) {}
  851. void suite_add_tcase(Suite *s, TCase *tc){};
  852. void _tcase_add_test(TCase *tc,
  853. TFun tf,
  854. const char *fname,
  855. int _signal,
  856. int allowed_exit_value,
  857. int start,
  858. int end){};
  859. TCase *tcase_create(const char *name) { return NULL; };
  860. Suite *suite_create(const char *name) { return NULL; };
  861. #endif