src/url_test.c
changeset 18 b12e78767248
parent 17 0a024b29b16d
equal deleted inserted replaced
17:0a024b29b16d 18:b12e78767248
     7 
     7 
     8 #define FAIL(...) do { printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); return -1; } while (0)
     8 #define FAIL(...) do { printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); return -1; } while (0)
     9 
     9 
    10 struct url_schema
    10 struct url_schema
    11     basic_http = { 1, { "http" } },
    11     basic_http = { 1, { "http" } },
    12     svn_ssh = { 2, { "svn", "ssh" } };
    12     svn_ssh = { 2, { "svn", "ssh" } },
       
    13     schema_unix = { 1, { "unix" } }
       
    14     ;
       
    15 
       
    16 struct url_opts
       
    17     opts_single = { 1, { { "key0", "val0" } } },
       
    18     opts_multi = { 2, { { "key0", "val0" }, { "key1", "val1" } } },
       
    19     opts_nullval = { 1, { { "keyN", NULL } } }
       
    20     ;
    13 
    21 
    14 struct url_test {
    22 struct url_test {
    15     const char *url;
    23     const char *url;
    16     const struct url expected;
    24     const struct url expected;
    17 } url_tests[] = {
    25 } url_tests[] = {
    28     } },
    36     } },
    29 
    37 
    30     {   "user@:service/",   {
    38     {   "user@:service/",   {
    31         NULL, "user", NULL, NULL, "service", NULL
    39         NULL, "user", NULL, NULL, "service", NULL
    32     } },
    40     } },
    33     
    41 
       
    42     {   "unix:////tmp/foo.sock",    {
       
    43         &schema_unix, NULL, NULL, NULL, NULL, "/tmp/foo.sock"
       
    44     } },
       
    45 
       
    46     {   "unix:///tmp/foo.sock", {
       
    47         &schema_unix, NULL, NULL, NULL, NULL, "tmp/foo.sock"
       
    48     } },
       
    49 
       
    50     {   "/tmp/foo.sock",    {
       
    51         NULL, NULL, NULL, NULL, NULL, "tmp/foo.sock"
       
    52     } },
       
    53 
       
    54     {   "?key0=val0",   {
       
    55         NULL, NULL, NULL, NULL, NULL, NULL, &opts_single
       
    56     } },
       
    57 
       
    58     {   "http://foo.com/index.php?key0=val0&key1=val1",  {
       
    59         &basic_http, NULL, NULL, "foo.com", NULL, "index.php", &opts_multi
       
    60     } },
       
    61 
       
    62     {   "example.org:81/?keyN", {
       
    63         NULL, NULL, NULL, "example.org", "81", NULL, &opts_nullval
       
    64     } },
       
    65 
    34     {   NULL,               {   } },
    66     {   NULL,               {   } },
    35 };
    67 };
    36 
    68 
    37 int cmp_url_str (const char *field, const char *test, const char *real) {
    69 int cmp_url_str (const char *field, const char *test, const char *real) {
    38     if (!test) {
    70     if (!test) {
   103     } else {
   135     } else {
   104         if (test->opts->count != test->opts->count)
   136         if (test->opts->count != test->opts->count)
   105             FAIL("inconsistent opts count");
   137             FAIL("inconsistent opts count");
   106         
   138         
   107         for (i = 0; i < test->opts->count; i++) {
   139         for (i = 0; i < test->opts->count; i++) {
   108             if (strcmp(test->opts->list[i].key, real->opts->list[i].key) != 0)
   140             if (cmp_url_str("opt key", test->opts->list[i].key, real->opts->list[i].key))
   109                 FAIL("differing scheme key #%d", i);
   141                 FAIL("differing opt key #%d", i);
   110             
   142             
   111             if (strcmp(test->opts->list[i].value, real->opts->list[i].value) != 0)
   143             if (cmp_url_str("opt value", test->opts->list[i].value, real->opts->list[i].value))
   112                 FAIL("differing scheme value #%d", i);
   144                 FAIL("differing opt value #%d", i);
   113         }
   145         }
   114     }
   146     }
   115 
   147 
   116     // ok
   148     // ok
   117     return 0;
   149     return 0;
   157 
   189 
   158         } else {
   190         } else {
   159             printf("OK\n\t");
   191             printf("OK\n\t");
   160             url_dump(&url, stdout);
   192             url_dump(&url, stdout);
   161         }
   193         }
       
   194 
       
   195         printf("\n");
   162     }
   196     }
   163 }
   197 }
   164 
   198