unit_test.c 28 KB

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