tst-ns_samebinaryname.c 2.1 KB

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