public_server.c 40 KB

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