public_server.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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 *
  44. locate_resources(void)
  45. {
  46. return
  47. #ifdef _WIN32
  48. #ifdef LOCAL_TEST
  49. "resources\\";
  50. #else
  51. /* Appveyor */
  52. "..\\..\\..\\resources\\"; /* TODO: the different paths
  53. * used in the different test
  54. * system is an unsolved
  55. * problem */
  56. #endif
  57. #else
  58. #ifdef LOCAL_TEST
  59. "resources/";
  60. #else
  61. /* Travis */
  62. "../../resources/"; // TODO: fix path in CI test environment
  63. #endif
  64. #endif
  65. }
  66. static const char *
  67. locate_ssl_cert(void)
  68. {
  69. static char cert_path[256];
  70. const char *res = locate_resources();
  71. size_t l;
  72. ck_assert(res != NULL);
  73. l = strlen(res);
  74. ck_assert_int_gt(l, 0);
  75. ck_assert_int_lt(l, 100); /* assume there is enough space left in our
  76. typical 255 character string buffers */
  77. strcpy(cert_path, res);
  78. strcat(cert_path, "ssl_cert.pem");
  79. return cert_path;
  80. }
  81. static int
  82. wait_not_null(void *volatile *data)
  83. {
  84. int i;
  85. for (i = 0; i < 100; i++) {
  86. test_sleep(1);
  87. if (*data != NULL) {
  88. return 1;
  89. }
  90. }
  91. return 0;
  92. }
  93. START_TEST(test_the_test_environment)
  94. {
  95. char wd[300];
  96. char buf[500];
  97. FILE *f;
  98. struct stat st;
  99. int ret;
  100. const char *ssl_cert = locate_ssl_cert();
  101. memset(wd, 0, sizeof(wd));
  102. memset(buf, 0, sizeof(buf));
  103. /* Get the current working directory */
  104. #ifdef _WIN32
  105. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  106. wd[sizeof(wd) - 1] = 0;
  107. #else
  108. (void)getcwd(wd, sizeof(wd));
  109. wd[sizeof(wd) - 1] = 0;
  110. #endif
  111. /* Check the pem file */
  112. #ifdef _WIN32
  113. strcpy(buf, wd);
  114. strcat(buf, "\\");
  115. strcat(buf, ssl_cert);
  116. f = fopen(buf, "rb");
  117. #else
  118. strcpy(buf, wd);
  119. strcat(buf, "/");
  120. strcat(buf, ssl_cert);
  121. f = fopen(buf, "r");
  122. #endif
  123. if (f) {
  124. fclose(f);
  125. } else {
  126. fprintf(stderr, "%s not found", buf);
  127. }
  128. /* Check the test dir */
  129. #ifdef _WIN32
  130. strcpy(buf, wd);
  131. strcat(buf, "\\test");
  132. #else
  133. strcpy(buf, wd);
  134. strcat(buf, "/test");
  135. #endif
  136. memset(&st, 0, sizeof(st));
  137. ret = stat(buf, &st);
  138. if (ret) {
  139. fprintf(stderr, "%s not found", buf);
  140. }
  141. }
  142. END_TEST
  143. static void *threading_data;
  144. static void *
  145. test_thread_func_t(void *param)
  146. {
  147. ck_assert_ptr_eq(param, &threading_data);
  148. ck_assert_ptr_eq(threading_data, NULL);
  149. threading_data = &threading_data;
  150. return NULL;
  151. }
  152. START_TEST(test_threading)
  153. {
  154. int ok;
  155. threading_data = NULL;
  156. ok = mg_start_thread(test_thread_func_t, &threading_data);
  157. ck_assert_int_eq(ok, 0);
  158. wait_not_null(&threading_data);
  159. ck_assert_ptr_eq(threading_data, &threading_data);
  160. }
  161. END_TEST
  162. static int
  163. log_msg_func(const struct mg_connection *conn, const char *message)
  164. {
  165. struct mg_context *ctx;
  166. char *ud;
  167. ck_assert(conn != NULL);
  168. ctx = mg_get_context(conn);
  169. ck_assert(ctx != NULL);
  170. ud = (char *)mg_get_user_data(ctx);
  171. strncpy(ud, message, 255);
  172. ud[255] = 0;
  173. return 1;
  174. }
  175. START_TEST(test_mg_start_stop_http_server)
  176. {
  177. struct mg_context *ctx;
  178. const char *OPTIONS[] = {
  179. #if !defined(NO_FILES)
  180. "document_root",
  181. ".",
  182. #endif
  183. "listening_ports",
  184. "8080",
  185. NULL,
  186. };
  187. size_t ports_cnt;
  188. int ports[16];
  189. int ssl[16];
  190. struct mg_callbacks callbacks;
  191. char errmsg[256];
  192. struct mg_connection *client_conn;
  193. char client_err[256];
  194. const struct mg_request_info *client_ri;
  195. int client_res;
  196. memset(ports, 0, sizeof(ports));
  197. memset(ssl, 0, sizeof(ssl));
  198. memset(&callbacks, 0, sizeof(callbacks));
  199. memset(errmsg, 0, sizeof(errmsg));
  200. callbacks.log_message = log_msg_func;
  201. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  202. test_sleep(1);
  203. ck_assert_str_eq(errmsg, "");
  204. ck_assert(ctx != NULL);
  205. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  206. ck_assert_uint_eq(ports_cnt, 1);
  207. ck_assert_int_eq(ports[0], 8080);
  208. ck_assert_int_eq(ssl[0], 0);
  209. ck_assert_int_eq(ports[1], 0);
  210. ck_assert_int_eq(ssl[1], 0);
  211. test_sleep(1);
  212. memset(client_err, 0, sizeof(client_err));
  213. client_conn =
  214. mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err));
  215. ck_assert(client_conn != NULL);
  216. ck_assert_str_eq(client_err, "");
  217. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  218. client_res =
  219. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  220. ck_assert_int_ge(client_res, 0);
  221. ck_assert_str_eq(client_err, "");
  222. client_ri = mg_get_request_info(client_conn);
  223. ck_assert(client_ri != NULL);
  224. ck_assert_str_eq(client_ri->uri, "200");
  225. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  226. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  227. ck_assert_int_gt(client_res, 0);
  228. ck_assert_int_le(client_res, sizeof(client_err));
  229. mg_close_connection(client_conn);
  230. test_sleep(1);
  231. mg_stop(ctx);
  232. }
  233. END_TEST
  234. START_TEST(test_mg_start_stop_https_server)
  235. {
  236. #ifndef NO_SSL
  237. struct mg_context *ctx;
  238. size_t ports_cnt;
  239. int ports[16];
  240. int ssl[16];
  241. struct mg_callbacks callbacks;
  242. char errmsg[256];
  243. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  244. int opt_idx = 0;
  245. const char *ssl_cert = locate_ssl_cert();
  246. struct mg_connection *client_conn;
  247. char client_err[256];
  248. const struct mg_request_info *client_ri;
  249. int client_res;
  250. ck_assert(ssl_cert != NULL);
  251. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  252. #if !defined(NO_FILES)
  253. OPTIONS[opt_idx++] = "document_root";
  254. OPTIONS[opt_idx++] = ".";
  255. #endif
  256. OPTIONS[opt_idx++] = "listening_ports";
  257. OPTIONS[opt_idx++] = "8080r,8443s";
  258. OPTIONS[opt_idx++] = "ssl_certificate";
  259. OPTIONS[opt_idx++] = ssl_cert;
  260. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  261. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  262. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  263. memset(ports, 0, sizeof(ports));
  264. memset(ssl, 0, sizeof(ssl));
  265. memset(&callbacks, 0, sizeof(callbacks));
  266. memset(errmsg, 0, sizeof(errmsg));
  267. callbacks.log_message = log_msg_func;
  268. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  269. test_sleep(1);
  270. ck_assert_str_eq(errmsg, "");
  271. ck_assert(ctx != NULL);
  272. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  273. ck_assert_uint_eq(ports_cnt, 2);
  274. ck_assert_int_eq(ports[0], 8080);
  275. ck_assert_int_eq(ssl[0], 0);
  276. ck_assert_int_eq(ports[1], 8443);
  277. ck_assert_int_eq(ssl[1], 1);
  278. ck_assert_int_eq(ports[2], 0);
  279. ck_assert_int_eq(ssl[2], 0);
  280. test_sleep(1);
  281. memset(client_err, 0, sizeof(client_err));
  282. client_conn =
  283. mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err));
  284. ck_assert(client_conn != NULL);
  285. ck_assert_str_eq(client_err, "");
  286. mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n");
  287. client_res =
  288. mg_get_response(client_conn, client_err, sizeof(client_err), 10000);
  289. ck_assert_int_ge(client_res, 0);
  290. ck_assert_str_eq(client_err, "");
  291. client_ri = mg_get_request_info(client_conn);
  292. ck_assert(client_ri != NULL);
  293. ck_assert_str_eq(client_ri->uri, "200");
  294. /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */
  295. client_res = (int)mg_read(client_conn, client_err, sizeof(client_err));
  296. ck_assert_int_gt(client_res, 0);
  297. ck_assert_int_le(client_res, sizeof(client_err));
  298. mg_close_connection(client_conn);
  299. test_sleep(1);
  300. mg_stop(ctx);
  301. #endif
  302. }
  303. END_TEST
  304. START_TEST(test_mg_server_and_client_tls)
  305. {
  306. #ifndef NO_SSL
  307. struct mg_context *ctx;
  308. size_t ports_cnt;
  309. struct mg_server_ports ports[16];
  310. struct mg_callbacks callbacks;
  311. char errmsg[256];
  312. const char *OPTIONS[32]; /* initializer list here is rejected by CI test */
  313. int opt_idx = 0;
  314. char server_cert[256];
  315. char client_cert[256];
  316. const char *res_dir = locate_resources();
  317. ck_assert(res_dir != NULL);
  318. strcpy(server_cert, res_dir);
  319. strcpy(client_cert, res_dir);
  320. #ifdef _WIN32
  321. strcat(server_cert, "cert\\server.pem");
  322. strcat(client_cert, "cert\\client.pem");
  323. #else
  324. strcat(server_cert, "cert/server.pem");
  325. strcat(client_cert, "cert/client.pem");
  326. #endif
  327. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  328. #if !defined(NO_FILES)
  329. OPTIONS[opt_idx++] = "document_root";
  330. OPTIONS[opt_idx++] = ".";
  331. #endif
  332. OPTIONS[opt_idx++] = "listening_ports";
  333. OPTIONS[opt_idx++] = "8080r,8443s";
  334. OPTIONS[opt_idx++] = "ssl_certificate";
  335. OPTIONS[opt_idx++] = server_cert;
  336. OPTIONS[opt_idx++] = "ssl_verify_peer";
  337. OPTIONS[opt_idx++] = "yes";
  338. OPTIONS[opt_idx++] = "ssl_ca_file";
  339. OPTIONS[opt_idx++] = client_cert;
  340. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  341. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  342. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  343. memset(ports, 0, sizeof(ports));
  344. memset(&callbacks, 0, sizeof(callbacks));
  345. memset(errmsg, 0, sizeof(errmsg));
  346. callbacks.log_message = log_msg_func;
  347. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  348. test_sleep(1);
  349. ck_assert_str_eq(errmsg, "");
  350. ck_assert(ctx != NULL);
  351. ports_cnt = mg_get_server_ports(ctx, 16, ports);
  352. ck_assert_uint_eq(ports_cnt, 2);
  353. ck_assert_int_eq(ports[0].protocol, 1);
  354. ck_assert_int_eq(ports[0].port, 8080);
  355. ck_assert_int_eq(ports[0].is_ssl, 0);
  356. ck_assert_int_eq(ports[0].is_redirect, 1);
  357. ck_assert_int_eq(ports[1].protocol, 1);
  358. ck_assert_int_eq(ports[1].port, 8443);
  359. ck_assert_int_eq(ports[1].is_ssl, 1);
  360. ck_assert_int_eq(ports[1].is_redirect, 0);
  361. ck_assert_int_eq(ports[2].protocol, 0);
  362. ck_assert_int_eq(ports[2].port, 0);
  363. ck_assert_int_eq(ports[2].is_ssl, 0);
  364. ck_assert_int_eq(ports[2].is_redirect, 0);
  365. test_sleep(1);
  366. /* TODO: A client API using a client certificate is missing */
  367. test_sleep(1);
  368. mg_stop(ctx);
  369. #endif
  370. }
  371. END_TEST
  372. static struct mg_context *g_ctx;
  373. static int
  374. request_test_handler(struct mg_connection *conn, void *cbdata)
  375. {
  376. int i;
  377. char chunk_data[32];
  378. const struct mg_request_info *ri;
  379. struct mg_context *ctx;
  380. void *ud, *cud;
  381. ctx = mg_get_context(conn);
  382. ud = mg_get_user_data(ctx);
  383. ri = mg_get_request_info(conn);
  384. ck_assert(ri != NULL);
  385. ck_assert(ctx == g_ctx);
  386. ck_assert(ud == &g_ctx);
  387. mg_set_user_connection_data(conn, (void *)6543);
  388. cud = mg_get_user_connection_data(conn);
  389. ck_assert_ptr_eq((void *)cud, (void *)6543);
  390. ck_assert_ptr_eq((void *)cbdata, (void *)7);
  391. strcpy(chunk_data, "123456789A123456789B123456789C");
  392. mg_printf(conn,
  393. "HTTP/1.1 200 OK\r\n"
  394. "Transfer-Encoding: chunked\r\n"
  395. "Content-Type: text/plain\r\n\r\n");
  396. for (i = 1; i <= 10; i++) {
  397. mg_printf(conn, "%x\r\n", i);
  398. mg_write(conn, chunk_data, (unsigned)i);
  399. mg_printf(conn, "\r\n");
  400. }
  401. mg_printf(conn, "0\r\n\r\n");
  402. return 1;
  403. }
  404. #ifdef USE_WEBSOCKET
  405. /****************************************************************************/
  406. /* WEBSOCKET SERVER */
  407. /****************************************************************************/
  408. static const char *websocket_welcome_msg = "websocket welcome\n";
  409. static const size_t websocket_welcome_msg_len =
  410. 18 /* strlen(websocket_welcome_msg) */;
  411. static const char *websocket_acknowledge_msg = "websocket msg ok\n";
  412. static const size_t websocket_acknowledge_msg_len =
  413. 17 /* strlen(websocket_acknowledge_msg) */;
  414. static const char *websocket_goodbye_msg = "websocket bye\n";
  415. static const size_t websocket_goodbye_msg_len =
  416. 14 /* strlen(websocket_goodbye_msg) */;
  417. static int
  418. websock_server_connect(const struct mg_connection *conn, void *udata)
  419. {
  420. (void)conn;
  421. ck_assert_ptr_eq((void *)udata, (void *)7531);
  422. printf("Server: Websocket connected\n");
  423. return 0; /* return 0 to accept every connection */
  424. }
  425. static void
  426. websock_server_ready(struct mg_connection *conn, void *udata)
  427. {
  428. ck_assert_ptr_eq((void *)udata, (void *)7531);
  429. printf("Server: Websocket ready\n");
  430. /* Send websocket welcome message */
  431. mg_lock_connection(conn);
  432. mg_websocket_write(conn,
  433. WEBSOCKET_OPCODE_TEXT,
  434. websocket_welcome_msg,
  435. websocket_welcome_msg_len);
  436. mg_unlock_connection(conn);
  437. printf("Server: Websocket ready X\n");
  438. }
  439. static int
  440. websock_server_data(struct mg_connection *conn,
  441. int bits,
  442. char *data,
  443. size_t data_len,
  444. void *udata)
  445. {
  446. (void)bits;
  447. ck_assert_ptr_eq((void *)udata, (void *)7531);
  448. printf("Server: Got %u bytes from the client\n", (unsigned)data_len);
  449. if (data_len < 3 || 0 != memcmp(data, "bye", 3)) {
  450. /* Send websocket acknowledge message */
  451. mg_lock_connection(conn);
  452. mg_websocket_write(conn,
  453. WEBSOCKET_OPCODE_TEXT,
  454. websocket_acknowledge_msg,
  455. websocket_acknowledge_msg_len);
  456. mg_unlock_connection(conn);
  457. } else {
  458. /* Send websocket acknowledge message */
  459. mg_lock_connection(conn);
  460. mg_websocket_write(conn,
  461. WEBSOCKET_OPCODE_TEXT,
  462. websocket_goodbye_msg,
  463. websocket_goodbye_msg_len);
  464. mg_unlock_connection(conn);
  465. }
  466. return 1; /* return 1 to keep the connetion open */
  467. }
  468. static void
  469. websock_server_close(const struct mg_connection *conn, void *udata)
  470. {
  471. (void)conn;
  472. ck_assert_ptr_eq((void *)udata, (void *)7531);
  473. printf("Server: Close connection\n");
  474. /* Can not send a websocket goodbye message here - the connection is already
  475. * closed */
  476. }
  477. /****************************************************************************/
  478. /* WEBSOCKET CLIENT */
  479. /****************************************************************************/
  480. struct tclient_data {
  481. void *data;
  482. size_t len;
  483. int closed;
  484. };
  485. static int
  486. websocket_client_data_handler(struct mg_connection *conn,
  487. int flags,
  488. char *data,
  489. size_t data_len,
  490. void *user_data)
  491. {
  492. struct mg_context *ctx = mg_get_context(conn);
  493. struct tclient_data *pclient_data =
  494. (struct tclient_data *)mg_get_user_data(ctx);
  495. (void)user_data; /* TODO: check this */
  496. ck_assert(pclient_data != NULL);
  497. ck_assert_int_eq(flags, (int)(128 | 1));
  498. printf("Client received data from server: ");
  499. fwrite(data, 1, data_len, stdout);
  500. printf("\n");
  501. pclient_data->data = malloc(data_len);
  502. ck_assert(pclient_data->data != NULL);
  503. memcpy(pclient_data->data, data, data_len);
  504. pclient_data->len = data_len;
  505. return 1;
  506. }
  507. static void
  508. websocket_client_close_handler(const struct mg_connection *conn,
  509. void *user_data)
  510. {
  511. struct mg_context *ctx = mg_get_context(conn);
  512. struct tclient_data *pclient_data =
  513. (struct tclient_data *)mg_get_user_data(ctx);
  514. (void)user_data; /* TODO: check this */
  515. ck_assert(pclient_data != NULL);
  516. printf("Client: Close handler\n");
  517. pclient_data->closed++;
  518. }
  519. #endif
  520. START_TEST(test_request_handlers)
  521. {
  522. char ebuf[100];
  523. struct mg_context *ctx;
  524. struct mg_connection *client_conn;
  525. const struct mg_request_info *ri;
  526. char uri[64];
  527. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  528. const char *expected =
  529. "112123123412345123456123456712345678123456789123456789A";
  530. int i;
  531. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  532. #if defined(USE_IPV6) && defined(NO_SSL)
  533. const char *HTTP_PORT = "8084,[::]:8086";
  534. short ipv4_port = 8084;
  535. short ipv6_port = 8086;
  536. #elif !defined(USE_IPV6) && defined(NO_SSL)
  537. const char *HTTP_PORT = "8084";
  538. short ipv4_port = 8084;
  539. #elif defined(USE_IPV6) && !defined(NO_SSL)
  540. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  541. short ipv4_port = 8084;
  542. short ipv4s_port = 8094;
  543. short ipv4r_port = 8194;
  544. short ipv6_port = 8086;
  545. short ipv6s_port = 8096;
  546. short ipv6r_port = 8196;
  547. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  548. const char *HTTP_PORT = "8084,8194r,8094s";
  549. short ipv4_port = 8084;
  550. short ipv4s_port = 8094;
  551. short ipv4r_port = 8194;
  552. #endif
  553. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  554. const char *opt;
  555. FILE *f;
  556. const char *plain_file_content;
  557. const char *encoded_file_content;
  558. int opt_idx = 0;
  559. #if !defined(NO_SSL)
  560. const char *ssl_cert = locate_ssl_cert();
  561. #endif
  562. #if defined(USE_WEBSOCKET)
  563. struct tclient_data ws_client1_data = {NULL, 0, 0};
  564. struct tclient_data ws_client2_data = {NULL, 0, 0};
  565. struct tclient_data ws_client3_data = {NULL, 0, 0};
  566. struct mg_connection *ws_client1_conn = NULL;
  567. struct mg_connection *ws_client2_conn = NULL;
  568. struct mg_connection *ws_client3_conn = NULL;
  569. #endif
  570. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  571. OPTIONS[opt_idx++] = "listening_ports";
  572. OPTIONS[opt_idx++] = HTTP_PORT;
  573. #if !defined(NO_FILES)
  574. OPTIONS[opt_idx++] = "document_root";
  575. OPTIONS[opt_idx++] = ".";
  576. #endif
  577. #ifndef NO_SSL
  578. ck_assert(ssl_cert != NULL);
  579. OPTIONS[opt_idx++] = "ssl_certificate";
  580. OPTIONS[opt_idx++] = ssl_cert;
  581. #endif
  582. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  583. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  584. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  585. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  586. ck_assert(ctx != NULL);
  587. g_ctx = ctx;
  588. opt = mg_get_option(ctx, "listening_ports");
  589. ck_assert_str_eq(opt, HTTP_PORT);
  590. opt = mg_get_option(ctx, "cgi_environment");
  591. ck_assert_str_eq(opt, "");
  592. opt = mg_get_option(ctx, "unknown_option_name");
  593. ck_assert(opt == NULL);
  594. for (i = 0; i < 1000; i++) {
  595. sprintf(uri, "/U%u", i);
  596. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  597. }
  598. for (i = 500; i < 800; i++) {
  599. sprintf(uri, "/U%u", i);
  600. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  601. }
  602. for (i = 600; i >= 0; i--) {
  603. sprintf(uri, "/U%u", i);
  604. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  605. }
  606. for (i = 750; i <= 1000; i++) {
  607. sprintf(uri, "/U%u", i);
  608. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  609. }
  610. for (i = 5; i < 9; i++) {
  611. sprintf(uri, "/U%u", i);
  612. mg_set_request_handler(ctx,
  613. uri,
  614. request_test_handler,
  615. (void *)(ptrdiff_t)i);
  616. }
  617. #ifdef USE_WEBSOCKET
  618. mg_set_websocket_handler(ctx,
  619. "/websocket",
  620. websock_server_connect,
  621. websock_server_ready,
  622. websock_server_data,
  623. websock_server_close,
  624. (void *)7531);
  625. #endif
  626. /* Try to load non existing file */
  627. client_conn = mg_download("localhost",
  628. ipv4_port,
  629. 0,
  630. ebuf,
  631. sizeof(ebuf),
  632. "%s",
  633. "GET /file/not/found HTTP/1.0\r\n\r\n");
  634. ck_assert(client_conn != NULL);
  635. ri = mg_get_request_info(client_conn);
  636. ck_assert(ri != NULL);
  637. ck_assert_str_eq(ri->uri, "404");
  638. mg_close_connection(client_conn);
  639. /* Get data from callback */
  640. client_conn = mg_download(
  641. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  642. ck_assert(client_conn != NULL);
  643. ri = mg_get_request_info(client_conn);
  644. ck_assert(ri != NULL);
  645. ck_assert_str_eq(ri->uri, "200");
  646. i = mg_read(client_conn, buf, sizeof(buf));
  647. ck_assert_int_eq(i, (int)strlen(expected));
  648. buf[i] = 0;
  649. ck_assert_str_eq(buf, expected);
  650. mg_close_connection(client_conn);
  651. /* Get data from callback using http://127.0.0.1 */
  652. client_conn = mg_download(
  653. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  654. ck_assert(client_conn != NULL);
  655. ri = mg_get_request_info(client_conn);
  656. ck_assert(ri != NULL);
  657. ck_assert_str_eq(ri->uri, "200");
  658. i = mg_read(client_conn, buf, sizeof(buf));
  659. ck_assert_int_eq(i, (int)strlen(expected));
  660. buf[i] = 0;
  661. ck_assert_str_eq(buf, expected);
  662. mg_close_connection(client_conn);
  663. #if defined(USE_IPV6)
  664. /* Get data from callback using http://[::1] */
  665. client_conn =
  666. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  667. ck_assert(client_conn != NULL);
  668. ri = mg_get_request_info(client_conn);
  669. ck_assert(ri != NULL);
  670. ck_assert_str_eq(ri->uri, "200");
  671. i = mg_read(client_conn, buf, sizeof(buf));
  672. ck_assert_int_eq(i, (int)strlen(expected));
  673. buf[i] = 0;
  674. ck_assert_str_eq(buf, expected);
  675. mg_close_connection(client_conn);
  676. #endif
  677. #if !defined(NO_SSL)
  678. /* Get data from callback using https://127.0.0.1 */
  679. client_conn = mg_download(
  680. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  681. ck_assert(client_conn != NULL);
  682. ri = mg_get_request_info(client_conn);
  683. ck_assert(ri != NULL);
  684. ck_assert_str_eq(ri->uri, "200");
  685. i = mg_read(client_conn, buf, sizeof(buf));
  686. ck_assert_int_eq(i, (int)strlen(expected));
  687. buf[i] = 0;
  688. ck_assert_str_eq(buf, expected);
  689. mg_close_connection(client_conn);
  690. /* Get redirect from callback using http://127.0.0.1 */
  691. client_conn = mg_download(
  692. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  693. ck_assert(client_conn != NULL);
  694. ri = mg_get_request_info(client_conn);
  695. ck_assert(ri != NULL);
  696. ck_assert_str_eq(ri->uri, "302");
  697. i = mg_read(client_conn, buf, sizeof(buf));
  698. ck_assert_int_eq(i, -1);
  699. mg_close_connection(client_conn);
  700. #endif
  701. #if defined(USE_IPV6) && !defined(NO_SSL)
  702. /* Get data from callback using https://[::1] */
  703. client_conn =
  704. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  705. ck_assert(client_conn != NULL);
  706. ri = mg_get_request_info(client_conn);
  707. ck_assert(ri != NULL);
  708. ck_assert_str_eq(ri->uri, "200");
  709. i = mg_read(client_conn, buf, sizeof(buf));
  710. ck_assert_int_eq(i, (int)strlen(expected));
  711. buf[i] = 0;
  712. ck_assert_str_eq(buf, expected);
  713. mg_close_connection(client_conn);
  714. /* Get redirect from callback using http://127.0.0.1 */
  715. client_conn =
  716. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  717. ck_assert(client_conn != NULL);
  718. ri = mg_get_request_info(client_conn);
  719. ck_assert(ri != NULL);
  720. ck_assert_str_eq(ri->uri, "302");
  721. i = mg_read(client_conn, buf, sizeof(buf));
  722. ck_assert_int_eq(i, -1);
  723. mg_close_connection(client_conn);
  724. #endif
  725. /* It seems to be impossible to find out what the actual working
  726. * directory of the CI test environment is. Before breaking another
  727. * dozen of builds by trying blindly with different paths, just
  728. * create the file here */
  729. #ifdef _WIN32
  730. f = fopen("test.txt", "wb");
  731. #else
  732. f = fopen("test.txt", "w");
  733. #endif
  734. plain_file_content = "simple text file\n";
  735. fwrite(plain_file_content, 17, 1, f);
  736. fclose(f);
  737. #ifdef _WIN32
  738. f = fopen("test_gz.txt.gz", "wb");
  739. #else
  740. f = fopen("test_gz.txt.gz", "w");
  741. #endif
  742. encoded_file_content = "\x1f\x8b\x08\x08\xf8\x9d\xcb\x55\x00\x00"
  743. "test_gz.txt"
  744. "\x00\x01\x11\x00\xee\xff"
  745. "zipped text file"
  746. "\x0a\x34\x5f\xcc\x49\x11\x00\x00\x00";
  747. fwrite(encoded_file_content, 1, 52, f);
  748. fclose(f);
  749. /* Get static data */
  750. client_conn = mg_download("localhost",
  751. ipv4_port,
  752. 0,
  753. ebuf,
  754. sizeof(ebuf),
  755. "%s",
  756. "GET /test.txt HTTP/1.0\r\n\r\n");
  757. ck_assert(client_conn != NULL);
  758. ri = mg_get_request_info(client_conn);
  759. ck_assert(ri != NULL);
  760. #if defined(NO_FILES)
  761. ck_assert_str_eq(ri->uri, "404");
  762. #else
  763. ck_assert_str_eq(ri->uri, "200");
  764. i = mg_read(client_conn, buf, sizeof(buf));
  765. ck_assert_int_eq(i, 17);
  766. if ((i >= 0) && (i < (int)sizeof(buf))) {
  767. buf[i] = 0;
  768. }
  769. ck_assert_str_eq(buf, plain_file_content);
  770. #endif
  771. mg_close_connection(client_conn);
  772. /* Get zipped static data - will not work if Accept-Encoding is not set */
  773. client_conn = mg_download("localhost",
  774. ipv4_port,
  775. 0,
  776. ebuf,
  777. sizeof(ebuf),
  778. "%s",
  779. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  780. ck_assert(client_conn != NULL);
  781. ri = mg_get_request_info(client_conn);
  782. ck_assert(ri != NULL);
  783. ck_assert_str_eq(ri->uri, "404");
  784. mg_close_connection(client_conn);
  785. /* Get zipped static data - with Accept-Encoding */
  786. client_conn = mg_download(
  787. "localhost",
  788. ipv4_port,
  789. 0,
  790. ebuf,
  791. sizeof(ebuf),
  792. "%s",
  793. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  794. ck_assert(client_conn != NULL);
  795. ri = mg_get_request_info(client_conn);
  796. ck_assert(ri != NULL);
  797. #if defined(NO_FILES)
  798. ck_assert_str_eq(ri->uri, "404");
  799. #else
  800. ck_assert_str_eq(ri->uri, "200");
  801. i = mg_read(client_conn, buf, sizeof(buf));
  802. ck_assert_int_eq(i, 52);
  803. if ((i >= 0) && (i < (int)sizeof(buf))) {
  804. buf[i] = 0;
  805. }
  806. ck_assert_int_eq(ri->content_length, 52);
  807. ck_assert_str_eq(buf, encoded_file_content);
  808. #endif
  809. mg_close_connection(client_conn);
  810. /* Get directory listing */
  811. client_conn = mg_download("localhost",
  812. ipv4_port,
  813. 0,
  814. ebuf,
  815. sizeof(ebuf),
  816. "%s",
  817. "GET / HTTP/1.0\r\n\r\n");
  818. ck_assert(client_conn != NULL);
  819. ri = mg_get_request_info(client_conn);
  820. ck_assert(ri != NULL);
  821. #if defined(NO_FILES)
  822. ck_assert_str_eq(ri->uri, "404");
  823. #else
  824. ck_assert_str_eq(ri->uri, "200");
  825. i = mg_read(client_conn, buf, sizeof(buf));
  826. ck_assert(i > 6);
  827. buf[6] = 0;
  828. ck_assert_str_eq(buf, "<html>");
  829. #endif
  830. mg_close_connection(client_conn);
  831. /* POST to static file (will not work) */
  832. client_conn = mg_download("localhost",
  833. ipv4_port,
  834. 0,
  835. ebuf,
  836. sizeof(ebuf),
  837. "%s",
  838. "POST /test.txt HTTP/1.0\r\n\r\n");
  839. ck_assert(client_conn != NULL);
  840. ri = mg_get_request_info(client_conn);
  841. ck_assert(ri != NULL);
  842. #if defined(NO_FILES)
  843. ck_assert_str_eq(ri->uri, "404");
  844. #else
  845. ck_assert_str_eq(ri->uri, "405");
  846. i = mg_read(client_conn, buf, sizeof(buf));
  847. ck_assert(i >= 29);
  848. buf[29] = 0;
  849. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  850. #endif
  851. mg_close_connection(client_conn);
  852. /* PUT to static file (will not work) */
  853. client_conn = mg_download("localhost",
  854. ipv4_port,
  855. 0,
  856. ebuf,
  857. sizeof(ebuf),
  858. "%s",
  859. "PUT /test.txt HTTP/1.0\r\n\r\n");
  860. ck_assert(client_conn != NULL);
  861. ri = mg_get_request_info(client_conn);
  862. ck_assert(ri != NULL);
  863. #if defined(NO_FILES)
  864. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  865. #else
  866. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  867. #endif
  868. mg_close_connection(client_conn);
  869. /* Websocket test */
  870. #ifdef USE_WEBSOCKET
  871. /* Then connect a first client */
  872. ws_client1_conn =
  873. mg_connect_websocket_client("localhost",
  874. ipv4_port,
  875. 0,
  876. ebuf,
  877. sizeof(ebuf),
  878. "/websocket",
  879. NULL,
  880. websocket_client_data_handler,
  881. websocket_client_close_handler,
  882. &ws_client1_data);
  883. ck_assert(ws_client1_conn != NULL);
  884. wait_not_null(
  885. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  886. ck_assert_int_eq(ws_client1_data.closed, 0);
  887. ck_assert_int_eq(ws_client2_data.closed, 0);
  888. ck_assert_int_eq(ws_client3_data.closed, 0);
  889. ck_assert(ws_client2_data.data == NULL);
  890. ck_assert_uint_eq(ws_client2_data.len, 0);
  891. ck_assert(ws_client1_data.data != NULL);
  892. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  893. ck_assert(!memcmp(ws_client1_data.data,
  894. websocket_welcome_msg,
  895. websocket_welcome_msg_len));
  896. free(ws_client1_data.data);
  897. ws_client1_data.data = NULL;
  898. ws_client1_data.len = 0;
  899. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data1", 5);
  900. wait_not_null(
  901. &(ws_client1_data
  902. .data)); /* Wait for the websocket acknowledge message */
  903. ck_assert_int_eq(ws_client1_data.closed, 0);
  904. ck_assert_int_eq(ws_client2_data.closed, 0);
  905. ck_assert(ws_client2_data.data == NULL);
  906. ck_assert_uint_eq(ws_client2_data.len, 0);
  907. ck_assert(ws_client1_data.data != NULL);
  908. ck_assert_uint_eq(ws_client1_data.len, websocket_acknowledge_msg_len);
  909. ck_assert(!memcmp(ws_client1_data.data,
  910. websocket_acknowledge_msg,
  911. websocket_acknowledge_msg_len));
  912. free(ws_client1_data.data);
  913. ws_client1_data.data = NULL;
  914. ws_client1_data.len = 0;
  915. /* Now connect a second client */
  916. #ifdef USE_IPV6
  917. ws_client2_conn =
  918. mg_connect_websocket_client("[::1]",
  919. ipv6_port,
  920. 0,
  921. ebuf,
  922. sizeof(ebuf),
  923. "/websocket",
  924. NULL,
  925. websocket_client_data_handler,
  926. websocket_client_close_handler,
  927. &ws_client2_data);
  928. #else
  929. ws_client2_conn =
  930. mg_connect_websocket_client("127.0.0.1",
  931. ipv4_port,
  932. 0,
  933. ebuf,
  934. sizeof(ebuf),
  935. "/websocket",
  936. NULL,
  937. websocket_client_data_handler,
  938. websocket_client_close_handler,
  939. &ws_client2_data);
  940. #endif
  941. ck_assert(ws_client2_conn != NULL);
  942. wait_not_null(
  943. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  944. ck_assert(ws_client1_data.closed == 0);
  945. ck_assert(ws_client2_data.closed == 0);
  946. ck_assert(ws_client1_data.data == NULL);
  947. ck_assert(ws_client1_data.len == 0);
  948. ck_assert(ws_client2_data.data != NULL);
  949. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  950. ck_assert(!memcmp(ws_client2_data.data,
  951. websocket_welcome_msg,
  952. websocket_welcome_msg_len));
  953. free(ws_client2_data.data);
  954. ws_client2_data.data = NULL;
  955. ws_client2_data.len = 0;
  956. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data2", 5);
  957. wait_not_null(
  958. &(ws_client1_data
  959. .data)); /* Wait for the websocket acknowledge message */
  960. ck_assert(ws_client1_data.closed == 0);
  961. ck_assert(ws_client2_data.closed == 0);
  962. ck_assert(ws_client2_data.data == NULL);
  963. ck_assert(ws_client2_data.len == 0);
  964. ck_assert(ws_client1_data.data != NULL);
  965. ck_assert(ws_client1_data.len == websocket_acknowledge_msg_len);
  966. ck_assert(!memcmp(ws_client1_data.data,
  967. websocket_acknowledge_msg,
  968. websocket_acknowledge_msg_len));
  969. free(ws_client1_data.data);
  970. ws_client1_data.data = NULL;
  971. ws_client1_data.len = 0;
  972. mg_websocket_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  973. wait_not_null(
  974. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  975. ck_assert(ws_client1_data.closed == 0);
  976. ck_assert(ws_client2_data.closed == 0);
  977. ck_assert(ws_client2_data.data == NULL);
  978. ck_assert(ws_client2_data.len == 0);
  979. ck_assert(ws_client1_data.data != NULL);
  980. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  981. ck_assert(!memcmp(ws_client1_data.data,
  982. websocket_goodbye_msg,
  983. websocket_goodbye_msg_len));
  984. free(ws_client1_data.data);
  985. ws_client1_data.data = NULL;
  986. ws_client1_data.len = 0;
  987. mg_close_connection(ws_client1_conn);
  988. test_sleep(3); /* Won't get any message */
  989. ck_assert(ws_client1_data.closed == 1);
  990. ck_assert(ws_client2_data.closed == 0);
  991. ck_assert(ws_client1_data.data == NULL);
  992. ck_assert(ws_client1_data.len == 0);
  993. ck_assert(ws_client2_data.data == NULL);
  994. ck_assert(ws_client2_data.len == 0);
  995. mg_websocket_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  996. wait_not_null(
  997. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  998. ck_assert(ws_client1_data.closed == 1);
  999. ck_assert(ws_client2_data.closed == 0);
  1000. ck_assert(ws_client1_data.data == NULL);
  1001. ck_assert(ws_client1_data.len == 0);
  1002. ck_assert(ws_client2_data.data != NULL);
  1003. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1004. ck_assert(!memcmp(ws_client2_data.data,
  1005. websocket_goodbye_msg,
  1006. websocket_goodbye_msg_len));
  1007. free(ws_client2_data.data);
  1008. ws_client2_data.data = NULL;
  1009. ws_client2_data.len = 0;
  1010. mg_close_connection(ws_client2_conn);
  1011. test_sleep(3); /* Won't get any message */
  1012. ck_assert(ws_client1_data.closed == 1);
  1013. ck_assert(ws_client2_data.closed == 1);
  1014. ck_assert(ws_client1_data.data == NULL);
  1015. ck_assert(ws_client1_data.len == 0);
  1016. ck_assert(ws_client2_data.data == NULL);
  1017. ck_assert(ws_client2_data.len == 0);
  1018. /* Connect client 3 */
  1019. ws_client3_conn =
  1020. mg_connect_websocket_client("localhost",
  1021. #if defined(NO_SSL)
  1022. ipv4_port,
  1023. 0,
  1024. #else
  1025. ipv4s_port,
  1026. 1,
  1027. #endif
  1028. ebuf,
  1029. sizeof(ebuf),
  1030. "/websocket",
  1031. NULL,
  1032. websocket_client_data_handler,
  1033. websocket_client_close_handler,
  1034. &ws_client3_data);
  1035. ck_assert(ws_client3_conn != NULL);
  1036. wait_not_null(
  1037. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1038. ck_assert(ws_client1_data.closed == 1);
  1039. ck_assert(ws_client2_data.closed == 1);
  1040. ck_assert(ws_client3_data.closed == 0);
  1041. ck_assert(ws_client1_data.data == NULL);
  1042. ck_assert(ws_client1_data.len == 0);
  1043. ck_assert(ws_client2_data.data == NULL);
  1044. ck_assert(ws_client2_data.len == 0);
  1045. ck_assert(ws_client3_data.data != NULL);
  1046. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1047. ck_assert(!memcmp(ws_client3_data.data,
  1048. websocket_welcome_msg,
  1049. websocket_welcome_msg_len));
  1050. free(ws_client3_data.data);
  1051. ws_client3_data.data = NULL;
  1052. ws_client3_data.len = 0;
  1053. #endif
  1054. /* Close the server */
  1055. g_ctx = NULL;
  1056. mg_stop(ctx);
  1057. #ifdef USE_WEBSOCKET
  1058. for (i = 0; i < 100; i++) {
  1059. test_sleep(1);
  1060. if (ws_client3_data.closed != 0) {
  1061. break;
  1062. }
  1063. }
  1064. ck_assert_int_eq(ws_client3_data.closed, 1);
  1065. #endif
  1066. }
  1067. END_TEST
  1068. Suite *
  1069. make_public_server_suite(void)
  1070. {
  1071. Suite *const suite = suite_create("PublicServer");
  1072. TCase *const checktestenv = tcase_create("Check test environment");
  1073. TCase *const startthreads = tcase_create("Start threads");
  1074. TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
  1075. TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
  1076. TCase *const serverandclienttls =
  1077. tcase_create("Start Stop TLS Server Client");
  1078. TCase *const serverrequests = tcase_create("Server Requests");
  1079. tcase_add_test(checktestenv, test_the_test_environment);
  1080. tcase_set_timeout(checktestenv, civetweb_min_test_timeout);
  1081. suite_add_tcase(suite, checktestenv);
  1082. tcase_add_test(startthreads, test_threading);
  1083. tcase_set_timeout(startthreads, civetweb_min_test_timeout);
  1084. suite_add_tcase(suite, startthreads);
  1085. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  1086. tcase_set_timeout(startstophttp, civetweb_min_test_timeout);
  1087. suite_add_tcase(suite, startstophttp);
  1088. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  1089. tcase_set_timeout(startstophttps, civetweb_min_test_timeout);
  1090. suite_add_tcase(suite, startstophttps);
  1091. tcase_add_test(serverandclienttls, test_mg_server_and_client_tls);
  1092. tcase_set_timeout(serverandclienttls, civetweb_min_test_timeout);
  1093. suite_add_tcase(suite, serverandclienttls);
  1094. tcase_add_test(serverrequests, test_request_handlers);
  1095. tcase_set_timeout(serverrequests, 120);
  1096. suite_add_tcase(suite, serverrequests);
  1097. return suite;
  1098. }
  1099. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  1100. /* Used to debug test cases without using the check framework */
  1101. static int chk_ok = 0;
  1102. static int chk_failed = 0;
  1103. void
  1104. main(void)
  1105. {
  1106. test_the_test_environment(0);
  1107. test_threading(0);
  1108. test_mg_start_stop_http_server(0);
  1109. test_mg_start_stop_https_server(0);
  1110. test_request_handlers(0);
  1111. test_mg_server_and_client_tls(0);
  1112. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  1113. }
  1114. void
  1115. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  1116. {
  1117. va_list va;
  1118. va_start(va, expr);
  1119. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  1120. vfprintf(stderr, expr, va);
  1121. fprintf(stderr, "\n\n");
  1122. va_end(va);
  1123. chk_failed++;
  1124. }
  1125. void
  1126. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  1127. {
  1128. va_list va;
  1129. if (cond) {
  1130. chk_ok++;
  1131. return;
  1132. }
  1133. va_start(va, expr);
  1134. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  1135. vfprintf(stderr, expr, va);
  1136. fprintf(stderr, "\n\n");
  1137. va_end(va);
  1138. chk_failed++;
  1139. }
  1140. void
  1141. _mark_point(const char *file, int line)
  1142. {
  1143. chk_ok++;
  1144. }
  1145. void
  1146. tcase_fn_start(const char *fname, const char *file, int line)
  1147. {
  1148. }
  1149. void suite_add_tcase(Suite *s, TCase *tc){};
  1150. void _tcase_add_test(TCase *tc,
  1151. TFun tf,
  1152. const char *fname,
  1153. int _signal,
  1154. int allowed_exit_value,
  1155. int start,
  1156. int end){};
  1157. TCase *
  1158. tcase_create(const char *name)
  1159. {
  1160. return NULL;
  1161. };
  1162. Suite *
  1163. suite_create(const char *name)
  1164. {
  1165. return NULL;
  1166. };
  1167. void tcase_set_timeout(TCase *tc, double timeout){};
  1168. #endif