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