From: Pietro Cerutti Date: Fri, 16 Nov 2018 07:57:41 +0000 (+0000) Subject: Avoid flexible array members, improve URL test code X-Git-Tag: 2019-10-25~525^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cd4282d88bd3cf2aa74d3dbf64461f06a96f6b07;p=neomutt Avoid flexible array members, improve URL test code --- diff --git a/email/url.c b/email/url.c index 8e5d95925..482549067 100644 --- a/email/url.c +++ b/email/url.c @@ -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; diff --git a/email/url.h b/email/url.h index cd3a9f300..a716d131a 100644 --- a/email/url.h +++ b/email/url.h @@ -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); diff --git a/test/url.c b/test/url.c index 10557a103..98bd2a32d 100644 --- a/test/url.c +++ b/test/url.c @@ -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); }