public_server.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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"
  833. "echo \"Connection: close\"\n"
  834. "echo \"Content-Type: text/plain\"\n"
  835. "echo \n"
  836. "echo \"CGI test\"\n"
  837. "\n";
  838. fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
  839. fclose(f);
  840. #endif
  841. expected_cgi_result = "CGI test";
  842. /* Get static data */
  843. client_conn = mg_download("localhost",
  844. ipv4_port,
  845. 0,
  846. ebuf,
  847. sizeof(ebuf),
  848. "%s",
  849. "GET /test.txt HTTP/1.0\r\n\r\n");
  850. ck_assert(client_conn != NULL);
  851. ri = mg_get_request_info(client_conn);
  852. ck_assert(ri != NULL);
  853. #if defined(NO_FILES)
  854. ck_assert_str_eq(ri->uri, "404");
  855. #else
  856. ck_assert_str_eq(ri->uri, "200");
  857. i = mg_read(client_conn, buf, sizeof(buf));
  858. ck_assert_int_eq(i, 17);
  859. if ((i >= 0) && (i < (int)sizeof(buf))) {
  860. buf[i] = 0;
  861. }
  862. ck_assert_str_eq(buf, plain_file_content);
  863. #endif
  864. mg_close_connection(client_conn);
  865. /* Get zipped static data - will not work if Accept-Encoding is not set */
  866. client_conn = mg_download("localhost",
  867. ipv4_port,
  868. 0,
  869. ebuf,
  870. sizeof(ebuf),
  871. "%s",
  872. "GET /test_gz.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. ck_assert_str_eq(ri->uri, "404");
  877. mg_close_connection(client_conn);
  878. /* Get zipped static data - with Accept-Encoding */
  879. client_conn = mg_download(
  880. "localhost",
  881. ipv4_port,
  882. 0,
  883. ebuf,
  884. sizeof(ebuf),
  885. "%s",
  886. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  887. ck_assert(client_conn != NULL);
  888. ri = mg_get_request_info(client_conn);
  889. ck_assert(ri != NULL);
  890. #if defined(NO_FILES)
  891. ck_assert_str_eq(ri->uri, "404");
  892. #else
  893. ck_assert_str_eq(ri->uri, "200");
  894. i = mg_read(client_conn, buf, sizeof(buf));
  895. ck_assert_int_eq(i, 52);
  896. if ((i >= 0) && (i < (int)sizeof(buf))) {
  897. buf[i] = 0;
  898. }
  899. ck_assert_int_eq(ri->content_length, 52);
  900. ck_assert_str_eq(buf, encoded_file_content);
  901. #endif
  902. mg_close_connection(client_conn);
  903. /* Get CGI generated data */
  904. #if !defined(NO_CGI)
  905. client_conn = mg_download("localhost",
  906. ipv4_port,
  907. 0,
  908. ebuf,
  909. sizeof(ebuf),
  910. "%s",
  911. "GET /test.cgi HTTP/1.0\r\n\r\n");
  912. ck_assert(client_conn != NULL);
  913. ri = mg_get_request_info(client_conn);
  914. ck_assert(ri != NULL);
  915. #if defined(NO_FILES)
  916. ck_assert_str_eq(ri->uri, "404");
  917. (void)expected_cgi_result;
  918. (void)cgi_script_content;
  919. #else
  920. i = mg_read(client_conn, buf, sizeof(buf));
  921. if ((i >= 0) && (i < (int)sizeof(buf))) {
  922. while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) {
  923. i--;
  924. }
  925. buf[i] = 0;
  926. }
  927. /* ck_assert_int_eq(i, (int)strlen(expected_cgi_result)); */
  928. ck_assert_str_eq(buf, expected_cgi_result);
  929. ck_assert_str_eq(ri->uri, "200");
  930. mg_close_connection(client_conn);
  931. #endif
  932. #else
  933. (void)expected_cgi_result;
  934. (void)cgi_script_content;
  935. #endif
  936. /* Get directory listing */
  937. client_conn = mg_download("localhost",
  938. ipv4_port,
  939. 0,
  940. ebuf,
  941. sizeof(ebuf),
  942. "%s",
  943. "GET / HTTP/1.0\r\n\r\n");
  944. ck_assert(client_conn != NULL);
  945. ri = mg_get_request_info(client_conn);
  946. ck_assert(ri != NULL);
  947. #if defined(NO_FILES)
  948. ck_assert_str_eq(ri->uri, "404");
  949. #else
  950. ck_assert_str_eq(ri->uri, "200");
  951. i = mg_read(client_conn, buf, sizeof(buf));
  952. ck_assert(i > 6);
  953. buf[6] = 0;
  954. ck_assert_str_eq(buf, "<html>");
  955. #endif
  956. mg_close_connection(client_conn);
  957. /* POST to static file (will not work) */
  958. client_conn = mg_download("localhost",
  959. ipv4_port,
  960. 0,
  961. ebuf,
  962. sizeof(ebuf),
  963. "%s",
  964. "POST /test.txt HTTP/1.0\r\n\r\n");
  965. ck_assert(client_conn != NULL);
  966. ri = mg_get_request_info(client_conn);
  967. ck_assert(ri != NULL);
  968. #if defined(NO_FILES)
  969. ck_assert_str_eq(ri->uri, "404");
  970. #else
  971. ck_assert_str_eq(ri->uri, "405");
  972. i = mg_read(client_conn, buf, sizeof(buf));
  973. ck_assert(i >= 29);
  974. buf[29] = 0;
  975. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  976. #endif
  977. mg_close_connection(client_conn);
  978. /* PUT to static file (will not work) */
  979. client_conn = mg_download("localhost",
  980. ipv4_port,
  981. 0,
  982. ebuf,
  983. sizeof(ebuf),
  984. "%s",
  985. "PUT /test.txt HTTP/1.0\r\n\r\n");
  986. ck_assert(client_conn != NULL);
  987. ri = mg_get_request_info(client_conn);
  988. ck_assert(ri != NULL);
  989. #if defined(NO_FILES)
  990. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  991. #else
  992. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  993. #endif
  994. mg_close_connection(client_conn);
  995. /* Websocket test */
  996. #ifdef USE_WEBSOCKET
  997. /* Then connect a first client */
  998. ws_client1_conn =
  999. mg_connect_websocket_client("localhost",
  1000. ipv4_port,
  1001. 0,
  1002. ebuf,
  1003. sizeof(ebuf),
  1004. "/websocket",
  1005. NULL,
  1006. websocket_client_data_handler,
  1007. websocket_client_close_handler,
  1008. &ws_client1_data);
  1009. ck_assert(ws_client1_conn != NULL);
  1010. wait_not_null(
  1011. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  1012. ck_assert_int_eq(ws_client1_data.closed, 0);
  1013. ck_assert_int_eq(ws_client2_data.closed, 0);
  1014. ck_assert_int_eq(ws_client3_data.closed, 0);
  1015. ck_assert(ws_client2_data.data == NULL);
  1016. ck_assert_uint_eq(ws_client2_data.len, 0);
  1017. ck_assert(ws_client1_data.data != NULL);
  1018. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  1019. ck_assert(!memcmp(ws_client1_data.data,
  1020. websocket_welcome_msg,
  1021. websocket_welcome_msg_len));
  1022. free(ws_client1_data.data);
  1023. ws_client1_data.data = NULL;
  1024. ws_client1_data.len = 0;
  1025. mg_websocket_client_write(ws_client1_conn,
  1026. WEBSOCKET_OPCODE_TEXT,
  1027. "data1",
  1028. 5);
  1029. wait_not_null(
  1030. &(ws_client1_data
  1031. .data)); /* Wait for the websocket acknowledge message */
  1032. ck_assert_int_eq(ws_client1_data.closed, 0);
  1033. ck_assert_int_eq(ws_client2_data.closed, 0);
  1034. ck_assert(ws_client2_data.data == NULL);
  1035. ck_assert_uint_eq(ws_client2_data.len, 0);
  1036. ck_assert(ws_client1_data.data != NULL);
  1037. ck_assert_uint_eq(ws_client1_data.len, 3);
  1038. ck_assert(!memcmp(ws_client1_data.data, "ok1", 3));
  1039. free(ws_client1_data.data);
  1040. ws_client1_data.data = NULL;
  1041. ws_client1_data.len = 0;
  1042. /* Now connect a second client */
  1043. #ifdef USE_IPV6
  1044. ws_client2_conn =
  1045. mg_connect_websocket_client("[::1]",
  1046. ipv6_port,
  1047. 0,
  1048. ebuf,
  1049. sizeof(ebuf),
  1050. "/websocket",
  1051. NULL,
  1052. websocket_client_data_handler,
  1053. websocket_client_close_handler,
  1054. &ws_client2_data);
  1055. #else
  1056. ws_client2_conn =
  1057. mg_connect_websocket_client("127.0.0.1",
  1058. ipv4_port,
  1059. 0,
  1060. ebuf,
  1061. sizeof(ebuf),
  1062. "/websocket",
  1063. NULL,
  1064. websocket_client_data_handler,
  1065. websocket_client_close_handler,
  1066. &ws_client2_data);
  1067. #endif
  1068. ck_assert(ws_client2_conn != NULL);
  1069. wait_not_null(
  1070. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  1071. ck_assert(ws_client1_data.closed == 0);
  1072. ck_assert(ws_client2_data.closed == 0);
  1073. ck_assert(ws_client1_data.data == NULL);
  1074. ck_assert(ws_client1_data.len == 0);
  1075. ck_assert(ws_client2_data.data != NULL);
  1076. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  1077. ck_assert(!memcmp(ws_client2_data.data,
  1078. websocket_welcome_msg,
  1079. websocket_welcome_msg_len));
  1080. free(ws_client2_data.data);
  1081. ws_client2_data.data = NULL;
  1082. ws_client2_data.len = 0;
  1083. mg_websocket_client_write(ws_client1_conn,
  1084. WEBSOCKET_OPCODE_TEXT,
  1085. "data2",
  1086. 5);
  1087. wait_not_null(
  1088. &(ws_client1_data
  1089. .data)); /* Wait for the websocket acknowledge message */
  1090. ck_assert(ws_client1_data.closed == 0);
  1091. ck_assert(ws_client2_data.closed == 0);
  1092. ck_assert(ws_client2_data.data == NULL);
  1093. ck_assert(ws_client2_data.len == 0);
  1094. ck_assert(ws_client1_data.data != NULL);
  1095. ck_assert(ws_client1_data.len == 4);
  1096. ck_assert(!memcmp(ws_client1_data.data, "ok 2", 4));
  1097. free(ws_client1_data.data);
  1098. ws_client1_data.data = NULL;
  1099. ws_client1_data.len = 0;
  1100. mg_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1101. wait_not_null(
  1102. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1103. ck_assert(ws_client1_data.closed == 0);
  1104. ck_assert(ws_client2_data.closed == 0);
  1105. ck_assert(ws_client2_data.data == NULL);
  1106. ck_assert(ws_client2_data.len == 0);
  1107. ck_assert(ws_client1_data.data != NULL);
  1108. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1109. ck_assert(!memcmp(ws_client1_data.data,
  1110. websocket_goodbye_msg,
  1111. websocket_goodbye_msg_len));
  1112. free(ws_client1_data.data);
  1113. ws_client1_data.data = NULL;
  1114. ws_client1_data.len = 0;
  1115. mg_close_connection(ws_client1_conn);
  1116. test_sleep(3); /* Won't get any message */
  1117. ck_assert(ws_client1_data.closed == 1);
  1118. ck_assert(ws_client2_data.closed == 0);
  1119. ck_assert(ws_client1_data.data == NULL);
  1120. ck_assert(ws_client1_data.len == 0);
  1121. ck_assert(ws_client2_data.data == NULL);
  1122. ck_assert(ws_client2_data.len == 0);
  1123. mg_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1124. wait_not_null(
  1125. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1126. ck_assert(ws_client1_data.closed == 1);
  1127. ck_assert(ws_client2_data.closed == 0);
  1128. ck_assert(ws_client1_data.data == NULL);
  1129. ck_assert(ws_client1_data.len == 0);
  1130. ck_assert(ws_client2_data.data != NULL);
  1131. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1132. ck_assert(!memcmp(ws_client2_data.data,
  1133. websocket_goodbye_msg,
  1134. websocket_goodbye_msg_len));
  1135. free(ws_client2_data.data);
  1136. ws_client2_data.data = NULL;
  1137. ws_client2_data.len = 0;
  1138. mg_close_connection(ws_client2_conn);
  1139. test_sleep(3); /* Won't get any message */
  1140. ck_assert(ws_client1_data.closed == 1);
  1141. ck_assert(ws_client2_data.closed == 1);
  1142. ck_assert(ws_client1_data.data == NULL);
  1143. ck_assert(ws_client1_data.len == 0);
  1144. ck_assert(ws_client2_data.data == NULL);
  1145. ck_assert(ws_client2_data.len == 0);
  1146. /* Connect client 3 */
  1147. ws_client3_conn =
  1148. mg_connect_websocket_client("localhost",
  1149. #if defined(NO_SSL)
  1150. ipv4_port,
  1151. 0,
  1152. #else
  1153. ipv4s_port,
  1154. 1,
  1155. #endif
  1156. ebuf,
  1157. sizeof(ebuf),
  1158. "/websocket",
  1159. NULL,
  1160. websocket_client_data_handler,
  1161. websocket_client_close_handler,
  1162. &ws_client3_data);
  1163. ck_assert(ws_client3_conn != NULL);
  1164. wait_not_null(
  1165. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1166. ck_assert(ws_client1_data.closed == 1);
  1167. ck_assert(ws_client2_data.closed == 1);
  1168. ck_assert(ws_client3_data.closed == 0);
  1169. ck_assert(ws_client1_data.data == NULL);
  1170. ck_assert(ws_client1_data.len == 0);
  1171. ck_assert(ws_client2_data.data == NULL);
  1172. ck_assert(ws_client2_data.len == 0);
  1173. ck_assert(ws_client3_data.data != NULL);
  1174. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1175. ck_assert(!memcmp(ws_client3_data.data,
  1176. websocket_welcome_msg,
  1177. websocket_welcome_msg_len));
  1178. free(ws_client3_data.data);
  1179. ws_client3_data.data = NULL;
  1180. ws_client3_data.len = 0;
  1181. #endif
  1182. /* Close the server */
  1183. g_ctx = NULL;
  1184. mg_stop(ctx);
  1185. #ifdef USE_WEBSOCKET
  1186. for (i = 0; i < 100; i++) {
  1187. test_sleep(1);
  1188. if (ws_client3_data.closed != 0) {
  1189. break;
  1190. }
  1191. }
  1192. ck_assert_int_eq(ws_client3_data.closed, 1);
  1193. #endif
  1194. }
  1195. END_TEST
  1196. Suite *
  1197. make_public_server_suite(void)
  1198. {
  1199. Suite *const suite = suite_create("PublicServer");
  1200. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  1201. TCase *const tcase_startthreads = tcase_create("Start threads");
  1202. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  1203. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  1204. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  1205. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  1206. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  1207. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  1208. suite_add_tcase(suite, tcase_checktestenv);
  1209. tcase_add_test(tcase_startthreads, test_threading);
  1210. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  1211. suite_add_tcase(suite, tcase_startthreads);
  1212. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  1213. tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout);
  1214. suite_add_tcase(suite, tcase_startstophttp);
  1215. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  1216. tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout);
  1217. suite_add_tcase(suite, tcase_startstophttps);
  1218. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  1219. tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout);
  1220. suite_add_tcase(suite, tcase_serverandclienttls);
  1221. tcase_add_test(tcase_serverrequests, test_request_handlers);
  1222. tcase_set_timeout(tcase_serverrequests, 120);
  1223. suite_add_tcase(suite, tcase_serverrequests);
  1224. return suite;
  1225. }
  1226. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  1227. /* Used to debug test cases without using the check framework */
  1228. static int chk_ok = 0;
  1229. static int chk_failed = 0;
  1230. void
  1231. main(void)
  1232. {
  1233. test_the_test_environment(0);
  1234. test_threading(0);
  1235. test_mg_start_stop_http_server(0);
  1236. test_mg_start_stop_https_server(0);
  1237. test_request_handlers(0);
  1238. test_mg_server_and_client_tls(0);
  1239. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  1240. }
  1241. void
  1242. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  1243. {
  1244. va_list va;
  1245. va_start(va, expr);
  1246. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  1247. vfprintf(stderr, expr, va);
  1248. fprintf(stderr, "\n\n");
  1249. va_end(va);
  1250. chk_failed++;
  1251. }
  1252. void
  1253. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  1254. {
  1255. va_list va;
  1256. if (cond) {
  1257. chk_ok++;
  1258. return;
  1259. }
  1260. va_start(va, expr);
  1261. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  1262. vfprintf(stderr, expr, va);
  1263. fprintf(stderr, "\n\n");
  1264. va_end(va);
  1265. chk_failed++;
  1266. }
  1267. void
  1268. _mark_point(const char *file, int line)
  1269. {
  1270. chk_ok++;
  1271. }
  1272. void
  1273. tcase_fn_start(const char *fname, const char *file, int line)
  1274. {
  1275. }
  1276. void suite_add_tcase(Suite *s, TCase *tc){};
  1277. void _tcase_add_test(TCase *tc,
  1278. TFun tf,
  1279. const char *fname,
  1280. int _signal,
  1281. int allowed_exit_value,
  1282. int start,
  1283. int end){};
  1284. TCase *
  1285. tcase_create(const char *name)
  1286. {
  1287. return NULL;
  1288. };
  1289. Suite *
  1290. suite_create(const char *name)
  1291. {
  1292. return NULL;
  1293. };
  1294. void tcase_set_timeout(TCase *tc, double timeout){};
  1295. #endif