backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / viruritest.c
blobd419711135859e55a529e3e660a6da7f53ccf2ad
1 /*
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/>.
19 #include <config.h>
21 #include <signal.h>
23 #include "testutils.h"
24 #include "virerror.h"
25 #include "viralloc.h"
26 #include "virlog.h"
28 #include "viruri.h"
30 #define VIR_FROM_THIS VIR_FROM_RPC
32 VIR_LOG_INIT("tests.uritest");
34 struct URIParseData {
35 const char *uri;
36 const char *uri_out;
37 const char *scheme;
38 const char *server;
39 int port;
40 const char *path;
41 const char *query;
42 const char *fragment;
43 const char *user;
44 virURIParamPtr params;
47 static int testURIParse(const void *args)
49 int ret = -1;
50 virURIPtr uri = NULL;
51 const struct URIParseData *data = args;
52 char *uristr = NULL;
53 size_t i;
54 bool fail = false;
56 if (!(uri = virURIParse(data->uri)))
57 goto cleanup;
59 if (STRNEQ(uri->scheme, data->scheme)) {
60 VIR_TEST_DEBUG("Expected scheme '%s', actual '%s'",
61 data->scheme, uri->scheme);
62 fail = true;
65 if (STRNEQ(uri->server, data->server)) {
66 VIR_TEST_DEBUG("Expected server '%s', actual '%s'",
67 data->server, uri->server);
68 fail = true;
71 if (uri->port != data->port) {
72 VIR_TEST_DEBUG("Expected port '%d', actual '%d'",
73 data->port, uri->port);
74 fail = true;
77 if (STRNEQ_NULLABLE(uri->path, data->path)) {
78 VIR_TEST_DEBUG("Expected path '%s', actual '%s'",
79 data->path, uri->path);
80 fail = true;
83 if (STRNEQ_NULLABLE(uri->query, data->query)) {
84 VIR_TEST_DEBUG("Expected query '%s', actual '%s'",
85 data->query, uri->query);
86 fail = true;
89 if (STRNEQ_NULLABLE(uri->fragment, data->fragment)) {
90 VIR_TEST_DEBUG("Expected fragment '%s', actual '%s'",
91 data->fragment, uri->fragment);
92 fail = true;
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);
99 fail = true;
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);
104 fail = true;
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);
110 fail = true;
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);
117 fail = true;
120 VIR_FREE(uri->query);
121 uri->query = virURIFormatParams(uri);
123 if (!(uristr = virURIFormat(uri)))
124 goto cleanup;
126 if (STRNEQ(uristr, data->uri_out)) {
127 VIR_TEST_DEBUG("URI did not roundtrip, expect '%s', actual '%s'",
128 data->uri_out, uristr);
129 fail = true;
132 if (fail)
133 goto cleanup;
135 ret = 0;
136 cleanup:
137 VIR_FREE(uristr);
138 virURIFree(uri);
139 return ret;
143 static int
144 mymain(void)
146 int ret = 0;
148 signal(SIGPIPE, SIG_IGN);
150 #define TEST_FULL(uri, uri_out, scheme, server, port, path, query, \
151 fragment, user, params) \
152 do { \
153 const struct URIParseData data = { \
154 uri, (uri_out) ? (uri_out) : (uri), scheme, server, port, \
155 path, query, fragment, user, params \
156 }; \
157 if (virTestRun("Test URI " # uri, testURIParse, &data) < 0) \
158 ret = -1; \
159 } while (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)