test-dns-domain.c revision 0a49b6b6dce3a756bd8c4d458a34c2d8035ae99d
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen This file is part of systemd.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Copyright 2014 Lennart Poettering
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen systemd is free software; you can redistribute it and/or modify it
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen under the terms of the GNU Lesser General Public License as published by
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen (at your option) any later version.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen systemd is distributed in the hope that it will be useful, but
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Lesser General Public License for more details.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen You should have received a copy of the GNU Lesser General Public License
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_label_unescape_one(const char *what, const char *expect, size_t buffer_sz, int ret) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = dns_label_unescape(&what, buffer, buffer_sz);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_label_unescape(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_one("hallo", "hallo", 6, 5);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_one("hallo", "hallo", 4, -ENOSPC);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering test_dns_label_unescape_one("", "", 10, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_one("hallo\\.foobar", "hallo.foobar", 20, 12);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_one("hallo.foobar", "hallo", 10, 5);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_one("hallo\n.foobar", "hallo", 20, -EINVAL);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering test_dns_label_unescape_one("hallo\\", "hallo", 20, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_one("hallo\\032 ", "hallo ", 20, 7);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_one("..", "", 20, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_one(".foobar", "", 20, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_one("foobar.", "foobar", 20, 6);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_to_wire_format_one(const char *what, const char *expect, size_t buffer_sz, int ret) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = dns_name_to_wire_format(what, buffer, buffer_sz);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_to_wire_format(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const char out2[] = { 5, 'h', 'a', 'l', 'l', 'o', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 };
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen const char out3[] = { 4, ' ', 'f', 'o', 'o', 3, 'b', 'a', 'r', 0 };
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_to_wire_format_one("", NULL, 0, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_to_wire_format_one("foo", out1, sizeof(out1), sizeof(out1));
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) + 1, sizeof(out1));
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_to_wire_format_one("foo", out1, sizeof(out1) - 1, -ENOBUFS);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_to_wire_format_one("hallo.foo.bar", out2, sizeof(out2), sizeof(out2));
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_to_wire_format_one("hallo.foo..bar", NULL, 32, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_to_wire_format_one("\\032foo.bar", out3, sizeof(out3), sizeof(out3));
79008bddf679a5e0900369950eb346c9fa687107Lennart Poetteringstatic void test_dns_label_unescape_suffix_one(const char *what, const char *expect1, const char *expect2, size_t buffer_sz, int ret1, int ret2) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_label_unescape_suffix(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOSPC, -ENOSPC);
79008bddf679a5e0900369950eb346c9fa687107Lennart Poettering test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("hallo\\.foobar", "hallo.foobar", "", 20, 12, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("hallo.foobar", "foobar", "hallo", 10, 6, 5);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("hallo.foobar\n", "foobar", "foobar", 20, -EINVAL, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("hallo\\", "hallo", "hallo", 20, -EINVAL, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("hallo\\032 ", "hallo ", "", 20, 7, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one(".", "", "", 20, 0, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("..", "", "", 20, 0, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one(".foobar", "foobar", "", 20, 6, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("foobar.", "", "foobar", 20, 0, 6);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("foo\\\\bar", "foo\\bar", "", 20, 7, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("foo.bar", "bar", "foo", 20, 3, 3);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("foo..bar", "bar", "", 20, 3, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("foo...bar", "bar", "", 20, 3, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("foo\\.bar", "foo.bar", "", 20, 7, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("foo\\\\.bar", "bar", "foo\\", 20, 3, 4);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_unescape_suffix_one("foo\\\\\\.bar", "foo\\.bar", "", 20, 8, 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_label_escape(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_escape_one("hallo", 5, "hallo", 5);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_escape_one("hallo", 6, NULL, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_normalize_one(const char *what, const char *expect, int ret) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_normalize(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_normalize_one("f.waldi", "f.waldi", 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_normalize_one("\\000", NULL, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_normalize_one("..", NULL, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_normalize_one(".foobar", NULL, -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_normalize_one("foobar.", "foobar", 0);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_equal_one(const char *a, const char *b, int ret) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_equal(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_equal_one("abc.def", "abc.def", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_equal_one("abc.def", "ABC.def", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_equal_one("abc.def", "CBA.def", false);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_equal_one("\\000", "xxxx", -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_between_one(const char *a, const char *b, const char *c, int ret) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_between(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen /* see https://tools.ietf.org/html/rfc4034#section-6.1
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen Note that we use "\033.z.example" in stead of "\001.z.example" as we
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen consider the latter invalid */
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("example", "a.example", "yljkjljk.a.example", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("a.example", "yljkjljk.a.example", "Z.a.example", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("yljkjljk.a.example", "Z.a.example", "zABC.a.EXAMPLE", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("Z.a.example", "zABC.a.EXAMPLE", "z.example", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("zABC.a.EXAMPLE", "z.example", "\\033.z.example", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("z.example", "\\033.z.example", "*.z.example", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("\\033.z.example", "*.z.example", "\\200.z.example", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("*.z.example", "\\200.z.example", "example", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("\\200.z.example", "example", "a.example", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("example", "a.example", "example", -EINVAL);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("example", "example", "yljkjljk.a.example", false);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_between_one("example", "yljkjljk.a.example", "yljkjljk.a.example", false);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_endswith_one(const char *a, const char *b, int ret) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersenstatic void test_dns_name_endswith(void) {
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_endswith_one("x.y", "x", false);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_endswith_one("x.y.z", "Z", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_endswith_one("x.y.z", "y.Z", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_endswith_one("x.y.z", "x.y.Z", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_endswith_one("x.y.z", "waldo", false);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true);
b22d8a00f48f3c5fc4510b4acd3e1a43e731e592Tom Gundersen test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL);
static void test_dns_name_root(void) {
static void test_dns_name_single_label(void) {
union in_addr_union a, b = {};
static void test_dns_name_reverse(void) {
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");
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");
static void test_dns_name_concat(void) {
static void test_dns_name_is_valid(void) {
static void test_dns_service_name_is_valid(void) {
assert_se(!dns_service_name_is_valid("this is an overly long string that is certainly longer than 63 characters"));
static void test_dns_srv_type_verify(void) {