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