| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/** |
| 3 |
* This file is part of libnvme. |
| 4 |
* Copyright (c) 2024 Tomas Bzatek <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <string.h> |
| 9 |
#include <stdbool.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
#include <ccan/array_size/array_size.h> |
| 13 |
|
| 14 |
#include <libnvme.h> |
| 15 |
#include <nvme/private.h> |
| 16 |
|
| 17 |
struct test_data { |
| 18 |
const char *uri; |
| 19 |
/* parsed data */ |
| 20 |
const char *scheme; |
| 21 |
const char *host; |
| 22 |
const char *user; |
| 23 |
const char *proto; |
| 24 |
int port; |
| 25 |
const char *path[7]; |
| 26 |
const char *query; |
| 27 |
const char *frag; |
| 28 |
}; |
| 29 |
|
| 30 |
static struct test_data test_data[] = { |
| 31 |
{ "nvme://192.168.1.1", "nvme", "192.168.1.1" }, |
| 32 |
{ "nvme://192.168.1.1/", "nvme", "192.168.1.1" }, |
| 33 |
{ "nvme://192.168.1.1:1234", "nvme", "192.168.1.1", .port = 1234 }, |
| 34 |
{ "nvme://192.168.1.1:1234/", "nvme", "192.168.1.1", .port = 1234 }, |
| 35 |
{ "nvme+tcp://192.168.1.1", "nvme", "192.168.1.1", .proto = "tcp" }, |
| 36 |
{ "nvme+rdma://192.168.1.1/", "nvme", "192.168.1.1", .proto = "rdma" }, |
| 37 |
{ "nvme+tcp://192.168.1.1:1234", |
| 38 |
"nvme", "192.168.1.1", .proto = "tcp", .port = 1234 }, |
| 39 |
{ "nvme+tcp://192.168.1.1:1234/", |
| 40 |
"nvme", "192.168.1.1", .proto = "tcp", .port = 1234 }, |
| 41 |
{ "nvme+tcp://192.168.1.1:4420/path", |
| 42 |
"nvme", "192.168.1.1", .proto = "tcp", .port = 4420, |
| 43 |
.path = { "path", NULL }}, |
| 44 |
{ "nvme+tcp://192.168.1.1/path/", |
| 45 |
"nvme", "192.168.1.1", .proto = "tcp", .path = { "path", NULL }}, |
| 46 |
{ "nvme+tcp://192.168.1.1:4420/p1/p2/p3", |
| 47 |
"nvme", "192.168.1.1", .proto = "tcp", .port = 4420, |
| 48 |
.path = { "p1", "p2", "p3", NULL }}, |
| 49 |
{ "nvme+tcp://192.168.1.1:4420/p1/p2/p3/", |
| 50 |
"nvme", "192.168.1.1", .proto = "tcp", .port = 4420, |
| 51 |
.path = { "p1", "p2", "p3", NULL }}, |
| 52 |
{ "nvme+tcp://192.168.1.1:4420//p1//p2/////p3", |
| 53 |
"nvme", "192.168.1.1", .proto = "tcp", .port = 4420, |
| 54 |
.path = { "p1", "p2", "p3", NULL }}, |
| 55 |
{ "nvme+tcp://192.168.1.1:4420//p1//p2/////p3/", |
| 56 |
"nvme", "192.168.1.1", .proto = "tcp", .port = 4420, |
| 57 |
.path = { "p1", "p2", "p3", NULL }}, |
| 58 |
{ "nvme://[fe80::1010]", "nvme", "fe80::1010" }, |
| 59 |
{ "nvme://[fe80::1010]/", "nvme", "fe80::1010" }, |
| 60 |
{ "nvme://[fe80::1010]:1234", "nvme", "fe80::1010", .port = 1234 }, |
| 61 |
{ "nvme://[fe80::1010]:1234/", "nvme", "fe80::1010", .port = 1234 }, |
| 62 |
{ "nvme+tcp://[fe80::1010]", "nvme", "fe80::1010", .proto = "tcp" }, |
| 63 |
{ "nvme+rdma://[fe80::1010]/", "nvme", "fe80::1010", .proto = "rdma" }, |
| 64 |
{ "nvme+tcp://[fe80::1010]:1234", |
| 65 |
"nvme", "fe80::1010", .proto = "tcp", .port = 1234 }, |
| 66 |
{ "nvme+tcp://[fe80::1010]:1234/", |
| 67 |
"nvme", "fe80::1010", .proto = "tcp", .port = 1234 }, |
| 68 |
{ "nvme+tcp://[fe80::1010]:4420/path", |
| 69 |
"nvme", "fe80::1010", .proto = "tcp", .port = 4420, |
| 70 |
.path = { "path", NULL }}, |
| 71 |
{ "nvme+tcp://[fe80::1010]/path/", |
| 72 |
"nvme", "fe80::1010", .proto = "tcp", .path = { "path", NULL }}, |
| 73 |
{ "nvme+tcp://[fe80::1010]:4420/p1/p2/p3", |
| 74 |
"nvme", "fe80::1010", .proto = "tcp", .port = 4420, |
| 75 |
.path = { "p1", "p2", "p3", NULL }}, |
| 76 |
{ "nvme+tcp://[fe80::fc7d:8cff:fe5b:962e]:666/p1/p2/p3/", |
| 77 |
"nvme", "fe80::fc7d:8cff:fe5b:962e", .proto = "tcp", .port = 666, |
| 78 |
.path = { "p1", "p2", "p3", NULL }}, |
| 79 |
{ "nvme://h?query", "nvme", "h", .query = "query" }, |
| 80 |
{ "nvme://h/?query", "nvme", "h", .query = "query" }, |
| 81 |
{ "nvme://h/x?query", |
| 82 |
"nvme", "h", .path = { "x" }, .query = "query" }, |
| 83 |
{ "nvme://h/p1/?query", |
| 84 |
"nvme", "h", .path = { "p1" }, .query = "query" }, |
| 85 |
{ "nvme://h/p1/x?query", |
| 86 |
"nvme", "h", .path = { "p1", "x" }, .query = "query" }, |
| 87 |
{ "nvme://h#fragment", "nvme", "h", .frag = "fragment" }, |
| 88 |
{ "nvme://h/#fragment", "nvme", "h", .frag = "fragment" }, |
| 89 |
{ "nvme://h/x#fragment", |
| 90 |
"nvme", "h", .path = { "x" }, .frag = "fragment" }, |
| 91 |
{ "nvme://h/p1/#fragment", |
| 92 |
"nvme", "h", .path = { "p1" }, .frag = "fragment" }, |
| 93 |
{ "nvme://h/p1/x#fragment", |
| 94 |
"nvme", "h", .path = { "p1", "x" }, .frag = "fragment" }, |
| 95 |
{ "nvme://h/?query#fragment", |
| 96 |
"nvme", "h", .query = "query", .frag = "fragment" }, |
| 97 |
{ "nvme://h/x?query#fragment", |
| 98 |
"nvme", "h", .path = { "x" }, .query = "query", .frag = "fragment" }, |
| 99 |
{ "nvme://h/p1/?query#fragment", |
| 100 |
"nvme", "h", .path = { "p1" }, .query = "query", .frag = "fragment" }, |
| 101 |
{ "nvme://h/p1/x?query#fragment", |
| 102 |
"nvme", "h", .path = { "p1", "x" }, .query = "query", |
| 103 |
.frag = "fragment" }, |
| 104 |
{ "nvme://h/#fragment?query", |
| 105 |
"nvme", "h", .frag = "fragment?query" }, |
| 106 |
{ "nvme://h/x#fragment?query", |
| 107 |
"nvme", "h", .path = { "x" }, .frag = "fragment?query" }, |
| 108 |
{ "nvme://h/p1/#fragment?query", |
| 109 |
"nvme", "h", .path = { "p1" }, .frag = "fragment?query" }, |
| 110 |
{ "nvme://h/p1/x#fragment?query", |
| 111 |
"nvme", "h", .path = { "p1", "x" }, .frag = "fragment?query" }, |
| 112 |
{ "nvme://user@h", "nvme", "h", .user = "user" }, |
| 113 |
{ "nvme://user@h/", "nvme", "h", .user = "user" }, |
| 114 |
{ "nvme://user:pass@h/", "nvme", "h", .user = "user:pass" }, |
| 115 |
{ "nvme://[fe80::1010]@h/", "nvme", "h", .user = "[fe80::1010]" }, |
| 116 |
{ "nvme://u[fe80::1010]@h/", "nvme", "h", .user = "u[fe80::1010]" }, |
| 117 |
{ "nvme://u[aa:bb::cc]@h/", "nvme", "h", .user = "u[aa:bb::cc]" }, |
| 118 |
{ "nvme+rdma://u[aa:bb::cc]@[aa:bb::cc]:12345/p1/x?q=val#fr", |
| 119 |
"nvme", "aa:bb::cc", .proto = "rdma", .port = 12345, |
| 120 |
.user = "u[aa:bb::cc]", .path = { "p1", "x" }, |
| 121 |
.query = "q=val", .frag = "fr" }, |
| 122 |
{ "nvme://ex%5Cmp%3Ae", "nvme", "ex\\mp:e" }, |
| 123 |
{ "nvme://ex%5Cmp%3Ae.com/", "nvme", "ex\\mp:e.com" }, |
| 124 |
{ "nvme://u%24er@ex%5Cmp%3Ae.com/", "nvme", "ex\\mp:e.com", |
| 125 |
.user = "u$er" }, |
| 126 |
{ "nvme+tcp://ex%5Cmp%3Ae.com:1234", |
| 127 |
"nvme", "ex\\mp:e.com", .proto = "tcp", .port = 1234 }, |
| 128 |
{ "nvme+tcp://ex%5Cmp%3Ae.com:1234/p1/ex%3Camp%3Ele/p3", |
| 129 |
"nvme", "ex\\mp:e.com", .proto = "tcp", .port = 1234, |
| 130 |
.path = { "p1", "ex<amp>le", "p3", NULL } }, |
| 131 |
{ "nvme+tcp://ex%5Cmp%3Ae.com:1234/p1/%3C%3E/p3?q%5E%24ry#fr%26gm%23nt", |
| 132 |
"nvme", "ex\\mp:e.com", .proto = "tcp", .port = 1234, |
| 133 |
.path = { "p1", "<>", "p3", NULL }, .query = "q^$ry", |
| 134 |
.frag = "fr&gm#nt" }, |
| 135 |
}; |
| 136 |
|
| 137 |
const char *test_data_bad[] = { |
| 138 |
"", |
| 139 |
" ", |
| 140 |
"nonsense", |
| 141 |
"vnme:", |
| 142 |
"vnme:/", |
| 143 |
"vnme://", |
| 144 |
"vnme:///", |
| 145 |
"vnme+foo://", |
| 146 |
"nvme:hostname/", |
| 147 |
"nvme:/hostname/", |
| 148 |
"nvme:///hostname/", |
| 149 |
"nvme+foo:///hostname/", |
| 150 |
}; |
| 151 |
|
| 152 |
static void test_uriparser(void) |
| 153 |
{ |
| 154 |
printf("Testing URI parser:\n"); |
| 155 |
for (int i = 0; i < ARRAY_SIZE(test_data); i++) { |
| 156 |
const struct test_data *d = &test_data[i]; |
| 157 |
struct nvme_fabrics_uri *parsed_data; |
| 158 |
char **s; |
| 159 |
int i; |
| 160 |
|
| 161 |
printf(" '%s'...", d->uri); |
| 162 |
parsed_data = nvme_parse_uri(d->uri); |
| 163 |
assert(parsed_data); |
| 164 |
|
| 165 |
assert(strcmp(d->scheme, parsed_data->scheme) == 0); |
| 166 |
if (d->proto) { |
| 167 |
assert(parsed_data->protocol != NULL); |
| 168 |
assert(strcmp(d->proto, parsed_data->protocol) == 0); |
| 169 |
} else |
| 170 |
assert(d->proto == parsed_data->protocol); |
| 171 |
assert(strcmp(d->host, parsed_data->host) == 0); |
| 172 |
assert(d->port == parsed_data->port); |
| 173 |
|
| 174 |
if (!parsed_data->path_segments) |
| 175 |
assert(d->path[0] == NULL); |
| 176 |
else { |
| 177 |
for (i = 0, s = parsed_data->path_segments; |
| 178 |
s && *s; s++, i++) { |
| 179 |
assert(d->path[i] != NULL); |
| 180 |
assert(strcmp(d->path[i], *s) == 0); |
| 181 |
} |
| 182 |
/* trailing NULL element */ |
| 183 |
assert(d->path[i] == parsed_data->path_segments[i]); |
| 184 |
} |
| 185 |
if (d->query) { |
| 186 |
assert(parsed_data->query != NULL); |
| 187 |
assert(strcmp(d->query, parsed_data->query) == 0); |
| 188 |
} else |
| 189 |
assert(d->query == parsed_data->query); |
| 190 |
if (d->frag) { |
| 191 |
assert(parsed_data->fragment != NULL); |
| 192 |
assert(strcmp(d->frag, parsed_data->fragment) == 0); |
| 193 |
} else |
| 194 |
assert(d->frag == parsed_data->fragment); |
| 195 |
nvme_free_uri(parsed_data); |
| 196 |
printf(" OK\n"); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
static void test_uriparser_bad(void) |
| 201 |
{ |
| 202 |
printf("Testing malformed URI strings:\n"); |
| 203 |
for (int i = 0; i < ARRAY_SIZE(test_data_bad); i++) { |
| 204 |
struct nvme_fabrics_uri *parsed_data; |
| 205 |
|
| 206 |
printf(" '%s'...", test_data_bad[i]); |
| 207 |
parsed_data = nvme_parse_uri(test_data_bad[i]); |
| 208 |
assert(parsed_data == NULL); |
| 209 |
printf(" OK\n"); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
int main(int argc, char *argv[]) |
| 214 |
{ |
| 215 |
test_uriparser(); |
| 216 |
test_uriparser_bad(); |
| 217 |
|
| 218 |
fflush(stdout); |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |