public_func.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 "public_func.h"
  27. #include <civetweb.h>
  28. /* This unit test file uses the excellent Check unit testing library.
  29. * The API documentation is available here:
  30. * http://check.sourceforge.net/doc/check_html/index.html
  31. */
  32. START_TEST(test_the_test_environment)
  33. {
  34. char wd[300];
  35. char buf[500];
  36. FILE *f;
  37. struct stat st;
  38. int ret;
  39. memset(wd, 0, sizeof(wd));
  40. memset(buf, 0, sizeof(buf));
  41. /* Get the current working directory */
  42. #ifdef _WIN32
  43. (void)GetCurrentDirectoryA(sizeof(wd), wd);
  44. wd[sizeof(wd) - 1] = 0;
  45. #else
  46. (void)getcwd(wd, sizeof(wd));
  47. wd[sizeof(wd) - 1] = 0;
  48. #endif
  49. /* Check the pem file */
  50. #ifdef _WIN32
  51. strcpy(buf, wd);
  52. strcat(buf, "\\resources\\ssl_cert.pem");
  53. f = fopen(buf, "rb");
  54. #else
  55. strcpy(buf, wd);
  56. strcat(buf, "/resources/ssl_cert.pem");
  57. f = fopen(buf, "r");
  58. #endif
  59. if (f) {
  60. fclose(f);
  61. } else {
  62. ck_abort_msg("%s not found", buf);
  63. }
  64. /* Check the test dir */
  65. #ifdef _WIN32
  66. strcpy(buf, wd);
  67. strcat(buf, "\\test");
  68. #else
  69. strcpy(buf, wd);
  70. strcat(buf, "/test");
  71. #endif
  72. memset(&st, 0, sizeof(st));
  73. ret = stat(buf, &st);
  74. if (!ret) {
  75. fclose(f);
  76. } else {
  77. ck_abort_msg("%s not found", buf);
  78. }
  79. }
  80. END_TEST
  81. static int log_msg_func(const struct mg_connection *conn, const char *message)
  82. {
  83. struct mg_context *ctx;
  84. char *ud;
  85. ck_assert(conn != NULL);
  86. ctx = mg_get_context(conn);
  87. ck_assert(ctx != NULL);
  88. ud = (char *)mg_get_user_data(ctx);
  89. strncpy(ud, message, 255);
  90. ud[255] = 0;
  91. return 1;
  92. }
  93. START_TEST(test_mg_start_stop_http_server)
  94. {
  95. struct mg_context *ctx;
  96. const char *OPTIONS[] = {
  97. "document_root", ".", "listening_ports", "8080", NULL,
  98. };
  99. size_t ports_cnt;
  100. int ports[16];
  101. int ssl[16];
  102. struct mg_callbacks callbacks;
  103. char errmsg[256];
  104. memset(ports, 0, sizeof(ports));
  105. memset(ssl, 0, sizeof(ssl));
  106. memset(&callbacks, 0, sizeof(callbacks));
  107. memset(errmsg, 0, sizeof(errmsg));
  108. callbacks.log_message = log_msg_func;
  109. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  110. mg_Sleep(1);
  111. ck_assert_str_eq(errmsg, "");
  112. ck_assert(ctx != NULL);
  113. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  114. ck_assert_uint_eq(ports_cnt, 1);
  115. ck_assert_int_eq(ports[0], 8080);
  116. ck_assert_int_eq(ssl[0], 0);
  117. ck_assert_int_eq(ports[1], 0);
  118. ck_assert_int_eq(ssl[1], 0);
  119. mg_Sleep(1);
  120. mg_stop(ctx);
  121. }
  122. END_TEST
  123. START_TEST(test_mg_start_stop_https_server)
  124. {
  125. struct mg_context *ctx;
  126. const char *OPTIONS[] = {
  127. "document_root",
  128. ".",
  129. "listening_ports",
  130. "8080,8443s",
  131. "ssl_certificate",
  132. "resources/ssl_cert.pem", // TODO: check working path of CI test
  133. // system
  134. NULL,
  135. };
  136. size_t ports_cnt;
  137. int ports[16];
  138. int ssl[16];
  139. struct mg_callbacks callbacks;
  140. char errmsg[256];
  141. memset(ports, 0, sizeof(ports));
  142. memset(ssl, 0, sizeof(ssl));
  143. memset(&callbacks, 0, sizeof(callbacks));
  144. memset(errmsg, 0, sizeof(errmsg));
  145. callbacks.log_message = log_msg_func;
  146. ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS);
  147. mg_Sleep(1);
  148. ck_assert_str_eq(errmsg, "");
  149. ck_assert(ctx != NULL);
  150. ports_cnt = mg_get_ports(ctx, 16, ports, ssl);
  151. ck_assert_uint_eq(ports_cnt, 2);
  152. ck_assert_int_eq(ports[0], 8080);
  153. ck_assert_int_eq(ssl[0], 0);
  154. ck_assert_int_eq(ports[1], 8443);
  155. ck_assert_int_eq(ssl[1], 1);
  156. ck_assert_int_eq(ports[2], 0);
  157. ck_assert_int_eq(ssl[2], 0);
  158. mg_Sleep(1);
  159. mg_stop(ctx);
  160. }
  161. END_TEST
  162. static struct mg_context *g_ctx;
  163. static int request_test_handler(struct mg_connection *conn, void *cbdata)
  164. {
  165. int i;
  166. char chunk_data[32];
  167. const struct mg_request_info *ri;
  168. struct mg_context *ctx;
  169. void *ud, *cud;
  170. ctx = mg_get_context(conn);
  171. ud = mg_get_user_data(ctx);
  172. ri = mg_get_request_info(conn);
  173. ck_assert(ri != NULL);
  174. ck_assert(ctx == g_ctx);
  175. ck_assert(ud == &g_ctx);
  176. mg_set_user_connection_data(conn, (void *)6543);
  177. cud = mg_get_user_connection_data(conn);
  178. ck_assert(cud == (void *)6543);
  179. ck_assert(cbdata == (void *)7);
  180. strcpy(chunk_data, "123456789A123456789B123456789C");
  181. mg_printf(conn,
  182. "HTTP/1.1 200 OK\r\n"
  183. "Transfer-Encoding: chunked\r\n"
  184. "Content-Type: text/plain\r\n\r\n");
  185. for (i = 1; i <= 10; i++) {
  186. mg_printf(conn, "%x\r\n", i);
  187. mg_write(conn, chunk_data, (unsigned)i);
  188. mg_printf(conn, "\r\n");
  189. }
  190. mg_printf(conn, "0\r\n\r\n");
  191. return 1;
  192. }
  193. START_TEST(test_request_handlers)
  194. {
  195. char ebuf[100];
  196. struct mg_context *ctx;
  197. struct mg_connection *conn;
  198. const struct mg_request_info *ri;
  199. char uri[64];
  200. char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8];
  201. const char *expected =
  202. "112123123412345123456123456712345678123456789123456789A";
  203. int i;
  204. const char *request = "GET /U7 HTTP/1.0\r\n\r\n";
  205. #if defined(USE_IPV6) && defined(NO_SSL)
  206. const char *HTTP_PORT = "8084,[::]:8086";
  207. short ipv4_port = 8084;
  208. short ipv6_port = 8086;
  209. #elif !defined(USE_IPV6) && defined(NO_SSL)
  210. const char *HTTP_PORT = "8084";
  211. short ipv4_port = 8084;
  212. #elif defined(USE_IPV6) && !defined(NO_SSL)
  213. const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s";
  214. short ipv4_port = 8084;
  215. short ipv4s_port = 8094;
  216. short ipv4r_port = 8194;
  217. short ipv6_port = 8086;
  218. short ipv6s_port = 8096;
  219. short ipv6r_port = 8196;
  220. #elif !defined(USE_IPV6) && !defined(NO_SSL)
  221. const char *HTTP_PORT = "8084,8194r,8094s";
  222. short ipv4_port = 8084;
  223. short ipv4s_port = 8094;
  224. short ipv4r_port = 8194;
  225. #endif
  226. const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
  227. const char *opt;
  228. FILE *f;
  229. memset((void *)OPTIONS, 0, sizeof(OPTIONS));
  230. OPTIONS[0] = "listening_ports";
  231. OPTIONS[1] = HTTP_PORT;
  232. OPTIONS[2] = "document_root";
  233. OPTIONS[3] = ".";
  234. #ifndef NO_SSL
  235. OPTIONS[4] = "ssl_certificate";
  236. OPTIONS[5] = "resources/ssl_cert.pem";
  237. #endif
  238. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
  239. ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
  240. ctx = mg_start(NULL, &g_ctx, OPTIONS);
  241. ck_assert(ctx != NULL);
  242. g_ctx = ctx;
  243. opt = mg_get_option(ctx, "listening_ports");
  244. ck_assert_str_eq(opt, HTTP_PORT);
  245. opt = mg_get_option(ctx, "cgi_environment");
  246. ck_assert_str_eq(opt, "");
  247. opt = mg_get_option(ctx, "unknown_option_name");
  248. ck_assert(opt == NULL);
  249. for (i = 0; i < 1000; i++) {
  250. sprintf(uri, "/U%u", i);
  251. mg_set_request_handler(ctx, uri, request_test_handler, NULL);
  252. }
  253. for (i = 500; i < 800; i++) {
  254. sprintf(uri, "/U%u", i);
  255. mg_set_request_handler(ctx, uri, NULL, (void *)1);
  256. }
  257. for (i = 600; i >= 0; i--) {
  258. sprintf(uri, "/U%u", i);
  259. mg_set_request_handler(ctx, uri, NULL, (void *)2);
  260. }
  261. for (i = 750; i <= 1000; i++) {
  262. sprintf(uri, "/U%u", i);
  263. mg_set_request_handler(ctx, uri, NULL, (void *)3);
  264. }
  265. for (i = 5; i < 9; i++) {
  266. sprintf(uri, "/U%u", i);
  267. mg_set_request_handler(
  268. ctx, uri, request_test_handler, (void *)(ptrdiff_t)i);
  269. }
  270. /* Try to load non existing file */
  271. conn = mg_download("localhost",
  272. ipv4_port,
  273. 0,
  274. ebuf,
  275. sizeof(ebuf),
  276. "%s",
  277. "GET /file/not/found HTTP/1.0\r\n\r\n");
  278. ck_assert(conn != NULL);
  279. ri = mg_get_request_info(conn);
  280. ck_assert(ri != NULL);
  281. ck_assert_str_eq(ri->uri, "404");
  282. mg_close_connection(conn);
  283. /* Get data from callback */
  284. conn = mg_download(
  285. "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  286. ck_assert(conn != NULL);
  287. ri = mg_get_request_info(conn);
  288. ck_assert(ri != NULL);
  289. ck_assert_str_eq(ri->uri, "200");
  290. i = mg_read(conn, buf, sizeof(buf));
  291. ck_assert_int_eq(i, (int)strlen(expected));
  292. buf[i] = 0;
  293. ck_assert_str_eq(buf, expected);
  294. mg_close_connection(conn);
  295. /* Get data from callback using http://127.0.0.1 */
  296. conn = mg_download(
  297. "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request);
  298. ck_assert(conn != NULL);
  299. ri = mg_get_request_info(conn);
  300. ck_assert(ri != NULL);
  301. ck_assert_str_eq(ri->uri, "200");
  302. i = mg_read(conn, buf, sizeof(buf));
  303. ck_assert_int_eq(i, (int)strlen(expected));
  304. buf[i] = 0;
  305. ck_assert_str_eq(buf, expected);
  306. mg_close_connection(conn);
  307. #if defined(USE_IPV6)
  308. /* Get data from callback using http://[::1] */
  309. conn =
  310. mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request);
  311. ck_assert(conn != NULL);
  312. ri = mg_get_request_info(conn);
  313. ck_assert(ri != NULL);
  314. ck_assert_str_eq(ri->uri, "200");
  315. i = mg_read(conn, buf, sizeof(buf));
  316. ck_assert_int_eq(i, (int)strlen(expected));
  317. buf[i] = 0;
  318. ck_assert_str_eq(buf, expected);
  319. mg_close_connection(conn);
  320. #endif
  321. #if !defined(NO_SSL)
  322. /* Get data from callback using https://127.0.0.1 */
  323. conn = mg_download(
  324. "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  325. ck_assert(conn != NULL);
  326. ri = mg_get_request_info(conn);
  327. ck_assert(ri != NULL);
  328. ck_assert_str_eq(ri->uri, "200");
  329. i = mg_read(conn, buf, sizeof(buf));
  330. ck_assert_int_eq(i, (int)strlen(expected));
  331. buf[i] = 0;
  332. ck_assert_str_eq(buf, expected);
  333. mg_close_connection(conn);
  334. /* Get redirect from callback using http://127.0.0.1 */
  335. conn = mg_download(
  336. "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  337. ck_assert(conn != NULL);
  338. ri = mg_get_request_info(conn);
  339. ck_assert(ri != NULL);
  340. ck_assert_str_eq(ri->uri, "302");
  341. i = mg_read(conn, buf, sizeof(buf));
  342. ck_assert_int_eq(i, -1);
  343. mg_close_connection(conn);
  344. #endif
  345. #if defined(USE_IPV6) && !defined(NO_SSL)
  346. /* Get data from callback using https://[::1] */
  347. conn =
  348. mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request);
  349. ck_assert(conn != NULL);
  350. ri = mg_get_request_info(conn);
  351. ck_assert(ri != NULL);
  352. ck_assert_str_eq(ri->uri, "200");
  353. i = mg_read(conn, buf, sizeof(buf));
  354. ck_assert_int_eq(i, (int)strlen(expected));
  355. buf[i] = 0;
  356. ck_assert_str_eq(buf, expected);
  357. mg_close_connection(conn);
  358. /* Get redirect from callback using http://127.0.0.1 */
  359. conn =
  360. mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request);
  361. ck_assert(conn != NULL);
  362. ri = mg_get_request_info(conn);
  363. ck_assert(ri != NULL);
  364. ck_assert_str_eq(ri->uri, "302");
  365. i = mg_read(conn, buf, sizeof(buf));
  366. ck_assert_int_eq(i, -1);
  367. mg_close_connection(conn);
  368. #endif
  369. /* It seems to be impossible to find out what the actual working
  370. * directory of the CI test environment is. Before breaking another
  371. * dozen of builds by trying blindly with different paths, just
  372. * create the file here */
  373. #ifdef _WIN32
  374. f = fopen("test.txt", "wb");
  375. #else
  376. f = fopen("test.txt", "w");
  377. #endif
  378. fwrite("simple text file\n", 17, 1, f);
  379. fclose(f);
  380. /* Get static data */
  381. conn = mg_download("localhost",
  382. ipv4_port,
  383. 0,
  384. ebuf,
  385. sizeof(ebuf),
  386. "%s",
  387. "GET /test.txt HTTP/1.0\r\n\r\n");
  388. ck_assert(conn != NULL);
  389. ri = mg_get_request_info(conn);
  390. ck_assert(ri != NULL);
  391. ck_assert_str_eq(ri->uri, "200");
  392. i = mg_read(conn, buf, sizeof(buf));
  393. ck_assert_int_eq(i, 17);
  394. if ((i >= 0) && (i < (int)sizeof(buf))) {
  395. buf[i] = 0;
  396. }
  397. ck_assert_str_eq(buf, "simple text file\n");
  398. mg_close_connection(conn);
  399. /* Get directory listing */
  400. conn = mg_download("localhost",
  401. ipv4_port,
  402. 0,
  403. ebuf,
  404. sizeof(ebuf),
  405. "%s",
  406. "GET / HTTP/1.0\r\n\r\n");
  407. ck_assert(conn != NULL);
  408. ri = mg_get_request_info(conn);
  409. ck_assert(ri != NULL);
  410. ck_assert_str_eq(ri->uri, "200");
  411. i = mg_read(conn, buf, sizeof(buf));
  412. ck_assert(i > 6);
  413. buf[6] = 0;
  414. ck_assert_str_eq(buf, "<html>");
  415. mg_close_connection(conn);
  416. /* POST to static file (will not work) */
  417. conn = mg_download("localhost",
  418. ipv4_port,
  419. 0,
  420. ebuf,
  421. sizeof(ebuf),
  422. "%s",
  423. "POST /test.txt HTTP/1.0\r\n\r\n");
  424. ck_assert(conn != NULL);
  425. ri = mg_get_request_info(conn);
  426. ck_assert(ri != NULL);
  427. ck_assert_str_eq(ri->uri, "405");
  428. i = mg_read(conn, buf, sizeof(buf));
  429. ck_assert(i >= 29);
  430. buf[29] = 0;
  431. ck_assert_str_eq(buf, "Error 405: Method Not Allowed");
  432. mg_close_connection(conn);
  433. /* PUT to static file (will not work) */
  434. conn = mg_download("localhost",
  435. ipv4_port,
  436. 0,
  437. ebuf,
  438. sizeof(ebuf),
  439. "%s",
  440. "PUT /test.txt HTTP/1.0\r\n\r\n");
  441. ck_assert(conn != NULL);
  442. ri = mg_get_request_info(conn);
  443. ck_assert(ri != NULL);
  444. ck_assert_str_eq(ri->uri, "401"); /* not authorized */
  445. mg_close_connection(conn);
  446. /* TODO: Test websockets */
  447. /* Close the server */
  448. g_ctx = NULL;
  449. mg_stop(ctx);
  450. mg_Sleep(1);
  451. }
  452. END_TEST
  453. Suite *make_public_suite(void)
  454. {
  455. Suite *const suite = suite_create("Public");
  456. TCase *const version = tcase_create("Version");
  457. TCase *const get_valid_options = tcase_create("Options");
  458. TCase *const get_builtin_mime_type = tcase_create("MIME types");
  459. TCase *const tstrncasecmp = tcase_create("strcasecmp");
  460. TCase *const urlencodingdecoding = tcase_create("URL encoding decoding");
  461. TCase *const cookies = tcase_create("Cookies and variables");
  462. TCase *const md5 = tcase_create("MD5");
  463. tcase_add_test(version, test_mg_version);
  464. suite_add_tcase(suite, version);
  465. tcase_add_test(get_valid_options, test_mg_get_valid_options);
  466. suite_add_tcase(suite, get_valid_options);
  467. tcase_add_test(get_builtin_mime_type, test_mg_get_builtin_mime_type);
  468. suite_add_tcase(suite, get_builtin_mime_type);
  469. tcase_add_test(tstrncasecmp, test_mg_strncasecmp);
  470. suite_add_tcase(suite, tstrncasecmp);
  471. tcase_add_test(urlencodingdecoding, test_mg_url_encode);
  472. tcase_add_test(urlencodingdecoding, test_mg_url_decode);
  473. suite_add_tcase(suite, urlencodingdecoding);
  474. tcase_add_test(cookies, test_mg_get_cookie);
  475. tcase_add_test(cookies, test_mg_get_var);
  476. suite_add_tcase(suite, cookies);
  477. tcase_add_test(md5, test_mg_md5);
  478. suite_add_tcase(suite, md5);
  479. return suite;
  480. }