unit_test.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 void *event_handler(enum mg_event event, struct mg_connection *conn) {
  168. const struct mg_request_info *request_info = mg_get_request_info(conn);
  169. if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/data")) {
  170. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  171. "Content-Length: %d\r\n"
  172. "Content-Type: text/plain\r\n\r\n"
  173. "%s", (int) strlen(fetch_data), fetch_data);
  174. return "";
  175. } else if (event == MG_NEW_REQUEST && !strcmp(request_info->uri, "/upload")) {
  176. ASSERT(mg_upload(conn, ".") == 1);
  177. } else if (event == MG_OPEN_FILE) {
  178. const char *path = request_info->ev_data;
  179. if (strcmp(path, "./blah") == 0) {
  180. mg_get_request_info(conn)->ev_data =
  181. (void *) (long) strlen(inmemory_file_data);
  182. return (void *) inmemory_file_data;
  183. }
  184. } else if (event == MG_EVENT_LOG) {
  185. } else if (event == MG_UPLOAD) {
  186. char *p1, *p2;
  187. int len1, len2;
  188. ASSERT(!strcmp((char *) request_info->ev_data, "./upload_test.txt"));
  189. ASSERT((p1 = read_file("mongoose.c", &len1)) != NULL);
  190. ASSERT((p2 = read_file(upload_filename, &len2)) != NULL);
  191. ASSERT(len1 == len2);
  192. ASSERT(memcmp(p1, p2, len1) == 0);
  193. free(p1), free(p2);
  194. remove(upload_filename);
  195. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s",
  196. (int) strlen(upload_ok_message), upload_ok_message);
  197. }
  198. return NULL;
  199. }
  200. static const char *OPTIONS[] = {
  201. "document_root", ".",
  202. "listening_ports", LISTENING_ADDR,
  203. "ssl_certificate", "build/ssl_cert.pem",
  204. NULL,
  205. };
  206. static char *read_conn(struct mg_connection *conn, int *size) {
  207. char buf[100], *data = NULL;
  208. int len;
  209. *size = 0;
  210. while ((len = mg_read(conn, buf, sizeof(buf))) > 0) {
  211. *size += len;
  212. ASSERT((data = realloc(data, *size)) != NULL);
  213. memcpy(data + *size - len, buf, len);
  214. }
  215. return data;
  216. }
  217. static void test_mg_download(void) {
  218. char *p1, *p2, ebuf[100];
  219. int len1, len2, port = atoi(HTTPS_PORT);
  220. struct mg_connection *conn;
  221. struct mg_context *ctx;
  222. ASSERT((ctx = mg_start(event_handler, NULL, OPTIONS)) != NULL);
  223. ASSERT(mg_download(NULL, port, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
  224. ASSERT(mg_download("localhost", 0, 0, ebuf, sizeof(ebuf), "%s", "") == NULL);
  225. ASSERT(mg_download("localhost", port, 1, ebuf, sizeof(ebuf),
  226. "%s", "") == NULL);
  227. // Fetch nonexistent file, should see 404
  228. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  229. "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL);
  230. ASSERT(strcmp(conn->request_info.uri, "404") == 0);
  231. mg_close_connection(conn);
  232. ASSERT((conn = mg_download("google.com", 443, 1, ebuf, sizeof(ebuf), "%s",
  233. "GET / HTTP/1.0\r\n\r\n")) != NULL);
  234. mg_close_connection(conn);
  235. // Fetch mongoose.c, should succeed
  236. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  237. "GET /mongoose.c HTTP/1.0\r\n\r\n")) != NULL);
  238. ASSERT(!strcmp(conn->request_info.uri, "200"));
  239. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  240. ASSERT((p2 = read_file("mongoose.c", &len2)) != NULL);
  241. ASSERT(len1 == len2);
  242. ASSERT(memcmp(p1, p2, len1) == 0);
  243. free(p1), free(p2);
  244. mg_close_connection(conn);
  245. // Fetch in-memory file, should succeed.
  246. ASSERT((conn = mg_download("localhost", port, 1, ebuf, sizeof(ebuf), "%s",
  247. "GET /blah HTTP/1.1\r\n\r\n")) != NULL);
  248. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  249. ASSERT(len1 == (int) strlen(inmemory_file_data));
  250. ASSERT(memcmp(p1, inmemory_file_data, len1) == 0);
  251. free(p1);
  252. mg_close_connection(conn);
  253. // Test SSL redirect, IP address
  254. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  255. ebuf, sizeof(ebuf), "%s",
  256. "GET /foo HTTP/1.1\r\n\r\n")) != NULL);
  257. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  258. ASSERT(strcmp(mg_get_header(conn, "Location"),
  259. "https://127.0.0.1:" HTTPS_PORT "/foo") == 0);
  260. mg_close_connection(conn);
  261. // Test SSL redirect, Host:
  262. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  263. ebuf, sizeof(ebuf), "%s",
  264. "GET /foo HTTP/1.1\r\nHost: a.b:77\n\n")) != NULL);
  265. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  266. ASSERT(strcmp(mg_get_header(conn, "Location"),
  267. "https://a.b:" HTTPS_PORT "/foo") == 0);
  268. mg_close_connection(conn);
  269. mg_stop(ctx);
  270. }
  271. static int alloc_printf(char **buf, size_t size, char *fmt, ...) {
  272. va_list ap;
  273. va_start(ap, fmt);
  274. return alloc_vprintf(buf, size, fmt, ap);
  275. }
  276. static void test_mg_upload(void) {
  277. static const char *boundary = "OOO___MY_BOUNDARY___OOO";
  278. struct mg_context *ctx;
  279. struct mg_connection *conn;
  280. char ebuf[100], buf[20], *file_data, *post_data = NULL;
  281. int file_len, post_data_len;
  282. ASSERT((ctx = mg_start(event_handler, NULL, OPTIONS)) != NULL);
  283. ASSERT((file_data = read_file("mongoose.c", &file_len)) != NULL);
  284. post_data_len = alloc_printf(&post_data, 0,
  285. "--%s\r\n"
  286. "Content-Disposition: form-data; "
  287. "name=\"file\"; "
  288. "filename=\"%s\"\r\n\r\n"
  289. "%.*s\r\n"
  290. "--%s\r\n",
  291. boundary, upload_filename,
  292. file_len, file_data, boundary);
  293. ASSERT(post_data_len > 0);
  294. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  295. ebuf, sizeof(ebuf),
  296. "POST /upload HTTP/1.1\r\n"
  297. "Content-Length: %d\r\n"
  298. "Content-Type: multipart/form-data; "
  299. "boundary=%s\r\n\r\n"
  300. "%.*s", post_data_len, boundary,
  301. post_data_len, post_data)) != NULL);
  302. free(file_data), free(post_data);
  303. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  304. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  305. mg_close_connection(conn);
  306. mg_stop(ctx);
  307. }
  308. static void test_base64_encode(void) {
  309. const char *in[] = {"a", "ab", "abc", "abcd", NULL};
  310. const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
  311. char buf[100];
  312. int i;
  313. for (i = 0; in[i] != NULL; i++) {
  314. base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
  315. ASSERT(!strcmp(buf, out[i]));
  316. }
  317. }
  318. static void test_mg_get_var(void) {
  319. static const char *post[] = {
  320. "a=1&&b=2&d&=&c=3%20&e=",
  321. "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=",
  322. NULL
  323. };
  324. char buf[20];
  325. ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
  326. ASSERT(buf[0] == '1' && buf[1] == '\0');
  327. ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
  328. ASSERT(buf[0] == '2' && buf[1] == '\0');
  329. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
  330. ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
  331. ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
  332. ASSERT(buf[0] == '\0');
  333. ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
  334. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2);
  335. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
  336. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
  337. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2);
  338. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);
  339. }
  340. static void test_set_throttle(void) {
  341. ASSERT(set_throttle(NULL, 0x0a000001, "/") == 0);
  342. ASSERT(set_throttle("10.0.0.0/8=20", 0x0a000001, "/") == 20);
  343. ASSERT(set_throttle("10.0.0.0/8=0.5k", 0x0a000001, "/") == 512);
  344. ASSERT(set_throttle("10.0.0.0/8=17m", 0x0a000001, "/") == 1048576 * 17);
  345. ASSERT(set_throttle("10.0.0.0/8=1x", 0x0a000001, "/") == 0);
  346. ASSERT(set_throttle("10.0.0.0/8=5,0.0.0.0/0=10", 0x0a000001, "/") == 10);
  347. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/index") == 5);
  348. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/foo/x") == 7);
  349. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0b000001, "/foxo/x") == 0);
  350. ASSERT(set_throttle("10.0.0.0/8=5,*=1", 0x0b000001, "/foxo/x") == 1);
  351. }
  352. static void test_next_option(void) {
  353. const char *p, *list = "x/8,/y**=1;2k,z";
  354. struct vec a, b;
  355. int i;
  356. ASSERT(next_option(NULL, &a, &b) == NULL);
  357. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  358. ASSERT(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  359. ASSERT(i != 1 || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9 &&
  360. b.len == 4));
  361. ASSERT(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  362. }
  363. }
  364. #ifdef USE_LUA
  365. static void check_lua_expr(lua_State *L, const char *expr, const char *value) {
  366. const char *v, *var_name = "myVar";
  367. char buf[100];
  368. snprintf(buf, sizeof(buf), "%s = %s", var_name, expr);
  369. (void) luaL_dostring(L, buf);
  370. lua_getglobal(L, var_name);
  371. v = lua_tostring(L, -1);
  372. ASSERT((value == NULL && v == NULL) ||
  373. (value != NULL && v != NULL && !strcmp(value, v)));
  374. }
  375. static void test_lua(void) {
  376. static struct mg_connection conn;
  377. static struct mg_context ctx;
  378. char http_request[] = "POST /foo/bar HTTP/1.1\r\n"
  379. "Content-Length: 12\r\n"
  380. "Connection: close\r\n\r\nhello world!";
  381. lua_State *L = luaL_newstate();
  382. conn.ctx = &ctx;
  383. conn.buf = http_request;
  384. conn.buf_size = conn.data_len = strlen(http_request);
  385. conn.request_len = parse_http_message(conn.buf, conn.data_len,
  386. &conn.request_info);
  387. conn.content_len = conn.data_len - conn.request_len;
  388. prepare_lua_environment(&conn, L);
  389. ASSERT(lua_gettop(L) == 0);
  390. check_lua_expr(L, "'hi'", "hi");
  391. check_lua_expr(L, "request_info.request_method", "POST");
  392. check_lua_expr(L, "request_info.uri", "/foo/bar");
  393. check_lua_expr(L, "request_info.num_headers", "2");
  394. check_lua_expr(L, "request_info.remote_ip", "0");
  395. check_lua_expr(L, "request_info.http_headers['Content-Length']", "12");
  396. check_lua_expr(L, "request_info.http_headers['Connection']", "close");
  397. (void) luaL_dostring(L, "post = read()");
  398. check_lua_expr(L, "# post", "12");
  399. check_lua_expr(L, "post", "hello world!");
  400. lua_close(L);
  401. }
  402. #endif
  403. static void *user_data_tester(enum mg_event event, struct mg_connection *conn) {
  404. struct mg_request_info *ri = mg_get_request_info(conn);
  405. ASSERT(ri->user_data == (void *) 123);
  406. ASSERT(event == MG_NEW_REQUEST || event == MG_INIT_SSL);
  407. return NULL;
  408. }
  409. static void test_user_data(void) {
  410. struct mg_context *ctx;
  411. ASSERT((ctx = mg_start(user_data_tester, (void *) 123, OPTIONS)) != NULL);
  412. ASSERT(ctx->user_data == (void *) 123);
  413. call_user(fc(ctx), MG_NEW_REQUEST);
  414. mg_stop(ctx);
  415. }
  416. static void test_mg_stat(void) {
  417. static struct mg_context ctx;
  418. struct file file = STRUCT_FILE_INITIALIZER;
  419. ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file));
  420. }
  421. static void test_skip_quoted(void) {
  422. char x[] = "a=1, b=2 c='hi \' there'", *s = x, *p;
  423. p = skip_quoted(&s, ", ", ", ", 0);
  424. ASSERT(p != NULL && !strcmp(p, "a=1"));
  425. p = skip_quoted(&s, ", ", ", ", 0);
  426. ASSERT(p != NULL && !strcmp(p, "b=2"));
  427. // TODO(lsm): fix this
  428. #if 0
  429. p = skip_quoted(&s, "'", ", ", '\\');
  430. p = skip_quoted(&s, "'", ", ", '\\');
  431. printf("[%s]\n", p);
  432. ASSERT(p != NULL && !strcmp(p, "hi ' there"));
  433. #endif
  434. }
  435. static void test_alloc_vprintf(void) {
  436. char buf[MG_BUF_LEN], *p = buf;
  437. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  438. ASSERT(p == buf);
  439. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  440. ASSERT(alloc_printf(&p, sizeof(buf), "") == 0);
  441. // Pass small buffer, make sure alloc_printf allocates
  442. ASSERT(alloc_printf(&p, 1, "%s", "hello") == 5);
  443. ASSERT(p != buf);
  444. free(p);
  445. }
  446. int __cdecl main(void) {
  447. test_alloc_vprintf();
  448. test_base64_encode();
  449. test_match_prefix();
  450. test_remove_double_dots();
  451. test_should_keep_alive();
  452. test_parse_http_message();
  453. test_mg_download();
  454. test_mg_get_var();
  455. test_set_throttle();
  456. test_next_option();
  457. test_user_data();
  458. test_mg_stat();
  459. test_skip_quoted();
  460. test_mg_upload();
  461. #ifdef USE_LUA
  462. test_lua();
  463. #endif
  464. printf("%s\n", "PASSED");
  465. return 0;
  466. }