public_server.c 42 KB

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