unit_test.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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 const char *fetch_data = "hello world!\n";
  189. static const char *inmemory_file_data = "hi there";
  190. static const char *upload_filename = "upload_test.txt";
  191. static const char *upload_filename2 = "upload_test2.txt";
  192. static const char *upload_ok_message = "upload successful";
  193. static const char *open_file_cb(const struct mg_connection *conn,
  194. const char *path, size_t *size) {
  195. (void) conn;
  196. if (!strcmp(path, "./blah")) {
  197. *size = strlen(inmemory_file_data);
  198. return inmemory_file_data;
  199. }
  200. return NULL;
  201. }
  202. static void upload_cb(struct mg_connection *conn, const char *path) {
  203. const struct mg_request_info *ri = mg_get_request_info(conn);
  204. char *p1, *p2;
  205. int len1, len2;
  206. if (atoi(ri->query_string) == 1) {
  207. ASSERT(!strcmp(path, "./upload_test.txt"));
  208. ASSERT((p1 = read_file("src/civetweb.c", &len1)) != NULL);
  209. ASSERT((p2 = read_file(path, &len2)) != NULL);
  210. ASSERT(len1 == len2);
  211. ASSERT(memcmp(p1, p2, len1) == 0);
  212. mg_free(p1), mg_free(p2);
  213. remove(upload_filename);
  214. } else if (atoi(ri->query_string) == 2) {
  215. if (!strcmp(path, "./upload_test.txt")) {
  216. ASSERT((p1 = read_file("include/civetweb.h", &len1)) != NULL);
  217. ASSERT((p2 = read_file(path, &len2)) != NULL);
  218. ASSERT(len1 == len2);
  219. ASSERT(memcmp(p1, p2, len1) == 0);
  220. mg_free(p1), mg_free(p2);
  221. remove(upload_filename);
  222. } else if (!strcmp(path, "./upload_test2.txt")) {
  223. ASSERT((p1 = read_file("README.md", &len1)) != NULL);
  224. ASSERT((p2 = read_file(path, &len2)) != NULL);
  225. ASSERT(len1 == len2);
  226. ASSERT(memcmp(p1, p2, len1) == 0);
  227. mg_free(p1), mg_free(p2);
  228. remove(upload_filename);
  229. } else {
  230. ASSERT(0);
  231. }
  232. } else {
  233. ASSERT(0);
  234. }
  235. mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s",
  236. (int) strlen(upload_ok_message), upload_ok_message);
  237. }
  238. static int begin_request_handler_cb(struct mg_connection *conn) {
  239. const struct mg_request_info *ri = mg_get_request_info(conn);
  240. int req_len = (int)(ri->content_length);
  241. const char * s_req_len = mg_get_header(conn, "Content-Length");
  242. char *data;
  243. int bytes_read;
  244. ASSERT( ((req_len == -1) && (s_req_len==NULL)) || ((s_req_len != NULL) && (req_len = atol(s_req_len))) );
  245. if (!strcmp(ri->uri, "/data")) {
  246. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  247. "Connection: close\r\n"
  248. "Content-Length: %d\r\n"
  249. "Content-Type: text/plain\r\n\r\n"
  250. "%s", strlen(fetch_data), fetch_data);
  251. close_connection(conn);
  252. return 1;
  253. }
  254. if (!strcmp(ri->uri, "/content_length")) {
  255. if (req_len>0) {
  256. data = mg_malloc(req_len);
  257. assert(data != NULL);
  258. bytes_read = mg_read(conn, data, req_len);
  259. ASSERT(bytes_read == req_len);
  260. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  261. "Connection: close\r\n"
  262. "Content-Length: %d\r\n" /* The official definition */
  263. "Content-Type: text/plain\r\n\r\n",
  264. bytes_read);
  265. mg_write(conn, data, bytes_read);
  266. mg_free(data);
  267. } else {
  268. data = mg_malloc(1024);
  269. assert(data != NULL);
  270. bytes_read = mg_read(conn, data, 1024);
  271. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  272. "Connection: close\r\n"
  273. "Content-Type: text/plain\r\n\r\n"
  274. );
  275. mg_write(conn, data, bytes_read);
  276. mg_free(data);
  277. }
  278. close_connection(conn);
  279. return 1;
  280. }
  281. if (!strcmp(ri->uri, "/upload")) {
  282. ASSERT(ri->query_string != NULL);
  283. ASSERT(mg_upload(conn, ".") == atoi(ri->query_string));
  284. }
  285. return 0;
  286. }
  287. static int log_message_cb(const struct mg_connection *conn, const char *msg) {
  288. (void) conn;
  289. printf("%s\n", msg);
  290. return 0;
  291. }
  292. int (*begin_request)(struct mg_connection *);
  293. void (*end_request)(const struct mg_connection *, int reply_status_code);
  294. int (*log_message)(const struct mg_connection *, const char *message);
  295. int (*init_ssl)(void *ssl_context, void *user_data);
  296. int (*websocket_connect)(const struct mg_connection *);
  297. void (*websocket_ready)(struct mg_connection *);
  298. int (*websocket_data)(struct mg_connection *, int bits,
  299. char *data, size_t data_len);
  300. void (*connection_close)(struct mg_connection *);
  301. const char * (*open_file)(const struct mg_connection *,
  302. const char *path, size_t *data_len);
  303. void (*init_lua)(struct mg_connection *, void *lua_context);
  304. void (*upload)(struct mg_connection *, const char *file_name);
  305. int (*http_error)(struct mg_connection *, int status);
  306. static const struct mg_callbacks CALLBACKS = {
  307. &begin_request_handler_cb, NULL, &log_message_cb, NULL, NULL, NULL, NULL, NULL,
  308. &open_file_cb, NULL, &upload_cb, NULL
  309. };
  310. static const char *OPTIONS[] = {
  311. "document_root", ".",
  312. "listening_ports", LISTENING_ADDR,
  313. "enable_keep_alive", "yes",
  314. #ifndef NO_SSL
  315. "ssl_certificate", "../resources/ssl_cert.pem",
  316. #endif
  317. NULL,
  318. };
  319. static char *read_conn(struct mg_connection *conn, int *size) {
  320. char buf[100], *data = NULL;
  321. int len;
  322. *size = 0;
  323. while ((len = mg_read(conn, buf, sizeof(buf))) > 0) {
  324. *size += len;
  325. ASSERT((data = mg_realloc(data, *size)) != NULL);
  326. memcpy(data + *size - len, buf, len);
  327. }
  328. return data;
  329. }
  330. static void test_mg_download(int use_ssl) {
  331. const char *test_data = "123456789A123456789B";
  332. char *p1, *p2, ebuf[100];
  333. const char *h;
  334. int len1, len2, port;
  335. struct mg_connection *conn;
  336. struct mg_context *ctx;
  337. if (use_ssl) port = atoi(HTTPS_PORT); else port = atoi(HTTP_PORT);
  338. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  339. ASSERT(mg_download(NULL, port, use_ssl, ebuf, sizeof(ebuf), "%s", "") == NULL);
  340. ASSERT(mg_download("localhost", 0, use_ssl, ebuf, sizeof(ebuf), "%s", "") == NULL);
  341. ASSERT(mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "") == NULL);
  342. /* Fetch nonexistent file, should see 404 */
  343. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  344. "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL);
  345. ASSERT(strcmp(conn->request_info.uri, "404") == 0);
  346. mg_close_connection(conn);
  347. if (use_ssl) {
  348. ASSERT((conn = mg_download("google.com", 443, 1, ebuf, sizeof(ebuf), "%s",
  349. "GET / HTTP/1.0\r\n\r\n")) != NULL);
  350. mg_close_connection(conn);
  351. } else {
  352. ASSERT((conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf), "%s",
  353. "GET / HTTP/1.0\r\n\r\n")) != NULL);
  354. mg_close_connection(conn);
  355. }
  356. /* Fetch unit_test.c, should succeed */
  357. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  358. "GET /unit_test.c HTTP/1.0\r\n\r\n")) != NULL);
  359. ASSERT(!strcmp(conn->request_info.uri, "200"));
  360. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  361. ASSERT((p2 = read_file("unit_test.c", &len2)) != NULL);
  362. ASSERT(len1 == len2);
  363. ASSERT(memcmp(p1, p2, len1) == 0);
  364. mg_free(p1), mg_free(p2);
  365. mg_close_connection(conn);
  366. /* Fetch in-memory file, should succeed. */
  367. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  368. "GET /blah HTTP/1.1\r\n\r\n")) != NULL);
  369. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  370. ASSERT(len1 == (int) strlen(inmemory_file_data));
  371. ASSERT(memcmp(p1, inmemory_file_data, len1) == 0);
  372. mg_free(p1);
  373. mg_close_connection(conn);
  374. /* Fetch in-memory data with no Content-Length, should succeed. */
  375. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s",
  376. "GET /data HTTP/1.1\r\n\r\n")) != NULL);
  377. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  378. ASSERT(len1 == (int) strlen(fetch_data));
  379. ASSERT(memcmp(p1, fetch_data, len1) == 0);
  380. mg_free(p1);
  381. mg_close_connection(conn);
  382. /* Fetch data with Content-Length, should succeed and return the defined length. */
  383. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf),
  384. "POST /content_length HTTP/1.1\r\nContent-Length: %u\r\n\r\n%s",
  385. strlen(test_data), test_data)) != NULL);
  386. h = mg_get_header(conn, "Content-Length");
  387. ASSERT((h != NULL) && (atoi(h)==strlen(test_data)));
  388. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  389. ASSERT(len1 == (int) strlen(test_data));
  390. ASSERT(memcmp(p1, test_data, len1) == 0);
  391. mg_free(p1);
  392. mg_close_connection(conn);
  393. /* Fetch data without Content-Length, should succeed. It has no content-length header field,
  394. but still returns the correct amount of data. It is much slower, since it needs to wait
  395. for the connection shutdown. */
  396. ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf),
  397. "POST /content_length HTTP/1.1\r\n\r\n%s", test_data)) != NULL);
  398. h = mg_get_header(conn, "Content-Length");
  399. ASSERT(h == NULL);
  400. ASSERT((p1 = read_conn(conn, &len1)) != NULL);
  401. ASSERT(len1 == (int) strlen(test_data));
  402. ASSERT(memcmp(p1, test_data, len1) == 0);
  403. mg_free(p1);
  404. mg_close_connection(conn);
  405. /* Test SSL redirect, IP address */
  406. /* TODO(bel):
  407. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  408. ebuf, sizeof(ebuf), "%s",
  409. "GET /foo HTTP/1.1\r\n\r\n")) != NULL);
  410. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  411. ASSERT(strcmp(mg_get_header(conn, "Location"),
  412. "https://127.0.0.1:" HTTPS_PORT "/foo") == 0);
  413. mg_close_connection(conn);
  414. */
  415. /* Test SSL redirect, Host: */
  416. /* TODO(bel):
  417. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  418. ebuf, sizeof(ebuf), "%s",
  419. "GET /foo HTTP/1.1\r\nHost: a.b:77\n\n")) != NULL);
  420. ASSERT(strcmp(conn->request_info.uri, "302") == 0);
  421. ASSERT(strcmp(mg_get_header(conn, "Location"),
  422. "https://a.b:" HTTPS_PORT "/foo") == 0);
  423. mg_close_connection(conn);
  424. */
  425. mg_stop(ctx);
  426. }
  427. static int alloc_printf(char **buf, size_t size, char *fmt, ...) {
  428. va_list ap;
  429. va_start(ap, fmt);
  430. return alloc_vprintf(buf, size, fmt, ap);
  431. }
  432. static void test_mg_upload(void) {
  433. static const char *boundary = "OOO___MY_BOUNDARY___OOO";
  434. struct mg_context *ctx;
  435. struct mg_connection *conn;
  436. char ebuf[100], buf[20], *file_data, *file2_data, *post_data;
  437. int file_len, file2_len, post_data_len;
  438. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  439. /* Upload one file */
  440. ASSERT((file_data = read_file("unit_test.c", &file_len)) != NULL);
  441. post_data = NULL;
  442. post_data_len = alloc_printf(&post_data, 0,
  443. "--%s\r\n"
  444. "Content-Disposition: form-data; "
  445. "name=\"file\"; "
  446. "filename=\"%s\"\r\n\r\n"
  447. "%.*s\r\n"
  448. "--%s--\r\n",
  449. boundary, upload_filename,
  450. file_len, file_data, boundary);
  451. ASSERT(post_data_len > 0);
  452. #if 0 /* TODO (bel): ... */
  453. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  454. ebuf, sizeof(ebuf),
  455. "POST /upload?1 HTTP/1.1\r\n"
  456. "Content-Length: %d\r\n"
  457. "Content-Type: multipart/form-data; "
  458. "boundary=%s\r\n\r\n"
  459. "%.*s", post_data_len, boundary,
  460. post_data_len, post_data)) != NULL);
  461. mg_free(file_data), mg_free(post_data);
  462. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  463. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  464. mg_close_connection(conn);
  465. /* Upload two files */
  466. ASSERT((file_data = read_file("include/civetweb.h", &file_len)) != NULL);
  467. ASSERT((file2_data = read_file("README.md", &file2_len)) != NULL);
  468. post_data = NULL;
  469. post_data_len = alloc_printf(&post_data, 0,
  470. /* First file */
  471. "--%s\r\n"
  472. "Content-Disposition: form-data; "
  473. "name=\"file\"; "
  474. "filename=\"%s\"\r\n\r\n"
  475. "%.*s\r\n"
  476. /* Second file */
  477. "--%s\r\n"
  478. "Content-Disposition: form-data; "
  479. "name=\"file\"; "
  480. "filename=\"%s\"\r\n\r\n"
  481. "%.*s\r\n"
  482. /* Final boundary */
  483. "--%s--\r\n",
  484. boundary, upload_filename,
  485. file_len, file_data,
  486. boundary, upload_filename2,
  487. file2_len, file2_data,
  488. boundary);
  489. ASSERT(post_data_len > 0);
  490. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1,
  491. ebuf, sizeof(ebuf),
  492. "POST /upload?2 HTTP/1.1\r\n"
  493. "Content-Length: %d\r\n"
  494. "Content-Type: multipart/form-data; "
  495. "boundary=%s\r\n\r\n"
  496. "%.*s", post_data_len, boundary,
  497. post_data_len, post_data)) != NULL);
  498. mg_free(file_data), mg_free(file2_data), mg_free(post_data);
  499. ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message));
  500. ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0);
  501. mg_close_connection(conn);
  502. #endif
  503. mg_stop(ctx);
  504. }
  505. static void test_base64_encode(void) {
  506. const char *in[] = {"a", "ab", "abc", "abcd", NULL};
  507. const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
  508. char buf[100];
  509. int i;
  510. for (i = 0; in[i] != NULL; i++) {
  511. base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
  512. ASSERT(!strcmp(buf, out[i]));
  513. }
  514. }
  515. static void test_mg_get_var(void) {
  516. static const char *post[] = {
  517. "a=1&&b=2&d&=&c=3%20&e=",
  518. "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=",
  519. NULL
  520. };
  521. char buf[20];
  522. ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
  523. ASSERT(buf[0] == '1' && buf[1] == '\0');
  524. ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
  525. ASSERT(buf[0] == '2' && buf[1] == '\0');
  526. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
  527. ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
  528. ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
  529. ASSERT(buf[0] == '\0');
  530. ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
  531. ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2);
  532. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
  533. ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
  534. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2);
  535. ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);
  536. }
  537. static void test_set_throttle(void) {
  538. ASSERT(set_throttle(NULL, 0x0a000001, "/") == 0);
  539. ASSERT(set_throttle("10.0.0.0/8=20", 0x0a000001, "/") == 20);
  540. ASSERT(set_throttle("10.0.0.0/8=0.5k", 0x0a000001, "/") == 512);
  541. ASSERT(set_throttle("10.0.0.0/8=17m", 0x0a000001, "/") == 1048576 * 17);
  542. ASSERT(set_throttle("10.0.0.0/8=1x", 0x0a000001, "/") == 0);
  543. ASSERT(set_throttle("10.0.0.0/8=5,0.0.0.0/0=10", 0x0a000001, "/") == 10);
  544. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/index") == 5);
  545. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0a000001, "/foo/x") == 7);
  546. ASSERT(set_throttle("10.0.0.0/8=5,/foo/**=7", 0x0b000001, "/foxo/x") == 0);
  547. ASSERT(set_throttle("10.0.0.0/8=5,*=1", 0x0b000001, "/foxo/x") == 1);
  548. }
  549. static void test_next_option(void) {
  550. const char *p, *list = "x/8,/y**=1;2k,z";
  551. struct vec a, b;
  552. int i;
  553. ASSERT(next_option(NULL, &a, &b) == NULL);
  554. for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
  555. ASSERT(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
  556. ASSERT(i != 1 || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9 && b.len == 4));
  557. ASSERT(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
  558. }
  559. }
  560. #if defined(USE_LUA)
  561. static void check_lua_expr(lua_State *L, const char *expr, const char *value) {
  562. const char *v, *var_name = "myVar";
  563. char buf[100];
  564. snprintf(buf, sizeof(buf), "%s = %s", var_name, expr);
  565. (void) luaL_dostring(L, buf);
  566. lua_getglobal(L, var_name);
  567. v = lua_tostring(L, -1);
  568. ASSERT((value == NULL && v == NULL) || (value != NULL && v != NULL && !strcmp(value, v)));
  569. }
  570. static void test_lua(void) {
  571. static struct mg_connection conn;
  572. static struct mg_context ctx;
  573. char http_request[] = "POST /foo/bar HTTP/1.1\r\n"
  574. "Content-Length: 12\r\n"
  575. "Connection: close\r\n\r\nhello world!";
  576. lua_State *L = luaL_newstate();
  577. conn.ctx = &ctx;
  578. conn.buf = http_request;
  579. conn.buf_size = conn.data_len = strlen(http_request);
  580. conn.request_len = parse_http_message(conn.buf, conn.data_len, &conn.request_info);
  581. conn.content_len = conn.data_len - conn.request_len;
  582. prepare_lua_environment(&conn, L, "unit_test", LUA_ENV_TYPE_PLAIN_LUA_PAGE);
  583. ASSERT(lua_gettop(L) == 4);
  584. check_lua_expr(L, "'hi'", "hi");
  585. check_lua_expr(L, "mg.request_info.request_method", "POST");
  586. check_lua_expr(L, "mg.request_info.uri", "/foo/bar");
  587. check_lua_expr(L, "mg.request_info.num_headers", "2");
  588. check_lua_expr(L, "mg.request_info.remote_ip", "0");
  589. check_lua_expr(L, "mg.request_info.http_headers['Content-Length']", "12");
  590. check_lua_expr(L, "mg.request_info.http_headers['Connection']", "close");
  591. (void) luaL_dostring(L, "post = mg.read()");
  592. check_lua_expr(L, "# post", "12");
  593. check_lua_expr(L, "post", "hello world!");
  594. lua_close(L);
  595. }
  596. #endif
  597. static void test_mg_stat(void) {
  598. static struct mg_context ctx;
  599. struct file file = STRUCT_FILE_INITIALIZER;
  600. ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file));
  601. }
  602. static void test_skip_quoted(void) {
  603. char x[] = "a=1, b=2 c='hi \' there'", *s = x, *p;
  604. p = skip_quoted(&s, ", ", ", ", 0);
  605. ASSERT(p != NULL && !strcmp(p, "a=1"));
  606. p = skip_quoted(&s, ", ", ", ", 0);
  607. ASSERT(p != NULL && !strcmp(p, "b=2"));
  608. /* TODO(lsm): fix this */
  609. #if 0
  610. p = skip_quoted(&s, "'", ", ", '\\');
  611. p = skip_quoted(&s, "'", ", ", '\\');
  612. printf("[%s]\n", p);
  613. ASSERT(p != NULL && !strcmp(p, "hi ' there"));
  614. #endif
  615. }
  616. static void test_alloc_vprintf(void) {
  617. char buf[MG_BUF_LEN], *p = buf;
  618. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "hi") == 2);
  619. ASSERT(p == buf);
  620. ASSERT(alloc_printf(&p, sizeof(buf), "%s", "") == 0);
  621. ASSERT(alloc_printf(&p, sizeof(buf), "") == 0);
  622. /* Pass small buffer, make sure alloc_printf allocates */
  623. ASSERT(alloc_printf(&p, 1, "%s", "hello") == 5);
  624. ASSERT(p != buf);
  625. mg_free(p);
  626. }
  627. static void test_request_replies(void) {
  628. char ebuf[100];
  629. int i;
  630. struct mg_connection *conn;
  631. struct mg_context *ctx;
  632. static struct { const char *request, *reply_regex; } tests[] = {
  633. {
  634. "GET hello.txt HTTP/1.0\r\nRange: bytes=3-5\r\n\r\n",
  635. "^HTTP/1.1 206 Partial Content"
  636. },
  637. {NULL, NULL},
  638. };
  639. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  640. for (i = 0; tests[i].request != NULL; i++) {
  641. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s",
  642. tests[i].request)) != NULL);
  643. mg_close_connection(conn);
  644. }
  645. mg_stop(ctx);
  646. /* TODO(bel):
  647. ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL);
  648. for (i = 0; tests[i].request != NULL; i++) {
  649. ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1, ebuf, sizeof(ebuf), "%s",
  650. tests[i].request)) != NULL);
  651. mg_close_connection(conn);
  652. }
  653. mg_stop(ctx);
  654. */
  655. }
  656. static int api_callback(struct mg_connection *conn) {
  657. struct mg_request_info *ri = mg_get_request_info(conn);
  658. char post_data[100] = "";
  659. ASSERT(ri->user_data == (void *) 123);
  660. ASSERT(ri->num_headers == 2);
  661. ASSERT(strcmp(mg_get_header(conn, "host"), "blah.com") == 0);
  662. ASSERT(mg_read(conn, post_data, sizeof(post_data)) == 3);
  663. ASSERT(memcmp(post_data, "b=1", 3) == 0);
  664. ASSERT(ri->query_string != NULL);
  665. ASSERT(ri->remote_ip > 0);
  666. ASSERT(ri->remote_port > 0);
  667. ASSERT(strcmp(ri->http_version, "1.0") == 0);
  668. mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\n");
  669. return 1;
  670. }
  671. static void test_api_calls(void) {
  672. char ebuf[100];
  673. struct mg_callbacks callbacks;
  674. struct mg_connection *conn;
  675. struct mg_context *ctx;
  676. static const char *request = "POST /?a=%20&b=&c=xx HTTP/1.0\r\n"
  677. "Host: blah.com\n" /* More spaces before */
  678. "content-length: 3\r\n" /* Lower case header name */
  679. "\r\nb=123456"; /* Content size > content-length, test for mg_read() */
  680. memset(&callbacks, 0, sizeof(callbacks));
  681. callbacks.begin_request = api_callback;
  682. ASSERT((ctx = mg_start(&callbacks, (void *) 123, OPTIONS)) != NULL);
  683. ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0,
  684. ebuf, sizeof(ebuf), "%s", request)) != NULL);
  685. mg_close_connection(conn);
  686. mg_stop(ctx);
  687. }
  688. static void test_url_decode(void) {
  689. char buf[100];
  690. ASSERT(mg_url_decode("foo", 3, buf, 3, 0) == -1); /* No space for \0 */
  691. ASSERT(mg_url_decode("foo", 3, buf, 4, 0) == 3);
  692. ASSERT(strcmp(buf, "foo") == 0);
  693. ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 0) == 2);
  694. ASSERT(strcmp(buf, "a+") == 0);
  695. ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 1) == 2);
  696. ASSERT(strcmp(buf, "a ") == 0);
  697. ASSERT(mg_url_decode("%61", 1, buf, sizeof(buf), 1) == 1);
  698. ASSERT(strcmp(buf, "%") == 0);
  699. ASSERT(mg_url_decode("%61", 2, buf, sizeof(buf), 1) == 2);
  700. ASSERT(strcmp(buf, "%6") == 0);
  701. ASSERT(mg_url_decode("%61", 3, buf, sizeof(buf), 1) == 1);
  702. ASSERT(strcmp(buf, "a") == 0);
  703. }
  704. static void test_mg_strcasestr(void) {
  705. static const char *big1 = "abcdef";
  706. ASSERT(mg_strcasestr("Y", "X") == NULL);
  707. ASSERT(mg_strcasestr("Y", "y") != NULL);
  708. ASSERT(mg_strcasestr(big1, "X") == NULL);
  709. ASSERT(mg_strcasestr(big1, "CD") == big1 + 2);
  710. ASSERT(mg_strcasestr("aa", "AAB") == NULL);
  711. }
  712. static void test_mg_get_cookie(void) {
  713. char buf[20];
  714. ASSERT(mg_get_cookie("", "foo", NULL, sizeof(buf)) == -2);
  715. ASSERT(mg_get_cookie("", "foo", buf, 0) == -2);
  716. ASSERT(mg_get_cookie("", "foo", buf, sizeof(buf)) == -1);
  717. ASSERT(mg_get_cookie("", NULL, buf, sizeof(buf)) == -1);
  718. ASSERT(mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)) == 1);
  719. ASSERT(strcmp(buf, "1") == 0);
  720. ASSERT(mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)) == 1);
  721. ASSERT(strcmp(buf, "2") == 0);
  722. ASSERT(mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)) == 3);
  723. ASSERT(strcmp(buf, "123") == 0);
  724. ASSERT(mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)) == -1);
  725. }
  726. static void test_strtoll(void) {
  727. ASSERT(strtoll("0", NULL, 10) == 0);
  728. ASSERT(strtoll("123", NULL, 10) == 123);
  729. ASSERT(strtoll("-34", NULL, 10) == -34);
  730. ASSERT(strtoll("3566626116", NULL, 10) == 3566626116);
  731. }
  732. static void test_parse_port_string(void) {
  733. static const char *valid[] = {
  734. "0", "1", "1s", "1r", "1.2.3.4:1", "1.2.3.4:1s", "1.2.3.4:1r",
  735. #if defined(USE_IPV6)
  736. "[::1]:123", "[3ffe:2a00:100:7031::1]:900",
  737. #endif
  738. NULL
  739. };
  740. static const char *invalid[] = {
  741. "99999", "1k", "1.2.3", "1.2.3.4:", "1.2.3.4:2p",
  742. NULL
  743. };
  744. struct socket so;
  745. struct vec vec;
  746. int i;
  747. for (i = 0; valid[i] != NULL; i++) {
  748. vec.ptr = valid[i];
  749. vec.len = strlen(vec.ptr);
  750. ASSERT(parse_port_string(&vec, &so) != 0);
  751. }
  752. for (i = 0; invalid[i] != NULL; i++) {
  753. vec.ptr = invalid[i];
  754. vec.len = strlen(vec.ptr);
  755. ASSERT(parse_port_string(&vec, &so) == 0);
  756. }
  757. }
  758. static void test_md5(void) {
  759. md5_state_t md5_state;
  760. unsigned char md5_val[16+1];
  761. char md5_str[32+1];
  762. const char *test_str = "The quick brown fox jumps over the lazy dog";
  763. md5_val[16]=0;
  764. md5_init(&md5_state);
  765. md5_finish(&md5_state, md5_val);
  766. ASSERT(strcmp(md5_val, "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e")==0);
  767. sprintf(md5_str, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  768. md5_val[0], md5_val[1], md5_val[2], md5_val[3],
  769. md5_val[4], md5_val[5], md5_val[6], md5_val[7],
  770. md5_val[8], md5_val[9], md5_val[10], md5_val[11],
  771. md5_val[12], md5_val[13], md5_val[14], md5_val[15]);
  772. ASSERT(strcmp(md5_str, "d41d8cd98f00b204e9800998ecf8427e")==0);
  773. mg_md5(md5_str, "", NULL);
  774. ASSERT(strcmp(md5_str, "d41d8cd98f00b204e9800998ecf8427e")==0);
  775. md5_init(&md5_state);
  776. md5_append(&md5_state, test_str, strlen(test_str));
  777. md5_finish(&md5_state, md5_val);
  778. sprintf(md5_str, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  779. md5_val[0], md5_val[1], md5_val[2], md5_val[3],
  780. md5_val[4], md5_val[5], md5_val[6], md5_val[7],
  781. md5_val[8], md5_val[9], md5_val[10], md5_val[11],
  782. md5_val[12], md5_val[13], md5_val[14], md5_val[15]);
  783. ASSERT(strcmp(md5_str, "9e107d9d372bb6826bd81d3542a419d6")==0);
  784. mg_md5(md5_str, test_str, NULL);
  785. ASSERT(strcmp(md5_str, "9e107d9d372bb6826bd81d3542a419d6")==0);
  786. mg_md5(md5_str, "The", " ", "quick brown fox", "", " jumps ", "over the lazy dog", "", "", NULL);
  787. ASSERT(strcmp(md5_str, "9e107d9d372bb6826bd81d3542a419d6")==0);
  788. mg_md5(md5_str, "civetweb", NULL);
  789. ASSERT(strcmp(md5_str, "95c098bd85b619b24a83d9cea5e8ba54")==0);
  790. }
  791. int __cdecl main(void) {
  792. char buffer[512];
  793. FILE * f;
  794. struct mg_context *ctx;
  795. /* print headline */
  796. printf("Civetweb %s unit test\n", mg_version());
  797. #if defined(_WIN32)
  798. GetCurrentDirectoryA(sizeof(buffer), buffer);
  799. #else
  800. getcwd(buffer, sizeof(buffer));
  801. #endif
  802. printf("Test directory is \"%s\"\n", buffer); /* should be the "test" directory */
  803. f = fopen("hello.txt", "r");
  804. if (f) {
  805. fclose(f);
  806. } else {
  807. printf("Error: Test directory does not contain hello.txt\n");
  808. }
  809. f = fopen("unit_test.c", "r");
  810. if (f) {
  811. fclose(f);
  812. } else {
  813. printf("Error: Test directory does not contain unit_test.c\n");
  814. }
  815. /* test local functions */
  816. test_parse_port_string();
  817. test_mg_strcasestr();
  818. test_alloc_vprintf();
  819. test_base64_encode();
  820. test_match_prefix();
  821. test_remove_double_dots();
  822. test_should_keep_alive();
  823. test_parse_http_message();
  824. test_mg_get_var();
  825. test_set_throttle();
  826. test_next_option();
  827. test_mg_stat();
  828. test_skip_quoted();
  829. test_url_decode();
  830. test_mg_get_cookie();
  831. test_strtoll();
  832. test_md5();
  833. /* start stop server */
  834. ctx = mg_start(NULL, NULL, OPTIONS);
  835. ASSERT(ctx != NULL);
  836. mg_sleep(1000);
  837. mg_stop(ctx);
  838. /* tests with network access */
  839. test_mg_download(0);
  840. #ifndef NO_SSL
  841. test_mg_download(1);
  842. #endif
  843. test_mg_upload();
  844. test_request_replies();
  845. test_api_calls();
  846. #if defined(USE_LUA)
  847. test_lua();
  848. #endif
  849. printf("TOTAL TESTS: %d, FAILED: %d\n", s_total_tests, s_failed_tests);
  850. return s_failed_tests == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
  851. }