public_server.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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. int opt_idx = 0;
  624. #if !defined(NO_SSL)
  625. const char *ssl_cert = locate_ssl_cert();
  626. #endif
  627. #if defined(USE_WEBSOCKET)
  628. struct tclient_data ws_client1_data = {NULL, 0, 0};
  629. struct tclient_data ws_client2_data = {NULL, 0, 0};
  630. struct tclient_data ws_client3_data = {NULL, 0, 0};
  631. struct mg_connection *ws_client1_conn = NULL;
  632. struct mg_connection *ws_client2_conn = NULL;
  633. struct mg_connection *ws_client3_conn = NULL;
  634. #endif
  635. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  636. OPTIONS[opt_idx++] = "listening_ports";
  637. OPTIONS[opt_idx++] = HTTP_PORT;
  638. #if !defined(NO_FILES)
  639. OPTIONS[opt_idx++] = "document_root";
  640. OPTIONS[opt_idx++] = ".";
  641. #endif
  642. #ifndef NO_SSL
  643. ck_assert(ssl_cert != NULL);
  644. OPTIONS[opt_idx++] = "ssl_certificate";
  645. OPTIONS[opt_idx++] = ssl_cert;
  646. #endif
  647. ck_assert_int_le(opt_idx, (int)(sizeof(OPTIONS) / sizeof(OPTIONS[0])));
  648. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  649. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  650. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  651. ck_assert(ctx != NULL);
  652. g_ctx = ctx;
  653. opt = mg_get_option(ctx, "listening_ports");
  654. ck_assert_str_eq(opt, HTTP_PORT);
  655. opt = mg_get_option(ctx, "cgi_environment");
  656. ck_assert_str_eq(opt, "");
  657. opt = mg_get_option(ctx, "unknown_option_name");
  658. ck_assert(opt == NULL);
  659. for (i = 0; i < 1000; i++) {
  660. sprintf(uri, "/U%u", i);
  661. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  662. }
  663. for (i = 500; i < 800; i++) {
  664. sprintf(uri, "/U%u", i);
  665. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  666. }
  667. for (i = 600; i >= 0; i--) {
  668. sprintf(uri, "/U%u", i);
  669. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  670. }
  671. for (i = 750; i <= 1000; i++) {
  672. sprintf(uri, "/U%u", i);
  673. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  674. }
  675. for (i = 5; i < 9; i++) {
  676. sprintf(uri, "/U%u", i);
  677. mg_set_request_handler(ctx,
  678. uri,
  679. request_test_handler,
  680. (void *)(ptrdiff_t)i);
  681. }
  682. #ifdef USE_WEBSOCKET
  683. mg_set_websocket_handler(ctx,
  684. "/websocket",
  685. websock_server_connect,
  686. websock_server_ready,
  687. websock_server_data,
  688. websock_server_close,
  689. (void *)7531);
  690. #endif
  691. /* Try to load non existing file */
  692. client_conn = mg_download("localhost",
  693. ipv4_port,
  694. 0,
  695. ebuf,
  696. sizeof(ebuf),
  697. "%s",
  698. "GET /file/not/found HTTP/1.0\r\n\r\n");
  699. ck_assert(client_conn != NULL);
  700. ri = mg_get_request_info(client_conn);
  701. ck_assert(ri != NULL);
  702. ck_assert_str_eq(ri->uri, "404");
  703. mg_close_connection(client_conn);
  704. /* Get data from callback */
  705. client_conn = mg_download(
  706. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  707. ck_assert(client_conn != NULL);
  708. ri = mg_get_request_info(client_conn);
  709. ck_assert(ri != NULL);
  710. ck_assert_str_eq(ri->uri, "200");
  711. i = mg_read(client_conn, buf, sizeof(buf));
  712. ck_assert_int_eq(i, (int)strlen(expected));
  713. buf[i] = 0;
  714. ck_assert_str_eq(buf, expected);
  715. mg_close_connection(client_conn);
  716. /* Get data from callback using http://127.0.0.1 */
  717. client_conn = mg_download(
  718. "127.0.0.1", ipv4_port, 0, 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. #if defined(USE_IPV6)
  729. /* Get data from callback using http://[::1] */
  730. client_conn =
  731. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  732. ck_assert(client_conn != NULL);
  733. ri = mg_get_request_info(client_conn);
  734. ck_assert(ri != NULL);
  735. ck_assert_str_eq(ri->uri, "200");
  736. i = mg_read(client_conn, buf, sizeof(buf));
  737. ck_assert_int_eq(i, (int)strlen(expected));
  738. buf[i] = 0;
  739. ck_assert_str_eq(buf, expected);
  740. mg_close_connection(client_conn);
  741. #endif
  742. #if !defined(NO_SSL)
  743. /* Get data from callback using https://127.0.0.1 */
  744. client_conn = mg_download(
  745. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  746. ck_assert(client_conn != NULL);
  747. ri = mg_get_request_info(client_conn);
  748. ck_assert(ri != NULL);
  749. ck_assert_str_eq(ri->uri, "200");
  750. i = mg_read(client_conn, buf, sizeof(buf));
  751. ck_assert_int_eq(i, (int)strlen(expected));
  752. buf[i] = 0;
  753. ck_assert_str_eq(buf, expected);
  754. mg_close_connection(client_conn);
  755. /* Get redirect from callback using http://127.0.0.1 */
  756. client_conn = mg_download(
  757. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  758. ck_assert(client_conn != NULL);
  759. ri = mg_get_request_info(client_conn);
  760. ck_assert(ri != NULL);
  761. ck_assert_str_eq(ri->uri, "302");
  762. i = mg_read(client_conn, buf, sizeof(buf));
  763. ck_assert_int_eq(i, -1);
  764. mg_close_connection(client_conn);
  765. #endif
  766. #if defined(USE_IPV6) && !defined(NO_SSL)
  767. /* Get data from callback using https://[::1] */
  768. client_conn =
  769. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  770. ck_assert(client_conn != NULL);
  771. ri = mg_get_request_info(client_conn);
  772. ck_assert(ri != NULL);
  773. ck_assert_str_eq(ri->uri, "200");
  774. i = mg_read(client_conn, buf, sizeof(buf));
  775. ck_assert_int_eq(i, (int)strlen(expected));
  776. buf[i] = 0;
  777. ck_assert_str_eq(buf, expected);
  778. mg_close_connection(client_conn);
  779. /* Get redirect from callback using http://127.0.0.1 */
  780. client_conn =
  781. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  782. ck_assert(client_conn != NULL);
  783. ri = mg_get_request_info(client_conn);
  784. ck_assert(ri != NULL);
  785. ck_assert_str_eq(ri->uri, "302");
  786. i = mg_read(client_conn, buf, sizeof(buf));
  787. ck_assert_int_eq(i, -1);
  788. mg_close_connection(client_conn);
  789. #endif
  790. /* It seems to be impossible to find out what the actual working
  791. * directory of the CI test environment is. Before breaking another
  792. * dozen of builds by trying blindly with different paths, just
  793. * create the file here */
  794. #ifdef _WIN32
  795. f = fopen("test.txt", "wb");
  796. #else
  797. f = fopen("test.txt", "w");
  798. #endif
  799. plain_file_content = "simple text file\n";
  800. fwrite(plain_file_content, 17, 1, f);
  801. fclose(f);
  802. #ifdef _WIN32
  803. f = fopen("test_gz.txt.gz", "wb");
  804. #else
  805. f = fopen("test_gz.txt.gz", "w");
  806. #endif
  807. encoded_file_content = "\x1f\x8b\x08\x08\xf8\x9d\xcb\x55\x00\x00"
  808. "test_gz.txt"
  809. "\x00\x01\x11\x00\xee\xff"
  810. "zipped text file"
  811. "\x0a\x34\x5f\xcc\x49\x11\x00\x00\x00";
  812. fwrite(encoded_file_content, 1, 52, f);
  813. fclose(f);
  814. /* Get static data */
  815. client_conn = mg_download("localhost",
  816. ipv4_port,
  817. 0,
  818. ebuf,
  819. sizeof(ebuf),
  820. "%s",
  821. "GET /test.txt HTTP/1.0\r\n\r\n");
  822. ck_assert(client_conn != NULL);
  823. ri = mg_get_request_info(client_conn);
  824. ck_assert(ri != NULL);
  825. #if defined(NO_FILES)
  826. ck_assert_str_eq(ri->uri, "404");
  827. #else
  828. ck_assert_str_eq(ri->uri, "200");
  829. i = mg_read(client_conn, buf, sizeof(buf));
  830. ck_assert_int_eq(i, 17);
  831. if ((i >= 0) && (i < (int)sizeof(buf))) {
  832. buf[i] = 0;
  833. }
  834. ck_assert_str_eq(buf, plain_file_content);
  835. #endif
  836. mg_close_connection(client_conn);
  837. /* Get zipped static data - will not work if Accept-Encoding is not set */
  838. client_conn = mg_download("localhost",
  839. ipv4_port,
  840. 0,
  841. ebuf,
  842. sizeof(ebuf),
  843. "%s",
  844. "GET /test_gz.txt HTTP/1.0\r\n\r\n");
  845. ck_assert(client_conn != NULL);
  846. ri = mg_get_request_info(client_conn);
  847. ck_assert(ri != NULL);
  848. ck_assert_str_eq(ri->uri, "404");
  849. mg_close_connection(client_conn);
  850. /* Get zipped static data - with Accept-Encoding */
  851. client_conn = mg_download(
  852. "localhost",
  853. ipv4_port,
  854. 0,
  855. ebuf,
  856. sizeof(ebuf),
  857. "%s",
  858. "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n");
  859. ck_assert(client_conn != NULL);
  860. ri = mg_get_request_info(client_conn);
  861. ck_assert(ri != NULL);
  862. #if defined(NO_FILES)
  863. ck_assert_str_eq(ri->uri, "404");
  864. #else
  865. ck_assert_str_eq(ri->uri, "200");
  866. i = mg_read(client_conn, buf, sizeof(buf));
  867. ck_assert_int_eq(i, 52);
  868. if ((i >= 0) && (i < (int)sizeof(buf))) {
  869. buf[i] = 0;
  870. }
  871. ck_assert_int_eq(ri->content_length, 52);
  872. ck_assert_str_eq(buf, encoded_file_content);
  873. #endif
  874. mg_close_connection(client_conn);
  875. /* Get directory listing */
  876. client_conn = mg_download("localhost",
  877. ipv4_port,
  878. 0,
  879. ebuf,
  880. sizeof(ebuf),
  881. "%s",
  882. "GET / HTTP/1.0\r\n\r\n");
  883. ck_assert(client_conn != NULL);
  884. ri = mg_get_request_info(client_conn);
  885. ck_assert(ri != NULL);
  886. #if defined(NO_FILES)
  887. ck_assert_str_eq(ri->uri, "404");
  888. #else
  889. ck_assert_str_eq(ri->uri, "200");
  890. i = mg_read(client_conn, buf, sizeof(buf));
  891. ck_assert(i > 6);
  892. buf[6] = 0;
  893. ck_assert_str_eq(buf, "<html>");
  894. #endif
  895. mg_close_connection(client_conn);
  896. /* POST to static file (will not work) */
  897. client_conn = mg_download("localhost",
  898. ipv4_port,
  899. 0,
  900. ebuf,
  901. sizeof(ebuf),
  902. "%s",
  903. "POST /test.txt HTTP/1.0\r\n\r\n");
  904. ck_assert(client_conn != NULL);
  905. ri = mg_get_request_info(client_conn);
  906. ck_assert(ri != NULL);
  907. #if defined(NO_FILES)
  908. ck_assert_str_eq(ri->uri, "404");
  909. #else
  910. ck_assert_str_eq(ri->uri, "405");
  911. i = mg_read(client_conn, buf, sizeof(buf));
  912. ck_assert(i >= 29);
  913. buf[29] = 0;
  914. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  915. #endif
  916. mg_close_connection(client_conn);
  917. /* PUT to static file (will not work) */
  918. client_conn = mg_download("localhost",
  919. ipv4_port,
  920. 0,
  921. ebuf,
  922. sizeof(ebuf),
  923. "%s",
  924. "PUT /test.txt HTTP/1.0\r\n\r\n");
  925. ck_assert(client_conn != NULL);
  926. ri = mg_get_request_info(client_conn);
  927. ck_assert(ri != NULL);
  928. #if defined(NO_FILES)
  929. ck_assert_str_eq(ri->uri, "405"); /* method not allowed */
  930. #else
  931. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  932. #endif
  933. mg_close_connection(client_conn);
  934. /* Websocket test */
  935. #ifdef USE_WEBSOCKET
  936. /* Then connect a first client */
  937. ws_client1_conn =
  938. mg_connect_websocket_client("localhost",
  939. ipv4_port,
  940. 0,
  941. ebuf,
  942. sizeof(ebuf),
  943. "/websocket",
  944. NULL,
  945. websocket_client_data_handler,
  946. websocket_client_close_handler,
  947. &ws_client1_data);
  948. ck_assert(ws_client1_conn != NULL);
  949. wait_not_null(
  950. &(ws_client1_data.data)); /* Wait for the websocket welcome message */
  951. ck_assert_int_eq(ws_client1_data.closed, 0);
  952. ck_assert_int_eq(ws_client2_data.closed, 0);
  953. ck_assert_int_eq(ws_client3_data.closed, 0);
  954. ck_assert(ws_client2_data.data == NULL);
  955. ck_assert_uint_eq(ws_client2_data.len, 0);
  956. ck_assert(ws_client1_data.data != NULL);
  957. ck_assert_uint_eq(ws_client1_data.len, websocket_welcome_msg_len);
  958. ck_assert(!memcmp(ws_client1_data.data,
  959. websocket_welcome_msg,
  960. websocket_welcome_msg_len));
  961. free(ws_client1_data.data);
  962. ws_client1_data.data = NULL;
  963. ws_client1_data.len = 0;
  964. mg_websocket_client_write(ws_client1_conn,
  965. WEBSOCKET_OPCODE_TEXT,
  966. "data1",
  967. 5);
  968. wait_not_null(
  969. &(ws_client1_data
  970. .data)); /* Wait for the websocket acknowledge message */
  971. ck_assert_int_eq(ws_client1_data.closed, 0);
  972. ck_assert_int_eq(ws_client2_data.closed, 0);
  973. ck_assert(ws_client2_data.data == NULL);
  974. ck_assert_uint_eq(ws_client2_data.len, 0);
  975. ck_assert(ws_client1_data.data != NULL);
  976. ck_assert_uint_eq(ws_client1_data.len, 3);
  977. ck_assert(!memcmp(ws_client1_data.data, "ok1", 3));
  978. free(ws_client1_data.data);
  979. ws_client1_data.data = NULL;
  980. ws_client1_data.len = 0;
  981. /* Now connect a second client */
  982. #ifdef USE_IPV6
  983. ws_client2_conn =
  984. mg_connect_websocket_client("[::1]",
  985. ipv6_port,
  986. 0,
  987. ebuf,
  988. sizeof(ebuf),
  989. "/websocket",
  990. NULL,
  991. websocket_client_data_handler,
  992. websocket_client_close_handler,
  993. &ws_client2_data);
  994. #else
  995. ws_client2_conn =
  996. mg_connect_websocket_client("127.0.0.1",
  997. ipv4_port,
  998. 0,
  999. ebuf,
  1000. sizeof(ebuf),
  1001. "/websocket",
  1002. NULL,
  1003. websocket_client_data_handler,
  1004. websocket_client_close_handler,
  1005. &ws_client2_data);
  1006. #endif
  1007. ck_assert(ws_client2_conn != NULL);
  1008. wait_not_null(
  1009. &(ws_client2_data.data)); /* Wait for the websocket welcome message */
  1010. ck_assert(ws_client1_data.closed == 0);
  1011. ck_assert(ws_client2_data.closed == 0);
  1012. ck_assert(ws_client1_data.data == NULL);
  1013. ck_assert(ws_client1_data.len == 0);
  1014. ck_assert(ws_client2_data.data != NULL);
  1015. ck_assert(ws_client2_data.len == websocket_welcome_msg_len);
  1016. ck_assert(!memcmp(ws_client2_data.data,
  1017. websocket_welcome_msg,
  1018. websocket_welcome_msg_len));
  1019. free(ws_client2_data.data);
  1020. ws_client2_data.data = NULL;
  1021. ws_client2_data.len = 0;
  1022. mg_websocket_client_write(ws_client1_conn,
  1023. WEBSOCKET_OPCODE_TEXT,
  1024. "data2",
  1025. 5);
  1026. wait_not_null(
  1027. &(ws_client1_data
  1028. .data)); /* Wait for the websocket acknowledge message */
  1029. ck_assert(ws_client1_data.closed == 0);
  1030. ck_assert(ws_client2_data.closed == 0);
  1031. ck_assert(ws_client2_data.data == NULL);
  1032. ck_assert(ws_client2_data.len == 0);
  1033. ck_assert(ws_client1_data.data != NULL);
  1034. ck_assert(ws_client1_data.len == 4);
  1035. ck_assert(!memcmp(ws_client1_data.data, "ok 2", 4));
  1036. free(ws_client1_data.data);
  1037. ws_client1_data.data = NULL;
  1038. ws_client1_data.len = 0;
  1039. mg_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1040. wait_not_null(
  1041. &(ws_client1_data.data)); /* Wait for the websocket goodbye message */
  1042. ck_assert(ws_client1_data.closed == 0);
  1043. ck_assert(ws_client2_data.closed == 0);
  1044. ck_assert(ws_client2_data.data == NULL);
  1045. ck_assert(ws_client2_data.len == 0);
  1046. ck_assert(ws_client1_data.data != NULL);
  1047. ck_assert(ws_client1_data.len == websocket_goodbye_msg_len);
  1048. ck_assert(!memcmp(ws_client1_data.data,
  1049. websocket_goodbye_msg,
  1050. websocket_goodbye_msg_len));
  1051. free(ws_client1_data.data);
  1052. ws_client1_data.data = NULL;
  1053. ws_client1_data.len = 0;
  1054. mg_close_connection(ws_client1_conn);
  1055. test_sleep(3); /* Won't get any message */
  1056. ck_assert(ws_client1_data.closed == 1);
  1057. ck_assert(ws_client2_data.closed == 0);
  1058. ck_assert(ws_client1_data.data == NULL);
  1059. ck_assert(ws_client1_data.len == 0);
  1060. ck_assert(ws_client2_data.data == NULL);
  1061. ck_assert(ws_client2_data.len == 0);
  1062. mg_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3);
  1063. wait_not_null(
  1064. &(ws_client2_data.data)); /* Wait for the websocket goodbye message */
  1065. ck_assert(ws_client1_data.closed == 1);
  1066. ck_assert(ws_client2_data.closed == 0);
  1067. ck_assert(ws_client1_data.data == NULL);
  1068. ck_assert(ws_client1_data.len == 0);
  1069. ck_assert(ws_client2_data.data != NULL);
  1070. ck_assert(ws_client2_data.len == websocket_goodbye_msg_len);
  1071. ck_assert(!memcmp(ws_client2_data.data,
  1072. websocket_goodbye_msg,
  1073. websocket_goodbye_msg_len));
  1074. free(ws_client2_data.data);
  1075. ws_client2_data.data = NULL;
  1076. ws_client2_data.len = 0;
  1077. mg_close_connection(ws_client2_conn);
  1078. test_sleep(3); /* Won't get any message */
  1079. ck_assert(ws_client1_data.closed == 1);
  1080. ck_assert(ws_client2_data.closed == 1);
  1081. ck_assert(ws_client1_data.data == NULL);
  1082. ck_assert(ws_client1_data.len == 0);
  1083. ck_assert(ws_client2_data.data == NULL);
  1084. ck_assert(ws_client2_data.len == 0);
  1085. /* Connect client 3 */
  1086. ws_client3_conn =
  1087. mg_connect_websocket_client("localhost",
  1088. #if defined(NO_SSL)
  1089. ipv4_port,
  1090. 0,
  1091. #else
  1092. ipv4s_port,
  1093. 1,
  1094. #endif
  1095. ebuf,
  1096. sizeof(ebuf),
  1097. "/websocket",
  1098. NULL,
  1099. websocket_client_data_handler,
  1100. websocket_client_close_handler,
  1101. &ws_client3_data);
  1102. ck_assert(ws_client3_conn != NULL);
  1103. wait_not_null(
  1104. &(ws_client3_data.data)); /* Wait for the websocket welcome message */
  1105. ck_assert(ws_client1_data.closed == 1);
  1106. ck_assert(ws_client2_data.closed == 1);
  1107. ck_assert(ws_client3_data.closed == 0);
  1108. ck_assert(ws_client1_data.data == NULL);
  1109. ck_assert(ws_client1_data.len == 0);
  1110. ck_assert(ws_client2_data.data == NULL);
  1111. ck_assert(ws_client2_data.len == 0);
  1112. ck_assert(ws_client3_data.data != NULL);
  1113. ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
  1114. ck_assert(!memcmp(ws_client3_data.data,
  1115. websocket_welcome_msg,
  1116. websocket_welcome_msg_len));
  1117. free(ws_client3_data.data);
  1118. ws_client3_data.data = NULL;
  1119. ws_client3_data.len = 0;
  1120. #endif
  1121. /* Close the server */
  1122. g_ctx = NULL;
  1123. mg_stop(ctx);
  1124. #ifdef USE_WEBSOCKET
  1125. for (i = 0; i < 100; i++) {
  1126. test_sleep(1);
  1127. if (ws_client3_data.closed != 0) {
  1128. break;
  1129. }
  1130. }
  1131. ck_assert_int_eq(ws_client3_data.closed, 1);
  1132. #endif
  1133. }
  1134. END_TEST
  1135. Suite *
  1136. make_public_server_suite(void)
  1137. {
  1138. Suite *const suite = suite_create("PublicServer");
  1139. TCase *const tcase_checktestenv = tcase_create("Check test environment");
  1140. TCase *const tcase_startthreads = tcase_create("Start threads");
  1141. TCase *const tcase_startstophttp = tcase_create("Start Stop HTTP Server");
  1142. TCase *const tcase_startstophttps = tcase_create("Start Stop HTTPS Server");
  1143. TCase *const tcase_serverandclienttls = tcase_create("TLS Server Client");
  1144. TCase *const tcase_serverrequests = tcase_create("Server Requests");
  1145. tcase_add_test(tcase_checktestenv, test_the_test_environment);
  1146. tcase_set_timeout(tcase_checktestenv, civetweb_min_test_timeout);
  1147. suite_add_tcase(suite, tcase_checktestenv);
  1148. tcase_add_test(tcase_startthreads, test_threading);
  1149. tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout);
  1150. suite_add_tcase(suite, tcase_startthreads);
  1151. tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server);
  1152. tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout);
  1153. suite_add_tcase(suite, tcase_startstophttp);
  1154. tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server);
  1155. tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout);
  1156. suite_add_tcase(suite, tcase_startstophttps);
  1157. tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls);
  1158. tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout);
  1159. suite_add_tcase(suite, tcase_serverandclienttls);
  1160. tcase_add_test(tcase_serverrequests, test_request_handlers);
  1161. tcase_set_timeout(tcase_serverrequests, 120);
  1162. suite_add_tcase(suite, tcase_serverrequests);
  1163. return suite;
  1164. }
  1165. #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
  1166. /* Used to debug test cases without using the check framework */
  1167. static int chk_ok = 0;
  1168. static int chk_failed = 0;
  1169. void
  1170. main(void)
  1171. {
  1172. test_the_test_environment(0);
  1173. test_threading(0);
  1174. test_mg_start_stop_http_server(0);
  1175. test_mg_start_stop_https_server(0);
  1176. test_request_handlers(0);
  1177. test_mg_server_and_client_tls(0);
  1178. printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
  1179. }
  1180. void
  1181. _ck_assert_failed(const char *file, int line, const char *expr, ...)
  1182. {
  1183. va_list va;
  1184. va_start(va, expr);
  1185. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  1186. vfprintf(stderr, expr, va);
  1187. fprintf(stderr, "\n\n");
  1188. va_end(va);
  1189. chk_failed++;
  1190. }
  1191. void
  1192. _ck_assert_msg(int cond, const char *file, int line, const char *expr, ...)
  1193. {
  1194. va_list va;
  1195. if (cond) {
  1196. chk_ok++;
  1197. return;
  1198. }
  1199. va_start(va, expr);
  1200. fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
  1201. vfprintf(stderr, expr, va);
  1202. fprintf(stderr, "\n\n");
  1203. va_end(va);
  1204. chk_failed++;
  1205. }
  1206. void
  1207. _mark_point(const char *file, int line)
  1208. {
  1209. chk_ok++;
  1210. }
  1211. void
  1212. tcase_fn_start(const char *fname, const char *file, int line)
  1213. {
  1214. }
  1215. void suite_add_tcase(Suite *s, TCase *tc){};
  1216. void _tcase_add_test(TCase *tc,
  1217. TFun tf,
  1218. const char *fname,
  1219. int _signal,
  1220. int allowed_exit_value,
  1221. int start,
  1222. int end){};
  1223. TCase *
  1224. tcase_create(const char *name)
  1225. {
  1226. return NULL;
  1227. };
  1228. Suite *
  1229. suite_create(const char *name)
  1230. {
  1231. return NULL;
  1232. };
  1233. void tcase_set_timeout(TCase *tc, double timeout){};
  1234. #endif