summaryrefslogtreecommitdiff
blob: d97e9c745fd4d306e2472c1807d2b4456eea4057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <stdlib.h>
#include <signal.h>

#include "testutils.h"
#include "util.h"
#include "virterror_internal.h"
#include "memory.h"
#include "logging.h"

#include "viruri.h"

#define VIR_FROM_THIS VIR_FROM_RPC

struct URIParseData {
    const char *uri;
    const char *uri_out;
    const char *scheme;
    const char *server;
    int port;
    const char *path;
    const char *query;
    const char *fragment;
    virURIParamPtr params;
};

static int testURIParse(const void *args)
{
    int ret = -1;
    virURIPtr uri = NULL;
    const struct URIParseData *data = args;
    char *uristr = NULL;
    size_t i;

    if (!(uri = virURIParse(data->uri)))
        goto cleanup;

    if (!STREQ(uri->scheme, data->scheme)) {
        VIR_DEBUG("Expected scheme '%s', actual '%s'",
                  data->scheme, uri->scheme);
        goto cleanup;
    }

    if (!STREQ(uri->server, data->server)) {
        VIR_DEBUG("Expected server '%s', actual '%s'",
                  data->server, uri->server);
        goto cleanup;
    }

    if (uri->port != data->port) {
        VIR_DEBUG("Expected port '%d', actual '%d'",
                  data->port, uri->port);
        goto cleanup;
    }

    if (!STREQ_NULLABLE(uri->path, data->path)) {
        VIR_DEBUG("Expected path '%s', actual '%s'",
                  data->path, uri->path);
        goto cleanup;
    }

    if (!STREQ_NULLABLE(uri->query, data->query)) {
        VIR_DEBUG("Expected query '%s', actual '%s'",
                  data->query, uri->query);
        goto cleanup;
    }

    if (!STREQ_NULLABLE(uri->fragment, data->fragment)) {
        VIR_DEBUG("Expected fragment '%s', actual '%s'",
                  data->fragment, uri->fragment);
        goto cleanup;
    }

    for (i = 0 ; data->params && data->params[i].name && i < uri->paramsCount ; i++) {
        if (!STREQ_NULLABLE(data->params[i].name, uri->params[i].name)) {
            VIR_DEBUG("Expected param name %zu '%s', actual '%s'",
                      i, data->params[i].name, uri->params[i].name);
            goto cleanup;
        }
        if (!STREQ_NULLABLE(data->params[i].value, uri->params[i].value)) {
            VIR_DEBUG("Expected param value %zu '%s', actual '%s'",
                      i, data->params[i].value, uri->params[i].value);
            goto cleanup;
        }
    }
    if (data->params && data->params[i].name) {
        VIR_DEBUG("Missing parameter %zu %s=%s",
                  i, data->params[i].name, data->params[i].value);
        goto cleanup;
    }
    if (i != uri->paramsCount) {
        VIR_DEBUG("Unexpected parameter %zu %s=%s",
                  i, uri->params[i].name, uri->params[i].value);
        goto cleanup;
    }

    VIR_FREE(uri->query);
    uri->query = virURIFormatParams(uri);

    if (!(uristr = virURIFormat(uri)))
        goto cleanup;

    if (!STREQ(uristr, data->uri_out)) {
        VIR_DEBUG("URI did not roundtrip, expect '%s', actual '%s'",
                  data->uri_out, uristr);
        goto cleanup;
    }

    ret = 0;
cleanup:
    VIR_FREE(uristr);
    virURIFree(uri);
    return ret;
}


static int
mymain(void)
{
    int ret = 0;

    signal(SIGPIPE, SIG_IGN);

#define TEST_FULL(uri, uri_out, scheme, server, port, path, query,      \
                  fragment, params)                                     \
    do  {                                                               \
        const struct URIParseData data = {                              \
            uri, (uri_out) ? (uri_out) : (uri), scheme, server, port,   \
            path, query, fragment, params                               \
        };                                                              \
        if (virtTestRun("Test URI " # uri,  1, testURIParse, &data) < 0) \
            ret = -1;                                                   \
    } while (0)
#define TEST_PARSE(uri, scheme, server, port, path, query, fragment, params) \
    TEST_FULL(uri, NULL, scheme, server, port, path, query, fragment, params)
#define TEST_PARAMS(query_in, query_out, params)                        \
    TEST_FULL("test://example.com/?" query_in,                          \
              *query_out ? "test://example.com/?" query_out : NULL,     \
              "test", "example.com", 0, "/", query_in, NULL, params)

    virURIParam params[] = {
        { (char*)"name", (char*)"value" },
        { NULL, NULL },
    };

    TEST_PARSE("test://example.com", "test", "example.com", 0, NULL, NULL, NULL, NULL);
    TEST_PARSE("test://example.com:123", "test", "example.com", 123, NULL, NULL, NULL, NULL);
    TEST_PARSE("test://example.com:123/system?name=value#foo", "test", "example.com", 123, "/system", "name=value", "foo", params);
    TEST_PARSE("test://127.0.0.1:123/system", "test", "127.0.0.1", 123, "/system", NULL, NULL, NULL);
    TEST_PARSE("test://[::1]:123/system", "test", "::1", 123, "/system", NULL, NULL, NULL);
    TEST_PARSE("test://[2001:41c8:1:4fd4::2]:123/system", "test", "2001:41c8:1:4fd4::2", 123, "/system", NULL, NULL, NULL);

    virURIParam params1[] = {
        { (char*)"foo", (char*)"one" },
        { (char*)"bar", (char*)"two" },
        { NULL, NULL },
    };
    virURIParam params2[] = {
        { (char*)"foo", (char*)"one" },
        { (char*)"foo", (char*)"two" },
        { NULL, NULL },
    };
    virURIParam params3[] = {
        { (char*)"foo", (char*)"&one" },
        { (char*)"bar", (char*)"&two" },
        { NULL, NULL },
    };
    virURIParam params4[] = {
        { (char*)"foo", (char*)"" },
        { NULL, NULL },
    };
    virURIParam params5[] = {
        { (char*)"foo", (char*)"one two" },
        { NULL, NULL },
    };
    virURIParam params6[] = {
        { (char*)"foo", (char*)"one" },
        { NULL, NULL },
    };

    TEST_PARAMS("foo=one&bar=two", "", params1);
    TEST_PARAMS("foo=one&foo=two", "", params2);
    TEST_PARAMS("foo=one&&foo=two", "foo=one&foo=two", params2);
    TEST_PARAMS("foo=one;foo=two", "foo=one&foo=two", params2);
    TEST_PARAMS("foo=%26one&bar=%26two", "", params3);
    TEST_PARAMS("foo", "foo=", params4);
    TEST_PARAMS("foo=", "", params4);
    TEST_PARAMS("foo=&", "foo=", params4);
    TEST_PARAMS("foo=&&", "foo=", params4);
    TEST_PARAMS("foo=one%20two", "", params5);
    TEST_PARAMS("=bogus&foo=one", "foo=one", params6);

    return (ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

VIRT_TEST_MAIN(mymain)