00db10
commit e2a9fca8101443076235a8dbcfceaa2d96bf4801
00db10
Author: Florian Weimer <fweimer@redhat.com>
00db10
Date:   Sat Nov 11 11:33:32 2017 +0100
00db10
00db10
    resolv: Add tst-ns_name_pton
00db10
00db10
diff --git a/resolv/Makefile b/resolv/Makefile
00db10
index b1fd2e2db8736f9b..0130a09db2d69451 100644
00db10
--- a/resolv/Makefile
00db10
+++ b/resolv/Makefile
00db10
@@ -49,6 +49,7 @@ tests += \
00db10
   tst-bug18665-tcp \
00db10
   tst-ns_name \
00db10
   tst-ns_name_compress \
00db10
+  tst-ns_name_pton \
00db10
   tst-res_hconf_reorder \
00db10
   tst-res_hnok \
00db10
   tst-res_use_inet6 \
00db10
@@ -183,6 +184,7 @@ $(objpfx)tst-resolv-canonname: \
00db10
 $(objpfx)tst-ns_name: $(objpfx)libresolv.so
00db10
 $(objpfx)tst-ns_name.out: tst-ns_name.data
00db10
 $(objpfx)tst-ns_name_compress: $(objpfx)libresolv.so
00db10
+$(objpfx)tst-ns_name_pton: $(objpfx)libresolv.so
00db10
 $(objpfx)tst-res_hnok: $(objpfx)libresolv.so
00db10
 
00db10
 
