]> granicus.if.org Git - neomutt/commitdiff
Avoid flexible array members, improve URL test code 1420/head
authorPietro Cerutti <gahr@gahr.ch>
Fri, 16 Nov 2018 07:57:41 +0000 (07:57 +0000)
committerRichard Russon <rich@flatcap.org>
Fri, 16 Nov 2018 11:21:19 +0000 (11:21 +0000)
email/url.c
email/url.h
test/url.c

index 8e5d95925dd959391534099d6e22f53480f3e5d4..482549067ef0b544e18b9907e2f941c6507b9c44 100644 (file)
@@ -179,6 +179,7 @@ struct Url *url_parse(const char *src)
   u->port = 0;
   u->path = NULL;
   STAILQ_INIT(&u->query_strings);
+  u->src = (char *)u + sizeof(struct Url);
   mutt_str_strfcpy(u->src, src, srcsize);
 
   char *it = u->src;
index cd3a9f300dd45d16733f0d2852f60f704156e647..a716d131ae7588ba984b95439c69e95ddf330ad5 100644 (file)
@@ -73,7 +73,7 @@ struct Url
   unsigned short port;
   char *path;
   struct UrlQueryStringHead query_strings;
-  char src[];
+  char *src;
 };
 
 enum UrlScheme url_check_scheme(const char *s);
index 10557a1034bf8b1439d4ab6410abed68c7c3bdcd..98bd2a32d25c64d25014dd7936a90028baa25153 100644 (file)
@@ -1,27 +1,50 @@
 #define TEST_NO_MAIN
 #include "acutest.h"
 #include "email/url.h"
+#include "mutt/string2.h"
 
-typedef bool (*check_query_string)(const struct UrlQueryStringHead *q);
-
-bool check_query_string_eq(const struct UrlQueryStringHead *q)
+void check_query_string(const char *exp, const struct UrlQueryStringHead *act)
 {
-  const struct UrlQueryString *np = STAILQ_FIRST(q);
-  if (strcmp(np->name, "encoding") != 0)
-    return false;
-  if (strcmp(np->value, "binary") != 0)
-    return false;
-  if (STAILQ_NEXT(np, entries) != NULL)
-    return false;
-  return true;
+  char *next;
+  char tmp[64];
+  const struct UrlQueryString *np = STAILQ_FIRST(act);
+  while (exp && *exp)
+  {
+    next = strchr(exp, '|');
+    mutt_str_strfcpy(tmp, exp, next - exp + 1);
+    exp = next + 1;
+    if (!TEST_CHECK(strcmp(tmp, np->name) == 0))
+    {
+      TEST_MSG("Expected: <%s>", tmp);
+      TEST_MSG("Actual  : <%s>", np->name);
+    }
+
+    next = strchr(exp, '|');
+    mutt_str_strfcpy(tmp, exp, next - exp + 1);
+    exp = next + 1;
+    if (!TEST_CHECK(strcmp(tmp, np->value) == 0))
+    {
+      TEST_MSG("Expected: <%s>", tmp);
+      TEST_MSG("Actual  : <%s>", np->value);
+    }
+
+    np = STAILQ_NEXT(np, entries);
+  }
+
+  if (!TEST_CHECK(np == NULL))
+  {
+    TEST_MSG("Expected: NULL");
+    TEST_MSG("Actual  : (%s, %s)", np->name, np->value);
+  }
 }
 
 static struct
 {
-  const char *source;
-  bool valid;
-  check_query_string f;
-  struct Url url;
+  const char *source;   /* source URL to parse                              */ 
+  bool valid;           /* expected validity                                */
+  struct Url url;       /* expected resulting URL                           */
+  const char *qs_elem;  /* expected elements of the query string, separated
+                           and terminated by a pipe '|' character           */
 } test[] = {
   {
     "foobar foobar",
@@ -30,7 +53,6 @@ static struct
   {
     "imaps://foouser:foopass@imap.example.com:456",
     true,
-    NULL,
     {
       U_IMAPS,
       "foouser",
@@ -38,12 +60,12 @@ static struct
       "imap.example.com",
       456,
       NULL
-    }
+    },
+    NULL
   },
   {
     "SmTp://user@example.com", /* scheme is lower-cased */
     true,
-    NULL,
     {
       U_SMTP,
       "user",
@@ -51,12 +73,12 @@ static struct
       "example.com",
       0,
       NULL
-    }
+    },
+    NULL
   },
   {
     "pop://user@example.com@pop.example.com:234/some/where?encoding=binary",
     true,
-    check_query_string_eq,
     {
       U_POP,
       "user@example.com",
@@ -64,7 +86,8 @@ static struct
       "pop.example.com",
       234,
       "some/where",
-    }
+    },
+    "encoding|binary|"
   }
 };
 
@@ -114,10 +137,7 @@ void test_url(void)
       TEST_MSG("Expected: %s", test[i].url.path);
       TEST_MSG("Actual  : %s", url->path);
     }
-    if (test[i].f)
-    {
-      TEST_CHECK(test[i].f(&url->query_strings));
-    }
+    check_query_string(test[i].qs_elem, &url->query_strings);
 
     url_free(&url);
   }