unit_test.c 36 KB

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