public_server.c 31 KB

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