public_server.c 40 KB

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