LCOV - code coverage report
Current view: top level - test - test-dns-domain.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 587 589 99.7 %
Date: 2019-08-22 15:41:25 Functions: 48 48 100.0 %

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

Generated by: LCOV version 1.14