1 From 70c07e01dee24df0a1591d65799b66a8e89a3bd6 Mon Sep 17 00:00:00 2001
2 From: Eric Blake <eblake@redhat.com>
3 Date: Mon, 26 Mar 2012 11:23:45 +0100
4 Subject: [PATCH] Fix and test round-trip of query parameters
6 When qparams support was dropped in commit bc1ff160, we forgot
7 to add tests to ensure that viruri can do the same round trip
8 handling of a URI. This round trip was broken, due to use
9 of the old 'query' field of xmlUriPtr, instead of the new
12 Also, we forgot to report an OOM error.
14 * tests/viruritest.c (mymain): Add tests based on just-deleted
16 (testURIParse): Allow difference in input and expected output.
17 * src/util/viruri.c (virURIFormat): Add missing error. Use
18 query_raw, instead of query for xmlUriPtr object.
20 src/util/viruri.c | 8 ++++-
21 tests/viruritest.c | 78 +++++++++++++++++++++++++++++++++++++++++++--------
22 2 files changed, 72 insertions(+), 14 deletions(-)
24 diff --git a/src/util/viruri.c b/src/util/viruri.c
25 index 7cca977..2c6de51 100644
26 --- a/src/util/viruri.c
27 +++ b/src/util/viruri.c
28 @@ -243,15 +243,21 @@ virURIFormat(virURIPtr uri)
29 xmluri.server = uri->server;
30 xmluri.port = uri->port;
31 xmluri.path = uri->path;
32 +#ifdef HAVE_XMLURI_QUERY_RAW
33 + xmluri.query_raw = uri->query;
35 xmluri.query = uri->query;
37 xmluri.fragment = uri->fragment;
39 /* First check: does it make sense to do anything */
40 if (xmluri.server != NULL &&
41 strchr(xmluri.server, ':') != NULL) {
43 - if (virAsprintf(&tmpserver, "[%s]", xmluri.server) < 0)
44 + if (virAsprintf(&tmpserver, "[%s]", xmluri.server) < 0) {
45 + virReportOOMError();
49 xmluri.server = tmpserver;
51 diff --git a/tests/viruritest.c b/tests/viruritest.c
52 index 9504a3b..d97e9c7 100644
53 --- a/tests/viruritest.c
54 +++ b/tests/viruritest.c
59 + const char *uri_out;
63 @@ -49,21 +50,12 @@ static int testURIParse(const void *args)
66 const struct URIParseData *data = args;
68 + char *uristr = NULL;
71 if (!(uri = virURIParse(data->uri)))
74 - if (!(uristr = virURIFormat(uri)))
77 - if (!STREQ(uristr, data->uri)) {
78 - VIR_DEBUG("URI did not roundtrip, expect '%s', actual '%s'",
83 if (!STREQ(uri->scheme, data->scheme)) {
84 VIR_DEBUG("Expected scheme '%s', actual '%s'",
85 data->scheme, uri->scheme);
86 @@ -123,6 +115,18 @@ static int testURIParse(const void *args)
90 + VIR_FREE(uri->query);
91 + uri->query = virURIFormatParams(uri);
93 + if (!(uristr = virURIFormat(uri)))
96 + if (!STREQ(uristr, data->uri_out)) {
97 + VIR_DEBUG("URI did not roundtrip, expect '%s', actual '%s'",
98 + data->uri_out, uristr);
105 @@ -138,14 +142,22 @@ mymain(void)
107 signal(SIGPIPE, SIG_IGN);
109 -#define TEST_PARSE(uri, scheme, server, port, path, query, fragment, params) \
110 +#define TEST_FULL(uri, uri_out, scheme, server, port, path, query, \
111 + fragment, params) \
113 const struct URIParseData data = { \
114 - uri, scheme, server, port, path, query, fragment, params \
115 + uri, (uri_out) ? (uri_out) : (uri), scheme, server, port, \
116 + path, query, fragment, params \
118 - if (virtTestRun("Test IPv6 " # uri, 1, testURIParse, &data) < 0) \
119 + if (virtTestRun("Test URI " # uri, 1, testURIParse, &data) < 0) \
122 +#define TEST_PARSE(uri, scheme, server, port, path, query, fragment, params) \
123 + TEST_FULL(uri, NULL, scheme, server, port, path, query, fragment, params)
124 +#define TEST_PARAMS(query_in, query_out, params) \
125 + TEST_FULL("test://example.com/?" query_in, \
126 + *query_out ? "test://example.com/?" query_out : NULL, \
127 + "test", "example.com", 0, "/", query_in, NULL, params)
129 virURIParam params[] = {
130 { (char*)"name", (char*)"value" },
131 @@ -159,6 +171,46 @@ mymain(void)
132 TEST_PARSE("test://[::1]:123/system", "test", "::1", 123, "/system", NULL, NULL, NULL);
133 TEST_PARSE("test://[2001:41c8:1:4fd4::2]:123/system", "test", "2001:41c8:1:4fd4::2", 123, "/system", NULL, NULL, NULL);
135 + virURIParam params1[] = {
136 + { (char*)"foo", (char*)"one" },
137 + { (char*)"bar", (char*)"two" },
140 + virURIParam params2[] = {
141 + { (char*)"foo", (char*)"one" },
142 + { (char*)"foo", (char*)"two" },
145 + virURIParam params3[] = {
146 + { (char*)"foo", (char*)"&one" },
147 + { (char*)"bar", (char*)"&two" },
150 + virURIParam params4[] = {
151 + { (char*)"foo", (char*)"" },
154 + virURIParam params5[] = {
155 + { (char*)"foo", (char*)"one two" },
158 + virURIParam params6[] = {
159 + { (char*)"foo", (char*)"one" },
163 + TEST_PARAMS("foo=one&bar=two", "", params1);
164 + TEST_PARAMS("foo=one&foo=two", "", params2);
165 + TEST_PARAMS("foo=one&&foo=two", "foo=one&foo=two", params2);
166 + TEST_PARAMS("foo=one;foo=two", "foo=one&foo=two", params2);
167 + TEST_PARAMS("foo=%26one&bar=%26two", "", params3);
168 + TEST_PARAMS("foo", "foo=", params4);
169 + TEST_PARAMS("foo=", "", params4);
170 + TEST_PARAMS("foo=&", "foo=", params4);
171 + TEST_PARAMS("foo=&&", "foo=", params4);
172 + TEST_PARAMS("foo=one%20two", "", params5);
173 + TEST_PARAMS("=bogus&foo=one", "foo=one", params6);
175 return (ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);