Sync "language", "lang_name", "territory", "country_name" with CLDR/langtable
[glibc.git] / nss / tst-nss-test1.c
blob4af3599bc1574984415a2d10595540df44983a80
1 /* Basic test of passwd database handling.
2 Copyright (C) 2017-2019 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/>. */
19 #include <nss.h>
20 #include <pwd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <support/support.h>
27 #include "nss_test.h"
29 static int hook_called = 0;
31 /* Note: the values chosen here are arbitrary; they need only be
32 unique within the table. However, they do need to match the
33 "pwdids" array further down. */
34 static struct passwd pwd_table[] = {
35 PWD (100),
36 PWD (30),
37 PWD (200),
38 PWD (60),
39 PWD (20000),
40 PWD_LAST ()
43 void
44 _nss_test1_init_hook(test_tables *t)
46 hook_called = 1;
47 t->pwd_table = pwd_table;
50 static int
51 do_test (void)
53 int retval = 0;
55 __nss_configure_lookup ("passwd", "test1");
57 /* This must match the pwd_table above. */
58 static const unsigned int pwdids[] = { 100, 30, 200, 60, 20000 };
59 #define npwdids (sizeof (pwdids) / sizeof (pwdids[0]))
61 setpwent ();
63 const unsigned int *np = pwdids;
64 for (struct passwd *p = getpwent (); p != NULL; ++np, p = getpwent ())
66 retval += compare_passwds (np-pwdids, p, & pwd_table[np-pwdids]);
68 if (p->pw_uid != *np || strncmp (p->pw_name, "name", 4) != 0
69 || atol (p->pw_name + 4) != *np)
71 printf ("FAIL: passwd entry %td wrong (%s, %u)\n",
72 np - pwdids, p->pw_name, p->pw_uid);
73 retval = 1;
74 break;
78 endpwent ();
80 for (int i = npwdids - 1; i >= 0; --i)
82 char buf[30];
83 snprintf (buf, sizeof (buf), "name%u", pwdids[i]);
85 struct passwd *p = getpwnam (buf);
86 if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
88 printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
89 retval = 1;
92 p = getpwuid (pwdids[i]);
93 if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
95 printf ("FAIL: passwd entry %u wrong\n", pwdids[i]);
96 retval = 1;
99 snprintf (buf, sizeof (buf), "name%u", pwdids[i] + 1);
101 p = getpwnam (buf);
102 if (p != NULL)
104 printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
105 retval = 1;
108 p = getpwuid (pwdids[i] + 1);
109 if (p != NULL)
111 printf ("FAIL: passwd entry %u wrong\n", pwdids[i] + 1);
112 retval = 1;
116 if (!hook_called)
118 retval = 1;
119 printf("FAIL: init hook never called\n");
122 return retval;
125 #include <support/test-driver.c>