unit_test.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. #define HTTPS_PORT HTTP_PORT
  55. #define LISTENING_ADDR "127.0.0.1:" HTTP_PORT
  56. static void test_parse_http_message() {
  57. struct mg_request_info ri;
  58. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  59. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  60. char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
  61. char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
  62. char req5[] = "GET / HTTP/1.1\r\n\r\n";
  63. char req6[] = "G";
  64. char req7[] = " blah ";
  65. char req8[] = " HTTP/1.1 200 OK \n\n";
  66. char req9[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
  67. ASSERT(parse_http_message(req9, sizeof(req9), &ri) == sizeof(req9) - 1);
  68. ASSERT(ri.num_headers == 1);
  69. ASSERT(parse_http_message(req1, sizeof(req1), &ri) == sizeof(req1) - 1);
  70. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  71. ASSERT(ri.num_headers == 0);
  72. ASSERT(parse_http_message(req2, sizeof(req2), &ri) == -1);
  73. ASSERT(parse_http_message(req3, sizeof(req3), &ri) == 0);
  74. ASSERT(parse_http_message(req6, sizeof(req6), &ri) == 0);
  75. ASSERT(parse_http_message(req7, sizeof(req7), &ri) == 0);
  76. ASSERT(parse_http_message("", 0, &ri) == 0);
  77. ASSERT(parse_http_message(req8, sizeof(req8), &ri) == sizeof(req8) - 1);
  78. /* TODO(lsm): Fix this. Header value may span multiple lines. */
  79. ASSERT(parse_http_message(req4, sizeof(req4), &ri) == sizeof(req4) - 1);
  80. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  81. ASSERT(ri.num_headers == 3);
  82. ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
  83. ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
  84. ASSERT(strcmp(ri.http_headers[1].name, "B") == 0);
  85. ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0);
  86. ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
  87. ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
  88. ASSERT(parse_http_message(req5, sizeof(req5), &ri) == sizeof(req5) - 1);
  89. ASSERT(strcmp(ri.request_method, "GET") == 0);
  90. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  91. }
  92. static void test_should_keep_alive(void) {
  93. struct mg_connection conn;
  94. struct mg_context ctx;
  95. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  96. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  97. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  98. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  99. memset(&conn, 0, sizeof(conn));
  100. conn.ctx = &ctx;
  101. ASSERT(parse_http_message(req1, sizeof(req1), &conn.request_info) ==
  102. sizeof(req1) - 1);
  103. ctx.config[ENABLE_KEEP_ALIVE] = "no";
  104. ASSERT(should_keep_alive(&conn) == 0);
  105. ctx.config[ENABLE_KEEP_ALIVE] = "yes";
  106. ASSERT(should_keep_alive(&conn) == 1);
  107. conn.must_close = 1;
  108. ASSERT(should_keep_alive(&conn) == 0);
  109. conn.must_close = 0;
  110. parse_http_message(req2, sizeof(req2), &conn.request_info);
  111. ASSERT(should_keep_alive(&conn) == 0);
  112. parse_http_message(req3, sizeof(req3), &conn.request_info);
  113. ASSERT(should_keep_alive(&conn) == 0);
  114. parse_http_message(req4, sizeof(req4), &conn.request_info);
  115. ASSERT(should_keep_alive(&conn) == 1);
  116. conn.status_code = 401;
  117. ASSERT(should_keep_alive(&conn) == 0);
  118. conn.status_code = 200;
  119. conn.must_close = 1;
  120. ASSERT(should_keep_alive(&conn) == 0);
  121. }
  122. static void test_match_prefix(void) {
  123. ASSERT(match_prefix("/api", 4, "/api") == 4);
  124. ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
  125. ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
  126. ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
  127. ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
  128. ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
  129. ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
  130. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  131. ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  132. ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
  133. ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
  134. ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
  135. ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  136. ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  137. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  138. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  139. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  140. ASSERT(match_prefix("$", 1, "") == 0);
  141. ASSERT(match_prefix("$", 1, "x") == -1);
  142. ASSERT(match_prefix("*$", 2, "x") == 1);
  143. ASSERT(match_prefix("/$", 2, "/") == 1);
  144. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  145. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  146. ASSERT(match_prefix("*", 1, "/hello/") == 0);
  147. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  148. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  149. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/B.A") == 6);
  150. ASSERT(match_prefix("**o$", 4, "HELLO") == 5);
  151. }
  152. static void test_remove_double_dots() {
  153. struct { char before[20], after[20]; } data[] = {
  154. {"////a", "/a"},
  155. {"/.....", "/."},
  156. {"/......", "/"},
  157. {"...", "..."},
  158. {"/...///", "/./"},
  159. {"/a...///", "/a.../"},
  160. {"/.x", "/.x"},
  161. {"/\\", "/"},
  162. {"/a\\", "/a\\"},
  163. {"/a\\\\...", "/a\\."},
  164. };
  165. size_t i;
  166. for (i = 0; i < ARRAY_SIZE(data); i++) {
  167. remove_double_dots_and_double_slashes(data[i].before);
  168. ASSERT(strcmp(data[i].before, data[i].after) == 0);
  169. }
  170. }
  171. static char *read_file(const char *path, int *size) {
  172. FILE *fp;
  173. struct stat st;
  174. char *data = NULL;
  175. if ((fp = fopen(path, "rb")) != NULL && !fstat(fileno(fp), &st)) {
  176. *size = (int) st.st_size;
  177. ASSERT((data = mg_malloc(*size)) != NULL);
  178. ASSERT(fread(data, 1, *size, fp) == (size_t) *size);
  179. fclose(fp);
  180. }
  181. return data;
  182. }
  183. static const char *fetch_data = "hello world!\n";
  184. static const char *inmemory_file_data = "hi there";
  185. static const char *upload_filename = "upload_test.txt";
  186. static const char *upload_filename2 = "upload_test2.txt";
  187. static const char *upload_ok_message = "upload successful";
  188. static const char *open_file_cb(const struct mg_connection *conn,
  189. const char *path, size_t *size) {
  190. (void) conn;
  191. if (!strcmp(path, "./blah")) {
  192. *size = strlen(inmemory_file_data);
  193. return inmemory_file_data;
  194. }
  195. return NULL;
  196. }
  197. static void upload_cb(struct mg_connection *conn, const char *path) {
  198. const struct mg_request_info *ri = mg_get_request_info(conn);
  199. char *p1, *p2;
  200. int len1, len2;
  201. if (atoi(ri->query_string) == 1) {
  202. ASSERT(!strcmp(path, "./upload_test.txt"));
  203. ASSERT((p1 = read_file("src/civetweb.c", &len1)) != NULL);
  204. ASSERT((p2 = read_file(path, &len2)) != NULL);
  205. ASSERT(len1 == len2);
  206. ASSERT(memcmp(p1, p2, len1) == 0);
  207. mg_free(p1), mg_free(p2);
  208. remove(upload_filename);
  209. } else if (atoi(ri->query_string) == 2) {
  210. if (!strcmp(path, "./upload_test.txt")) {
  211. ASSERT((p1 = read_file("include/civetweb.h", &len1)) != NULL);
  212. ASSERT((p2 = read_file(path, &len2)) != NULL);
  213. ASSERT(len1 == len2);
  214. ASSERT(memcmp(p1, p2, len1) == 0);
  215. mg_free(p1), mg_free(p2);
  216. remove(upload_filename);
  217. } else if (!strcmp(path, "./upload_test2.txt")) {
  218. ASSERT((p1 = read_file("README.md", &len1)) != NULL);
  219. ASSERT((p2 = read_file(path, &len2)) != NULL);
  220. ASSERT(len1 == len2);
  221. ASSERT(memcmp(p1, p2, len1) == 0);
  222. mg_free(p1), mg_free(p2);
  223. remove(upload_filename);
  224. } else {
  225. ASSERT(0);
  226. }
  227. } else {
  228. ASSERT(0);
  229. }
  230. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s",
  231. (int) strlen(upload_ok_message), upload_ok_message);
  232. }
  233. static int begin_request_handler_cb(struct mg_connection *conn) {
  234. const struct mg_request_info *ri = mg_get_request_info(conn);
  235. if (!strcmp(ri->uri, "/data")) {
  236. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  237. "Content-Type: text/plain\r\n\r\n"
  238. "%s", fetch_data);
  239. close_connection(conn);
  240. return 1;
  241. }
  242. if (!strcmp(ri->uri, "/upload")) {
  243. ASSERT(ri->query_string != NULL);
  244. ASSERT(mg_upload(conn, ".") == atoi(ri->query_string));
  245. }
  246. return 0;
  247. }
  248. static int log_message_cb(const struct mg_connection *conn, const char *msg) {
  249. (void) conn;
  250. printf("%s\n", msg);
  251. return 0;
  252. }
  253. int (*begin_request)(struct mg_connection *);
  254. void (*end_request)(const struct mg_connection *, int reply_status_code);
  255. int (*log_message)(const struct mg_connection *, const char *message);
  256. int (*init_ssl)(void *ssl_context, void *user_data);
  257. int (*websocket_connect)(const struct mg_connection *);
  258. void (*websocket_ready)(struct mg_connection *);
  259. int (*websocket_data)(struct mg_connection *, int bits,
  260. char *data, size_t data_len);
  261. void (*connection_close)(struct mg_connection *);
  262. const char * (*open_file)(const struct mg_connection *,
  263. const char *path, size_t *data_len);
  264. void (*init_lua)(struct mg_connection *, void *lua_context);
  265. void (*upload)(struct mg_connection *, const char *file_name);
  266. int (*http_error)(struct mg_connection *, int status);
  267. static const struct mg_callbacks CALLBACKS = {
  268. &begin_request_handler_cb, NULL, &log_message_cb, NULL, NULL, NULL, NULL, NULL,
  269. &open_file_cb, NULL, &upload_cb, NULL
  270. };
  271. static const char *OPTIONS[] = {
  272. "document_root", ".",
  273. "listening_ports", LISTENING_ADDR,
  274. "enable_keep_alive", "yes",
  275. /* TODO(bel): "ssl_certificate", "resources/ssl_cert.pem", */
  276. NULL,
  277. };
  278. static char *read_conn(struct mg_connection *conn, int *size) {
  279. char buf[100], *data = NULL;
  280. int len;
  281. *size = 0;
  282. while ((len = mg_read(conn, buf, sizeof(buf))) > 0) {
  283. *size += len;
  284. ASSERT((data = mg_realloc(data, *size)) != NULL);
  285. memcpy(data + *size - len, buf, len);
  286. }
  287. return data;
  288. }
  289. static void test_mg_download(void) {
  290. char *p1, *p2, ebuf[100];
  291. int len1, len2, port = atoi(HTTPS_PORT);
  292. struct mg_connection *conn;
  293. struct mg_context *ctx;
  294. /* TODO(bel): int use_ssl = 1; */
  295. int use_ssl = 0;
  296. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  297. ASSERT(mg_download(NULL, port, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
  298. ASSERT(mg_download("localhost", 0, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
  299. ASSERT(mg_download("localhost", port, 1, ebuf, sizeof(ebuf),
  300. "%s", "") == NULL);
  301. /* Fetch nonexistent file, should see 404 */
  302. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  303. "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL);
  304. ASSERT(strcmp(conn->request_info.uri, "404") == 0);
  305. mg_close_connection(conn);
  306. if (use_ssl) {
  307. ASSERT((conn = mg_download("google.com", 443, 1, ebuf, sizeof(ebuf), "%s",
  308. "GET / HTTP/1.0\r\n\r\n")) != NULL);
  309. mg_close_connection(conn);
  310. }
  311. ASSERT((conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf), "%s",
  312. "GET / HTTP/1.0\r\n\r\n")) != NULL);
  313. mg_close_connection(conn);
  314. /* Fetch unit_test.c, should succeed */
  315. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  316. "GET /unit_test.c HTTP/1.0\r\n\r\n")) != NULL);
  317. ASSERT(!strcmp(conn->request_info.uri, "200"));
  318. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  319. ASSERT((p2 = read_file("unit_test.c", &len2)) != NULL);
  320. ASSERT(len1 == len2);
  321. ASSERT(memcmp(p1, p2, len1) == 0);
  322. mg_free(p1), mg_free(p2);
  323. mg_close_connection(conn);
  324. /* Fetch in-memory file, should succeed. */
  325. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  326. "GET /blah HTTP/1.1\r\n\r\n")) != NULL);
  327. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  328. ASSERT(len1 == (int) strlen(inmemory_file_data));
  329. ASSERT(memcmp(p1, inmemory_file_data, len1) == 0);
  330. mg_free(p1);
  331. mg_close_connection(conn);
  332. /* Fetch in-memory data with no Content-Length, should succeed. */
  333. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  334. "GET /data HTTP/1.1\r\n\r\n")) != NULL);
  335. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  336. ASSERT(len1 == (int) strlen(fetch_data));
  337. ASSERT(memcmp(p1, fetch_data, len1) == 0);
  338. mg_free(p1);
  339. mg_close_connection(conn);
  340. /* Test SSL redirect, IP address */
  341. /* TODO(bel):
  342. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  343. ebuf, sizeof(ebuf), "%s",
  344. "GET /foo HTTP/1.1\r\n\r\n")) != NULL);
  345. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  346. ASSERT(strcmp(mg_get_header(conn, "Location"),
  347. "https://127.0.0.1:" HTTPS_PORT "/foo") == 0);
  348. mg_close_connection(conn);
  349. */
  350. /* Test SSL redirect, Host: */
  351. /* TODO(bel):
  352. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  353. ebuf, sizeof(ebuf), "%s",
  354. "GET /foo HTTP/1.1\r\nHost: a.b:77\n\n")) != NULL);
  355. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  356. ASSERT(strcmp(mg_get_header(conn, "Location"),
  357. "https://a.b:" HTTPS_PORT "/foo") == 0);
  358. mg_close_connection(conn);
  359. */
  360. mg_stop(ctx);
  361. }
  362. static int alloc_printf(char **buf, size_t size, char *fmt, ...) {
  363. va_list ap;
  364. va_start(ap, fmt);
  365. return alloc_vprintf(buf, size, fmt, ap);
  366. }
  367. static void test_mg_upload(void) {
  368. static const char *boundary = "OOO___MY_BOUNDARY___OOO";
  369. struct mg_context *ctx;
  370. struct mg_connection *conn;
  371. char ebuf[100], buf[20], *file_data, *file2_data, *post_data;
  372. int file_len, file2_len, post_data_len;
  373. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  374. /* Upload one file */
  375. ASSERT((file_data = read_file("unit_test.c", &file_len)) != NULL);
  376. post_data = NULL;
  377. post_data_len = alloc_printf(&post_data, 0,
  378. "--%s\r\n"
  379. "Content-Disposition: form-data; "
  380. "name=\"file\"; "
  381. "filename=\"%s\"\r\n\r\n"
  382. "%.*s\r\n"
  383. "--%s--\r\n",
  384. boundary, upload_filename,
  385. file_len, file_data, boundary);
  386. ASSERT(post_data_len > 0);
  387. #if 0 /* TODO (bel): ... */
  388. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  389. ebuf, sizeof(ebuf),
  390. "POST /upload?1 HTTP/1.1\r\n"
  391. "Content-Length: %d\r\n"
  392. "Content-Type: multipart/form-data; "
  393. "boundary=%s\r\n\r\n"
  394. "%.*s", post_data_len, boundary,
  395. post_data_len, post_data)) != NULL);
  396. mg_free(file_data), mg_free(post_data);
  397. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  398. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  399. mg_close_connection(conn);
  400. /* Upload two files */
  401. ASSERT((file_data = read_file("include/civetweb.h", &file_len)) != NULL);
  402. ASSERT((file2_data = read_file("README.md", &file2_len)) != NULL);
  403. post_data = NULL;
  404. post_data_len = alloc_printf(&post_data, 0,
  405. /* First file */
  406. "--%s\r\n"
  407. "Content-Disposition: form-data; "
  408. "name=\"file\"; "
  409. "filename=\"%s\"\r\n\r\n"
  410. "%.*s\r\n"
  411. /* Second file */
  412. "--%s\r\n"
  413. "Content-Disposition: form-data; "
  414. "name=\"file\"; "
  415. "filename=\"%s\"\r\n\r\n"
  416. "%.*s\r\n"
  417. /* Final boundary */
  418. "--%s--\r\n",
  419. boundary, upload_filename,
  420. file_len, file_data,
  421. boundary, upload_filename2,
  422. file2_len, file2_data,
  423. boundary);
  424. ASSERT(post_data_len > 0);
  425. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  426. ebuf, sizeof(ebuf),
  427. "POST /upload?2 HTTP/1.1\r\n"
  428. "Content-Length: %d\r\n"
  429. "Content-Type: multipart/form-data; "
  430. "boundary=%s\r\n\r\n"
  431. "%.*s", post_data_len, boundary,
  432. post_data_len, post_data)) != NULL);
  433. mg_free(file_data), mg_free(file2_data), mg_free(post_data);
  434. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  435. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  436. mg_close_connection(conn);
  437. #endif
  438. mg_stop(ctx);
  439. }
  440. static void test_base64_encode(void) {
  441. const char *in[] = {"a", "ab", "abc", "abcd", NULL};
  442. const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
  443. char buf[100];
  444. int i;
  445. for (i = 0; in[i] != NULL; i++) {
  446. base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
  447. ASSERT(!strcmp(buf, out[i]));
  448. }
  449. }
  450. static void test_mg_get_var(void) {
  451. static const char *post[] = {
  452. "a=1&&b=2&d&=&c=3%20&e=",
  453. "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=",
  454. NULL
  455. };
  456. char buf[20];
  457. ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
  458. ASSERT(buf[0] == '1' && buf[1] == '\0');
  459. ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
  460. ASSERT(buf[0] == '2' && buf[1] == '\0');
  461. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
  462. ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
  463. ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
  464. ASSERT(buf[0] == '\0');
  465. ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
  466. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2);
  467. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
  468. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
  469. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2);
  470. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);
  471. }
  472. static void test_set_throttle(void) {
  473. ASSERT(set_throttle(NULL, 0x0a000001, "/") == 0);
  474. ASSERT(set_throttle("10.0.0.0/8=20", 0x0a000001, "/") == 20);
  475. ASSERT(set_throttle("10.0.0.0/8=0.5k", 0x0a000001, "/") == 512);
  476. ASSERT(set_throttle("10.0.0.0/8=17m", 0x0a000001, "/") == 1048576 * 17);
  477. ASSERT(set_throttle("10.0.0.0/8=1x", 0x0a000001, "/") == 0);
  478. ASSERT(set_throttle("10.0.0.0/8=5,0.0.0.0/0=10", 0x0a000001, "/") == 10);
  479. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/index") == 5);
  480. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/foo/x") == 7);
  481. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0b000001, "/foxo/x") == 0);
  482. ASSERT(set_throttle("10.0.0.0/8=5,*=1", 0x0b000001, "/foxo/x") == 1);
  483. }
  484. static void test_next_option(void) {
  485. const char *p, *list = "x/8,/y**=1;2k,z";
  486. struct vec a, b;
  487. int i;
  488. ASSERT(next_option(NULL, &a, &b) == NULL);
  489. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  490. ASSERT(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  491. ASSERT(i != 1 || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9 &&
  492. b.len == 4));
  493. ASSERT(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  494. }
  495. }
  496. #if defined(USE_LUA)
  497. static void check_lua_expr(lua_State *L, const char *expr, const char *value) {
  498. const char *v, *var_name = "myVar";
  499. char buf[100];
  500. snprintf(buf, sizeof(buf), "%s = %s", var_name, expr);
  501. (void) luaL_dostring(L, buf);
  502. lua_getglobal(L, var_name);
  503. v = lua_tostring(L, -1);
  504. ASSERT((value == NULL && v == NULL) ||
  505. (value != NULL && v != NULL && !strcmp(value, v)));
  506. }
  507. static void test_lua(void) {
  508. static struct mg_connection conn;
  509. static struct mg_context ctx;
  510. char http_request[] = "POST /foo/bar HTTP/1.1\r\n"
  511. "Content-Length: 12\r\n"
  512. "Connection: close\r\n\r\nhello world!";
  513. lua_State *L = luaL_newstate();
  514. conn.ctx = &ctx;
  515. conn.buf = http_request;
  516. conn.buf_size = conn.data_len = strlen(http_request);
  517. conn.request_len = parse_http_message(conn.buf, conn.data_len,
  518. &conn.request_info);
  519. conn.content_len = conn.data_len - conn.request_len;
  520. prepare_lua_environment(&conn, L, "unit_test", LUA_ENV_TYPE_PLAIN_LUA_PAGE);
  521. ASSERT(lua_gettop(L) == 4);
  522. check_lua_expr(L, "'hi'", "hi");
  523. check_lua_expr(L, "mg.request_info.request_method", "POST");
  524. check_lua_expr(L, "mg.request_info.uri", "/foo/bar");
  525. check_lua_expr(L, "mg.request_info.num_headers", "2");
  526. check_lua_expr(L, "mg.request_info.remote_ip", "0");
  527. check_lua_expr(L, "mg.request_info.http_headers['Content-Length']", "12");
  528. check_lua_expr(L, "mg.request_info.http_headers['Connection']", "close");
  529. (void) luaL_dostring(L, "post = mg.read()");
  530. check_lua_expr(L, "# post", "12");
  531. check_lua_expr(L, "post", "hello world!");
  532. lua_close(L);
  533. }
  534. #endif
  535. static void test_mg_stat(void) {
  536. static struct mg_context ctx;
  537. struct file file = STRUCT_FILE_INITIALIZER;
  538. ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file));
  539. }
  540. static void test_skip_quoted(void) {
  541. char x[] = "a=1, b=2 c='hi \' there'", *s = x, *p;
  542. p = skip_quoted(&s, ", ", ", ", 0);
  543. ASSERT(p != NULL && !strcmp(p, "a=1"));
  544. p = skip_quoted(&s, ", ", ", ", 0);
  545. ASSERT(p != NULL && !strcmp(p, "b=2"));
  546. /* TODO(lsm): fix this */
  547. #if 0
  548. p = skip_quoted(&s, "'", ", ", '\\');
  549. p = skip_quoted(&s, "'", ", ", '\\');
  550. printf("[%s]\n", p);
  551. ASSERT(p != NULL && !strcmp(p, "hi ' there"));
  552. #endif
  553. }
  554. static void test_alloc_vprintf(void) {
  555. char buf[MG_BUF_LEN], *p = buf;
  556. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  557. ASSERT(p == buf);
  558. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  559. ASSERT(alloc_printf(&p, sizeof(buf), "") == 0);
  560. /* Pass small buffer, make sure alloc_printf allocates */
  561. ASSERT(alloc_printf(&p, 1, "%s", "hello") == 5);
  562. ASSERT(p != buf);
  563. mg_free(p);
  564. }
  565. static void test_request_replies(void) {
  566. char ebuf[100];
  567. int i;
  568. struct mg_connection *conn;
  569. struct mg_context *ctx;
  570. static struct { const char *request, *reply_regex; } tests[] = {
  571. {
  572. "GET hello.txt HTTP/1.0\r\nRange: bytes=3-5\r\n\r\n",
  573. "^HTTP/1.1 206 Partial Content"
  574. },
  575. {NULL, NULL},
  576. };
  577. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  578. for (i = 0; tests[i].request != NULL; i++) {
  579. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s",
  580. tests[i].request)) != NULL);
  581. mg_close_connection(conn);
  582. }
  583. mg_stop(ctx);
  584. /* TODO(bel):
  585. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  586. for (i = 0; tests[i].request != NULL; i++) {
  587. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1, ebuf, sizeof(ebuf), "%s",
  588. tests[i].request)) != NULL);
  589. mg_close_connection(conn);
  590. }
  591. mg_stop(ctx);
  592. */
  593. }
  594. static int api_callback(struct mg_connection *conn) {
  595. struct mg_request_info *ri = mg_get_request_info(conn);
  596. char post_data[100] = "";
  597. ASSERT(ri->user_data == (void *) 123);
  598. ASSERT(ri->num_headers == 2);
  599. ASSERT(strcmp(mg_get_header(conn, "host"), "blah.com") == 0);
  600. ASSERT(mg_read(conn, post_data, sizeof(post_data)) == 3);
  601. ASSERT(memcmp(post_data, "b=1", 3) == 0);
  602. ASSERT(ri->query_string != NULL);
  603. ASSERT(ri->remote_ip > 0);
  604. ASSERT(ri->remote_port > 0);
  605. ASSERT(strcmp(ri->http_version, "1.0") == 0);
  606. mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\n");
  607. return 1;
  608. }
  609. static void test_api_calls(void) {
  610. char ebuf[100];
  611. struct mg_callbacks callbacks;
  612. struct mg_connection *conn;
  613. struct mg_context *ctx;
  614. static const char *request = "POST /?a=%20&b=&c=xx HTTP/1.0\r\n"
  615. "Host: blah.com\n" /* More spaces before */
  616. "content-length: 3\r\n" /* Lower case header name */
  617. "\r\nb=123456"; /* Content size > content-length, test for mg_read() */
  618. memset(&callbacks, 0, sizeof(callbacks));
  619. callbacks.begin_request = api_callback;
  620. ASSERT((ctx = mg_start(&callbacks, (void *) 123, OPTIONS)) != NULL);
  621. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  622. ebuf, sizeof(ebuf), "%s", request)) != NULL);
  623. mg_close_connection(conn);
  624. mg_stop(ctx);
  625. }
  626. static void test_url_decode(void) {
  627. char buf[100];
  628. ASSERT(mg_url_decode("foo", 3, buf, 3, 0) == -1); /* No space for \0 */
  629. ASSERT(mg_url_decode("foo", 3, buf, 4, 0) == 3);
  630. ASSERT(strcmp(buf, "foo") == 0);
  631. ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 0) == 2);
  632. ASSERT(strcmp(buf, "a+") == 0);
  633. ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 1) == 2);
  634. ASSERT(strcmp(buf, "a ") == 0);
  635. ASSERT(mg_url_decode("%61", 1, buf, sizeof(buf), 1) == 1);
  636. ASSERT(strcmp(buf, "%") == 0);
  637. ASSERT(mg_url_decode("%61", 2, buf, sizeof(buf), 1) == 2);
  638. ASSERT(strcmp(buf, "%6") == 0);
  639. ASSERT(mg_url_decode("%61", 3, buf, sizeof(buf), 1) == 1);
  640. ASSERT(strcmp(buf, "a") == 0);
  641. }
  642. static void test_mg_strcasestr(void) {
  643. static const char *big1 = "abcdef";
  644. ASSERT(mg_strcasestr("Y", "X") == NULL);
  645. ASSERT(mg_strcasestr("Y", "y") != NULL);
  646. ASSERT(mg_strcasestr(big1, "X") == NULL);
  647. ASSERT(mg_strcasestr(big1, "CD") == big1 + 2);
  648. ASSERT(mg_strcasestr("aa", "AAB") == NULL);
  649. }
  650. static void test_mg_get_cookie(void) {
  651. char buf[20];
  652. ASSERT(mg_get_cookie("", "foo", NULL, sizeof(buf)) == -2);
  653. ASSERT(mg_get_cookie("", "foo", buf, 0) == -2);
  654. ASSERT(mg_get_cookie("", "foo", buf, sizeof(buf)) == -1);
  655. ASSERT(mg_get_cookie("", NULL, buf, sizeof(buf)) == -1);
  656. ASSERT(mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)) == 1);
  657. ASSERT(strcmp(buf, "1") == 0);
  658. ASSERT(mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)) == 1);
  659. ASSERT(strcmp(buf, "2") == 0);
  660. ASSERT(mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)) == 3);
  661. ASSERT(strcmp(buf, "123") == 0);
  662. ASSERT(mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)) == -1);
  663. }
  664. static void test_strtoll(void) {
  665. ASSERT(strtoll("0", NULL, 10) == 0);
  666. ASSERT(strtoll("123", NULL, 10) == 123);
  667. ASSERT(strtoll("-34", NULL, 10) == -34);
  668. ASSERT(strtoll("3566626116", NULL, 10) == 3566626116);
  669. }
  670. static void test_parse_port_string(void) {
  671. static const char *valid[] = {
  672. "0", "1", "1s", "1r", "1.2.3.4:1", "1.2.3.4:1s", "1.2.3.4:1r",
  673. #if defined(USE_IPV6)
  674. "[::1]:123", "[3ffe:2a00:100:7031::1]:900",
  675. #endif
  676. NULL
  677. };
  678. static const char *invalid[] = {
  679. "99999", "1k", "1.2.3", "1.2.3.4:", "1.2.3.4:2p",
  680. NULL
  681. };
  682. struct socket so;
  683. struct vec vec;
  684. int i;
  685. for (i = 0; valid[i] != NULL; i++) {
  686. vec.ptr = valid[i];
  687. vec.len = strlen(vec.ptr);
  688. ASSERT(parse_port_string(&vec, &so) != 0);
  689. }
  690. for (i = 0; invalid[i] != NULL; i++) {
  691. vec.ptr = invalid[i];
  692. vec.len = strlen(vec.ptr);
  693. ASSERT(parse_port_string(&vec, &so) == 0);
  694. }
  695. }
  696. int __cdecl main(void) {
  697. char buffer[512];
  698. FILE * f;
  699. /* print headline */
  700. printf("Civetweb %s unit test\n", mg_version());
  701. #if defined(_WIN32)
  702. GetCurrentDirectoryA(sizeof(buffer), buffer);
  703. #else
  704. getcwd(buffer, sizeof(buffer));
  705. #endif
  706. printf("Test directory is \"%s\"\n", buffer); /* should be the "test" directory */
  707. f = fopen("hello.txt", "r");
  708. if (f) {
  709. fclose(f);
  710. } else {
  711. printf("Error: Test directory does not contain hello.txt\n", buffer);
  712. }
  713. f = fopen("unit_test.c", "r");
  714. if (f) {
  715. fclose(f);
  716. } else {
  717. printf("Error: Test directory does not contain unit_test.c\n", buffer);
  718. }
  719. /* test local functions */
  720. test_parse_port_string();
  721. test_mg_strcasestr();
  722. test_alloc_vprintf();
  723. test_base64_encode();
  724. test_match_prefix();
  725. test_remove_double_dots();
  726. test_should_keep_alive();
  727. test_parse_http_message();
  728. test_mg_get_var();
  729. test_set_throttle();
  730. test_next_option();
  731. test_mg_stat();
  732. test_skip_quoted();
  733. test_url_decode();
  734. test_mg_get_cookie();
  735. test_strtoll();
  736. /* tests with network access */
  737. test_mg_download();
  738. test_mg_upload();
  739. test_request_replies();
  740. test_api_calls();
  741. #if defined(USE_LUA)
  742. test_lua();
  743. #endif
  744. printf("TOTAL TESTS: %d, FAILED: %d\n", s_total_tests, s_failed_tests);
  745. return s_failed_tests == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
  746. }