2 * Copyright (C) 2012, 2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
23 #include "testutils.h"
30 #define VIR_FROM_THIS VIR_FROM_RPC
32 VIR_LOG_INIT("tests.uritest");
44 virURIParamPtr params
;
47 static int testURIParse(const void *args
)
51 const struct URIParseData
*data
= args
;
56 if (!(uri
= virURIParse(data
->uri
)))
59 if (STRNEQ(uri
->scheme
, data
->scheme
)) {
60 VIR_TEST_DEBUG("Expected scheme '%s', actual '%s'",
61 data
->scheme
, uri
->scheme
);
65 if (STRNEQ(uri
->server
, data
->server
)) {
66 VIR_TEST_DEBUG("Expected server '%s', actual '%s'",
67 data
->server
, uri
->server
);
71 if (uri
->port
!= data
->port
) {
72 VIR_TEST_DEBUG("Expected port '%d', actual '%d'",
73 data
->port
, uri
->port
);
77 if (STRNEQ_NULLABLE(uri
->path
, data
->path
)) {
78 VIR_TEST_DEBUG("Expected path '%s', actual '%s'",
79 data
->path
, uri
->path
);
83 if (STRNEQ_NULLABLE(uri
->query
, data
->query
)) {
84 VIR_TEST_DEBUG("Expected query '%s', actual '%s'",
85 data
->query
, uri
->query
);
89 if (STRNEQ_NULLABLE(uri
->fragment
, data
->fragment
)) {
90 VIR_TEST_DEBUG("Expected fragment '%s', actual '%s'",
91 data
->fragment
, uri
->fragment
);
95 for (i
= 0; data
->params
&& data
->params
[i
].name
&& i
< uri
->paramsCount
; i
++) {
96 if (STRNEQ_NULLABLE(data
->params
[i
].name
, uri
->params
[i
].name
)) {
97 VIR_TEST_DEBUG("Expected param name %zu '%s', actual '%s'",
98 i
, data
->params
[i
].name
, uri
->params
[i
].name
);
101 if (STRNEQ_NULLABLE(data
->params
[i
].value
, uri
->params
[i
].value
)) {
102 VIR_TEST_DEBUG("Expected param value %zu '%s', actual '%s'",
103 i
, data
->params
[i
].value
, uri
->params
[i
].value
);
107 if (data
->params
&& data
->params
[i
].name
) {
108 VIR_TEST_DEBUG("Missing parameter %zu %s=%s",
109 i
, data
->params
[i
].name
, data
->params
[i
].value
);
112 if (i
!= uri
->paramsCount
) {
113 for (; i
< uri
->paramsCount
; i
++) {
114 VIR_TEST_DEBUG("Unexpected parameter %zu %s=%s",
115 i
, uri
->params
[i
].name
, uri
->params
[i
].value
);
120 VIR_FREE(uri
->query
);
121 uri
->query
= virURIFormatParams(uri
);
123 if (!(uristr
= virURIFormat(uri
)))
126 if (STRNEQ(uristr
, data
->uri_out
)) {
127 VIR_TEST_DEBUG("URI did not roundtrip, expect '%s', actual '%s'",
128 data
->uri_out
, uristr
);
148 signal(SIGPIPE
, SIG_IGN
);
150 #define TEST_FULL(uri, uri_out, scheme, server, port, path, query, \
151 fragment, user, params) \
153 const struct URIParseData data = { \
154 uri, (uri_out) ? (uri_out) : (uri), scheme, server, port, \
155 path, query, fragment, user, params \
157 if (virTestRun("Test URI " # uri, testURIParse, &data) < 0) \
160 #define TEST_PARSE(uri, scheme, server, port, path, query, fragment, user, params) \
161 TEST_FULL(uri, NULL, scheme, server, port, path, query, fragment, user, params)
162 #define TEST_PARAMS(query_in, query_out, params) \
163 TEST_FULL("test://example.com/?" query_in, \
164 *query_out ? "test://example.com/?" query_out : NULL, \
165 "test", "example.com", 0, "/", query_in, NULL, NULL, params)
167 virURIParam params
[] = {
168 { (char*)"name", (char*)"value", false },
169 { NULL
, NULL
, false },
172 TEST_PARSE("test://example.com", "test", "example.com", 0, NULL
, NULL
, NULL
, NULL
, NULL
);
173 TEST_PARSE("test://foo@example.com", "test", "example.com", 0, NULL
, NULL
, NULL
, "foo", NULL
);
174 TEST_PARSE("test://example.com:123", "test", "example.com", 123, NULL
, NULL
, NULL
, NULL
, NULL
);
175 TEST_PARSE("test://example.com:123/system?name=value#foo", "test", "example.com", 123, "/system", "name=value", "foo", NULL
, params
);
176 TEST_PARSE("test://127.0.0.1:123/system", "test", "127.0.0.1", 123, "/system", NULL
, NULL
, NULL
, NULL
);
177 TEST_PARSE("test://[::1]:123/system", "test", "::1", 123, "/system", NULL
, NULL
, NULL
, NULL
);
178 TEST_PARSE("test://[2001:41c8:1:4fd4::2]:123/system", "test", "2001:41c8:1:4fd4::2", 123, "/system", NULL
, NULL
, NULL
, NULL
);
179 TEST_PARSE("gluster+rdma://example.com:1234/gv0/vol.img", "gluster+rdma", "example.com", 1234, "/gv0/vol.img", NULL
, NULL
, NULL
, NULL
);
181 virURIParam spiceparams
[] = {
182 { (char *) "tlsSubject", (char *) "C=XX,L=Testtown,O=Test Company,CN=tester.test", false },
183 { NULL
, NULL
, false },
185 TEST_FULL("spice://[3ffe::104]:5900/?tlsSubject=C=XX,L=Testtown,O=Test%20Company,CN=tester.test",
186 "spice://[3ffe::104]:5900/?tlsSubject=C%3dXX%2cL%3dTesttown%2cO%3dTest%20Company%2cCN%3dtester%2etest",
187 "spice", "3ffe::104", 5900, "/", "tlsSubject=C=XX,L=Testtown,O=Test%20Company,CN=tester.test", NULL
, NULL
, spiceparams
);
189 virURIParam params1
[] = {
190 { (char*)"foo", (char*)"one", false },
191 { (char*)"bar", (char*)"two", false },
192 { NULL
, NULL
, false },
194 virURIParam params2
[] = {
195 { (char*)"foo", (char*)"one", false },
196 { (char*)"foo", (char*)"two", false },
197 { NULL
, NULL
, false },
199 virURIParam params3
[] = {
200 { (char*)"foo", (char*)"&one", false },
201 { (char*)"bar", (char*)"&two", false },
202 { NULL
, NULL
, false },
204 virURIParam params4
[] = {
205 { (char*)"foo", (char*)"", false },
206 { NULL
, NULL
, false },
208 virURIParam params5
[] = {
209 { (char*)"foo", (char*)"one two", false },
210 { NULL
, NULL
, false },
212 virURIParam params6
[] = {
213 { (char*)"foo", (char*)"one", false },
214 { NULL
, NULL
, false },
217 TEST_PARAMS("foo=one&bar=two", "", params1
);
218 TEST_PARAMS("foo=one&foo=two", "", params2
);
219 TEST_PARAMS("foo=one&&foo=two", "foo=one&foo=two", params2
);
220 TEST_PARAMS("foo=one;foo=two", "foo=one&foo=two", params2
);
221 TEST_PARAMS("foo=%26one&bar=%26two", "", params3
);
222 TEST_PARAMS("foo", "foo=", params4
);
223 TEST_PARAMS("foo=", "", params4
);
224 TEST_PARAMS("foo=&", "foo=", params4
);
225 TEST_PARAMS("foo=&&", "foo=", params4
);
226 TEST_PARAMS("foo=one%20two", "", params5
);
227 TEST_PARAMS("=bogus&foo=one", "foo=one", params6
);
229 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
232 VIR_TEST_MAIN(mymain
)