1 /* Test for processing of invalid group entries. [BZ #18724]
2 Copyright (C) 2015-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
29 check (struct group e
, const char *expected
)
33 FILE *f
= open_memstream (&buf
, &buf_size
);
37 printf ("open_memstream: %m\n");
42 int ret
= putgrent (&e
, f
);
50 printf ("putgrent: unexpected error code: %m\n");
56 printf ("putgrent: unexpected success (\"%s\", \"%s\")\n",
57 e
.gr_name
, e
.gr_passwd
);
64 size_t expected_length
= strlen (expected
);
67 long written
= ftell (f
);
69 if (written
<= 0 || fflush (f
) < 0)
71 printf ("stream error: %m\n");
74 else if (buf
[written
- 1] != '\n')
76 printf ("FAILED: \"%s\" without newline\n", expected
);
79 else if (strncmp (buf
, expected
, written
- 1) != 0
80 || written
- 1 != expected_length
)
82 buf
[written
- 1] = '\0';
83 printf ("FAILED: \"%s\" (%ld), expected \"%s\" (%zu)\n",
84 buf
, written
- 1, expected
, expected_length
);
90 printf ("FAILED: putgrent (expected \"%s\"): %m\n", expected
);
102 check ((struct group
) {
103 .gr_name
= (char *) "root",
106 check ((struct group
) {
107 .gr_name
= (char *) "root",
108 .gr_passwd
= (char *) "password",
110 .gr_mem
= (char *[2]) {(char *) "member1", NULL
}
112 "root:password:1234:member1");
113 check ((struct group
) {
114 .gr_name
= (char *) "root",
115 .gr_passwd
= (char *) "password",
117 .gr_mem
= (char *[3]) {(char *) "member1", (char *) "member2", NULL
}
119 "root:password:1234:member1,member2");
123 static const char *const bad_strings
[] = {
141 for (const char *const *bad
= bad_strings
; *bad
!= NULL
; ++bad
)
144 = {(char *) "first", (char *) *bad
, (char *) "last", NULL
};
145 if (strpbrk (*bad
, ":\n") != NULL
)
147 check ((struct group
) {
148 .gr_name
= (char *) *bad
,
150 check ((struct group
) {
151 .gr_name
= (char *) "root",
152 .gr_passwd
= (char *) *bad
,
155 check ((struct group
) {
156 .gr_name
= (char *) "root",
157 .gr_passwd
= (char *) "password",
166 #define TEST_FUNCTION do_test ()
167 #include "../test-skeleton.c"