unit_test.c 41 KB

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