public_server.c 40 KB

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