test-dns-domain.c revision 9436e8cae4709b50ed57f2f5858a3ffad03d5d32
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering This file is part of systemd.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering Copyright 2014 Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering systemd is free software; you can redistribute it and/or modify it
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering under the terms of the GNU Lesser General Public License as published by
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering (at your option) any later version.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering systemd is distributed in the hope that it will be useful, but
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering Lesser General Public License for more details.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering You should have received a copy of the GNU Lesser General Public License
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poetteringstatic void test_dns_label_unescape_one(const char *what, const char *expect, size_t buffer_sz, int ret) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = dns_label_unescape(&what, buffer, buffer_sz);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_label_unescape(void) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("hallo", "hallo", 6, 5);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("hallo", "hallo", 4, -ENOSPC);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("", "", 10, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("hallo\\.foobar", "hallo.foobar", 20, 12);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("hallo.foobar", "hallo", 10, 5);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("hallo\n.foobar", "hallo", 20, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("hallo\\", "hallo", 20, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("hallo\\032 ", "hallo ", 20, 7);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one(".", "", 20, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("..", "", 20, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one(".foobar", "", 20, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_one("foobar.", "foobar", 20, 6);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart 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) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = dns_label_unescape_suffix(what, &label, buffer, buffer_sz);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_label_unescape_suffix(void) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("hallo", "hallo", "", 6, 5, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("hallo", "hallo", "", 4, -ENOSPC, -ENOSPC);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("", "", "", 10, 0, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("hallo\\.foobar", "hallo.foobar", "", 20, 12, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("hallo.foobar", "foobar", "hallo", 10, 6, 5);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("hallo.foobar\n", "foobar", "foobar", 20, -EINVAL, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("hallo\\", "hallo", "hallo", 20, -EINVAL, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("hallo\\032 ", "hallo ", "", 20, 7, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one(".", "", "", 20, 0, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("..", "", "", 20, 0, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one(".foobar", "foobar", "", 20, 6, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("foobar.", "", "foobar", 20, 0, 6);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("foo\\\\bar", "foo\\bar", "", 20, 7, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("foo.bar", "bar", "foo", 20, 3, 3);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("foo..bar", "bar", "", 20, 3, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("foo...bar", "bar", "", 20, 3, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("foo\\.bar", "foo.bar", "", 20, 7, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("foo\\\\.bar", "bar", "foo\\", 20, 3, 4);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_unescape_suffix_one("foo\\\\\\.bar", "foo\\.bar", "", 20, 8, 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_label_escape_one(const char *what, size_t l, const char *expect, int ret) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_label_escape(void) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_escape_one("hallo", 5, "hallo", 5);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_escape_one("hallo", 6, NULL, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_label_escape_one("hallo hallo.foobar,waldi", 24, "hallo\\032hallo\\.foobar\\044waldi", 31);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_normalize_one(const char *what, const char *expect, int ret) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_normalize(void) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_normalize_one("f", "f", 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_normalize_one("f.waldi", "f.waldi", 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_normalize_one("f \\032.waldi", "f\\032\\032.waldi", 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_normalize_one("\\000", NULL, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_normalize_one("..", NULL, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_normalize_one(".foobar", NULL, -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_normalize_one("foobar.", "foobar", 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_equal_one(const char *a, const char *b, int ret) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_equal(void) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_equal_one("x", "x.", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_equal_one("abc.def", "abc.def", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_equal_one("abc.def", "ABC.def", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_equal_one("abc.def", "CBA.def", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_equal_one("", "xxx", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_equal_one("ab", "a", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_equal_one("\\000", "xxxx", -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_equal_one("..", "..", -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_between_one(const char *a, const char *b, const char *c, int ret) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_between(void) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering /* see https://tools.ietf.org/html/rfc4034#section-6.1
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering Note that we use "\033.z.example" in stead of "\001.z.example" as we
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering consider the latter invalid */
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("example", "a.example", "yljkjljk.a.example", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("a.example", "yljkjljk.a.example", "Z.a.example", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("yljkjljk.a.example", "Z.a.example", "zABC.a.EXAMPLE", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("Z.a.example", "zABC.a.EXAMPLE", "z.example", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("zABC.a.EXAMPLE", "z.example", "\\033.z.example", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("z.example", "\\033.z.example", "*.z.example", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("\\033.z.example", "*.z.example", "\\200.z.example", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("*.z.example", "\\200.z.example", "example", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("\\200.z.example", "example", "a.example", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("example", "a.example", "example", -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("example", "example", "yljkjljk.a.example", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_between_one("example", "yljkjljk.a.example", "yljkjljk.a.example", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_endswith_one(const char *a, const char *b, int ret) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_endswith(a, b) == ret);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_endswith(void) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("", "", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("", "xxx", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("xxx", "", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x", "x", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x", "y", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y", "y", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y", "Y", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y", "x", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y.z", "Z", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y.z", "y.Z", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y.z", "x.y.Z", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y.z", "waldo", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y.z.u.v.w", "y.z", false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y.z.u.v.w", "u.v.w", true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_endswith_one("x.y\001.z", "waldo", -EINVAL);
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poetteringstatic void test_dns_name_root(void) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_root("xxx") == false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_root("xxx.") == false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_root("..") == -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_single_label(void) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_single_label("") == false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_single_label(".") == false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_single_label("..") == -EINVAL);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_single_label("x") == true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_single_label("x.") == true);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_single_label("xx.yy") == false);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void test_dns_name_reverse_one(const char *address, const char *name) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering union in_addr_union a, b = {};
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(in_addr_from_string_auto(address, &familya, &a) >= 0);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering assert_se(dns_name_reverse(familya, &a, &p) >= 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(dns_name_address(p, &familyb, &b) > 0);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert_se(in_addr_equal(familya, &a, &b));
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic void test_dns_name_reverse(void) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering test_dns_name_reverse_one("47.11.8.15", "15.8.11.47.in-addr.arpa");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering 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");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering test_dns_name_reverse_one("127.0.0.1", "1.0.0.127.in-addr.arpa");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering 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");