(root)/
glibc-2.38/
resolv/
tst-ns_samebinaryname.c
       1  /* Test the __ns_samebinaryname function.
       2     Copyright (C) 2022-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <arpa/nameser.h>
      20  #include <array_length.h>
      21  #include <stdbool.h>
      22  #include <stdio.h>
      23  #include <support/check.h>
      24  
      25  /* First character denotes the comparison group: All names with the
      26     same first character are expected to compare equal.  */
      27  static const char *const cases[] =
      28    {
      29      " ",
      30      "1\001a", "1\001A",
      31      "2\002ab", "2\002aB", "2\002Ab", "2\002AB",
      32      "3\001a\002ab", "3\001A\002ab",
      33      "w\003www\007example\003com", "w\003Www\007Example\003Com",
      34      "w\003WWW\007EXAMPLE\003COM",
      35      "W\003WWW", "W\003www",
      36    };
      37  
      38  static int
      39  do_test (void)
      40  {
      41    for (int i = 0; i < array_length (cases); ++i)
      42      for (int j = 0; j < array_length (cases); ++j)
      43        {
      44          unsigned char *a = (unsigned char *) &cases[i][1];
      45          unsigned char *b = (unsigned char *) &cases[j][1];
      46          bool actual = __ns_samebinaryname (a, b);
      47          bool expected = cases[i][0] == cases[j][0];
      48          if (actual != expected)
      49            {
      50              char a1[NS_MAXDNAME];
      51              TEST_VERIFY (ns_name_ntop (a, a1, sizeof (a1)) > 0);
      52              char b1[NS_MAXDNAME];
      53              TEST_VERIFY (ns_name_ntop (b, b1, sizeof (b1)) > 0);
      54              printf ("error: \"%s\" \"%s\": expected %s\n",
      55                      a1, b1, expected ? "equal" : "unqueal");
      56              support_record_failure ();
      57            }
      58        }
      59    return 0;
      60  }
      61  
      62  #include <support/test-driver.c>