public_server.c 42 KB

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