unit_test.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /* Copyright (c) 2013-2014 the Civetweb developers
  2. * Copyright (c) 2004-2013 Sergey Lyubka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /* Unit test for the civetweb web server. Tests embedded API.
  23. */
  24. #define USE_WEBSOCKET
  25. #ifndef _WIN32
  26. #define __cdecl
  27. #define USE_IPV6
  28. #endif
  29. /* USE_* definitions must be made before #include "civetweb.c" !
  30. * We include the source file so that our object file will have visibility to
  31. * all the static functions.
  32. */
  33. #include "civetweb.c"
  34. static int s_total_tests = 0;
  35. static int s_failed_tests = 0;
  36. #define FAIL(str, line) do { \
  37. printf("Fail on line %d: [%s]\n", line, str); \
  38. s_failed_tests++; \
  39. } while (0)
  40. #define ASSERT(expr) do { \
  41. s_total_tests++; \
  42. if (!(expr)) FAIL(#expr, __LINE__); \
  43. } while (0)
  44. /* TODO(bel):
  45. #define HTTP_PORT "56789"
  46. #define HTTPS_PORT "56790"
  47. #define HTTP_PORT2 "56791"
  48. #define LISTENING_ADDR \
  49. "127.0.0.1:" HTTP_PORT "r" \
  50. ",127.0.0.1:" HTTPS_PORT "s" \
  51. ",127.0.0.1:" HTTP_PORT2
  52. */
  53. #define HTTP_PORT "8080"
  54. #ifdef NO_SSL
  55. #define HTTPS_PORT HTTP_PORT
  56. #define LISTENING_ADDR "127.0.0.1:" HTTP_PORT
  57. #else
  58. #define HTTPS_PORT "8443"
  59. #define LISTENING_ADDR "127.0.0.1:" HTTP_PORT ",127.0.0.1:" HTTPS_PORT
  60. #endif
  61. static void test_parse_http_message() {
  62. struct mg_request_info ri;
  63. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  64. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  65. char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
  66. char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
  67. char req5[] = "GET / HTTP/1.1\r\n\r\n";
  68. char req6[] = "G";
  69. char req7[] = " blah ";
  70. char req8[] = " HTTP/1.1 200 OK \n\n";
  71. char req9[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
  72. ASSERT(parse_http_message(req9, sizeof(req9), &ri) == sizeof(req9) - 1);
  73. ASSERT(ri.num_headers == 1);
  74. ASSERT(parse_http_message(req1, sizeof(req1), &ri) == sizeof(req1) - 1);
  75. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  76. ASSERT(ri.num_headers == 0);
  77. ASSERT(parse_http_message(req2, sizeof(req2), &ri) == -1);
  78. ASSERT(parse_http_message(req3, sizeof(req3), &ri) == 0);
  79. ASSERT(parse_http_message(req6, sizeof(req6), &ri) == 0);
  80. ASSERT(parse_http_message(req7, sizeof(req7), &ri) == 0);
  81. ASSERT(parse_http_message("", 0, &ri) == 0);
  82. ASSERT(parse_http_message(req8, sizeof(req8), &ri) == sizeof(req8) - 1);
  83. /* TODO(lsm): Fix this. Header value may span multiple lines. */
  84. ASSERT(parse_http_message(req4, sizeof(req4), &ri) == sizeof(req4) - 1);
  85. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  86. ASSERT(ri.num_headers == 3);
  87. ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
  88. ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
  89. ASSERT(strcmp(ri.http_headers[1].name, "B") == 0);
  90. ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0);
  91. ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
  92. ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
  93. ASSERT(parse_http_message(req5, sizeof(req5), &ri) == sizeof(req5) - 1);
  94. ASSERT(strcmp(ri.request_method, "GET") == 0);
  95. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  96. }
  97. static void test_should_keep_alive(void) {
  98. struct mg_connection conn;
  99. struct mg_context ctx;
  100. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  101. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  102. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  103. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  104. memset(&conn, 0, sizeof(conn));
  105. conn.ctx = &ctx;
  106. ASSERT(parse_http_message(req1, sizeof(req1), &conn.request_info) ==
  107. sizeof(req1) - 1);
  108. ctx.config[ENABLE_KEEP_ALIVE] = "no";
  109. ASSERT(should_keep_alive(&conn) == 0);
  110. ctx.config[ENABLE_KEEP_ALIVE] = "yes";
  111. ASSERT(should_keep_alive(&conn) == 1);
  112. conn.must_close = 1;
  113. ASSERT(should_keep_alive(&conn) == 0);
  114. conn.must_close = 0;
  115. parse_http_message(req2, sizeof(req2), &conn.request_info);
  116. ASSERT(should_keep_alive(&conn) == 0);
  117. parse_http_message(req3, sizeof(req3), &conn.request_info);
  118. ASSERT(should_keep_alive(&conn) == 0);
  119. parse_http_message(req4, sizeof(req4), &conn.request_info);
  120. ASSERT(should_keep_alive(&conn) == 1);
  121. conn.status_code = 401;
  122. ASSERT(should_keep_alive(&conn) == 0);
  123. conn.status_code = 200;
  124. conn.must_close = 1;
  125. ASSERT(should_keep_alive(&conn) == 0);
  126. }
  127. static void test_match_prefix(void) {
  128. ASSERT(match_prefix("/api", 4, "/api") == 4);
  129. ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
  130. ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
  131. ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
  132. ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
  133. ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
  134. ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
  135. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  136. ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  137. ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
  138. ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
  139. ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
  140. ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  141. ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  142. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  143. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  144. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  145. ASSERT(match_prefix("$", 1, "") == 0);
  146. ASSERT(match_prefix("$", 1, "x") == -1);
  147. ASSERT(match_prefix("*$", 2, "x") == 1);
  148. ASSERT(match_prefix("/$", 2, "/") == 1);
  149. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  150. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  151. ASSERT(match_prefix("*", 1, "/hello/") == 0);
  152. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  153. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  154. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/B.A") == 6);
  155. ASSERT(match_prefix("**o$", 4, "HELLO") == 5);
  156. }
  157. static void test_remove_double_dots() {
  158. struct { char before[20], after[20]; } data[] = {
  159. {"////a", "/a"},
  160. {"/.....", "/."},
  161. {"/......", "/"},
  162. {"...", "..."},
  163. {"/...///", "/./"},
  164. {"/a...///", "/a.../"},
  165. {"/.x", "/.x"},
  166. {"/\\", "/"},
  167. {"/a\\", "/a\\"},
  168. {"/a\\\\...", "/a\\."},
  169. };
  170. size_t i;
  171. for (i = 0; i < ARRAY_SIZE(data); i++) {
  172. remove_double_dots_and_double_slashes(data[i].before);
  173. ASSERT(strcmp(data[i].before, data[i].after) == 0);
  174. }
  175. }
  176. static char *read_file(const char *path, int *size) {
  177. FILE *fp;
  178. struct stat st;
  179. char *data = NULL;
  180. if ((fp = fopen(path, "rb")) != NULL && !fstat(fileno(fp), &st)) {
  181. *size = (int) st.st_size;
  182. ASSERT((data = mg_malloc(*size)) != NULL);
  183. ASSERT(fread(data, 1, *size, fp) == (size_t) *size);
  184. fclose(fp);
  185. }
  186. return data;
  187. }
  188. static const char *fetch_data = "hello world!\n";
  189. static const char *inmemory_file_data = "hi there";
  190. static const char *upload_filename = "upload_test.txt";
  191. static const char *upload_filename2 = "upload_test2.txt";
  192. static const char *upload_ok_message = "upload successful";
  193. static const char *open_file_cb(const struct mg_connection *conn,
  194. const char *path, size_t *size) {
  195. (void) conn;
  196. if (!strcmp(path, "./blah")) {
  197. *size = strlen(inmemory_file_data);
  198. return inmemory_file_data;
  199. }
  200. return NULL;
  201. }
  202. static void upload_cb(struct mg_connection *conn, const char *path) {
  203. const struct mg_request_info *ri = mg_get_request_info(conn);
  204. char *p1, *p2;
  205. int len1, len2;
  206. if (atoi(ri->query_string) == 1) {
  207. ASSERT(!strcmp(path, "./upload_test.txt"));
  208. ASSERT((p1 = read_file("src/civetweb.c", &len1)) != NULL);
  209. ASSERT((p2 = read_file(path, &len2)) != NULL);
  210. ASSERT(len1 == len2);
  211. ASSERT(memcmp(p1, p2, len1) == 0);
  212. mg_free(p1), mg_free(p2);
  213. remove(upload_filename);
  214. } else if (atoi(ri->query_string) == 2) {
  215. if (!strcmp(path, "./upload_test.txt")) {
  216. ASSERT((p1 = read_file("include/civetweb.h", &len1)) != NULL);
  217. ASSERT((p2 = read_file(path, &len2)) != NULL);
  218. ASSERT(len1 == len2);
  219. ASSERT(memcmp(p1, p2, len1) == 0);
  220. mg_free(p1), mg_free(p2);
  221. remove(upload_filename);
  222. } else if (!strcmp(path, "./upload_test2.txt")) {
  223. ASSERT((p1 = read_file("README.md", &len1)) != NULL);
  224. ASSERT((p2 = read_file(path, &len2)) != NULL);
  225. ASSERT(len1 == len2);
  226. ASSERT(memcmp(p1, p2, len1) == 0);
  227. mg_free(p1), mg_free(p2);
  228. remove(upload_filename);
  229. } else {
  230. ASSERT(0);
  231. }
  232. } else {
  233. ASSERT(0);
  234. }
  235. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s",
  236. (int) strlen(upload_ok_message), upload_ok_message);
  237. }
  238. static int begin_request_handler_cb(struct mg_connection *conn) {
  239. const struct mg_request_info *ri = mg_get_request_info(conn);
  240. if (!strcmp(ri->uri, "/data")) {
  241. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  242. "Content-Type: text/plain\r\n\r\n"
  243. "%s", fetch_data);
  244. close_connection(conn);
  245. return 1;
  246. }
  247. if (!strcmp(ri->uri, "/content_length")) {
  248. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  249. "X-Content-Length: %d\r\n"
  250. "Content-Type: text/plain\r\n\r\n"
  251. "%s", ri->content_length, fetch_data);
  252. close_connection(conn);
  253. return 1;
  254. }
  255. if (!strcmp(ri->uri, "/upload")) {
  256. ASSERT(ri->query_string != NULL);
  257. ASSERT(mg_upload(conn, ".") == atoi(ri->query_string));
  258. }
  259. return 0;
  260. }
  261. static int log_message_cb(const struct mg_connection *conn, const char *msg) {
  262. (void) conn;
  263. printf("%s\n", msg);
  264. return 0;
  265. }
  266. int (*begin_request)(struct mg_connection *);
  267. void (*end_request)(const struct mg_connection *, int reply_status_code);
  268. int (*log_message)(const struct mg_connection *, const char *message);
  269. int (*init_ssl)(void *ssl_context, void *user_data);
  270. int (*websocket_connect)(const struct mg_connection *);
  271. void (*websocket_ready)(struct mg_connection *);
  272. int (*websocket_data)(struct mg_connection *, int bits,
  273. char *data, size_t data_len);
  274. void (*connection_close)(struct mg_connection *);
  275. const char * (*open_file)(const struct mg_connection *,
  276. const char *path, size_t *data_len);
  277. void (*init_lua)(struct mg_connection *, void *lua_context);
  278. void (*upload)(struct mg_connection *, const char *file_name);
  279. int (*http_error)(struct mg_connection *, int status);
  280. static const struct mg_callbacks CALLBACKS = {
  281. &begin_request_handler_cb, NULL, &log_message_cb, NULL, NULL, NULL, NULL, NULL,
  282. &open_file_cb, NULL, &upload_cb, NULL
  283. };
  284. static const char *OPTIONS[] = {
  285. "document_root", ".",
  286. "listening_ports", LISTENING_ADDR,
  287. "enable_keep_alive", "yes",
  288. #ifndef NO_SSL
  289. "ssl_certificate", "../resources/ssl_cert.pem",
  290. #endif
  291. NULL,
  292. };
  293. static char *read_conn(struct mg_connection *conn, int *size) {
  294. char buf[100], *data = NULL;
  295. int len;
  296. *size = 0;
  297. while ((len = mg_read(conn, buf, sizeof(buf))) > 0) {
  298. *size += len;
  299. ASSERT((data = mg_realloc(data, *size)) != NULL);
  300. memcpy(data + *size - len, buf, len);
  301. }
  302. return data;
  303. }
  304. static void test_mg_download(int use_ssl) {
  305. char *p1, *p2, *h, ebuf[100];
  306. int len1, len2, port;
  307. struct mg_connection *conn;
  308. struct mg_context *ctx;
  309. if (use_ssl) port = atoi(HTTPS_PORT); else port = atoi(HTTP_PORT);
  310. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  311. ASSERT(mg_download(NULL, port, use_ssl, ebuf, sizeof(ebuf), "%s", "") == NULL);
  312. ASSERT(mg_download("localhost", 0, use_ssl, ebuf, sizeof(ebuf), "%s", "") == NULL);
  313. ASSERT(mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "") == NULL);
  314. /* Fetch nonexistent file, should see 404 */
  315. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  316. "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL);
  317. ASSERT(strcmp(conn->request_info.uri, "404") == 0);
  318. mg_close_connection(conn);
  319. if (use_ssl) {
  320. ASSERT((conn = mg_download("google.com", 443, 1, ebuf, sizeof(ebuf), "%s",
  321. "GET / HTTP/1.0\r\n\r\n")) != NULL);
  322. mg_close_connection(conn);
  323. } else {
  324. ASSERT((conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf), "%s",
  325. "GET / HTTP/1.0\r\n\r\n")) != NULL);
  326. mg_close_connection(conn);
  327. }
  328. /* Fetch unit_test.c, should succeed */
  329. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  330. "GET /unit_test.c HTTP/1.0\r\n\r\n")) != NULL);
  331. ASSERT(!strcmp(conn->request_info.uri, "200"));
  332. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  333. ASSERT((p2 = read_file("unit_test.c", &len2)) != NULL);
  334. ASSERT(len1 == len2);
  335. ASSERT(memcmp(p1, p2, len1) == 0);
  336. mg_free(p1), mg_free(p2);
  337. mg_close_connection(conn);
  338. /* Fetch in-memory file, should succeed. */
  339. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  340. "GET /blah HTTP/1.1\r\n\r\n")) != NULL);
  341. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  342. ASSERT(len1 == (int) strlen(inmemory_file_data));
  343. ASSERT(memcmp(p1, inmemory_file_data, len1) == 0);
  344. mg_free(p1);
  345. mg_close_connection(conn);
  346. /* Fetch in-memory data with no Content-Length, should succeed. */
  347. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  348. "GET /data HTTP/1.1\r\n\r\n")) != NULL);
  349. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  350. ASSERT(len1 == (int) strlen(fetch_data));
  351. ASSERT(memcmp(p1, fetch_data, len1) == 0);
  352. mg_free(p1);
  353. mg_close_connection(conn);
  354. /* Fetch in-memory data with Content-Length, should succeed and return the defined length. */
  355. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  356. "GET /content_length HTTP/1.1\r\nContent-Length: 10\r\n\r\n0123456789")) != NULL);
  357. ASSERT((h = mg_get_header(conn, "X-Content-Length")) != NULL);
  358. ASSERT(strcmp(h, "10") == 0);
  359. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  360. ASSERT(len1 == (int) strlen(fetch_data));
  361. ASSERT(memcmp(p1, fetch_data, len1) == 0);
  362. mg_free(p1);
  363. mg_close_connection(conn);
  364. /* Fetch in-memory data without Content-Length, should succeed and return an undefined length. */
  365. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  366. "GET /content_length HTTP/1.1\r\n\r\n0123456789")) != NULL);
  367. ASSERT((h = mg_get_header(conn, "X-Content-Length")) != NULL);
  368. ASSERT(strcmp(h, "-1") == 0);
  369. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  370. ASSERT(len1 == (int) strlen(fetch_data));
  371. ASSERT(memcmp(p1, fetch_data, len1) == 0);
  372. mg_free(p1);
  373. mg_close_connection(conn);
  374. /* Test SSL redirect, IP address */
  375. /* TODO(bel):
  376. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  377. ebuf, sizeof(ebuf), "%s",
  378. "GET /foo HTTP/1.1\r\n\r\n")) != NULL);
  379. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  380. ASSERT(strcmp(mg_get_header(conn, "Location"),
  381. "https://127.0.0.1:" HTTPS_PORT "/foo") == 0);
  382. mg_close_connection(conn);
  383. */
  384. /* Test SSL redirect, Host: */
  385. /* TODO(bel):
  386. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  387. ebuf, sizeof(ebuf), "%s",
  388. "GET /foo HTTP/1.1\r\nHost: a.b:77\n\n")) != NULL);
  389. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  390. ASSERT(strcmp(mg_get_header(conn, "Location"),
  391. "https://a.b:" HTTPS_PORT "/foo") == 0);
  392. mg_close_connection(conn);
  393. */
  394. mg_stop(ctx);
  395. }
  396. static int alloc_printf(char **buf, size_t size, char *fmt, ...) {
  397. va_list ap;
  398. va_start(ap, fmt);
  399. return alloc_vprintf(buf, size, fmt, ap);
  400. }
  401. static void test_mg_upload(void) {
  402. static const char *boundary = "OOO___MY_BOUNDARY___OOO";
  403. struct mg_context *ctx;
  404. struct mg_connection *conn;
  405. char ebuf[100], buf[20], *file_data, *file2_data, *post_data;
  406. int file_len, file2_len, post_data_len;
  407. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  408. /* Upload one file */
  409. ASSERT((file_data = read_file("unit_test.c", &file_len)) != NULL);
  410. post_data = NULL;
  411. post_data_len = alloc_printf(&post_data, 0,
  412. "--%s\r\n"
  413. "Content-Disposition: form-data; "
  414. "name=\"file\"; "
  415. "filename=\"%s\"\r\n\r\n"
  416. "%.*s\r\n"
  417. "--%s--\r\n",
  418. boundary, upload_filename,
  419. file_len, file_data, boundary);
  420. ASSERT(post_data_len > 0);
  421. #if 0 /* TODO (bel): ... */
  422. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  423. ebuf, sizeof(ebuf),
  424. "POST /upload?1 HTTP/1.1\r\n"
  425. "Content-Length: %d\r\n"
  426. "Content-Type: multipart/form-data; "
  427. "boundary=%s\r\n\r\n"
  428. "%.*s", post_data_len, boundary,
  429. post_data_len, post_data)) != NULL);
  430. mg_free(file_data), mg_free(post_data);
  431. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  432. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  433. mg_close_connection(conn);
  434. /* Upload two files */
  435. ASSERT((file_data = read_file("include/civetweb.h", &file_len)) != NULL);
  436. ASSERT((file2_data = read_file("README.md", &file2_len)) != NULL);
  437. post_data = NULL;
  438. post_data_len = alloc_printf(&post_data, 0,
  439. /* First file */
  440. "--%s\r\n"
  441. "Content-Disposition: form-data; "
  442. "name=\"file\"; "
  443. "filename=\"%s\"\r\n\r\n"
  444. "%.*s\r\n"
  445. /* Second file */
  446. "--%s\r\n"
  447. "Content-Disposition: form-data; "
  448. "name=\"file\"; "
  449. "filename=\"%s\"\r\n\r\n"
  450. "%.*s\r\n"
  451. /* Final boundary */
  452. "--%s--\r\n",
  453. boundary, upload_filename,
  454. file_len, file_data,
  455. boundary, upload_filename2,
  456. file2_len, file2_data,
  457. boundary);
  458. ASSERT(post_data_len > 0);
  459. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  460. ebuf, sizeof(ebuf),
  461. "POST /upload?2 HTTP/1.1\r\n"
  462. "Content-Length: %d\r\n"
  463. "Content-Type: multipart/form-data; "
  464. "boundary=%s\r\n\r\n"
  465. "%.*s", post_data_len, boundary,
  466. post_data_len, post_data)) != NULL);
  467. mg_free(file_data), mg_free(file2_data), mg_free(post_data);
  468. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  469. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  470. mg_close_connection(conn);
  471. #endif
  472. mg_stop(ctx);
  473. }
  474. static void test_base64_encode(void) {
  475. const char *in[] = {"a", "ab", "abc", "abcd", NULL};
  476. const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
  477. char buf[100];
  478. int i;
  479. for (i = 0; in[i] != NULL; i++) {
  480. base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
  481. ASSERT(!strcmp(buf, out[i]));
  482. }
  483. }
  484. static void test_mg_get_var(void) {
  485. static const char *post[] = {
  486. "a=1&&b=2&d&=&c=3%20&e=",
  487. "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=",
  488. NULL
  489. };
  490. char buf[20];
  491. ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
  492. ASSERT(buf[0] == '1' && buf[1] == '\0');
  493. ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
  494. ASSERT(buf[0] == '2' && buf[1] == '\0');
  495. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
  496. ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
  497. ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
  498. ASSERT(buf[0] == '\0');
  499. ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
  500. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2);
  501. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
  502. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
  503. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2);
  504. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);
  505. }
  506. static void test_set_throttle(void) {
  507. ASSERT(set_throttle(NULL, 0x0a000001, "/") == 0);
  508. ASSERT(set_throttle("10.0.0.0/8=20", 0x0a000001, "/") == 20);
  509. ASSERT(set_throttle("10.0.0.0/8=0.5k", 0x0a000001, "/") == 512);
  510. ASSERT(set_throttle("10.0.0.0/8=17m", 0x0a000001, "/") == 1048576 * 17);
  511. ASSERT(set_throttle("10.0.0.0/8=1x", 0x0a000001, "/") == 0);
  512. ASSERT(set_throttle("10.0.0.0/8=5,0.0.0.0/0=10", 0x0a000001, "/") == 10);
  513. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/index") == 5);
  514. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/foo/x") == 7);
  515. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0b000001, "/foxo/x") == 0);
  516. ASSERT(set_throttle("10.0.0.0/8=5,*=1", 0x0b000001, "/foxo/x") == 1);
  517. }
  518. static void test_next_option(void) {
  519. const char *p, *list = "x/8,/y**=1;2k,z";
  520. struct vec a, b;
  521. int i;
  522. ASSERT(next_option(NULL, &a, &b) == NULL);
  523. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  524. ASSERT(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  525. ASSERT(i != 1 || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9 && b.len == 4));
  526. ASSERT(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  527. }
  528. }
  529. #if defined(USE_LUA)
  530. static void check_lua_expr(lua_State *L, const char *expr, const char *value) {
  531. const char *v, *var_name = "myVar";
  532. char buf[100];
  533. snprintf(buf, sizeof(buf), "%s = %s", var_name, expr);
  534. (void) luaL_dostring(L, buf);
  535. lua_getglobal(L, var_name);
  536. v = lua_tostring(L, -1);
  537. ASSERT((value == NULL && v == NULL) || (value != NULL && v != NULL && !strcmp(value, v)));
  538. }
  539. static void test_lua(void) {
  540. static struct mg_connection conn;
  541. static struct mg_context ctx;
  542. char http_request[] = "POST /foo/bar HTTP/1.1\r\n"
  543. "Content-Length: 12\r\n"
  544. "Connection: close\r\n\r\nhello world!";
  545. lua_State *L = luaL_newstate();
  546. conn.ctx = &ctx;
  547. conn.buf = http_request;
  548. conn.buf_size = conn.data_len = strlen(http_request);
  549. conn.request_len = parse_http_message(conn.buf, conn.data_len, &conn.request_info);
  550. conn.content_len = conn.data_len - conn.request_len;
  551. prepare_lua_environment(&conn, L, "unit_test", LUA_ENV_TYPE_PLAIN_LUA_PAGE);
  552. ASSERT(lua_gettop(L) == 4);
  553. check_lua_expr(L, "'hi'", "hi");
  554. check_lua_expr(L, "mg.request_info.request_method", "POST");
  555. check_lua_expr(L, "mg.request_info.uri", "/foo/bar");
  556. check_lua_expr(L, "mg.request_info.num_headers", "2");
  557. check_lua_expr(L, "mg.request_info.remote_ip", "0");
  558. check_lua_expr(L, "mg.request_info.http_headers['Content-Length']", "12");
  559. check_lua_expr(L, "mg.request_info.http_headers['Connection']", "close");
  560. (void) luaL_dostring(L, "post = mg.read()");
  561. check_lua_expr(L, "# post", "12");
  562. check_lua_expr(L, "post", "hello world!");
  563. lua_close(L);
  564. }
  565. #endif
  566. static void test_mg_stat(void) {
  567. static struct mg_context ctx;
  568. struct file file = STRUCT_FILE_INITIALIZER;
  569. ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file));
  570. }
  571. static void test_skip_quoted(void) {
  572. char x[] = "a=1, b=2 c='hi \' there'", *s = x, *p;
  573. p = skip_quoted(&s, ", ", ", ", 0);
  574. ASSERT(p != NULL && !strcmp(p, "a=1"));
  575. p = skip_quoted(&s, ", ", ", ", 0);
  576. ASSERT(p != NULL && !strcmp(p, "b=2"));
  577. /* TODO(lsm): fix this */
  578. #if 0
  579. p = skip_quoted(&s, "'", ", ", '\\');
  580. p = skip_quoted(&s, "'", ", ", '\\');
  581. printf("[%s]\n", p);
  582. ASSERT(p != NULL && !strcmp(p, "hi ' there"));
  583. #endif
  584. }
  585. static void test_alloc_vprintf(void) {
  586. char buf[MG_BUF_LEN], *p = buf;
  587. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  588. ASSERT(p == buf);
  589. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  590. ASSERT(alloc_printf(&p, sizeof(buf), "") == 0);
  591. /* Pass small buffer, make sure alloc_printf allocates */
  592. ASSERT(alloc_printf(&p, 1, "%s", "hello") == 5);
  593. ASSERT(p != buf);
  594. mg_free(p);
  595. }
  596. static void test_request_replies(void) {
  597. char ebuf[100];
  598. int i;
  599. struct mg_connection *conn;
  600. struct mg_context *ctx;
  601. static struct { const char *request, *reply_regex; } tests[] = {
  602. {
  603. "GET hello.txt HTTP/1.0\r\nRange: bytes=3-5\r\n\r\n",
  604. "^HTTP/1.1 206 Partial Content"
  605. },
  606. {NULL, NULL},
  607. };
  608. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  609. for (i = 0; tests[i].request != NULL; i++) {
  610. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s",
  611. tests[i].request)) != NULL);
  612. mg_close_connection(conn);
  613. }
  614. mg_stop(ctx);
  615. /* TODO(bel):
  616. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  617. for (i = 0; tests[i].request != NULL; i++) {
  618. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1, ebuf, sizeof(ebuf), "%s",
  619. tests[i].request)) != NULL);
  620. mg_close_connection(conn);
  621. }
  622. mg_stop(ctx);
  623. */
  624. }
  625. static int api_callback(struct mg_connection *conn) {
  626. struct mg_request_info *ri = mg_get_request_info(conn);
  627. char post_data[100] = "";
  628. ASSERT(ri->user_data == (void *) 123);
  629. ASSERT(ri->num_headers == 2);
  630. ASSERT(strcmp(mg_get_header(conn, "host"), "blah.com") == 0);
  631. ASSERT(mg_read(conn, post_data, sizeof(post_data)) == 3);
  632. ASSERT(memcmp(post_data, "b=1", 3) == 0);
  633. ASSERT(ri->query_string != NULL);
  634. ASSERT(ri->remote_ip > 0);
  635. ASSERT(ri->remote_port > 0);
  636. ASSERT(strcmp(ri->http_version, "1.0") == 0);
  637. mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\n");
  638. return 1;
  639. }
  640. static void test_api_calls(void) {
  641. char ebuf[100];
  642. struct mg_callbacks callbacks;
  643. struct mg_connection *conn;
  644. struct mg_context *ctx;
  645. static const char *request = "POST /?a=%20&b=&c=xx HTTP/1.0\r\n"
  646. "Host: blah.com\n" /* More spaces before */
  647. "content-length: 3\r\n" /* Lower case header name */
  648. "\r\nb=123456"; /* Content size > content-length, test for mg_read() */
  649. memset(&callbacks, 0, sizeof(callbacks));
  650. callbacks.begin_request = api_callback;
  651. ASSERT((ctx = mg_start(&callbacks, (void *) 123, OPTIONS)) != NULL);
  652. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  653. ebuf, sizeof(ebuf), "%s", request)) != NULL);
  654. mg_close_connection(conn);
  655. mg_stop(ctx);
  656. }
  657. static void test_url_decode(void) {
  658. char buf[100];
  659. ASSERT(mg_url_decode("foo", 3, buf, 3, 0) == -1); /* No space for \0 */
  660. ASSERT(mg_url_decode("foo", 3, buf, 4, 0) == 3);
  661. ASSERT(strcmp(buf, "foo") == 0);
  662. ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 0) == 2);
  663. ASSERT(strcmp(buf, "a+") == 0);
  664. ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 1) == 2);
  665. ASSERT(strcmp(buf, "a ") == 0);
  666. ASSERT(mg_url_decode("%61", 1, buf, sizeof(buf), 1) == 1);
  667. ASSERT(strcmp(buf, "%") == 0);
  668. ASSERT(mg_url_decode("%61", 2, buf, sizeof(buf), 1) == 2);
  669. ASSERT(strcmp(buf, "%6") == 0);
  670. ASSERT(mg_url_decode("%61", 3, buf, sizeof(buf), 1) == 1);
  671. ASSERT(strcmp(buf, "a") == 0);
  672. }
  673. static void test_mg_strcasestr(void) {
  674. static const char *big1 = "abcdef";
  675. ASSERT(mg_strcasestr("Y", "X") == NULL);
  676. ASSERT(mg_strcasestr("Y", "y") != NULL);
  677. ASSERT(mg_strcasestr(big1, "X") == NULL);
  678. ASSERT(mg_strcasestr(big1, "CD") == big1 + 2);
  679. ASSERT(mg_strcasestr("aa", "AAB") == NULL);
  680. }
  681. static void test_mg_get_cookie(void) {
  682. char buf[20];
  683. ASSERT(mg_get_cookie("", "foo", NULL, sizeof(buf)) == -2);
  684. ASSERT(mg_get_cookie("", "foo", buf, 0) == -2);
  685. ASSERT(mg_get_cookie("", "foo", buf, sizeof(buf)) == -1);
  686. ASSERT(mg_get_cookie("", NULL, buf, sizeof(buf)) == -1);
  687. ASSERT(mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)) == 1);
  688. ASSERT(strcmp(buf, "1") == 0);
  689. ASSERT(mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)) == 1);
  690. ASSERT(strcmp(buf, "2") == 0);
  691. ASSERT(mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)) == 3);
  692. ASSERT(strcmp(buf, "123") == 0);
  693. ASSERT(mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)) == -1);
  694. }
  695. static void test_strtoll(void) {
  696. ASSERT(strtoll("0", NULL, 10) == 0);
  697. ASSERT(strtoll("123", NULL, 10) == 123);
  698. ASSERT(strtoll("-34", NULL, 10) == -34);
  699. ASSERT(strtoll("3566626116", NULL, 10) == 3566626116);
  700. }
  701. static void test_parse_port_string(void) {
  702. static const char *valid[] = {
  703. "0", "1", "1s", "1r", "1.2.3.4:1", "1.2.3.4:1s", "1.2.3.4:1r",
  704. #if defined(USE_IPV6)
  705. "[::1]:123", "[3ffe:2a00:100:7031::1]:900",
  706. #endif
  707. NULL
  708. };
  709. static const char *invalid[] = {
  710. "99999", "1k", "1.2.3", "1.2.3.4:", "1.2.3.4:2p",
  711. NULL
  712. };
  713. struct socket so;
  714. struct vec vec;
  715. int i;
  716. for (i = 0; valid[i] != NULL; i++) {
  717. vec.ptr = valid[i];
  718. vec.len = strlen(vec.ptr);
  719. ASSERT(parse_port_string(&vec, &so) != 0);
  720. }
  721. for (i = 0; invalid[i] != NULL; i++) {
  722. vec.ptr = invalid[i];
  723. vec.len = strlen(vec.ptr);
  724. ASSERT(parse_port_string(&vec, &so) == 0);
  725. }
  726. }
  727. static void test_md5(void) {
  728. md5_state_t md5_state;
  729. unsigned char md5_val[16+1];
  730. char md5_str[32+1];
  731. const char *test_str = "The quick brown fox jumps over the lazy dog";
  732. md5_val[16]=0;
  733. md5_init(&md5_state);
  734. md5_finish(&md5_state, md5_val);
  735. ASSERT(strcmp(md5_val, "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e")==0);
  736. sprintf(md5_str, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  737. md5_val[0], md5_val[1], md5_val[2], md5_val[3],
  738. md5_val[4], md5_val[5], md5_val[6], md5_val[7],
  739. md5_val[8], md5_val[9], md5_val[10], md5_val[11],
  740. md5_val[12], md5_val[13], md5_val[14], md5_val[15]);
  741. ASSERT(strcmp(md5_str, "d41d8cd98f00b204e9800998ecf8427e")==0);
  742. mg_md5(md5_str, "", NULL);
  743. ASSERT(strcmp(md5_str, "d41d8cd98f00b204e9800998ecf8427e")==0);
  744. md5_init(&md5_state);
  745. md5_append(&md5_state, test_str, strlen(test_str));
  746. md5_finish(&md5_state, md5_val);
  747. sprintf(md5_str, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  748. md5_val[0], md5_val[1], md5_val[2], md5_val[3],
  749. md5_val[4], md5_val[5], md5_val[6], md5_val[7],
  750. md5_val[8], md5_val[9], md5_val[10], md5_val[11],
  751. md5_val[12], md5_val[13], md5_val[14], md5_val[15]);
  752. ASSERT(strcmp(md5_str, "9e107d9d372bb6826bd81d3542a419d6")==0);
  753. mg_md5(md5_str, test_str, NULL);
  754. ASSERT(strcmp(md5_str, "9e107d9d372bb6826bd81d3542a419d6")==0);
  755. mg_md5(md5_str, "The", " ", "quick brown fox", "", " jumps ", "over the lazy dog", "", "", NULL);
  756. ASSERT(strcmp(md5_str, "9e107d9d372bb6826bd81d3542a419d6")==0);
  757. mg_md5(md5_str, "civetweb", NULL);
  758. ASSERT(strcmp(md5_str, "95c098bd85b619b24a83d9cea5e8ba54")==0);
  759. }
  760. int __cdecl main(void) {
  761. char buffer[512];
  762. FILE * f;
  763. struct mg_context *ctx;
  764. /* print headline */
  765. printf("Civetweb %s unit test\n", mg_version());
  766. #if defined(_WIN32)
  767. GetCurrentDirectoryA(sizeof(buffer), buffer);
  768. #else
  769. getcwd(buffer, sizeof(buffer));
  770. #endif
  771. printf("Test directory is \"%s\"\n", buffer); /* should be the "test" directory */
  772. f = fopen("hello.txt", "r");
  773. if (f) {
  774. fclose(f);
  775. } else {
  776. printf("Error: Test directory does not contain hello.txt\n");
  777. }
  778. f = fopen("unit_test.c", "r");
  779. if (f) {
  780. fclose(f);
  781. } else {
  782. printf("Error: Test directory does not contain unit_test.c\n");
  783. }
  784. /* test local functions */
  785. test_parse_port_string();
  786. test_mg_strcasestr();
  787. test_alloc_vprintf();
  788. test_base64_encode();
  789. test_match_prefix();
  790. test_remove_double_dots();
  791. test_should_keep_alive();
  792. test_parse_http_message();
  793. test_mg_get_var();
  794. test_set_throttle();
  795. test_next_option();
  796. test_mg_stat();
  797. test_skip_quoted();
  798. test_url_decode();
  799. test_mg_get_cookie();
  800. test_strtoll();
  801. test_md5();
  802. /* start stop server */
  803. ctx = mg_start(NULL, NULL, OPTIONS);
  804. ASSERT(ctx != NULL);
  805. mg_sleep(1000);
  806. mg_stop(ctx);
  807. /* tests with network access */
  808. test_mg_download(0);
  809. #ifndef NO_SSL
  810. test_mg_download(1);
  811. #endif
  812. test_mg_upload();
  813. test_request_replies();
  814. test_api_calls();
  815. #if defined(USE_LUA)
  816. test_lua();
  817. #endif
  818. printf("TOTAL TESTS: %d, FAILED: %d\n", s_total_tests, s_failed_tests);
  819. return s_failed_tests == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
  820. }