unit_test.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. // Copyright (c) 2004-2013 Sergey Lyubka
  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. // Unit test for the mongoose web server. Tests embedded API.
  22. #define USE_WEBSOCKET
  23. #define USE_LUA
  24. #include "mongoose.c"
  25. #define FATAL(str, line) do { \
  26. printf("Fail on line %d: [%s]\n", line, str); \
  27. abort(); \
  28. } while (0)
  29. #define ASSERT(expr) do { if (!(expr)) FATAL(#expr, __LINE__); } while (0)
  30. #define HTTP_PORT "56789"
  31. #define HTTPS_PORT "56790"
  32. #define HTTP_PORT2 "56791"
  33. #define LISTENING_ADDR \
  34. "127.0.0.1:" HTTP_PORT "r" \
  35. ",127.0.0.1:" HTTPS_PORT "s" \
  36. ",127.0.0.1:" HTTP_PORT2
  37. #ifndef _WIN32
  38. #define __cdecl
  39. #endif
  40. static void test_parse_http_message() {
  41. struct mg_request_info ri;
  42. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  43. char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
  44. char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
  45. char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
  46. char req5[] = "GET / HTTP/1.1\r\n\r\n";
  47. char req6[] = "G";
  48. char req7[] = " blah ";
  49. char req8[] = " HTTP/1.1 200 OK \n\n";
  50. ASSERT(parse_http_message(req1, sizeof(req1), &ri) == sizeof(req1) - 1);
  51. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  52. ASSERT(ri.num_headers == 0);
  53. ASSERT(parse_http_message(req2, sizeof(req2), &ri) == -1);
  54. ASSERT(parse_http_message(req3, sizeof(req3), &ri) == 0);
  55. ASSERT(parse_http_message(req6, sizeof(req6), &ri) == 0);
  56. ASSERT(parse_http_message(req7, sizeof(req7), &ri) == 0);
  57. ASSERT(parse_http_message("", 0, &ri) == 0);
  58. ASSERT(parse_http_message(req8, sizeof(req8), &ri) == sizeof(req8) - 1);
  59. // TODO(lsm): Fix this. Header value may span multiple lines.
  60. ASSERT(parse_http_message(req4, sizeof(req4), &ri) == sizeof(req4) - 1);
  61. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  62. ASSERT(ri.num_headers == 3);
  63. ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
  64. ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
  65. ASSERT(strcmp(ri.http_headers[1].name, "B") == 0);
  66. ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0);
  67. ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
  68. ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
  69. ASSERT(parse_http_message(req5, sizeof(req5), &ri) == sizeof(req5) - 1);
  70. ASSERT(strcmp(ri.request_method, "GET") == 0);
  71. ASSERT(strcmp(ri.http_version, "1.1") == 0);
  72. }
  73. static void test_should_keep_alive(void) {
  74. struct mg_connection conn;
  75. struct mg_context ctx;
  76. char req1[] = "GET / HTTP/1.1\r\n\r\n";
  77. char req2[] = "GET / HTTP/1.0\r\n\r\n";
  78. char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  79. char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
  80. memset(&conn, 0, sizeof(conn));
  81. conn.ctx = &ctx;
  82. ASSERT(parse_http_message(req1, sizeof(req1), &conn.request_info) ==
  83. sizeof(req1) - 1);
  84. ctx.config[ENABLE_KEEP_ALIVE] = "no";
  85. ASSERT(should_keep_alive(&conn) == 0);
  86. ctx.config[ENABLE_KEEP_ALIVE] = "yes";
  87. ASSERT(should_keep_alive(&conn) == 1);
  88. conn.must_close = 1;
  89. ASSERT(should_keep_alive(&conn) == 0);
  90. conn.must_close = 0;
  91. parse_http_message(req2, sizeof(req2), &conn.request_info);
  92. ASSERT(should_keep_alive(&conn) == 0);
  93. parse_http_message(req3, sizeof(req3), &conn.request_info);
  94. ASSERT(should_keep_alive(&conn) == 0);
  95. parse_http_message(req4, sizeof(req4), &conn.request_info);
  96. ASSERT(should_keep_alive(&conn) == 1);
  97. conn.status_code = 401;
  98. ASSERT(should_keep_alive(&conn) == 0);
  99. conn.status_code = 200;
  100. conn.must_close = 1;
  101. ASSERT(should_keep_alive(&conn) == 0);
  102. }
  103. static void test_match_prefix(void) {
  104. ASSERT(match_prefix("/api", 4, "/api") == 4);
  105. ASSERT(match_prefix("/a/", 3, "/a/b/c") == 3);
  106. ASSERT(match_prefix("/a/", 3, "/ab/c") == -1);
  107. ASSERT(match_prefix("/*/", 3, "/ab/c") == 4);
  108. ASSERT(match_prefix("**", 2, "/a/b/c") == 6);
  109. ASSERT(match_prefix("/*", 2, "/a/b/c") == 2);
  110. ASSERT(match_prefix("*/*", 3, "/a/b/c") == 2);
  111. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  112. ASSERT(match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
  113. ASSERT(match_prefix("a|b|cd", 6, "cdef") == 2);
  114. ASSERT(match_prefix("a|b|c?", 6, "cdef") == 2);
  115. ASSERT(match_prefix("a|?|cd", 6, "cdef") == 1);
  116. ASSERT(match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
  117. ASSERT(match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
  118. ASSERT(match_prefix("**/", 3, "/a/b/c") == 5);
  119. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  120. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  121. ASSERT(match_prefix("$", 1, "") == 0);
  122. ASSERT(match_prefix("$", 1, "x") == -1);
  123. ASSERT(match_prefix("*$", 2, "x") == 1);
  124. ASSERT(match_prefix("/$", 2, "/") == 1);
  125. ASSERT(match_prefix("**/$", 4, "/a/b/c") == -1);
  126. ASSERT(match_prefix("**/$", 4, "/a/b/") == 5);
  127. ASSERT(match_prefix("*", 1, "/hello/") == 0);
  128. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
  129. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
  130. ASSERT(match_prefix("**.a$|**.b$", 11, "/a/b.a") == 6);
  131. }
  132. static void test_remove_double_dots() {
  133. struct { char before[20], after[20]; } data[] = {
  134. {"////a", "/a"},
  135. {"/.....", "/."},
  136. {"/......", "/"},
  137. {"...", "..."},
  138. {"/...///", "/./"},
  139. {"/a...///", "/a.../"},
  140. {"/.x", "/.x"},
  141. {"/\\", "/"},
  142. {"/a\\", "/a\\"},
  143. {"/a\\\\...", "/a\\."},
  144. };
  145. size_t i;
  146. for (i = 0; i < ARRAY_SIZE(data); i++) {
  147. remove_double_dots_and_double_slashes(data[i].before);
  148. ASSERT(strcmp(data[i].before, data[i].after) == 0);
  149. }
  150. }
  151. static char *read_file(const char *path, int *size) {
  152. FILE *fp;
  153. struct stat st;
  154. char *data = NULL;
  155. if ((fp = fopen(path, "rb")) != NULL && !fstat(fileno(fp), &st)) {
  156. *size = (int) st.st_size;
  157. ASSERT((data = malloc(*size)) != NULL);
  158. ASSERT(fread(data, 1, *size, fp) == (size_t) *size);
  159. fclose(fp);
  160. }
  161. return data;
  162. }
  163. static const char *fetch_data = "hello world!\n";
  164. static const char *inmemory_file_data = "hi there";
  165. static const char *upload_filename = "upload_test.txt";
  166. static const char *upload_ok_message = "upload successful";
  167. static const char *open_file_cb(const struct mg_connection *conn,
  168. const char *path, size_t *size) {
  169. (void) conn;
  170. if (!strcmp(path, "./blah")) {
  171. *size = strlen(inmemory_file_data);
  172. return inmemory_file_data;
  173. }
  174. return NULL;
  175. }
  176. static void upload_cb(struct mg_connection *conn, const char *path) {
  177. char *p1, *p2;
  178. int len1, len2;
  179. ASSERT(!strcmp(path, "./upload_test.txt"));
  180. ASSERT((p1 = read_file("mongoose.c", &len1)) != NULL);
  181. ASSERT((p2 = read_file(path, &len2)) != NULL);
  182. ASSERT(len1 == len2);
  183. ASSERT(memcmp(p1, p2, len1) == 0);
  184. free(p1), free(p2);
  185. remove(upload_filename);
  186. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s",
  187. (int) strlen(upload_ok_message), upload_ok_message);
  188. }
  189. static int begin_request_handler_cb(struct mg_connection *conn) {
  190. const struct mg_request_info *ri = mg_get_request_info(conn);
  191. if (!strcmp(ri->uri, "/data")) {
  192. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  193. "Content-Length: %d\r\n"
  194. "Content-Type: text/plain\r\n\r\n"
  195. "%s", (int) strlen(fetch_data), fetch_data);
  196. return 1;
  197. }
  198. if (!strcmp(ri->uri, "/upload")) {
  199. ASSERT(mg_upload(conn, ".") == 1);
  200. }
  201. return 0;
  202. }
  203. static int log_message_cb(const struct mg_connection *conn, const char *msg) {
  204. (void) conn;
  205. printf("%s\n", msg);
  206. return 0;
  207. }
  208. static const struct mg_callbacks CALLBACKS = {
  209. &begin_request_handler_cb, NULL, &log_message_cb, NULL, NULL, NULL, NULL,
  210. &open_file_cb, NULL, &upload_cb
  211. };
  212. static const char *OPTIONS[] = {
  213. "document_root", ".",
  214. "listening_ports", LISTENING_ADDR,
  215. "ssl_certificate", "build/ssl_cert.pem",
  216. NULL,
  217. };
  218. static char *read_conn(struct mg_connection *conn, int *size) {
  219. char buf[100], *data = NULL;
  220. int len;
  221. *size = 0;
  222. while ((len = mg_read(conn, buf, sizeof(buf))) > 0) {
  223. *size += len;
  224. ASSERT((data = realloc(data, *size)) != NULL);
  225. memcpy(data + *size - len, buf, len);
  226. }
  227. return data;
  228. }
  229. static void test_mg_download(void) {
  230. char *p1, *p2, ebuf[100];
  231. int len1, len2, port = atoi(HTTPS_PORT);
  232. struct mg_connection *conn;
  233. struct mg_context *ctx;
  234. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  235. ASSERT(mg_download(NULL, port, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
  236. ASSERT(mg_download("localhost", 0, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
  237. ASSERT(mg_download("localhost", port, 1, ebuf, sizeof(ebuf),
  238. "%s", "") == NULL);
  239. // Fetch nonexistent file, should see 404
  240. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  241. "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL);
  242. ASSERT(strcmp(conn->request_info.uri, "404") == 0);
  243. mg_close_connection(conn);
  244. ASSERT((conn = mg_download("google.com", 443, 1, ebuf, sizeof(ebuf), "%s",
  245. "GET / HTTP/1.0\r\n\r\n")) != NULL);
  246. mg_close_connection(conn);
  247. // Fetch mongoose.c, should succeed
  248. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  249. "GET /mongoose.c HTTP/1.0\r\n\r\n")) != NULL);
  250. ASSERT(!strcmp(conn->request_info.uri, "200"));
  251. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  252. ASSERT((p2 = read_file("mongoose.c", &len2)) != NULL);
  253. ASSERT(len1 == len2);
  254. ASSERT(memcmp(p1, p2, len1) == 0);
  255. free(p1), free(p2);
  256. mg_close_connection(conn);
  257. // Fetch in-memory file, should succeed.
  258. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  259. "GET /blah HTTP/1.1\r\n\r\n")) != NULL);
  260. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  261. ASSERT(len1 == (int) strlen(inmemory_file_data));
  262. ASSERT(memcmp(p1, inmemory_file_data, len1) == 0);
  263. free(p1);
  264. mg_close_connection(conn);
  265. // Test SSL redirect, IP address
  266. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  267. ebuf, sizeof(ebuf), "%s",
  268. "GET /foo HTTP/1.1\r\n\r\n")) != NULL);
  269. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  270. ASSERT(strcmp(mg_get_header(conn, "Location"),
  271. "https://127.0.0.1:" HTTPS_PORT "/foo") == 0);
  272. mg_close_connection(conn);
  273. // Test SSL redirect, Host:
  274. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  275. ebuf, sizeof(ebuf), "%s",
  276. "GET /foo HTTP/1.1\r\nHost: a.b:77\n\n")) != NULL);
  277. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  278. ASSERT(strcmp(mg_get_header(conn, "Location"),
  279. "https://a.b:" HTTPS_PORT "/foo") == 0);
  280. mg_close_connection(conn);
  281. mg_stop(ctx);
  282. }
  283. static int alloc_printf(char **buf, size_t size, char *fmt, ...) {
  284. va_list ap;
  285. va_start(ap, fmt);
  286. return alloc_vprintf(buf, size, fmt, ap);
  287. }
  288. static void test_mg_upload(void) {
  289. static const char *boundary = "OOO___MY_BOUNDARY___OOO";
  290. struct mg_context *ctx;
  291. struct mg_connection *conn;
  292. char ebuf[100], buf[20], *file_data, *post_data = NULL;
  293. int file_len, post_data_len;
  294. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  295. ASSERT((file_data = read_file("mongoose.c", &file_len)) != NULL);
  296. post_data_len = alloc_printf(&post_data, 0,
  297. "--%s\r\n"
  298. "Content-Disposition: form-data; "
  299. "name=\"file\"; "
  300. "filename=\"%s\"\r\n\r\n"
  301. "%.*s\r\n"
  302. "--%s\r\n",
  303. boundary, upload_filename,
  304. file_len, file_data, boundary);
  305. ASSERT(post_data_len > 0);
  306. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  307. ebuf, sizeof(ebuf),
  308. "POST /upload HTTP/1.1\r\n"
  309. "Content-Length: %d\r\n"
  310. "Content-Type: multipart/form-data; "
  311. "boundary=%s\r\n\r\n"
  312. "%.*s", post_data_len, boundary,
  313. post_data_len, post_data)) != NULL);
  314. free(file_data), free(post_data);
  315. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  316. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  317. mg_close_connection(conn);
  318. mg_stop(ctx);
  319. }
  320. static void test_base64_encode(void) {
  321. const char *in[] = {"a", "ab", "abc", "abcd", NULL};
  322. const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
  323. char buf[100];
  324. int i;
  325. for (i = 0; in[i] != NULL; i++) {
  326. base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
  327. ASSERT(!strcmp(buf, out[i]));
  328. }
  329. }
  330. static void test_mg_get_var(void) {
  331. static const char *post[] = {
  332. "a=1&&b=2&d&=&c=3%20&e=",
  333. "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=",
  334. NULL
  335. };
  336. char buf[20];
  337. ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
  338. ASSERT(buf[0] == '1' && buf[1] == '\0');
  339. ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
  340. ASSERT(buf[0] == '2' && buf[1] == '\0');
  341. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
  342. ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
  343. ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
  344. ASSERT(buf[0] == '\0');
  345. ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
  346. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2);
  347. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
  348. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
  349. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2);
  350. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);
  351. }
  352. static void test_set_throttle(void) {
  353. ASSERT(set_throttle(NULL, 0x0a000001, "/") == 0);
  354. ASSERT(set_throttle("10.0.0.0/8=20", 0x0a000001, "/") == 20);
  355. ASSERT(set_throttle("10.0.0.0/8=0.5k", 0x0a000001, "/") == 512);
  356. ASSERT(set_throttle("10.0.0.0/8=17m", 0x0a000001, "/") == 1048576 * 17);
  357. ASSERT(set_throttle("10.0.0.0/8=1x", 0x0a000001, "/") == 0);
  358. ASSERT(set_throttle("10.0.0.0/8=5,0.0.0.0/0=10", 0x0a000001, "/") == 10);
  359. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/index") == 5);
  360. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/foo/x") == 7);
  361. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0b000001, "/foxo/x") == 0);
  362. ASSERT(set_throttle("10.0.0.0/8=5,*=1", 0x0b000001, "/foxo/x") == 1);
  363. }
  364. static void test_next_option(void) {
  365. const char *p, *list = "x/8,/y**=1;2k,z";
  366. struct vec a, b;
  367. int i;
  368. ASSERT(next_option(NULL, &a, &b) == NULL);
  369. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  370. ASSERT(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  371. ASSERT(i != 1 || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9 &&
  372. b.len == 4));
  373. ASSERT(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  374. }
  375. }
  376. #ifdef USE_LUA
  377. static void check_lua_expr(lua_State *L, const char *expr, const char *value) {
  378. const char *v, *var_name = "myVar";
  379. char buf[100];
  380. snprintf(buf, sizeof(buf), "%s = %s", var_name, expr);
  381. (void) luaL_dostring(L, buf);
  382. lua_getglobal(L, var_name);
  383. v = lua_tostring(L, -1);
  384. ASSERT((value == NULL && v == NULL) ||
  385. (value != NULL && v != NULL && !strcmp(value, v)));
  386. }
  387. static void test_lua(void) {
  388. static struct mg_connection conn;
  389. static struct mg_context ctx;
  390. char http_request[] = "POST /foo/bar HTTP/1.1\r\n"
  391. "Content-Length: 12\r\n"
  392. "Connection: close\r\n\r\nhello world!";
  393. lua_State *L = luaL_newstate();
  394. conn.ctx = &ctx;
  395. conn.buf = http_request;
  396. conn.buf_size = conn.data_len = strlen(http_request);
  397. conn.request_len = parse_http_message(conn.buf, conn.data_len,
  398. &conn.request_info);
  399. conn.content_len = conn.data_len - conn.request_len;
  400. prepare_lua_environment(&conn, L);
  401. ASSERT(lua_gettop(L) == 0);
  402. check_lua_expr(L, "'hi'", "hi");
  403. check_lua_expr(L, "request_info.request_method", "POST");
  404. check_lua_expr(L, "request_info.uri", "/foo/bar");
  405. check_lua_expr(L, "request_info.num_headers", "2");
  406. check_lua_expr(L, "request_info.remote_ip", "0");
  407. check_lua_expr(L, "request_info.http_headers['Content-Length']", "12");
  408. check_lua_expr(L, "request_info.http_headers['Connection']", "close");
  409. (void) luaL_dostring(L, "post = read()");
  410. check_lua_expr(L, "# post", "12");
  411. check_lua_expr(L, "post", "hello world!");
  412. lua_close(L);
  413. }
  414. #endif
  415. static void test_mg_stat(void) {
  416. static struct mg_context ctx;
  417. struct file file = STRUCT_FILE_INITIALIZER;
  418. ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file));
  419. }
  420. static void test_skip_quoted(void) {
  421. char x[] = "a=1, b=2 c='hi \' there'", *s = x, *p;
  422. p = skip_quoted(&s, ", ", ", ", 0);
  423. ASSERT(p != NULL && !strcmp(p, "a=1"));
  424. p = skip_quoted(&s, ", ", ", ", 0);
  425. ASSERT(p != NULL && !strcmp(p, "b=2"));
  426. // TODO(lsm): fix this
  427. #if 0
  428. p = skip_quoted(&s, "'", ", ", '\\');
  429. p = skip_quoted(&s, "'", ", ", '\\');
  430. printf("[%s]\n", p);
  431. ASSERT(p != NULL && !strcmp(p, "hi ' there"));
  432. #endif
  433. }
  434. static void test_alloc_vprintf(void) {
  435. char buf[MG_BUF_LEN], *p = buf;
  436. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  437. ASSERT(p == buf);
  438. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  439. ASSERT(alloc_printf(&p, sizeof(buf), "") == 0);
  440. // Pass small buffer, make sure alloc_printf allocates
  441. ASSERT(alloc_printf(&p, 1, "%s", "hello") == 5);
  442. ASSERT(p != buf);
  443. free(p);
  444. }
  445. static void test_request_replies(void) {
  446. char ebuf[100];
  447. int i, port = atoi(HTTPS_PORT);
  448. struct mg_connection *conn;
  449. struct mg_context *ctx;
  450. static struct { const char *request, *reply_regex; } tests[] = {
  451. {
  452. "GET test/hello.txt HTTP/1.0\r\nRange: bytes=3-5\r\n\r\n",
  453. "^HTTP/1.1 206 Partial Content"
  454. },
  455. {NULL, NULL},
  456. };
  457. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  458. for (i = 0; tests[i].request != NULL; i++) {
  459. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  460. tests[i].request)) != NULL);
  461. mg_close_connection(conn);
  462. }
  463. mg_stop(ctx);
  464. }
  465. int __cdecl main(void) {
  466. test_alloc_vprintf();
  467. test_base64_encode();
  468. test_match_prefix();
  469. test_remove_double_dots();
  470. test_should_keep_alive();
  471. test_parse_http_message();
  472. test_mg_download();
  473. test_mg_get_var();
  474. test_set_throttle();
  475. test_next_option();
  476. test_mg_stat();
  477. test_skip_quoted();
  478. test_mg_upload();
  479. test_request_replies();
  480. #ifdef USE_LUA
  481. test_lua();
  482. #endif
  483. printf("%s\n", "PASSED");
  484. return 0;
  485. }