Blame resolv/tst-res_hnok.c

Packit 6c4009
/* Tests for res_hnok and related functions.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <array_length.h>
Packit 6c4009
#include <resolv.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
Packit 6c4009
/* Bits which indicate which functions are supposed to report
Packit 6c4009
   success.  */
Packit 6c4009
enum
Packit 6c4009
  {
Packit 6c4009
    hnok = 1,
Packit 6c4009
    dnok = 2,
Packit 6c4009
    mailok = 4,
Packit 6c4009
    ownok = 8,
Packit 6c4009
    allnomailok = hnok | dnok | ownok,
Packit 6c4009
    allok = hnok | dnok | mailok | ownok
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
/* A string of 60 characters.  */
Packit 6c4009
#define STRING60 "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
Packit 6c4009
Packit 6c4009
/* A string of 63 characters (maximum label length).  */
Packit 6c4009
#define STRING63 STRING60 "zzz"
Packit 6c4009
Packit 6c4009
/* Combines a test name with the expected results.  */
Packit 6c4009
struct test_case
Packit 6c4009
{
Packit 6c4009
  const char *dn;
Packit 6c4009
  unsigned int result;          /* Combination of the *ok flags.  */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const struct test_case tests[] =
Packit 6c4009
  {
Packit 6c4009
    { "", allok },
Packit 6c4009
    { ".", allok },
Packit 6c4009
    { "..", 0 },
Packit 6c4009
    { "www", allnomailok },
Packit 6c4009
    { "www.", allnomailok },
Packit 6c4009
    { "example", allnomailok },
Packit 6c4009
    { "example.com", allok },
Packit 6c4009
    { "www.example.com", allok },
Packit 6c4009
    { "www.example.com.", allok },
Packit 6c4009
    { "www-.example.com.", allok },
Packit 6c4009
    { "www.-example.com.", allok },
Packit 6c4009
    { "*.example.com", dnok | mailok | ownok },
Packit 6c4009
    { "-v", dnok },
Packit 6c4009
    { "-v.example.com", mailok | dnok },
Packit 6c4009
    { "**.example.com", dnok | mailok },
Packit 6c4009
    { "www.example.com\\", 0 },
Packit 6c4009
    { STRING63, allnomailok },
Packit 6c4009
    { STRING63 ".", allnomailok },
Packit 6c4009
    { STRING63 "\\.", 0 },
Packit 6c4009
    { STRING63 "z", 0 },
Packit 6c4009
    { STRING63 ".example.com", allok },
Packit 6c4009
    { STRING63 "." STRING63 "." STRING63 "." STRING60 "z", allok },
Packit 6c4009
    { STRING63 "." STRING63 "." STRING63 "." STRING60 "z.", allok },
Packit 6c4009
    { STRING63 "." STRING63 "." STRING63 "." STRING60 "zz", 0 },
Packit 6c4009
    { STRING63 "." STRING63 "." STRING63 "." STRING60 "zzz", 0 },
Packit 6c4009
    { "hostmaster@mail.example.com", dnok | mailok },
Packit 6c4009
    { "hostmaster\\@mail.example.com", dnok | mailok },
Packit 6c4009
    { "with whitespace", 0 },
Packit 6c4009
    { "with\twhitespace", 0 },
Packit 6c4009
    { "with\nwhitespace", 0 },
Packit 6c4009
    { "with.whitespace ", 0 },
Packit 6c4009
    { "with.whitespace\t", 0 },
Packit 6c4009
    { "with.whitespace\n", 0 },
Packit 6c4009
    { "with\\ whitespace", 0 },
Packit 6c4009
    { "with\\\twhitespace", 0 },
Packit 6c4009
    { "with\\\nwhitespace", 0 },
Packit 6c4009
    { "with.whitespace\\ ", 0 },
Packit 6c4009
    { "with.whitespace\\\t", 0 },
Packit 6c4009
    { "with.whitespace\\\n", 0 },
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
/* Run test case *TEST with FUNC (named FUNCNAME) and report an error
Packit 6c4009
   if the result does not match the result flag at BIT.  */
Packit 6c4009
static void
Packit 6c4009
one_test (const struct test_case *test, const char *funcname,
Packit 6c4009
          int (*func) (const char *), unsigned int bit)
Packit 6c4009
{
Packit 6c4009
  int expected = (test->result & bit) != 0;
Packit 6c4009
  int actual = func (test->dn);
Packit 6c4009
  if (actual != expected)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: %s (\"%s\"): expected=%d, actual=%d\n",
Packit 6c4009
              funcname, test->dn, expected, actual);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Run 255 tests using all the bytes from 1 to 255, surround the byte
Packit 6c4009
   with the strings PREFIX and SUFFIX, and check that FUNC (named
Packit 6c4009
   FUNCNAME) accepts only those bytes listed in ACCEPTED.  */
Packit 6c4009
static void
Packit 6c4009
one_char (const char *prefix, const char *accepted, const char *suffix,
Packit 6c4009
          const char *funcname, int (*func) (const char *))
Packit 6c4009
{
Packit 6c4009
  for (int ch = 1; ch <= 255; ++ch)
Packit 6c4009
    {
Packit 6c4009
      char dn[1024];
Packit 6c4009
      snprintf (dn, sizeof (dn), "%s%c%s", prefix, ch, suffix);
Packit 6c4009
      int expected = strchr (accepted, ch) != NULL;
Packit 6c4009
      int actual = func (dn);
Packit 6c4009
      if (actual != expected)
Packit 6c4009
        {
Packit 6c4009
          support_record_failure ();
Packit 6c4009
          printf ("error: %s (\"%s\"): expected=%d, actual=%d\n",
Packit 6c4009
                  funcname, dn, expected, actual);
Packit 6c4009
        }
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define LETTERSDIGITS \
Packit 6c4009
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Packit 6c4009
Packit 6c4009
#define PRINTABLE \
Packit 6c4009
  "!\"#$%&'()*+,/:;<=>?@[\\]^`{|}~"
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  for (const struct test_case *test = tests; test < array_end (tests); ++test)
Packit 6c4009
    {
Packit 6c4009
      if (test_verbose)
Packit 6c4009
        printf ("info: testing domain name [[[%s]]] (0x%x)\n",
Packit 6c4009
                test->dn, test->result);
Packit 6c4009
      one_test (test, "res_hnok", res_hnok, hnok);
Packit 6c4009
      one_test (test, "res_dnok", res_dnok, dnok);
Packit 6c4009
      one_test (test, "res_mailok", res_mailok, mailok);
Packit 6c4009
      one_test (test, "res_ownok", res_ownok, ownok);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  one_char
Packit 6c4009
    ("", LETTERSDIGITS "._", "", "res_hnok", res_hnok);
Packit 6c4009
  one_char
Packit 6c4009
    ("middle",
Packit 6c4009
     LETTERSDIGITS ".-_\\", /* "middle\\suffix" == "middlesuffix", so good.  */
Packit 6c4009
     "suffix", "res_hnok", res_hnok);
Packit 6c4009
  one_char
Packit 6c4009
    ("middle",
Packit 6c4009
     LETTERSDIGITS ".-_" PRINTABLE,
Packit 6c4009
     "suffix.example", "res_mailok", res_mailok);
Packit 6c4009
  one_char
Packit 6c4009
    ("mailbox.middle",
Packit 6c4009
     LETTERSDIGITS ".-_\\",
Packit 6c4009
     "suffix.example", "res_mailok", res_mailok);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>