unit_test.c 38 KB

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