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