]>
Commit | Line | Data |
---|---|---|
f4fa2857 JR |
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 | |
5 | ||
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 | |
10 | 'query_raw' | |
11 | ||
12 | Also, we forgot to report an OOM error. | |
13 | ||
14 | * tests/viruritest.c (mymain): Add tests based on just-deleted | |
15 | qparamtest. | |
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. | |
19 | --- | |
20 | src/util/viruri.c | 8 ++++- | |
21 | tests/viruritest.c | 78 +++++++++++++++++++++++++++++++++++++++++++-------- | |
22 | 2 files changed, 72 insertions(+), 14 deletions(-) | |
23 | ||
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; | |
34 | +#else | |
35 | xmluri.query = uri->query; | |
36 | +#endif | |
37 | xmluri.fragment = uri->fragment; | |
38 | ||
39 | /* First check: does it make sense to do anything */ | |
40 | if (xmluri.server != NULL && | |
41 | strchr(xmluri.server, ':') != NULL) { | |
42 | ||
43 | - if (virAsprintf(&tmpserver, "[%s]", xmluri.server) < 0) | |
44 | + if (virAsprintf(&tmpserver, "[%s]", xmluri.server) < 0) { | |
45 | + virReportOOMError(); | |
46 | return NULL; | |
47 | + } | |
48 | ||
49 | xmluri.server = tmpserver; | |
50 | } | |
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 | |
55 | @@ -35,6 +35,7 @@ | |
56 | ||
57 | struct URIParseData { | |
58 | const char *uri; | |
59 | + const char *uri_out; | |
60 | const char *scheme; | |
61 | const char *server; | |
62 | int port; | |
63 | @@ -49,21 +50,12 @@ static int testURIParse(const void *args) | |
64 | int ret = -1; | |
65 | virURIPtr uri = NULL; | |
66 | const struct URIParseData *data = args; | |
67 | - char *uristr; | |
68 | + char *uristr = NULL; | |
69 | size_t i; | |
70 | ||
71 | if (!(uri = virURIParse(data->uri))) | |
72 | goto cleanup; | |
73 | ||
74 | - if (!(uristr = virURIFormat(uri))) | |
75 | - goto cleanup; | |
76 | - | |
77 | - if (!STREQ(uristr, data->uri)) { | |
78 | - VIR_DEBUG("URI did not roundtrip, expect '%s', actual '%s'", | |
79 | - data->uri, uristr); | |
80 | - goto cleanup; | |
81 | - } | |
82 | - | |
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) | |
87 | goto cleanup; | |
88 | } | |
89 | ||
90 | + VIR_FREE(uri->query); | |
91 | + uri->query = virURIFormatParams(uri); | |
92 | + | |
93 | + if (!(uristr = virURIFormat(uri))) | |
94 | + goto cleanup; | |
95 | + | |
96 | + if (!STREQ(uristr, data->uri_out)) { | |
97 | + VIR_DEBUG("URI did not roundtrip, expect '%s', actual '%s'", | |
98 | + data->uri_out, uristr); | |
99 | + goto cleanup; | |
100 | + } | |
101 | + | |
102 | ret = 0; | |
103 | cleanup: | |
104 | VIR_FREE(uristr); | |
105 | @@ -138,14 +142,22 @@ mymain(void) | |
106 | ||
107 | signal(SIGPIPE, SIG_IGN); | |
108 | ||
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) \ | |
112 | do { \ | |
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 \ | |
117 | }; \ | |
118 | - if (virtTestRun("Test IPv6 " # uri, 1, testURIParse, &data) < 0) \ | |
119 | + if (virtTestRun("Test URI " # uri, 1, testURIParse, &data) < 0) \ | |
120 | ret = -1; \ | |
121 | } while (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) | |
128 | ||
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); | |
134 | ||
135 | + virURIParam params1[] = { | |
136 | + { (char*)"foo", (char*)"one" }, | |
137 | + { (char*)"bar", (char*)"two" }, | |
138 | + { NULL, NULL }, | |
139 | + }; | |
140 | + virURIParam params2[] = { | |
141 | + { (char*)"foo", (char*)"one" }, | |
142 | + { (char*)"foo", (char*)"two" }, | |
143 | + { NULL, NULL }, | |
144 | + }; | |
145 | + virURIParam params3[] = { | |
146 | + { (char*)"foo", (char*)"&one" }, | |
147 | + { (char*)"bar", (char*)"&two" }, | |
148 | + { NULL, NULL }, | |
149 | + }; | |
150 | + virURIParam params4[] = { | |
151 | + { (char*)"foo", (char*)"" }, | |
152 | + { NULL, NULL }, | |
153 | + }; | |
154 | + virURIParam params5[] = { | |
155 | + { (char*)"foo", (char*)"one two" }, | |
156 | + { NULL, NULL }, | |
157 | + }; | |
158 | + virURIParam params6[] = { | |
159 | + { (char*)"foo", (char*)"one" }, | |
160 | + { NULL, NULL }, | |
161 | + }; | |
162 | + | |
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); | |
174 | + | |
175 | return (ret==0 ? EXIT_SUCCESS : EXIT_FAILURE); | |
176 | } | |
177 | ||
178 | -- | |
179 | 1.7.1 | |
180 |