unit_test.c 41 KB

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