unit_test.c
Go to the documentation of this file.
00001 // Unit test for the mongoose web server.
00002 // g++ -W -Wall -pedantic -g unit_test.c -lssl && ./a.out
00003 // cl unit_test.c /MD
00004 
00005 #ifndef _WIN32
00006 #define NS_ENABLE_IPV6
00007 #define NS_ENABLE_SSL
00008 #endif
00009 #define MONGOOSE_POST_SIZE_LIMIT 999
00010 
00011 // USE_* definitions must be made before #include "mongoose.c" !
00012 #include "../mongoose.c"
00013 
00014 #define FAIL(str, line) do {                    \
00015   printf("Fail on line %d: [%s]\n", line, str); \
00016   return str;                                   \
00017 } while (0)
00018 
00019 #define ASSERT(expr) do {             \
00020   static_num_tests++;               \
00021   if (!(expr)) FAIL(#expr, __LINE__); \
00022 } while (0)
00023 
00024 #define RUN_TEST(test) do { const char *msg = test(); \
00025   if (msg) return msg; } while (0)
00026 
00027 #define HTTP_PORT "45772"
00028 #define LISTENING_ADDR "127.0.0.1:" HTTP_PORT
00029 
00030 static int static_num_tests = 0;
00031 
00032 #if 0
00033 // Connects to host:port, and sends formatted request to it. Returns
00034 // malloc-ed reply and reply length, or NULL on error. Reply contains
00035 // everything including headers, not just the message body.
00036 static char *wget(const char *host, int port, int *len, const char *fmt, ...) {
00037   char buf[2000], *reply = NULL;
00038   int request_len, reply_size = 0, n, sock = -1;
00039   struct sockaddr_in sin;
00040   struct hostent *he = NULL;
00041   va_list ap;
00042 
00043   if (host != NULL &&
00044       (he = gethostbyname(host)) != NULL &&
00045       (sock = socket(PF_INET, SOCK_STREAM, 0)) != -1) {
00046     sin.sin_family = AF_INET;
00047     sin.sin_port = htons((uint16_t) port);
00048     sin.sin_addr = * (struct in_addr *) he->h_addr_list[0];
00049     if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) == 0) {
00050 
00051       // Format and send the request.
00052       va_start(ap, fmt);
00053       request_len = vsnprintf(buf, sizeof(buf), fmt, ap);
00054       va_end(ap);
00055       while (request_len > 0 && (n = send(sock, buf, request_len, 0)) > 0) {
00056         request_len -= n;
00057       }
00058       if (request_len == 0) {
00059         *len = 0;
00060         while ((n = recv(sock, buf, sizeof(buf), 0)) > 0) {
00061           if (*len + n > reply_size) {
00062             // Leak possible
00063             reply = (char *) realloc(reply, reply_size + sizeof(buf));
00064             reply_size += sizeof(buf);
00065           }
00066           if (reply != NULL) {
00067             memcpy(reply + *len, buf, n);
00068             *len += n;
00069           }
00070         }
00071       }
00072       closesocket(sock);
00073     }
00074   }
00075 
00076   return reply;
00077 }
00078 #endif
00079 
00080 static char *read_file(const char *path, int *size) {
00081   FILE *fp;
00082   struct stat st;
00083   char *data = NULL;
00084   if ((fp = fopen(path, "rb")) != NULL && !fstat(fileno(fp), &st)) {
00085     *size = (int) st.st_size;
00086     data = (char *) malloc(*size);
00087     fread(data, 1, *size, fp);
00088     fclose(fp);
00089   }
00090   return data;
00091 }
00092 
00093 static const char *test_parse_http_message() {
00094   struct mg_connection ri;
00095   char req1[] = "GET / HTTP/1.1\r\n\r\n";
00096   char req2[] = "BLAH / HTTP/1.1\r\n\r\n";
00097   char req3[] = "GET / HTTP/1.1\r\nBah\r\n";
00098   char req4[] = "GET / HTTP/1.1\r\nA: foo bar\r\nB: bar\r\nbaz\r\n\r\n";
00099   char req5[] = "GET / HTTP/1.1\r\n\r\n";
00100   char req6[] = "G";
00101   char req7[] = " blah ";
00102   char req8[] = " HTTP/1.1 200 OK \n\n";
00103   char req9[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
00104 
00105   ASSERT(get_request_len("\r\n", 3) == -1);
00106   ASSERT(get_request_len("\r\n", 2) == 0);
00107   ASSERT(get_request_len("GET", 3) == 0);
00108   ASSERT(get_request_len("\n\n", 2) == 2);
00109   ASSERT(get_request_len("\n\r\n", 3) == 3);
00110   ASSERT(get_request_len("\xdd\xdd", 2) == 0);
00111   ASSERT(get_request_len("\xdd\x03", 2) == -1);
00112   ASSERT(get_request_len(req3, sizeof(req3) - 1) == 0);
00113   ASSERT(get_request_len(req6, sizeof(req6) - 1) == 0);
00114   ASSERT(get_request_len(req7, sizeof(req7) - 1) == 0);
00115 
00116   ASSERT(parse_http_message(req9, sizeof(req9) - 1, &ri) == sizeof(req9) - 1);
00117   ASSERT(ri.num_headers == 1);
00118 
00119   ASSERT(parse_http_message(req1, sizeof(req1) - 1, &ri) == sizeof(req1) - 1);
00120   ASSERT(strcmp(ri.http_version, "1.1") == 0);
00121   ASSERT(ri.num_headers == 0);
00122 
00123   ASSERT(parse_http_message(req2, sizeof(req2) - 1, &ri) == (size_t) ~0);
00124   ASSERT(parse_http_message(req6, 0, &ri) == (size_t) ~0);
00125   ASSERT(parse_http_message(req8, sizeof(req8) - 1, &ri) == sizeof(req8) - 1);
00126 
00127   // TODO(lsm): Fix this. Header value may span multiple lines.
00128   ASSERT(parse_http_message(req4, sizeof(req4) - 1, &ri) == sizeof(req4) - 1);
00129   ASSERT(strcmp(ri.http_version, "1.1") == 0);
00130   ASSERT(ri.num_headers == 3);
00131   ASSERT(strcmp(ri.http_headers[0].name, "A") == 0);
00132   ASSERT(strcmp(ri.http_headers[0].value, "foo bar") == 0);
00133   ASSERT(strcmp(ri.http_headers[1].name, "B") == 0);
00134   ASSERT(strcmp(ri.http_headers[1].value, "bar") == 0);
00135   ASSERT(strcmp(ri.http_headers[2].name, "baz\r\n\r") == 0);
00136   ASSERT(strcmp(ri.http_headers[2].value, "") == 0);
00137 
00138   ASSERT(parse_http_message(req5, sizeof(req5) - 1, &ri) == sizeof(req5) - 1);
00139   ASSERT(strcmp(ri.request_method, "GET") == 0);
00140   ASSERT(strcmp(ri.http_version, "1.1") == 0);
00141 
00142   return NULL;
00143 }
00144 
00145 static const char *test_should_keep_alive(void) {
00146   struct mg_connection conn;
00147   char req1[] = "GET / HTTP/1.1\r\n\r\n";
00148   char req2[] = "GET / HTTP/1.0\r\n\r\n";
00149   char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
00150   char req4[] = "GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
00151 
00152   memset(&conn, 0, sizeof(conn));
00153   ASSERT(parse_http_message(req1, sizeof(req1) - 1, &conn) == sizeof(req1) - 1);
00154   ASSERT(should_keep_alive(&conn) != 0);
00155 
00156   parse_http_message(req2, sizeof(req2) - 1, &conn);
00157   ASSERT(should_keep_alive(&conn) == 0);
00158 
00159   parse_http_message(req3, sizeof(req3) - 1, &conn);
00160   ASSERT(should_keep_alive(&conn) == 0);
00161 
00162   parse_http_message(req4, sizeof(req4) - 1, &conn);
00163   ASSERT(should_keep_alive(&conn) != 0);
00164 
00165   return NULL;
00166 }
00167 
00168 static const char *test_match_prefix(void) {
00169   ASSERT(mg_match_prefix("/api", 4, "/api") == 4);
00170   ASSERT(mg_match_prefix("/a/", 3, "/a/b/c") == 3);
00171   ASSERT(mg_match_prefix("/a/", 3, "/ab/c") == -1);
00172   ASSERT(mg_match_prefix("/*/", 3, "/ab/c") == 4);
00173   ASSERT(mg_match_prefix("**", 2, "/a/b/c") == 6);
00174   ASSERT(mg_match_prefix("/*", 2, "/a/b/c") == 2);
00175   ASSERT(mg_match_prefix("*/*", 3, "/a/b/c") == 2);
00176   ASSERT(mg_match_prefix("**/", 3, "/a/b/c") == 5);
00177   ASSERT(mg_match_prefix("**.foo|**.bar", 13, "a.bar") == 5);
00178   ASSERT(mg_match_prefix("a|b|cd", 6, "cdef") == 2);
00179   ASSERT(mg_match_prefix("a|b|c?", 6, "cdef") == 2);
00180   ASSERT(mg_match_prefix("a|?|cd", 6, "cdef") == 1);
00181   ASSERT(mg_match_prefix("/a/**.cgi", 9, "/foo/bar/x.cgi") == -1);
00182   ASSERT(mg_match_prefix("/a/**.cgi", 9, "/a/bar/x.cgi") == 12);
00183   ASSERT(mg_match_prefix("**/", 3, "/a/b/c") == 5);
00184   ASSERT(mg_match_prefix("**/$", 4, "/a/b/c") == -1);
00185   ASSERT(mg_match_prefix("**/$", 4, "/a/b/") == 5);
00186   ASSERT(mg_match_prefix("$", 1, "") == 0);
00187   ASSERT(mg_match_prefix("$", 1, "x") == -1);
00188   ASSERT(mg_match_prefix("*$", 2, "x") == 1);
00189   ASSERT(mg_match_prefix("/$", 2, "/") == 1);
00190   ASSERT(mg_match_prefix("**/$", 4, "/a/b/c") == -1);
00191   ASSERT(mg_match_prefix("**/$", 4, "/a/b/") == 5);
00192   ASSERT(mg_match_prefix("*", 1, "/hello/") == 0);
00193   ASSERT(mg_match_prefix("**.a$|**.b$", 11, "/a/b.b/") == -1);
00194   ASSERT(mg_match_prefix("**.a$|**.b$", 11, "/a/b.b") == 6);
00195   ASSERT(mg_match_prefix("**.a$|**.b$", 11, "/a/B.A") == 6);
00196   ASSERT(mg_match_prefix("**o$", 4, "HELLO") == 5);
00197   return NULL;
00198 }
00199 
00200 static const char *test_remove_double_dots() {
00201   struct { char before[30], after[30]; } data[] = {
00202     {"////a", "/a"},
00203     {"/.....", "/....."},
00204     {"/......", "/......"},
00205     {"...", "..."},
00206     {"/...///", "/.../"},
00207     {"/a...///", "/a.../"},
00208     {"/.x", "/.x"},
00209     {"/\\", "/"},
00210     {"/a\\", "/a\\"},
00211     {"/a\\\\...", "/a\\..."},
00212     {"foo/x..y/././y/../../..", "foo/x..y/y/"},
00213     {"foo/..x", "foo/..x"},
00214   };
00215   size_t i;
00216 
00217   for (i = 0; i < ARRAY_SIZE(data); i++) {
00218     remove_double_dots_and_double_slashes(data[i].before);
00219     ASSERT(strcmp(data[i].before, data[i].after) == 0);
00220   }
00221 
00222   return NULL;
00223 }
00224 
00225 static const char *test_get_var(void) {
00226   static const char *post[] = {
00227     "a=1&&b=2&d&=&c=3%20&e=",
00228     "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=",
00229     NULL
00230   };
00231   char buf[20];
00232 
00233   ASSERT(get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1);
00234   ASSERT(buf[0] == '1' && buf[1] == '\0');
00235   ASSERT(get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1);
00236   ASSERT(buf[0] == '2' && buf[1] == '\0');
00237   ASSERT(get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2);
00238   ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0');
00239   ASSERT(get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0);
00240   ASSERT(buf[0] == '\0');
00241 
00242   ASSERT(get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1);
00243   ASSERT(get_var(post[0], strlen(post[0]), "c", buf, 2) == -2);
00244 
00245   ASSERT(get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2);
00246   ASSERT(get_var(post[0], strlen(post[0]), "x", buf, 0) == -2);
00247   ASSERT(get_var(post[1], strlen(post[1]), "st", buf, 16) == -2);
00248   ASSERT(get_var(post[1], strlen(post[1]), "st", buf, 17) == 16);
00249   return NULL;
00250 }
00251 
00252 static const char *test_url_decode(void) {
00253   char buf[100];
00254 
00255   ASSERT(mg_url_decode("foo", 3, buf, 3, 0) == -1);  // No space for \0
00256   ASSERT(mg_url_decode("foo", 3, buf, 4, 0) == 3);
00257   ASSERT(strcmp(buf, "foo") == 0);
00258 
00259   ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 0) == 2);
00260   ASSERT(strcmp(buf, "a+") == 0);
00261 
00262   ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 1) == 2);
00263   ASSERT(strcmp(buf, "a ") == 0);
00264 
00265   ASSERT(mg_url_decode("%61", 1, buf, sizeof(buf), 1) == 1);
00266   printf("[%s]\n", buf);
00267   ASSERT(strcmp(buf, "%") == 0);
00268 
00269   ASSERT(mg_url_decode("%61", 2, buf, sizeof(buf), 1) == 2);
00270   ASSERT(strcmp(buf, "%6") == 0);
00271 
00272   ASSERT(mg_url_decode("%61", 3, buf, sizeof(buf), 1) == 1);
00273   ASSERT(strcmp(buf, "a") == 0);
00274   return NULL;
00275 }
00276 
00277 static const char *test_url_encode(void) {
00278   char buf[100];
00279   ASSERT(mg_url_encode("", 0, buf, sizeof(buf)) == 0);
00280   ASSERT(buf[0] == '\0');
00281   ASSERT(mg_url_encode("foo", 3, buf, sizeof(buf)) == 3);
00282   ASSERT(strcmp(buf, "foo") == 0);
00283   ASSERT(mg_url_encode("f o", 3, buf, sizeof(buf)) == 5);
00284   ASSERT(strcmp(buf, "f%20o") == 0);
00285   return NULL;
00286 }
00287 
00288 static const char *test_to64(void) {
00289   ASSERT(to64("0") == 0);
00290   ASSERT(to64("") == 0);
00291   ASSERT(to64("123") == 123);
00292   ASSERT(to64("-34") == -34);
00293   ASSERT(to64("3566626116") == 3566626116);
00294   return NULL;
00295 }
00296 
00297 static const char *test_base64_encode(void) {
00298   const char *in[] = {"a", "ab", "abc", "abcd", NULL};
00299   const char *out[] = {"YQ==", "YWI=", "YWJj", "YWJjZA=="};
00300   char buf[100];
00301   int i;
00302 
00303   for (i = 0; in[i] != NULL; i++) {
00304     base64_encode((unsigned char *) in[i], strlen(in[i]), buf);
00305     ASSERT(!strcmp(buf, out[i]));
00306   }
00307 
00308   return NULL;
00309 }
00310 
00311 static const char *test_mg_parse_header(void) {
00312   const char *str = "xx=1 kl yy, ert=234 kl=123, uri=\"/?name=x,y\", "
00313     "ii=\"12\\\"34\" zz='aa bb',tt=2,gf=\"xx d=1234";
00314   char buf[20];
00315   ASSERT(mg_parse_header(str, "yy", buf, sizeof(buf)) == 0);
00316   ASSERT(mg_parse_header(str, "ert", buf, sizeof(buf)) == 3);
00317   ASSERT(strcmp(buf, "234") == 0);
00318   ASSERT(mg_parse_header(str, "ert", buf, 2) == 0);
00319   ASSERT(mg_parse_header(str, "ert", buf, 3) == 0);
00320   ASSERT(mg_parse_header(str, "ert", buf, 4) == 3);
00321   ASSERT(mg_parse_header(str, "gf", buf, sizeof(buf)) == 0);
00322   ASSERT(mg_parse_header(str, "zz", buf, sizeof(buf)) == 5);
00323   ASSERT(strcmp(buf, "aa bb") == 0);
00324   ASSERT(mg_parse_header(str, "d", buf, sizeof(buf)) == 4);
00325   ASSERT(strcmp(buf, "1234") == 0);
00326   buf[0] = 'x';
00327   ASSERT(mg_parse_header(str, "MMM", buf, sizeof(buf)) == 0);
00328   ASSERT(buf[0] == '\0');
00329   ASSERT(mg_parse_header(str, "kl", buf, sizeof(buf)) == 3);
00330   ASSERT(strcmp(buf, "123") == 0);
00331   ASSERT(mg_parse_header(str, "xx", buf, sizeof(buf)) == 1);
00332   ASSERT(strcmp(buf, "1") == 0);
00333   ASSERT(mg_parse_header(str, "ii", buf, sizeof(buf)) == 5);
00334   ASSERT(strcmp(buf, "12\"34") == 0);
00335   ASSERT(mg_parse_header(str, "tt", buf, sizeof(buf)) == 1);
00336   ASSERT(strcmp(buf, "2") == 0);
00337   ASSERT(mg_parse_header(str, "uri", buf, sizeof(buf)) == 10);
00338   return NULL;
00339 }
00340 
00341 static const char *test_next_option(void) {
00342   const char *p, *list = "x/8,/y**=1;2k,z";
00343   struct vec a, b;
00344   int i;
00345 
00346   ASSERT(next_option(NULL, &a, &b) == NULL);
00347   for (i = 0, p = list; (p = next_option(p, &a, &b)) != NULL; i++) {
00348     ASSERT(i != 0 || (a.ptr == list && a.len == 3 && b.len == 0));
00349     ASSERT(i != 1 || (a.ptr == list + 4 && a.len == 4 && b.ptr == list + 9 &&
00350                       b.len == 4));
00351 
00352     ASSERT(i != 2 || (a.ptr == list + 14 && a.len == 1 && b.len == 0));
00353   }
00354   return NULL;
00355 }
00356 
00357 static int evh1(struct mg_connection *conn, enum mg_event ev) {
00358   char *buf = (char *) conn->connection_param;
00359   int result = MG_FALSE;
00360 
00361   switch (ev) {
00362     case MG_CONNECT:
00363       mg_printf(conn,  "GET %s HTTP/1.0\r\n\r\n",
00364                 buf[0] == '1' ? "/cb1" : "/non_exist");
00365       result = MG_TRUE;
00366       break;
00367     case MG_HTTP_ERROR:
00368       mg_printf(conn, "HTTP/1.0 404 NF\r\n\r\nERR: %d", conn->status_code);
00369       result = MG_TRUE;
00370       break;
00371     case MG_REQUEST:
00372       if (!strcmp(conn->uri, "/cb1")) {
00373         mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\n%s %s %s",
00374                   (char *) conn->server_param,
00375                   buf == NULL ? "?" : "!", conn->remote_ip);
00376         result = MG_TRUE;
00377       }
00378       break;
00379     case MG_REPLY:
00380       if (buf != NULL) {
00381         sprintf(buf + 1, "%.*s", (int) conn->content_len, conn->content);
00382       }
00383       break;
00384     case MG_AUTH:
00385       result = MG_TRUE;
00386       break;
00387     default:
00388       break;
00389   }
00390 
00391   return result;
00392 }
00393 
00394 static const char *test_server(void) {
00395   char buf1[100] = "1", buf2[100] = "2";
00396   struct mg_server *server = mg_create_server((void *) "foo", evh1);
00397   struct mg_connection *conn;
00398 
00399   ASSERT(server != NULL);
00400   ASSERT(mg_set_option(server, "listening_port", LISTENING_ADDR) == NULL);
00401   ASSERT(mg_set_option(server, "document_root", ".") == NULL);
00402 
00403   ASSERT((conn = mg_connect(server, "127.0.0.1:" HTTP_PORT)) != NULL);
00404   conn->connection_param = buf1;
00405   ASSERT((conn = mg_connect(server, "127.0.0.1:" HTTP_PORT)) != NULL);
00406   conn->connection_param = buf2;
00407 
00408   { int i; for (i = 0; i < 50; i++) mg_poll_server(server, 1); }
00409   ASSERT(strcmp(buf1, "1foo ? 127.0.0.1") == 0);
00410   ASSERT(strcmp(buf2, "2ERR: 404") == 0);
00411 
00412   ASSERT(strcmp(static_config_options[URL_REWRITES * 2], "url_rewrites") == 0);
00413   mg_destroy_server(&server);
00414   ASSERT(server == NULL);
00415   return NULL;
00416 }
00417 
00418 #define DISP "Content-Disposition: form/data; "
00419 #define CRLF "\r\n"
00420 #define BOUNDARY "--xyz"
00421 static const char *test_parse_multipart(void) {
00422   char a[100], b[100];
00423   const char *p;
00424   static const char f1[] = BOUNDARY CRLF DISP "name=f1" CRLF CRLF
00425     "some_content" CRLF BOUNDARY CRLF
00426     BOUNDARY CRLF DISP "name=f2; filename=\"foo bar.txt\"" CRLF CRLF
00427     "another_content" CRLF BOUNDARY CRLF
00428     "--" CRLF;
00429   int n, n2, len, f1_len = sizeof(f1) - 1;
00430 
00431   ASSERT(mg_parse_multipart("", 0, a, sizeof(a), b, sizeof(b), &p, &len) == 0);
00432   ASSERT(mg_parse_multipart("a", 1, a, sizeof(a), b, sizeof(b), &p, &len) == 0);
00433   ASSERT((n = mg_parse_multipart(f1, f1_len, a, sizeof(a),
00434                                  b, sizeof(b), &p, &len)) > 0);
00435   ASSERT(len == 12);
00436   ASSERT(memcmp(p, "some_content", len) == 0);
00437   ASSERT(strcmp(a, "f1") == 0);
00438   ASSERT(b[0] == '\0');
00439 
00440   ASSERT((n2 = mg_parse_multipart(f1 + n, f1_len - n, a, sizeof(a),
00441                                   b, sizeof(b), &p, &len)) > 0);
00442   ASSERT(len == 15);
00443   ASSERT(memcmp(p, "another_content", len) == 0);
00444   ASSERT(strcmp(a, "f2") == 0);
00445   ASSERT(strcmp(b, "foo bar.txt") == 0);
00446 
00447   ASSERT((n2 = mg_parse_multipart(f1 + n + n2, f1_len - (n + n2), a, sizeof(a),
00448                                   b, sizeof(b), &p, &len)) == 0);
00449 
00450   return NULL;
00451 }
00452 
00453 static int evh2(struct mg_connection *conn, enum mg_event ev) {
00454   char *file_data, *cp = (char *) conn->connection_param;
00455   int file_size, result = MG_FALSE;
00456 
00457   switch (ev) {
00458     case MG_AUTH:
00459       result = MG_TRUE;
00460       break;
00461     case MG_CONNECT:
00462       mg_printf(conn, "GET /%s HTTP/1.0\r\n\r\n", cp);
00463       result = MG_TRUE;
00464       break;
00465     case MG_REQUEST:
00466       break;
00467     case MG_REPLY:
00468       file_data = read_file("unit_test.c", &file_size);
00469       sprintf(cp, "%d %s", (size_t) file_size == conn->content_len &&
00470               memcmp(file_data, conn->content, file_size) == 0 ? 1 : 0,
00471               conn->query_string == NULL ? "?" : conn->query_string);
00472       free(file_data);
00473       break;
00474     default:
00475       break;
00476   }
00477 
00478   return result;
00479 }
00480 
00481 static const char *test_mg_set_option(void) {
00482   struct mg_server *server = mg_create_server(NULL, NULL);
00483   ASSERT(mg_set_option(server, "listening_port", "0") == NULL);
00484   ASSERT(mg_get_option(server, "listening_port")[0] != '\0');
00485   mg_destroy_server(&server);
00486   return NULL;
00487 }
00488 
00489 static const char *test_rewrites(void) {
00490   char buf1[100] = "xx", addr[50];
00491   struct mg_server *server = mg_create_server(NULL, evh2);
00492   struct mg_connection *conn;
00493   const char *port;
00494 
00495   ASSERT(mg_set_option(server, "listening_port", "0") == NULL);
00496   ASSERT(mg_set_option(server, "document_root", ".") == NULL);
00497   ASSERT(mg_set_option(server, "url_rewrites", "/xx=unit_test.c") == NULL);
00498   ASSERT((port = mg_get_option(server, "listening_port")) != NULL);
00499   snprintf(addr, sizeof(addr), "127.0.0.1:%s", port);
00500   ASSERT((conn = mg_connect(server, addr)) != NULL);
00501   conn->connection_param = buf1;
00502 
00503   { int i; for (i = 0; i < 50; i++) mg_poll_server(server, 1); }
00504 
00505   ASSERT(strcmp(buf1, "1 ?") == 0);
00506   mg_destroy_server(&server);
00507   return NULL;
00508 }
00509 
00510 static const char *run_all_tests(void) {
00511   RUN_TEST(test_should_keep_alive);
00512   RUN_TEST(test_match_prefix);
00513   RUN_TEST(test_remove_double_dots);
00514   RUN_TEST(test_parse_http_message);
00515   RUN_TEST(test_to64);
00516   RUN_TEST(test_url_decode);
00517   RUN_TEST(test_url_encode);
00518   RUN_TEST(test_base64_encode);
00519   RUN_TEST(test_mg_parse_header);
00520   RUN_TEST(test_get_var);
00521   RUN_TEST(test_next_option);
00522   RUN_TEST(test_parse_multipart);
00523   RUN_TEST(test_mg_set_option);
00524   RUN_TEST(test_server);
00525   RUN_TEST(test_rewrites);
00526   return NULL;
00527 }
00528 
00529 int __cdecl main(void) {
00530   const char *fail_msg = run_all_tests();
00531   printf("%s, tests run: %d\n", fail_msg ? "FAIL" : "PASS", static_num_tests);
00532   return fail_msg == NULL ? EXIT_SUCCESS : EXIT_FAILURE;
00533 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07