tst-field.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Test for invalid field handling in file-style NSS databases. [BZ #18724]
  2. Copyright (C) 2015-2019 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. <http://www.gnu.org/licenses/>. */
  15. /* This test needs to be statically linked because it access hidden
  16. functions. */
  17. #include <nss.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <support/support.h>
  23. static bool errors;
  24. static void
  25. check (const char *what, bool expr)
  26. {
  27. if (!expr)
  28. {
  29. printf ("FAIL: %s\n", what);
  30. errors = true;
  31. }
  32. }
  33. #define CHECK(expr) check (#expr, (expr))
  34. static void
  35. check_rewrite (const char *input, const char *expected)
  36. {
  37. char *to_free;
  38. const char *result = __nss_rewrite_field (input, &to_free);
  39. CHECK (result != NULL);
  40. if (result != NULL && strcmp (result, expected) != 0)
  41. {
  42. printf ("FAIL: rewrite \"%s\" -> \"%s\", expected \"%s\"\n",
  43. input, result, expected);
  44. errors = true;
  45. }
  46. free (to_free);
  47. }
  48. static int
  49. do_test (void)
  50. {
  51. CHECK (__nss_valid_field (NULL));
  52. CHECK (__nss_valid_field (""));
  53. CHECK (__nss_valid_field ("+"));
  54. CHECK (__nss_valid_field ("-"));
  55. CHECK (__nss_valid_field (" "));
  56. CHECK (__nss_valid_field ("abcdef"));
  57. CHECK (__nss_valid_field ("abc def"));
  58. CHECK (__nss_valid_field ("abc\tdef"));
  59. CHECK (!__nss_valid_field ("abcdef:"));
  60. CHECK (!__nss_valid_field ("abcde:f"));
  61. CHECK (!__nss_valid_field (":abcdef"));
  62. CHECK (!__nss_valid_field ("abcdef\n"));
  63. CHECK (!__nss_valid_field ("\nabcdef"));
  64. CHECK (!__nss_valid_field (":"));
  65. CHECK (!__nss_valid_field ("\n"));
  66. CHECK (__nss_valid_list_field (NULL));
  67. CHECK (__nss_valid_list_field ((char *[]) {(char *) "good", NULL}));
  68. CHECK (!__nss_valid_list_field ((char *[]) {(char *) "g,ood", NULL}));
  69. CHECK (!__nss_valid_list_field ((char *[]) {(char *) "g\nood", NULL}));
  70. CHECK (!__nss_valid_list_field ((char *[]) {(char *) "g:ood", NULL}));
  71. check_rewrite (NULL, "");
  72. check_rewrite ("", "");
  73. check_rewrite ("abc", "abc");
  74. check_rewrite ("abc\n", "abc ");
  75. check_rewrite ("abc:", "abc ");
  76. check_rewrite ("\nabc", " abc");
  77. check_rewrite (":abc", " abc");
  78. check_rewrite (":", " ");
  79. check_rewrite ("\n", " ");
  80. check_rewrite ("a:b:c", "a b c");
  81. check_rewrite ("a\nb\nc", "a b c");
  82. check_rewrite ("a\nb:c", "a b c");
  83. check_rewrite ("aa\nbb\ncc", "aa bb cc");
  84. check_rewrite ("aa\nbb:cc", "aa bb cc");
  85. return errors;
  86. }
  87. #include <support/test-driver.c>