public_server.c 40 KB

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