unit_test.c 40 KB

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