00db10
diff --git a/resolv/tst-ns_name_pton.c b/resolv/tst-ns_name_pton.c
00db10
new file mode 100644
00db10
index 0000000000000000..879d97c9d3816210
00db10
--- /dev/null
00db10
+++ b/resolv/tst-ns_name_pton.c
00db10
@@ -0,0 +1,203 @@
00db10
+/* Tests for ns_name_pton.
00db10
+   Copyright (C) 2017 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <arpa/nameser.h>
00db10
+#include <array_length.h>
00db10
+#include <stdbool.h>
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <string.h>
00db10
+#include <support/check.h>
00db10
+#include <support/support.h>
00db10
+#include <support/test-driver.h>
00db10
+
00db10
+/* Bits which indicate which functions are supposed to report
00db10
+   success.  */
00db10
+enum
00db10
+  {
00db10
+    hnok = 1,
00db10
+    dnok = 2,
00db10
+    mailok = 4,
00db10
+    ownok = 8,
00db10
+    allnomailok = hnok | dnok | ownok,
00db10
+    allok = hnok | dnok | mailok | ownok
00db10
+  };
00db10
+
00db10
+/* A string of 60 characters.  */
00db10
+#define STRING60 "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
00db10
+
00db10
+/* A string of 63 characters (maximum label length).  */
00db10
+#define STRING63 STRING60 "zzz"
00db10
+
00db10
+/* A string of 60 bytes (non-ASCII).  */
00db10
+#define STRING60OCT \
00db10
+  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" \
00db10
+  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" \
00db10
+  "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" \
00db10
+  "\377\377\377\377\377\377\377\377\377"
00db10
+
00db10
+/* A string of 63 bytes (non-ASCII).  */
00db10
+#define STRING63OCT STRING60OCT "\377\377\377"
00db10
+
00db10
+/* A string of 60 bytes (non-ASCII, quoted decimal).  */
00db10
+#define STRING60DEC \
00db10
+  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
00db10
+  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
00db10
+  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
00db10
+  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
00db10
+  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255" \
00db10
+  "\\255\\255\\255\\255\\255\\255\\255\\255\\255\\255"
00db10
+
00db10
+/* A string of 63 bytes (non-ASCII, quoted decimal).  */
00db10
+#define STRING63DEC STRING60DEC "\\255\\255\\255"
00db10
+
00db10
+/* Combines a test name with the expected results.  */
00db10
+struct test_case
00db10
+{
00db10
+  const char *dn;
00db10
+  const char *back; /* Expected test result converted using ns_name_ntop.  */
00db10
+  bool fully_qualified; /* True if the domain name has a trailing dot.  */
00db10
+};
00db10
+
00db10
+static const struct test_case tests[] =
00db10
+  {
00db10
+    { "", ".", false },
00db10
+    { ".", ".", true },
00db10
+    { "..", NULL, },
00db10
+    { "www", "www", false },
00db10
+    { "www.", "www", true },
00db10
+    { "www\\.", "www\\.", false },
00db10
+    { ".www", NULL, },
00db10
+    { ".www\\.", NULL, },
00db10
+    { "example.com", "example.com", false },
00db10
+    { "example.com.", "example.com", true },
00db10
+    { ".example.com", NULL, },
00db10
+    { ".example.com.", NULL, },
00db10
+    { "example\\.com", "example\\.com", false },
00db10
+    { "example\\.com.", "example\\.com", true },
00db10
+    { "example..", NULL, },
00db10
+    { "example..com", NULL, },
00db10
+    { "example..com", NULL, },
00db10
+    { "\\0", NULL, },
00db10
+    { "\\00", NULL, },
00db10
+    { "\\000", "\\000", false },
00db10
+    { "\\1", NULL, },
00db10
+    { "\\01", NULL, },
00db10
+    { "\\001", "\\001", false },
00db10
+    { "\\1x", NULL, },
00db10
+    { "\\01x", NULL, },
00db10
+    { "\\001x", "\\001x", false },
00db10
+    { "\\256", NULL, },
00db10
+    { "\\0641", "\\@1", false },
00db10
+    { "\\0011", "\\0011", false },
00db10
+    { STRING63, STRING63, false },
00db10
+    { STRING63 ".", STRING63, true },
00db10
+    { STRING63 "z", NULL, },
00db10
+    { STRING63 "\\.", NULL, },
00db10
+    { STRING60 "zz\\.", STRING60 "zz\\.", false },
00db10
+    { STRING60 "zz\\..", STRING60 "zz\\.", true },
00db10
+    { STRING63 "." STRING63 "." STRING63 "." STRING60 "z",
00db10
+      STRING63 "." STRING63 "." STRING63 "." STRING60 "z", false },
00db10
+    { STRING63 "." STRING63 "." STRING63 "." STRING60 "z.",
00db10
+      STRING63 "." STRING63 "." STRING63 "." STRING60 "z", true },
00db10
+    { STRING63 "." STRING63 "." STRING63 "." STRING60 "zz", NULL, },
00db10
+    { STRING63 "." STRING63 "." STRING63 "." STRING60 "zzz", NULL, },
00db10
+    { STRING63OCT "." STRING63OCT "." STRING63OCT "." STRING60OCT "\377",
00db10
+      STRING63DEC "." STRING63DEC "." STRING63DEC "." STRING60DEC "\\255",
00db10
+      false },
00db10
+    { STRING63OCT "." STRING63OCT "." STRING63OCT "." STRING60OCT "\377.",
00db10
+      STRING63DEC "." STRING63DEC "." STRING63DEC "." STRING60DEC "\\255",
00db10
+      true },
00db10
+    { STRING63OCT "." STRING63OCT "." STRING63OCT "." STRING60OCT
00db10
+      "\377\377", NULL, },
00db10
+    { STRING63OCT "." STRING63OCT "." STRING63OCT "." STRING60OCT
00db10
+      "\377\377\377", NULL, },
00db10
+  };
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  unsigned char *wire = xmalloc (NS_MAXCDNAME);
00db10
+  char *text = xmalloc (NS_MAXDNAME);
00db10
+  for (const struct test_case *test = tests; test < array_end (tests); ++test)
00db10
+    {
00db10
+      if (test_verbose)
00db10
+        printf ("info: testing domain name [[[%s]]]\n", test->dn);
00db10
+      int ret = ns_name_pton (test->dn, wire, NS_MAXCDNAME);
00db10
+      if (ret == -1)
00db10
+        {
00db10
+          if (test->back != NULL)
00db10
+            {
00db10
+              support_record_failure ();
00db10
+              printf ("error: unexpected decoding failure for [[%s]]\n",
00db10
+                      test->dn);
00db10
+            }
00db10
+          /* Otherwise, we have an expected decoding failure.  */
00db10
+          continue;
00db10
+        }
00db10
+
00db10
+      if (ret < -1 || ret > 1)
00db10
+        {
00db10
+          support_record_failure ();
00db10
+          printf ("error: invalid return value %d for [[%s]]\n",
00db10
+                  ret, test->dn);
00db10
+          continue;
00db10
+        }
00db10
+
00db10
+      int ret2 = ns_name_ntop (wire, text, NS_MAXDNAME);
00db10
+
00db10
+      if (ret2 < 0)
00db10
+        {
00db10
+          support_record_failure ();
00db10
+          printf ("error: failure to convert back [[%s]]\n", test->dn);
00db10
+        }
00db10
+
00db10
+      if (test->back == NULL)
00db10
+        {
00db10
+          support_record_failure ();
00db10
+          printf ("error: unexpected success converting [[%s]]\n", test->dn);
00db10
+          if (ret2 >= 1)
00db10
+            printf ("error:   result converts back to [[%s]]\n", test->dn);
00db10
+          continue;
00db10
+        }
00db10
+
00db10
+      if (strcmp (text, test->back) != 0)
00db10
+        {
00db10
+          support_record_failure ();
00db10
+          printf ("error: back-conversion of [[%s]] did not match\n",
00db10
+                  test->dn);
00db10
+          printf ("error:   expected: [[%s]]\n", test->back);
00db10
+          printf ("error:     actual: [[%s]]\n", text);
00db10
+        }
00db10
+
00db10
+      if (ret != test->fully_qualified)
00db10
+        {
00db10
+          support_record_failure ();
00db10
+          printf ("error: invalid fully-qualified status for [[%s]]\n",
00db10
+                  test->dn);
00db10
+          printf ("error:   expected: %d\n", (int) test->fully_qualified);
00db10
+          printf ("error:     actual: %d\n", ret);
00db10
+        }
00db10
+    }
00db10
+
00db10
+  free (text);
00db10
+  free (wire);
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+#include <support/test-driver.c>