| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "dns-domain.h" |
| 5 |
#include "in-addr-util.h" |
| 6 |
#include "string-util.h" |
| 7 |
#include "tests.h" |
| 8 |
|
| 9 |
static void test_dns_label_unescape_one(const char *what, const char *expect, size_t buffer_sz, int ret, int ret_ldh) { |
| 10 |
char buffer[buffer_sz]; |
| 11 |
int r; |
| 12 |
const char *w = what; |
| 13 |
|
| 14 |
log_info("%s, %s, %zu, →%d/%d", what, expect, buffer_sz, ret, ret_ldh); |
| 15 |
|
| 16 |
r = dns_label_unescape(&w, buffer, buffer_sz, 0); |
| 17 |
assert_se(r == ret); |
| 18 |
if (r >= 0) |
| 19 |
ASSERT_STREQ(buffer, expect); |
| 20 |
|
| 21 |
w = what; |
| 22 |
r = dns_label_unescape(&w, buffer, buffer_sz, DNS_LABEL_LDH); |
| 23 |
assert_se(r == ret_ldh); |
| 24 |
if (r >= 0) |
| 25 |
ASSERT_STREQ(buffer, expect); |
| 26 |
|
| 27 |
w = what; |
| 28 |
r = dns_label_unescape(&w, buffer, buffer_sz, DNS_LABEL_NO_ESCAPES); |
| 29 |
const int ret_noe = strchr(what, '\\') ? -EINVAL : ret; |
| 30 |
assert_se(r == ret_noe); |
| 31 |
if (r >= 0) |
| 32 |
ASSERT_STREQ(buffer, expect); |
| 33 |
} |
| 34 |
|
| 35 |
TEST(dns_label_unescape) { |
| 36 |
test_dns_label_unescape_one("hallo", "hallo", 6, 5, 5); |
| 37 |
test_dns_label_unescape_one("hallo", "hallo", 4, -ENOBUFS, -ENOBUFS); |
| 38 |
test_dns_label_unescape_one("", "", 10, 0, 0); |
| 39 |
test_dns_label_unescape_one("hallo\\.foobar", "hallo.foobar", 20, 12, -EINVAL); |
| 40 |
test_dns_label_unescape_one("hallo.foobar", "hallo", 10, 5, 5); |
| 41 |
test_dns_label_unescape_one("hallo\n.foobar", "hallo", 20, -EINVAL, -EINVAL); |
| 42 |
test_dns_label_unescape_one("hallo\\", "hallo", 20, -EINVAL, -EINVAL); |
| 43 |
test_dns_label_unescape_one("hallo\\032 ", "hallo ", 20, 7, -EINVAL); |
| 44 |
test_dns_label_unescape_one(".", "", 20, 0, 0); |
| 45 |
test_dns_label_unescape_one("..", "", 20, -EINVAL, -EINVAL); |
| 46 |
test_dns_label_unescape_one(".foobar", "", 20, -EINVAL, -EINVAL); |
| 47 |
test_dns_label_unescape_one("foobar.", "foobar", 20, 6, 6); |
| 48 |
test_dns_label_unescape_one("foobar..", "foobar", 20, -EINVAL, -EINVAL); |
| 49 |
test_dns_label_unescape_one("foo-bar", "foo-bar", 20, 7, 7); |
| 50 |
test_dns_label_unescape_one("foo-", "foo-", 20, 4, -EINVAL); |
| 51 |
test_dns_label_unescape_one("-foo", "-foo", 20, 4, -EINVAL); |
| 52 |
test_dns_label_unescape_one("-foo-", "-foo-", 20, 5, -EINVAL); |
| 53 |
test_dns_label_unescape_one("foo-.", "foo-", 20, 4, -EINVAL); |
| 54 |
test_dns_label_unescape_one("foo.-", "foo", 20, 3, 3); |
| 55 |
test_dns_label_unescape_one("foo\\032", "foo ", 20, 4, -EINVAL); |
| 56 |
test_dns_label_unescape_one("foo\\045", "foo-", 20, 4, -EINVAL); |
| 57 |
test_dns_label_unescape_one("głąb", "głąb", 20, 6, -EINVAL); |
| 58 |
} |
| 59 |
|
| 60 |
static void test_dns_name_to_wire_format_one(const char *what, const char *expect, size_t buffer_sz, int ret) { |
| 61 |
uint8_t buffer[buffer_sz]; |
| 62 |
int r; |
| 63 |
|
| 64 |
log_info("%s, %s, %zu, →%d", what, strnull(expect), buffer_sz, ret); |
| 65 |
|
| 66 |
r = dns_name_to_wire_format(what, buffer, buffer_sz, false); |
| 67 |
assert_se(r == ret); |
| 68 |
|
| 69 |
if (r >= 0) { |
| 70 |
assert(expect); /* for gcc */ |
| 71 |
assert_se(memcmp(buffer, expect, r) == 0); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
TEST(dns_name_to_wire_format) { |
| 76 |
static const char out0[] = { 0 }; |
| 77 |
static const char out1[] = { 3, 'f', 'o', 'o', 0 }; |
| 78 |
static const char out2[] = { 5, 'h', 'a', 'l', 'l', 'o', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 }; |
| 79 |
static const char out3[] = { 4, ' ', 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 }; |
| 80 |
static const char out4[] = { 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 81 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 82 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 83 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 84 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 85 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 86 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 87 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 88 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 89 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 90 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 91 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 92 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 93 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 94 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 95 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 96 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 97 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 98 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 99 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 100 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 101 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 102 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 103 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 104 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 105 |
3, 'a', '1', '2', 0 }; |
| 106 |
|
| 107 |
test_dns_name_to_wire_format_one("", out0, sizeof(out0), sizeof(out0)); |
| 108 |
|
| 109 |
test_dns_name_to_wire_format_one("foo", out1, sizeof(out1), sizeof(out1)); |
| 110 |
test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) + 1, sizeof(out1)); |
| 111 |
test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) - 1, -ENOBUFS); |
| 112 |
|
| 113 |
test_dns_name_to_wire_format_one("hallo.foo.bar", out2, sizeof(out2), sizeof(out2)); |
| 114 |
test_dns_name_to_wire_format_one("hallo.foo..bar", NULL, 32, -EINVAL); |
| 115 |
|
| 116 |
test_dns_name_to_wire_format_one("\\032foo.bar", out3, sizeof(out3), sizeof(out3)); |
| 117 |
|
| 118 |
test_dns_name_to_wire_format_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a123", NULL, 500, -EINVAL); |
| 119 |
test_dns_name_to_wire_format_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12", out4, sizeof(out4), sizeof(out4)); |
| 120 |
} |
| 121 |
|
| 122 |
static void test_dns_name_from_wire_format_one(const char *expect, const uint8_t *what, size_t len, int ret) { |
| 123 |
_cleanup_free_ char *name = NULL; |
| 124 |
int r; |
| 125 |
|
| 126 |
log_info("%s, %s, %zu, →%d", what, strnull(expect), len, ret); |
| 127 |
|
| 128 |
r = dns_name_from_wire_format(&what, &len, &name); |
| 129 |
assert_se(r == ret); |
| 130 |
|
| 131 |
if (r >= 0) { |
| 132 |
assert(expect); /* for gcc */ |
| 133 |
assert_se(memcmp(name, expect, r) == 0); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
TEST(dns_name_from_wire_format) { |
| 138 |
static const uint8_t in0[] = { 0 }; |
| 139 |
static const uint8_t in1[] = { 3, 'f', 'o', 'o', 0 }; |
| 140 |
static const uint8_t in2[] = { 5, 'h', 'a', 'l', 'l', 'o', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 }; |
| 141 |
static const uint8_t in2_1[] = { 5, 'h', 'a', 'l', 'l', 'o', 3, 'f', 'o', 'o', 0, 'b', 'a', 'r', 0 }; |
| 142 |
static const uint8_t in3[] = { 4, ' ', 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 }; |
| 143 |
static const uint8_t in4[] = { 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 144 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 145 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 146 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 147 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 148 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 149 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 150 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 151 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 152 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 153 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 154 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 155 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 156 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 157 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 158 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 159 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 160 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 161 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 162 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 163 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 164 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 165 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 166 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 167 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 168 |
3, 'a', '1', '2', 0 }; /* 255 octets */ |
| 169 |
static const uint8_t in5[] = { 9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 170 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 171 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 172 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 173 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 174 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 175 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 176 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 177 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 178 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 179 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 180 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 181 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 182 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 183 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 184 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 185 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 186 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 187 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 188 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 189 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 190 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 191 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 192 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 193 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 194 |
9, 'a', '1', '2', '3', '4', '5', '6', '7', '8', |
| 195 |
3, 'a', '1', '2', 0 }; /* 265 octets */ |
| 196 |
|
| 197 |
test_dns_name_from_wire_format_one("", in0, sizeof(in0), strlen("")); |
| 198 |
|
| 199 |
test_dns_name_from_wire_format_one("foo", in1, sizeof(in1), strlen("foo")); |
| 200 |
test_dns_name_from_wire_format_one("foo", in1, sizeof(in1) - 1, strlen("foo")); |
| 201 |
|
| 202 |
test_dns_name_from_wire_format_one("hallo.foo.bar", in2, sizeof(in2), strlen("hallo.foo.bar")); |
| 203 |
test_dns_name_from_wire_format_one("hallo.foo", in2_1, sizeof(in2_1), strlen("hallo.foo")); |
| 204 |
|
| 205 |
test_dns_name_from_wire_format_one("\\032foo.bar", in3, sizeof(in3), strlen("\\032foo.bar")); |
| 206 |
|
| 207 |
test_dns_name_from_wire_format_one(NULL, in5, sizeof(in5), -EMSGSIZE); |
| 208 |
test_dns_name_from_wire_format_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12", in4, sizeof(in4), 253); |
| 209 |
} |
| 210 |
|
| 211 |
static void test_dns_label_unescape_suffix_one(const char *what, const char *expect1, const char *expect2, size_t buffer_sz, int ret1, int ret2) { |
| 212 |
char buffer[buffer_sz]; |
| 213 |
const char *label; |
| 214 |
int r; |
| 215 |
|
| 216 |
log_info("%s, %s, %s, %zu, %d, %d", what, expect1, expect2, buffer_sz, ret1, ret2); |
| 217 |
|
| 218 |
label = what + strlen(what); |
| 219 |
|
| 220 |
r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz); |
| 221 |
assert_se(r == ret1); |
| 222 |
if (r >= 0) |
| 223 |
ASSERT_STREQ(buffer, expect1); |
| 224 |
|
| 225 |
r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz); |
| 226 |
assert_se(r == ret2); |
| 227 |
if (r >= 0) |
| 228 |
ASSERT_STREQ(buffer, expect2); |
| 229 |
} |
| 230 |
|
| 231 |
TEST(dns_label_unescape_suffix) { |
| 232 |
test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0); |
| 233 |
test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOBUFS, -ENOBUFS); |
| 234 |
test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0); |
| 235 |
test_dns_label_unescape_suffix_one("hallo\\.foobar", "hallo.foobar", "", 20, 12, 0); |
| 236 |
test_dns_label_unescape_suffix_one("hallo.foobar", "foobar", "hallo", 10, 6, 5); |
| 237 |
test_dns_label_unescape_suffix_one("hallo.foobar\n", "foobar", "foobar", 20, -EINVAL, -EINVAL); |
| 238 |
test_dns_label_unescape_suffix_one("hallo\\", "hallo", "hallo", 20, -EINVAL, -EINVAL); |
| 239 |
test_dns_label_unescape_suffix_one("hallo\\032 ", "hallo ", "", 20, 7, 0); |
| 240 |
test_dns_label_unescape_suffix_one(".", "", "", 20, 0, 0); |
| 241 |
test_dns_label_unescape_suffix_one("..", "", "", 20, 0, -EINVAL); |
| 242 |
test_dns_label_unescape_suffix_one(".foobar", "foobar", "", 20, 6, -EINVAL); |
| 243 |
test_dns_label_unescape_suffix_one("foobar.", "foobar", "", 20, 6, 0); |
| 244 |
test_dns_label_unescape_suffix_one("foo\\\\bar", "foo\\bar", "", 20, 7, 0); |
| 245 |
test_dns_label_unescape_suffix_one("foo.bar", "bar", "foo", 20, 3, 3); |
| 246 |
test_dns_label_unescape_suffix_one("foo..bar", "bar", "", 20, 3, -EINVAL); |
| 247 |
test_dns_label_unescape_suffix_one("foo...bar", "bar", "", 20, 3, -EINVAL); |
| 248 |
test_dns_label_unescape_suffix_one("foo\\.bar", "foo.bar", "", 20, 7, 0); |
| 249 |
test_dns_label_unescape_suffix_one("foo\\\\.bar", "bar", "foo\\", 20, 3, 4); |
| 250 |
test_dns_label_unescape_suffix_one("foo\\\\\\.bar", "foo\\.bar", "", 20, 8, 0); |
| 251 |
} |
| 252 |
|
| 253 |
static void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) { |
| 254 |
_cleanup_free_ char *t = NULL; |
| 255 |
int r; |
| 256 |
|
| 257 |
log_info("%s, %zu, %s, →%d", what, l, strnull(expect), ret); |
| 258 |
|
| 259 |
r = dns_label_escape_new(what, l, &t); |
| 260 |
assert_se(r == ret); |
| 261 |
|
| 262 |
if (r < 0) |
| 263 |
return; |
| 264 |
|
| 265 |
ASSERT_STREQ(expect, t); |
| 266 |
} |
| 267 |
|
| 268 |
TEST(dns_label_escape) { |
| 269 |
test_dns_label_escape_one("", 0, NULL, -EINVAL); |
| 270 |
test_dns_label_escape_one("hallo", 5, "hallo", 5); |
| 271 |
test_dns_label_escape_one("hallo", 6, "hallo\\000", 9); |
| 272 |
test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31); |
| 273 |
} |
| 274 |
|
| 275 |
static void test_dns_name_normalize_one(const char *what, const char *expect, int ret) { |
| 276 |
_cleanup_free_ char *t = NULL; |
| 277 |
int r; |
| 278 |
|
| 279 |
r = dns_name_normalize(what, 0, &t); |
| 280 |
assert_se(r == ret); |
| 281 |
|
| 282 |
if (r < 0) |
| 283 |
return; |
| 284 |
|
| 285 |
ASSERT_STREQ(expect, t); |
| 286 |
} |
| 287 |
|
| 288 |
TEST(dns_name_normalize) { |
| 289 |
test_dns_name_normalize_one("", ".", 0); |
| 290 |
test_dns_name_normalize_one("f", "f", 0); |
| 291 |
test_dns_name_normalize_one("f.waldi", "f.waldi", 0); |
| 292 |
test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0); |
| 293 |
test_dns_name_normalize_one("\\000", "\\000", 0); |
| 294 |
test_dns_name_normalize_one("..", NULL, -EINVAL); |
| 295 |
test_dns_name_normalize_one(".foobar", NULL, -EINVAL); |
| 296 |
test_dns_name_normalize_one("foobar.", "foobar", 0); |
| 297 |
test_dns_name_normalize_one(".", ".", 0); |
| 298 |
} |
| 299 |
|
| 300 |
static void test_dns_name_equal_one(const char *a, const char *b, int ret) { |
| 301 |
int r; |
| 302 |
|
| 303 |
r = dns_name_equal(a, b); |
| 304 |
assert_se(r == ret); |
| 305 |
|
| 306 |
r = dns_name_equal(b, a); |
| 307 |
assert_se(r == ret); |
| 308 |
} |
| 309 |
|
| 310 |
TEST(dns_name_equal) { |
| 311 |
test_dns_name_equal_one("", "", true); |
| 312 |
test_dns_name_equal_one("x", "x", true); |
| 313 |
test_dns_name_equal_one("x", "x.", true); |
| 314 |
test_dns_name_equal_one("abc.def", "abc.def", true); |
| 315 |
test_dns_name_equal_one("abc.def", "ABC.def", true); |
| 316 |
test_dns_name_equal_one("abc.def", "CBA.def", false); |
| 317 |
test_dns_name_equal_one("", "xxx", false); |
| 318 |
test_dns_name_equal_one("ab", "a", false); |
| 319 |
test_dns_name_equal_one("\\000", "\\000", true); |
| 320 |
test_dns_name_equal_one(".", "", true); |
| 321 |
test_dns_name_equal_one(".", ".", true); |
| 322 |
test_dns_name_equal_one("..", "..", -EINVAL); |
| 323 |
} |
| 324 |
|
| 325 |
static void test_dns_name_between_one(const char *a, const char *b, const char *c, int ret) { |
| 326 |
int r; |
| 327 |
|
| 328 |
r = dns_name_between(a, b, c); |
| 329 |
assert_se(r == ret); |
| 330 |
|
| 331 |
r = dns_name_between(c, b, a); |
| 332 |
if (ret >= 0) |
| 333 |
assert_se(r == 0 || dns_name_equal(a, c) > 0); |
| 334 |
else |
| 335 |
assert_se(r == ret); |
| 336 |
} |
| 337 |
|
| 338 |
TEST(dns_name_between) { |
| 339 |
/* see https://tools.ietf.org/html/rfc4034#section-6.1 |
| 340 |
Note that we use "\033.z.example" in stead of "\001.z.example" as we |
| 341 |
consider the latter invalid */ |
| 342 |
test_dns_name_between_one("example", "a.example", "yljkjljk.a.example", true); |
| 343 |
test_dns_name_between_one("a.example", "yljkjljk.a.example", "Z.a.example", true); |
| 344 |
test_dns_name_between_one("yljkjljk.a.example", "Z.a.example", "zABC.a.EXAMPLE", true); |
| 345 |
test_dns_name_between_one("Z.a.example", "zABC.a.EXAMPLE", "z.example", true); |
| 346 |
test_dns_name_between_one("zABC.a.EXAMPLE", "z.example", "\\033.z.example", true); |
| 347 |
test_dns_name_between_one("z.example", "\\033.z.example", "*.z.example", true); |
| 348 |
test_dns_name_between_one("\\033.z.example", "*.z.example", "\\200.z.example", true); |
| 349 |
test_dns_name_between_one("*.z.example", "\\200.z.example", "example", true); |
| 350 |
test_dns_name_between_one("\\200.z.example", "example", "a.example", true); |
| 351 |
|
| 352 |
test_dns_name_between_one("example", "a.example", "example", true); |
| 353 |
test_dns_name_between_one("example", "example", "example", false); |
| 354 |
test_dns_name_between_one("example", "example", "yljkjljk.a.example", false); |
| 355 |
test_dns_name_between_one("example", "yljkjljk.a.example", "yljkjljk.a.example", false); |
| 356 |
test_dns_name_between_one("hkps.pool.sks-keyservers.net", "_pgpkey-https._tcp.hkps.pool.sks-keyservers.net", "ipv4.pool.sks-keyservers.net", true); |
| 357 |
} |
| 358 |
|
| 359 |
static void test_dns_name_endswith_one(const char *a, const char *b, int ret) { |
| 360 |
assert_se(dns_name_endswith(a, b) == ret); |
| 361 |
} |
| 362 |
|
| 363 |
TEST(dns_name_endswith) { |
| 364 |
test_dns_name_endswith_one("", "", true); |
| 365 |
test_dns_name_endswith_one("", "xxx", false); |
| 366 |
test_dns_name_endswith_one("xxx", "", true); |
| 367 |
test_dns_name_endswith_one("x", "x", true); |
| 368 |
test_dns_name_endswith_one("x", "y", false); |
| 369 |
test_dns_name_endswith_one("x.y", "y", true); |
| 370 |
test_dns_name_endswith_one("x.y", "Y", true); |
| 371 |
test_dns_name_endswith_one("x.y", "x", false); |
| 372 |
test_dns_name_endswith_one("x.y.z", "Z", true); |
| 373 |
test_dns_name_endswith_one("x.y.z", "y.Z", true); |
| 374 |
test_dns_name_endswith_one("x.y.z", "x.y.Z", true); |
| 375 |
test_dns_name_endswith_one("x.y.z", "waldo", false); |
| 376 |
test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false); |
| 377 |
test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true); |
| 378 |
test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL); |
| 379 |
} |
| 380 |
|
| 381 |
static void test_dns_name_startswith_one(const char *a, const char *b, int ret) { |
| 382 |
assert_se(dns_name_startswith(a, b) == ret); |
| 383 |
} |
| 384 |
|
| 385 |
TEST(dns_name_startswith) { |
| 386 |
test_dns_name_startswith_one("", "", true); |
| 387 |
test_dns_name_startswith_one("", "xxx", false); |
| 388 |
test_dns_name_startswith_one("xxx", "", true); |
| 389 |
test_dns_name_startswith_one("x", "x", true); |
| 390 |
test_dns_name_startswith_one("x", "y", false); |
| 391 |
test_dns_name_startswith_one("x.y", "x.y", true); |
| 392 |
test_dns_name_startswith_one("x.y", "y.x", false); |
| 393 |
test_dns_name_startswith_one("x.y", "x", true); |
| 394 |
test_dns_name_startswith_one("x.y", "X", true); |
| 395 |
test_dns_name_startswith_one("x.y", "y", false); |
| 396 |
test_dns_name_startswith_one("x.y", "", true); |
| 397 |
test_dns_name_startswith_one("x.y", "X", true); |
| 398 |
} |
| 399 |
|
| 400 |
TEST(dns_name_is_root) { |
| 401 |
assert_se(dns_name_is_root("")); |
| 402 |
assert_se(dns_name_is_root(".")); |
| 403 |
assert_se(!dns_name_is_root("xxx")); |
| 404 |
assert_se(!dns_name_is_root("xxx.")); |
| 405 |
assert_se(!dns_name_is_root("..")); |
| 406 |
} |
| 407 |
|
| 408 |
TEST(dns_name_is_single_label) { |
| 409 |
assert_se(!dns_name_is_single_label("")); |
| 410 |
assert_se(!dns_name_is_single_label(".")); |
| 411 |
assert_se(!dns_name_is_single_label("..")); |
| 412 |
assert_se(dns_name_is_single_label("x")); |
| 413 |
assert_se(dns_name_is_single_label("x.")); |
| 414 |
assert_se(!dns_name_is_single_label("xx.yy")); |
| 415 |
} |
| 416 |
|
| 417 |
static void test_dns_name_reverse_one(const char *address, const char *name) { |
| 418 |
_cleanup_free_ char *p = NULL; |
| 419 |
union in_addr_union a, b = {}; |
| 420 |
int familya, familyb; |
| 421 |
|
| 422 |
assert_se(in_addr_from_string_auto(address, &familya, &a) >= 0); |
| 423 |
assert_se(dns_name_reverse(familya, &a, &p) >= 0); |
| 424 |
ASSERT_STREQ(p, name); |
| 425 |
assert_se(dns_name_address(p, &familyb, &b) > 0); |
| 426 |
assert_se(familya == familyb); |
| 427 |
assert_se(in_addr_equal(familya, &a, &b)); |
| 428 |
} |
| 429 |
|
| 430 |
TEST(dns_name_reverse) { |
| 431 |
test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa"); |
| 432 |
test_dns_name_reverse_one("fe80::47", "7.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa"); |
| 433 |
test_dns_name_reverse_one("127.0.0.1", "1.0.0.127.in-addr.arpa"); |
| 434 |
test_dns_name_reverse_one("::1", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa"); |
| 435 |
} |
| 436 |
|
| 437 |
static void test_dns_name_concat_one(const char *a, const char *b, int r, const char *result) { |
| 438 |
_cleanup_free_ char *p = NULL; |
| 439 |
|
| 440 |
assert_se(dns_name_concat(a, b, 0, &p) == r); |
| 441 |
ASSERT_STREQ(p, result); |
| 442 |
} |
| 443 |
|
| 444 |
TEST(dns_name_concat) { |
| 445 |
test_dns_name_concat_one("", "", 0, "."); |
| 446 |
test_dns_name_concat_one(".", "", 0, "."); |
| 447 |
test_dns_name_concat_one("", ".", 0, "."); |
| 448 |
test_dns_name_concat_one(".", ".", 0, "."); |
| 449 |
test_dns_name_concat_one("foo", "bar", 0, "foo.bar"); |
| 450 |
test_dns_name_concat_one("foo.foo", "bar.bar", 0, "foo.foo.bar.bar"); |
| 451 |
test_dns_name_concat_one("foo", NULL, 0, "foo"); |
| 452 |
test_dns_name_concat_one("foo", ".", 0, "foo"); |
| 453 |
test_dns_name_concat_one("foo.", "bar.", 0, "foo.bar"); |
| 454 |
test_dns_name_concat_one(NULL, NULL, 0, "."); |
| 455 |
test_dns_name_concat_one(NULL, ".", 0, "."); |
| 456 |
test_dns_name_concat_one(NULL, "foo", 0, "foo"); |
| 457 |
} |
| 458 |
|
| 459 |
static void test_dns_name_is_valid_one(const char *s, int ret, int ret_ldh) { |
| 460 |
log_info("%s, →%d", s, ret); |
| 461 |
|
| 462 |
assert_se(dns_name_is_valid(s) == ret); |
| 463 |
assert_se(dns_name_is_valid_ldh(s) == ret_ldh); |
| 464 |
} |
| 465 |
|
| 466 |
TEST(dns_name_is_valid) { |
| 467 |
test_dns_name_is_valid_one("[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[._qotd._tcp.local", 1, 0); |
| 468 |
test_dns_name_is_valid_one("[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]._qotd._tcp.local", 0, 0); |
| 469 |
|
| 470 |
test_dns_name_is_valid_one("foo", 1, 1); |
| 471 |
test_dns_name_is_valid_one("foo.", 1, 1); |
| 472 |
test_dns_name_is_valid_one("foo..", 0, 0); |
| 473 |
test_dns_name_is_valid_one("Foo", 1, 1); |
| 474 |
test_dns_name_is_valid_one("foo.bar", 1, 1); |
| 475 |
test_dns_name_is_valid_one("foo.bar.baz", 1, 1); |
| 476 |
test_dns_name_is_valid_one("", 1, 1); |
| 477 |
test_dns_name_is_valid_one("foo..bar", 0, 0); |
| 478 |
test_dns_name_is_valid_one(".foo.bar", 0, 0); |
| 479 |
test_dns_name_is_valid_one("foo.bar.", 1, 1); |
| 480 |
test_dns_name_is_valid_one("foo.bar..", 0, 0); |
| 481 |
test_dns_name_is_valid_one("\\zbar", 0, 0); |
| 482 |
test_dns_name_is_valid_one("ä", 1, 0); |
| 483 |
test_dns_name_is_valid_one("\n", 0, 0); |
| 484 |
|
| 485 |
test_dns_name_is_valid_one("dash-", 1, 0); |
| 486 |
test_dns_name_is_valid_one("-dash", 1, 0); |
| 487 |
test_dns_name_is_valid_one("dash-dash", 1, 1); |
| 488 |
test_dns_name_is_valid_one("foo.dash-", 1, 0); |
| 489 |
test_dns_name_is_valid_one("foo.-dash", 1, 0); |
| 490 |
test_dns_name_is_valid_one("foo.dash-dash", 1, 1); |
| 491 |
test_dns_name_is_valid_one("foo.dash-.bar", 1, 0); |
| 492 |
test_dns_name_is_valid_one("foo.-dash.bar", 1, 0); |
| 493 |
test_dns_name_is_valid_one("foo.dash-dash.bar", 1, 1); |
| 494 |
test_dns_name_is_valid_one("dash-.bar", 1, 0); |
| 495 |
test_dns_name_is_valid_one("-dash.bar", 1, 0); |
| 496 |
test_dns_name_is_valid_one("dash-dash.bar", 1, 1); |
| 497 |
test_dns_name_is_valid_one("-.bar", 1, 0); |
| 498 |
test_dns_name_is_valid_one("foo.-", 1, 0); |
| 499 |
|
| 500 |
/* 256 characters */ |
| 501 |
test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345", 0, 0); |
| 502 |
|
| 503 |
/* 255 characters */ |
| 504 |
test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a1234", 0, 0); |
| 505 |
|
| 506 |
/* 254 characters */ |
| 507 |
test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a123", 0, 0); |
| 508 |
|
| 509 |
/* 253 characters */ |
| 510 |
test_dns_name_is_valid_one("a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12345678.a12", 1, 1); |
| 511 |
|
| 512 |
/* label of 64 chars length */ |
| 513 |
test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a123", 0, 0); |
| 514 |
|
| 515 |
/* label of 63 chars length */ |
| 516 |
test_dns_name_is_valid_one("a123456789a123456789a123456789a123456789a123456789a123456789a12", 1, 1); |
| 517 |
} |
| 518 |
|
| 519 |
TEST(dns_service_name_is_valid) { |
| 520 |
assert_se(dns_service_name_is_valid("Lennart's Compüter")); |
| 521 |
assert_se(dns_service_name_is_valid("piff.paff")); |
| 522 |
|
| 523 |
assert_se(!dns_service_name_is_valid(NULL)); |
| 524 |
assert_se(!dns_service_name_is_valid("")); |
| 525 |
assert_se(!dns_service_name_is_valid("foo\nbar")); |
| 526 |
assert_se(!dns_service_name_is_valid("foo\201bar")); |
| 527 |
assert_se(!dns_service_name_is_valid("this is an overly long string that is certainly longer than 63 characters")); |
| 528 |
} |
| 529 |
|
| 530 |
TEST(dns_srv_type_is_valid) { |
| 531 |
assert_se(dns_srv_type_is_valid("_http._tcp")); |
| 532 |
assert_se(dns_srv_type_is_valid("_foo-bar._tcp")); |
| 533 |
assert_se(dns_srv_type_is_valid("_w._udp")); |
| 534 |
assert_se(dns_srv_type_is_valid("_a800._tcp")); |
| 535 |
assert_se(dns_srv_type_is_valid("_a-800._tcp")); |
| 536 |
|
| 537 |
assert_se(!dns_srv_type_is_valid(NULL)); |
| 538 |
assert_se(!dns_srv_type_is_valid("")); |
| 539 |
assert_se(!dns_srv_type_is_valid("x")); |
| 540 |
assert_se(!dns_srv_type_is_valid("_foo")); |
| 541 |
assert_se(!dns_srv_type_is_valid("_tcp")); |
| 542 |
assert_se(!dns_srv_type_is_valid("_")); |
| 543 |
assert_se(!dns_srv_type_is_valid("_foo.")); |
| 544 |
assert_se(!dns_srv_type_is_valid("_föo._tcp")); |
| 545 |
assert_se(!dns_srv_type_is_valid("_f\no._tcp")); |
| 546 |
assert_se(!dns_srv_type_is_valid("_800._tcp")); |
| 547 |
assert_se(!dns_srv_type_is_valid("_-800._tcp")); |
| 548 |
assert_se(!dns_srv_type_is_valid("_-foo._tcp")); |
| 549 |
assert_se(!dns_srv_type_is_valid("_piep._foo._udp")); |
| 550 |
} |
| 551 |
|
| 552 |
TEST(dnssd_srv_type_is_valid) { |
| 553 |
assert_se(dnssd_srv_type_is_valid("_http._tcp")); |
| 554 |
assert_se(dnssd_srv_type_is_valid("_foo-bar._tcp")); |
| 555 |
assert_se(dnssd_srv_type_is_valid("_w._udp")); |
| 556 |
assert_se(dnssd_srv_type_is_valid("_a800._tcp")); |
| 557 |
assert_se(dnssd_srv_type_is_valid("_a-800._tcp")); |
| 558 |
|
| 559 |
assert_se(!dnssd_srv_type_is_valid(NULL)); |
| 560 |
assert_se(!dnssd_srv_type_is_valid("")); |
| 561 |
assert_se(!dnssd_srv_type_is_valid("x")); |
| 562 |
assert_se(!dnssd_srv_type_is_valid("_foo")); |
| 563 |
assert_se(!dnssd_srv_type_is_valid("_tcp")); |
| 564 |
assert_se(!dnssd_srv_type_is_valid("_")); |
| 565 |
assert_se(!dnssd_srv_type_is_valid("_foo.")); |
| 566 |
assert_se(!dnssd_srv_type_is_valid("_föo._tcp")); |
| 567 |
assert_se(!dnssd_srv_type_is_valid("_f\no._tcp")); |
| 568 |
assert_se(!dnssd_srv_type_is_valid("_800._tcp")); |
| 569 |
assert_se(!dnssd_srv_type_is_valid("_-800._tcp")); |
| 570 |
assert_se(!dnssd_srv_type_is_valid("_-foo._tcp")); |
| 571 |
assert_se(!dnssd_srv_type_is_valid("_piep._foo._udp")); |
| 572 |
assert_se(!dnssd_srv_type_is_valid("_foo._unknown")); |
| 573 |
} |
| 574 |
|
| 575 |
static void test_dns_service_join_one(const char *a, const char *b, const char *c, int r, const char *d) { |
| 576 |
_cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *t = NULL; |
| 577 |
|
| 578 |
log_info("%s, %s, %s, →%d, %s", strnull(a), strnull(b), strnull(c), r, strnull(d)); |
| 579 |
|
| 580 |
assert_se(dns_service_join(a, b, c, &t) == r); |
| 581 |
ASSERT_STREQ(t, d); |
| 582 |
|
| 583 |
if (r < 0) |
| 584 |
return; |
| 585 |
|
| 586 |
assert_se(dns_service_split(t, &x, &y, &z) >= 0); |
| 587 |
ASSERT_STREQ(a, x); |
| 588 |
ASSERT_STREQ(b, y); |
| 589 |
assert_se(dns_name_equal(c, z) > 0); |
| 590 |
} |
| 591 |
|
| 592 |
TEST(dns_service_join) { |
| 593 |
test_dns_service_join_one("", "", "", -EINVAL, NULL); |
| 594 |
test_dns_service_join_one("", "_http._tcp", "", -EINVAL, NULL); |
| 595 |
test_dns_service_join_one("", "_http._tcp", "foo", -EINVAL, NULL); |
| 596 |
test_dns_service_join_one("foo", "", "foo", -EINVAL, NULL); |
| 597 |
test_dns_service_join_one("foo", "foo", "foo", -EINVAL, NULL); |
| 598 |
|
| 599 |
test_dns_service_join_one("foo", "_http._tcp", "", 0, "foo._http._tcp"); |
| 600 |
test_dns_service_join_one(NULL, "_http._tcp", "", 0, "_http._tcp"); |
| 601 |
test_dns_service_join_one("foo", "_http._tcp", "foo", 0, "foo._http._tcp.foo"); |
| 602 |
test_dns_service_join_one(NULL, "_http._tcp", "foo", 0, "_http._tcp.foo"); |
| 603 |
test_dns_service_join_one("Lennart's PC", "_pc._tcp", "foo.bar.com", 0, "Lennart\\039s\\032PC._pc._tcp.foo.bar.com"); |
| 604 |
test_dns_service_join_one(NULL, "_pc._tcp", "foo.bar.com", 0, "_pc._tcp.foo.bar.com"); |
| 605 |
} |
| 606 |
|
| 607 |
static void test_dns_service_split_one(const char *joined, const char *a, const char *b, const char *c, int r) { |
| 608 |
_cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *t = NULL; |
| 609 |
|
| 610 |
log_info("%s, %s, %s, %s, →%d", joined, strnull(a), strnull(b), strnull(c), r); |
| 611 |
|
| 612 |
assert_se(dns_service_split(joined, &x, &y, &z) == r); |
| 613 |
ASSERT_STREQ(x, a); |
| 614 |
ASSERT_STREQ(y, b); |
| 615 |
ASSERT_STREQ(z, c); |
| 616 |
|
| 617 |
if (r < 0) |
| 618 |
return; |
| 619 |
|
| 620 |
if (y) { |
| 621 |
assert_se(dns_service_join(x, y, z, &t) == 0); |
| 622 |
assert_se(dns_name_equal(joined, t) > 0); |
| 623 |
} else |
| 624 |
assert_se(!x && dns_name_equal(z, joined) > 0); |
| 625 |
} |
| 626 |
|
| 627 |
TEST(dns_service_split) { |
| 628 |
test_dns_service_split_one("", NULL, NULL, ".", 0); |
| 629 |
test_dns_service_split_one("foo", NULL, NULL, "foo", 0); |
| 630 |
test_dns_service_split_one("foo.bar", NULL, NULL, "foo.bar", 0); |
| 631 |
test_dns_service_split_one("_foo.bar", NULL, NULL, "_foo.bar", 0); |
| 632 |
test_dns_service_split_one("_foo._bar", NULL, "_foo._bar", ".", 0); |
| 633 |
test_dns_service_split_one("_meh._foo._bar", "_meh", "_foo._bar", ".", 0); |
| 634 |
test_dns_service_split_one("Wuff\\032Wuff._foo._bar.waldo.com", "Wuff Wuff", "_foo._bar", "waldo.com", 0); |
| 635 |
test_dns_service_split_one("_Q._Q-------------------------------------------------------------", NULL, "_Q._Q-------------------------------------------------------------", ".", 0); |
| 636 |
} |
| 637 |
|
| 638 |
static void test_dns_name_change_suffix_one(const char *name, const char *old_suffix, const char *new_suffix, int r, const char *result) { |
| 639 |
_cleanup_free_ char *s = NULL; |
| 640 |
|
| 641 |
log_info("%s, %s, %s, →%s", name, old_suffix, new_suffix, strnull(result)); |
| 642 |
|
| 643 |
assert_se(dns_name_change_suffix(name, old_suffix, new_suffix, &s) == r); |
| 644 |
ASSERT_STREQ(s, result); |
| 645 |
} |
| 646 |
|
| 647 |
TEST(dns_name_change_suffix) { |
| 648 |
test_dns_name_change_suffix_one("foo.bar", "bar", "waldo", 1, "foo.waldo"); |
| 649 |
test_dns_name_change_suffix_one("foo.bar.waldi.quux", "foo.bar.waldi.quux", "piff.paff", 1, "piff.paff"); |
| 650 |
test_dns_name_change_suffix_one("foo.bar.waldi.quux", "bar.waldi.quux", "piff.paff", 1, "foo.piff.paff"); |
| 651 |
test_dns_name_change_suffix_one("foo.bar.waldi.quux", "waldi.quux", "piff.paff", 1, "foo.bar.piff.paff"); |
| 652 |
test_dns_name_change_suffix_one("foo.bar.waldi.quux", "quux", "piff.paff", 1, "foo.bar.waldi.piff.paff"); |
| 653 |
test_dns_name_change_suffix_one("foo.bar.waldi.quux", "", "piff.paff", 1, "foo.bar.waldi.quux.piff.paff"); |
| 654 |
test_dns_name_change_suffix_one("", "", "piff.paff", 1, "piff.paff"); |
| 655 |
test_dns_name_change_suffix_one("", "", "", 1, "."); |
| 656 |
test_dns_name_change_suffix_one("a", "b", "c", 0, NULL); |
| 657 |
test_dns_name_change_suffix_one("wau.wau", "wau", NULL, 1, "wau"); |
| 658 |
test_dns_name_change_suffix_one("wau.wau", NULL, "wau", 1, "wau.wau.wau"); |
| 659 |
test_dns_name_change_suffix_one("wau.wau", NULL, NULL, 1, "wau.wau"); |
| 660 |
} |
| 661 |
|
| 662 |
static void test_dns_name_suffix_one(const char *name, unsigned n_labels, const char *result, int ret) { |
| 663 |
const char *p = NULL; |
| 664 |
|
| 665 |
log_info("%s, %u, → %s, %d", name, n_labels, strnull(result), ret); |
| 666 |
|
| 667 |
assert_se(ret == dns_name_suffix(name, n_labels, &p)); |
| 668 |
ASSERT_STREQ(p, result); |
| 669 |
} |
| 670 |
|
| 671 |
TEST(dns_name_suffix) { |
| 672 |
test_dns_name_suffix_one("foo.bar", 2, "foo.bar", 0); |
| 673 |
test_dns_name_suffix_one("foo.bar", 1, "bar", 1); |
| 674 |
test_dns_name_suffix_one("foo.bar", 0, "", 2); |
| 675 |
test_dns_name_suffix_one("foo.bar", 3, NULL, -EINVAL); |
| 676 |
test_dns_name_suffix_one("foo.bar", 4, NULL, -EINVAL); |
| 677 |
|
| 678 |
test_dns_name_suffix_one("bar", 1, "bar", 0); |
| 679 |
test_dns_name_suffix_one("bar", 0, "", 1); |
| 680 |
test_dns_name_suffix_one("bar", 2, NULL, -EINVAL); |
| 681 |
test_dns_name_suffix_one("bar", 3, NULL, -EINVAL); |
| 682 |
|
| 683 |
test_dns_name_suffix_one("", 0, "", 0); |
| 684 |
test_dns_name_suffix_one("", 1, NULL, -EINVAL); |
| 685 |
test_dns_name_suffix_one("", 2, NULL, -EINVAL); |
| 686 |
} |
| 687 |
|
| 688 |
static void test_dns_name_count_labels_one(const char *name, int n) { |
| 689 |
log_info("%s, →%d", name, n); |
| 690 |
|
| 691 |
assert_se(dns_name_count_labels(name) == n); |
| 692 |
} |
| 693 |
|
| 694 |
TEST(dns_name_count_labels) { |
| 695 |
test_dns_name_count_labels_one("foo.bar.quux.", 3); |
| 696 |
test_dns_name_count_labels_one("foo.bar.quux", 3); |
| 697 |
test_dns_name_count_labels_one("foo.bar.", 2); |
| 698 |
test_dns_name_count_labels_one("foo.bar", 2); |
| 699 |
test_dns_name_count_labels_one("foo.", 1); |
| 700 |
test_dns_name_count_labels_one("foo", 1); |
| 701 |
test_dns_name_count_labels_one("", 0); |
| 702 |
test_dns_name_count_labels_one(".", 0); |
| 703 |
test_dns_name_count_labels_one("..", -EINVAL); |
| 704 |
} |
| 705 |
|
| 706 |
static void test_dns_name_equal_skip_one(const char *a, unsigned n_labels, const char *b, int ret) { |
| 707 |
log_info("%s, %u, %s, →%d", a, n_labels, b, ret); |
| 708 |
|
| 709 |
assert_se(dns_name_equal_skip(a, n_labels, b) == ret); |
| 710 |
} |
| 711 |
|
| 712 |
TEST(dns_name_equal_skip) { |
| 713 |
test_dns_name_equal_skip_one("foo", 0, "bar", 0); |
| 714 |
test_dns_name_equal_skip_one("foo", 0, "foo", 1); |
| 715 |
test_dns_name_equal_skip_one("foo", 1, "foo", 0); |
| 716 |
test_dns_name_equal_skip_one("foo", 2, "foo", 0); |
| 717 |
|
| 718 |
test_dns_name_equal_skip_one("foo.bar", 0, "foo.bar", 1); |
| 719 |
test_dns_name_equal_skip_one("foo.bar", 1, "foo.bar", 0); |
| 720 |
test_dns_name_equal_skip_one("foo.bar", 2, "foo.bar", 0); |
| 721 |
test_dns_name_equal_skip_one("foo.bar", 3, "foo.bar", 0); |
| 722 |
|
| 723 |
test_dns_name_equal_skip_one("foo.bar", 0, "bar", 0); |
| 724 |
test_dns_name_equal_skip_one("foo.bar", 1, "bar", 1); |
| 725 |
test_dns_name_equal_skip_one("foo.bar", 2, "bar", 0); |
| 726 |
test_dns_name_equal_skip_one("foo.bar", 3, "bar", 0); |
| 727 |
|
| 728 |
test_dns_name_equal_skip_one("foo.bar", 0, "", 0); |
| 729 |
test_dns_name_equal_skip_one("foo.bar", 1, "", 0); |
| 730 |
test_dns_name_equal_skip_one("foo.bar", 2, "", 1); |
| 731 |
test_dns_name_equal_skip_one("foo.bar", 3, "", 0); |
| 732 |
|
| 733 |
test_dns_name_equal_skip_one("", 0, "", 1); |
| 734 |
test_dns_name_equal_skip_one("", 1, "", 0); |
| 735 |
test_dns_name_equal_skip_one("", 1, "foo", 0); |
| 736 |
test_dns_name_equal_skip_one("", 2, "foo", 0); |
| 737 |
} |
| 738 |
|
| 739 |
TEST(dns_name_compare_func) { |
| 740 |
assert_se(dns_name_compare_func("", "") == 0); |
| 741 |
assert_se(dns_name_compare_func("", ".") == 0); |
| 742 |
assert_se(dns_name_compare_func(".", "") == 0); |
| 743 |
assert_se(dns_name_compare_func("foo", "foo.") == 0); |
| 744 |
assert_se(dns_name_compare_func("foo.", "foo") == 0); |
| 745 |
assert_se(dns_name_compare_func("foo", "foo") == 0); |
| 746 |
assert_se(dns_name_compare_func("foo.", "foo.") == 0); |
| 747 |
assert_se(dns_name_compare_func("heise.de", "HEISE.DE.") == 0); |
| 748 |
|
| 749 |
assert_se(dns_name_compare_func("de.", "heise.de") != 0); |
| 750 |
} |
| 751 |
|
| 752 |
static void test_dns_name_common_suffix_one(const char *a, const char *b, const char *result) { |
| 753 |
const char *c; |
| 754 |
|
| 755 |
log_info("%s, %s, →%s", a, b, result); |
| 756 |
|
| 757 |
assert_se(dns_name_common_suffix(a, b, &c) >= 0); |
| 758 |
ASSERT_STREQ(c, result); |
| 759 |
} |
| 760 |
|
| 761 |
TEST(dns_name_common_suffix) { |
| 762 |
test_dns_name_common_suffix_one("", "", ""); |
| 763 |
test_dns_name_common_suffix_one("foo", "", ""); |
| 764 |
test_dns_name_common_suffix_one("", "foo", ""); |
| 765 |
test_dns_name_common_suffix_one("foo", "bar", ""); |
| 766 |
test_dns_name_common_suffix_one("bar", "foo", ""); |
| 767 |
test_dns_name_common_suffix_one("foo", "foo", "foo"); |
| 768 |
test_dns_name_common_suffix_one("quux.foo", "foo", "foo"); |
| 769 |
test_dns_name_common_suffix_one("foo", "quux.foo", "foo"); |
| 770 |
test_dns_name_common_suffix_one("this.is.a.short.sentence", "this.is.another.short.sentence", "short.sentence"); |
| 771 |
test_dns_name_common_suffix_one("FOO.BAR", "tEST.bAR", "BAR"); |
| 772 |
} |
| 773 |
|
| 774 |
static void test_dns_name_apply_idna_one(const char *s, int expected, const char *result) { |
| 775 |
_cleanup_free_ char *buf = NULL; |
| 776 |
int r; |
| 777 |
|
| 778 |
r = dns_name_apply_idna(s, &buf); |
| 779 |
log_debug("dns_name_apply_idna: \"%s\" → %d/\"%s\" (expected %d/\"%s\")", |
| 780 |
s, r, strnull(buf), expected, strnull(result)); |
| 781 |
|
| 782 |
/* Different libidn2 versions are more and less accepting |
| 783 |
* of underscore-prefixed names. So let's list the lowest |
| 784 |
* expected return value. */ |
| 785 |
assert_se(r >= expected); |
| 786 |
if (expected == 1) |
| 787 |
assert_se(dns_name_equal(buf, result) == 1); |
| 788 |
} |
| 789 |
|
| 790 |
TEST(dns_name_apply_idna) { |
| 791 |
const int ret = HAVE_LIBIDN2; |
| 792 |
|
| 793 |
/* IDNA2008 forbids names with hyphens in third and fourth positions |
| 794 |
* (https://tools.ietf.org/html/rfc5891#section-4.2.3.1). |
| 795 |
* IDNA2003 does not have this restriction |
| 796 |
* (https://tools.ietf.org/html/rfc3490#section-5). |
| 797 |
* This means that when using libidn we will transform and test more |
| 798 |
* labels. If registrars follow IDNA2008 we'll just be performing a |
| 799 |
* useless lookup. |
| 800 |
*/ |
| 801 |
|
| 802 |
test_dns_name_apply_idna_one("", ret, ""); |
| 803 |
test_dns_name_apply_idna_one("foo", ret, "foo"); |
| 804 |
test_dns_name_apply_idna_one("foo.", ret, "foo"); |
| 805 |
test_dns_name_apply_idna_one("foo.bar", ret, "foo.bar"); |
| 806 |
test_dns_name_apply_idna_one("foo.bar.", ret, "foo.bar"); |
| 807 |
test_dns_name_apply_idna_one("föö", ret, "xn--f-1gaa"); |
| 808 |
test_dns_name_apply_idna_one("föö.", ret, "xn--f-1gaa"); |
| 809 |
test_dns_name_apply_idna_one("föö.bär", ret, "xn--f-1gaa.xn--br-via"); |
| 810 |
test_dns_name_apply_idna_one("föö.bär.", ret, "xn--f-1gaa.xn--br-via"); |
| 811 |
test_dns_name_apply_idna_one("xn--f-1gaa.xn--br-via", ret, "xn--f-1gaa.xn--br-via"); |
| 812 |
|
| 813 |
test_dns_name_apply_idna_one("_443._tcp.fedoraproject.org", 0, |
| 814 |
"_443._tcp.fedoraproject.org"); |
| 815 |
test_dns_name_apply_idna_one("_443", 0, "_443"); |
| 816 |
test_dns_name_apply_idna_one("gateway", 0, "gateway"); |
| 817 |
test_dns_name_apply_idna_one("_gateway", 0, "_gateway"); |
| 818 |
|
| 819 |
test_dns_name_apply_idna_one("r3---sn-ab5l6ne7.googlevideo.com", 0, ""); |
| 820 |
} |
| 821 |
|
| 822 |
TEST(dns_name_is_valid_or_address) { |
| 823 |
assert_se(dns_name_is_valid_or_address(NULL) == 0); |
| 824 |
assert_se(dns_name_is_valid_or_address("") == 0); |
| 825 |
assert_se(dns_name_is_valid_or_address("foobar") > 0); |
| 826 |
assert_se(dns_name_is_valid_or_address("foobar.com") > 0); |
| 827 |
assert_se(dns_name_is_valid_or_address("foobar..com") == 0); |
| 828 |
assert_se(dns_name_is_valid_or_address("foobar.com.") > 0); |
| 829 |
assert_se(dns_name_is_valid_or_address("127.0.0.1") > 0); |
| 830 |
assert_se(dns_name_is_valid_or_address("::") > 0); |
| 831 |
assert_se(dns_name_is_valid_or_address("::1") > 0); |
| 832 |
} |
| 833 |
|
| 834 |
TEST(dns_name_dot_suffixed) { |
| 835 |
assert_se(dns_name_dot_suffixed("") == 0); |
| 836 |
assert_se(dns_name_dot_suffixed(".") > 0); |
| 837 |
assert_se(dns_name_dot_suffixed("foo") == 0); |
| 838 |
assert_se(dns_name_dot_suffixed("foo.") > 0); |
| 839 |
assert_se(dns_name_dot_suffixed("foo\\..") > 0); |
| 840 |
assert_se(dns_name_dot_suffixed("foo\\.") == 0); |
| 841 |
assert_se(dns_name_dot_suffixed("foo.bar.") > 0); |
| 842 |
assert_se(dns_name_dot_suffixed("foo.bar\\.\\.\\..") > 0); |
| 843 |
assert_se(dns_name_dot_suffixed("foo.bar\\.\\.\\.\\.") == 0); |
| 844 |
} |
| 845 |
|
| 846 |
TEST(dns_name_parent) { |
| 847 |
const char *name = "hoge.hoge.foo.bar.example.com"; |
| 848 |
|
| 849 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("hoge")); |
| 850 |
ASSERT_STREQ(name, "hoge.foo.bar.example.com"); |
| 851 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("hoge")); |
| 852 |
ASSERT_STREQ(name, "foo.bar.example.com"); |
| 853 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("foo")); |
| 854 |
ASSERT_STREQ(name, "bar.example.com"); |
| 855 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("bar")); |
| 856 |
ASSERT_STREQ(name, "example.com"); |
| 857 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("example")); |
| 858 |
ASSERT_STREQ(name, "com"); |
| 859 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("com")); |
| 860 |
ASSERT_STREQ(name, ""); |
| 861 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("")); |
| 862 |
ASSERT_STREQ(name, ""); |
| 863 |
|
| 864 |
name = "hoge.hoge.foo.bar.example.com."; |
| 865 |
|
| 866 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("hoge")); |
| 867 |
ASSERT_STREQ(name, "hoge.foo.bar.example.com."); |
| 868 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("hoge")); |
| 869 |
ASSERT_STREQ(name, "foo.bar.example.com."); |
| 870 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("foo")); |
| 871 |
ASSERT_STREQ(name, "bar.example.com."); |
| 872 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("bar")); |
| 873 |
ASSERT_STREQ(name, "example.com."); |
| 874 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("example")); |
| 875 |
ASSERT_STREQ(name, "com."); |
| 876 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("com")); |
| 877 |
ASSERT_STREQ(name, ""); /* The trailint dot is suppressed. */ |
| 878 |
ASSERT_OK_EQ(dns_name_parent(&name), (int) STRLEN("")); |
| 879 |
ASSERT_STREQ(name, ""); |
| 880 |
} |
| 881 |
|
| 882 |
DEFINE_TEST_MAIN(LOG_DEBUG); |