about summary refs log tree commit diff
path: root/resolv/tst-res_hnok.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2017-11-11 11:23:40 +0100
committerFlorian Weimer <fweimer@redhat.com>2017-11-11 11:23:40 +0100
commit5c1a69238fcb87ff7f916a5ce7960b2864afb3a1 (patch)
tree8aa7c3df4c077ae7d01a84d4ab006f6bcf8129ef /resolv/tst-res_hnok.c
parenta19c0a1db50ed3fd58715a1d78ea465d82fe9fd5 (diff)
downloadglibc-5c1a69238fcb87ff7f916a5ce7960b2864afb3a1.tar.gz
glibc-5c1a69238fcb87ff7f916a5ce7960b2864afb3a1.tar.xz
glibc-5c1a69238fcb87ff7f916a5ce7960b2864afb3a1.zip
resolv: Add tst-res_hnok
Diffstat (limited to 'resolv/tst-res_hnok.c')
-rw-r--r--resolv/tst-res_hnok.c153
1 files changed, 153 insertions, 0 deletions
diff --git a/resolv/tst-res_hnok.c b/resolv/tst-res_hnok.c
new file mode 100644
index 0000000000..9c92303821
--- /dev/null
+++ b/resolv/tst-res_hnok.c
@@ -0,0 +1,153 @@
+/* Tests for res_hnok and related functions.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <array_length.h>
+#include <resolv.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/test-driver.h>
+
+/* Bits which indicate which functions are supposed to report
+   success.  */
+enum
+  {
+    hnok = 1,
+    dnok = 2,
+    mailok = 4,
+    ownok = 8,
+    allnomailok = hnok | dnok | ownok,
+    allok = hnok | dnok | mailok | ownok
+  };
+
+/* A string of 60 characters.  */
+#define STRING60 "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
+
+/* A string of 63 characters (maximum label length).  */
+#define STRING63 STRING60 "zzz"
+
+/* Combines a test name with the expected results.  */
+struct test_case
+{
+  const char *dn;
+  unsigned int result;          /* Combination of the *ok flags.  */
+};
+
+static const struct test_case tests[] =
+  {
+    { "", allok },
+    { ".", allok },
+    { "www", allnomailok },
+    { "example", allnomailok },
+    { "example.com", allok },
+    { "www.example.com", allok },
+    { "www.example.com.", allok },
+    { "*.example.com", dnok | mailok | ownok },
+    { "-v", dnok },
+    { "-v.example.com", mailok | dnok },
+    { "**.example.com", dnok | mailok },
+    { STRING63, allnomailok },
+    { STRING63 ".example.com", allok },
+    { STRING63 "." STRING63 "." STRING63 "." STRING60 "z", allok },
+    { "hostmaster@mail.example.com", dnok | mailok },
+    { "with whitespace", 0 },
+    { "with\twhitespace", 0 },
+    { "with\nwhitespace", 0 },
+    { "with.whitespace ", 0 },
+    { "with.whitespace\t", 0 },
+    { "with.whitespace\n", 0 },
+    { "with\\ whitespace", 0 },
+    { "with\\\twhitespace", 0 },
+    { "with\\\nwhitespace", 0 },
+    { "with.whitespace\\ ", 0 },
+    { "with.whitespace\\\t", 0 },
+    { "with.whitespace\\\n", 0 },
+  };
+
+/* Run test case *TEST with FUNC (named FUNCNAME) and report an error
+   if the result does not match the result flag at BIT.  */
+static void
+one_test (const struct test_case *test, const char *funcname,
+          int (*func) (const char *), unsigned int bit)
+{
+  int expected = (test->result & bit) != 0;
+  int actual = func (test->dn);
+  if (actual != expected)
+    {
+      support_record_failure ();
+      printf ("error: %s (\"%s\"): expected=%d, actual=%d\n",
+              funcname, test->dn, expected, actual);
+    }
+}
+
+/* Run 255 tests using all the bytes from 1 to 255, surround the byte
+   with the strings PREFIX and SUFFIX, and check that FUNC (named
+   FUNCNAME) accepts only those bytes listed in ACCEPTED.  */
+static void
+one_char (const char *prefix, const char *accepted, const char *suffix,
+          const char *funcname, int (*func) (const char *))
+{
+  for (int ch = 1; ch <= 255; ++ch)
+    {
+      char dn[1024];
+      snprintf (dn, sizeof (dn), "%s%c%s", prefix, ch, suffix);
+      int expected = strchr (accepted, ch) != NULL;
+      int actual = func (dn);
+      if (actual != expected)
+        {
+          support_record_failure ();
+          printf ("error: %s (\"%s\"): expected=%d, actual=%d\n",
+                  funcname, dn, expected, actual);
+        }
+    }
+}
+
+static int
+do_test (void)
+{
+  for (const struct test_case *test = tests; test < array_end (tests); ++test)
+    {
+      if (test_verbose)
+        printf ("info: testing domain name [[[%s]]] (0x%x)\n",
+                test->dn, test->result);
+      one_test (test, "res_hnok", res_hnok, hnok);
+      one_test (test, "res_dnok", res_dnok, dnok);
+      one_test (test, "res_mailok", res_mailok, mailok);
+      one_test (test, "res_ownok", res_ownok, ownok);
+    }
+
+  one_char
+    ("", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.",
+     "", "res_hnok", res_hnok);
+  one_char
+    ("middle",
+     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_",
+     "suffix", "res_hnok", res_hnok);
+  one_char
+    ("middle",
+     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_"
+     "!\"#$%&'()*+,/:;<=>?@[\\]^`{|}~",
+     "suffix.example", "res_mailok", res_mailok);
+  one_char
+    ("mailbox.middle",
+     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_",
+     "suffix.example", "res_mailok", res_mailok);
+
+  return 0;
+}
+
+#include <support/test-driver.c>