Consolidate pread/pread64 implementations
[glibc.git] / pwd / tst-putpwent.c
blob0e5547cd4364b65d8f9c2442462843c59a32a242
1 /* Test for processing of invalid passwd entries. [BZ #18724]
2 Copyright (C) 2015-2016 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 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <pwd.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
26 static bool errors;
28 static void
29 check (struct passwd p, const char *expected)
31 char *buf;
32 size_t buf_size;
33 FILE *f = open_memstream (&buf, &buf_size);
35 if (f == NULL)
37 printf ("open_memstream: %m\n");
38 errors = true;
39 return;
42 int ret = putpwent (&p, f);
44 if (expected == NULL)
46 if (ret == -1)
48 if (errno != EINVAL)
50 printf ("putpwent: unexpected error code: %m\n");
51 errors = true;
54 else
56 printf ("putpwent: unexpected success (\"%s\")\n", p.pw_name);
57 errors = true;
60 else
62 /* Expect success. */
63 size_t expected_length = strlen (expected);
64 if (ret == 0)
66 long written = ftell (f);
68 if (written <= 0 || fflush (f) < 0)
70 printf ("stream error: %m\n");
71 errors = true;
73 else if (buf[written - 1] != '\n')
75 printf ("FAILED: \"%s\" without newline\n", expected);
76 errors = true;
78 else if (strncmp (buf, expected, written - 1) != 0
79 || written - 1 != expected_length)
81 printf ("FAILED: \"%s\" (%ld), expected \"%s\" (%zu)\n",
82 buf, written - 1, expected, expected_length);
83 errors = true;
86 else
88 printf ("FAILED: putpwent (expected \"%s\"): %m\n", expected);
89 errors = true;
93 fclose (f);
94 free (buf);
97 static int
98 do_test (void)
100 check ((struct passwd) {
101 .pw_name = (char *) "root",
103 "root::0:0:::");
104 check ((struct passwd) {
105 .pw_name = (char *) "root",
106 .pw_passwd = (char *) "password",
108 "root:password:0:0:::");
109 check ((struct passwd) {
110 .pw_name = (char *) "root",
111 .pw_passwd = (char *) "password",
112 .pw_uid = 12,
113 .pw_gid = 34,
114 .pw_gecos = (char *) "gecos",
115 .pw_dir = (char *) "home",
116 .pw_shell = (char *) "shell",
118 "root:password:12:34:gecos:home:shell");
119 check ((struct passwd) {
120 .pw_name = (char *) "root",
121 .pw_passwd = (char *) "password",
122 .pw_uid = 12,
123 .pw_gid = 34,
124 .pw_gecos = (char *) ":ge\n:cos\n",
125 .pw_dir = (char *) "home",
126 .pw_shell = (char *) "shell",
128 "root:password:12:34: ge cos :home:shell");
130 /* Bad values. */
132 static const char *const bad_strings[] = {
133 ":",
134 "\n",
135 ":bad",
136 "\nbad",
137 "b:ad",
138 "b\nad",
139 "bad:",
140 "bad\n",
141 "b:a\nd",
142 NULL
144 for (const char *const *bad = bad_strings; *bad != NULL; ++bad)
146 check ((struct passwd) {
147 .pw_name = (char *) *bad,
148 }, NULL);
149 check ((struct passwd) {
150 .pw_name = (char *) "root",
151 .pw_passwd = (char *) *bad,
152 }, NULL);
153 check ((struct passwd) {
154 .pw_name = (char *) "root",
155 .pw_dir = (char *) *bad,
156 }, NULL);
157 check ((struct passwd) {
158 .pw_name = (char *) "root",
159 .pw_shell = (char *) *bad,
160 }, NULL);
164 return errors > 0;
167 #define TEST_FUNCTION do_test ()
168 #include "../test-skeleton.c"