unit_test.c 41 KB

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