Add math benchmark latency test
[glibc.git] / nss / tst-nss-test1.c
blobff1327221921ad24ef183c42fbbafce128548fee
1 /* Basic test of passwd database handling.
2 Copyright (C) 2017 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 <nss.h>
20 #include <pwd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "nss_test.h"
27 static int hook_called = 0;
29 /* Note: the values chosen here are arbitrary; they need only be
30 unique within the table. However, they do need to match the
31 "pwdids" array further down. */
32 static struct passwd pwd_table[] = {
33 PWD (100),
34 PWD (30),
35 PWD (200),
36 PWD (60),
37 PWD (20000),
38 PWD_LAST ()
41 void
42 _nss_test1_init_hook(test_tables *t)
44 hook_called = 1;
45 t->pwd_table = pwd_table;
48 static int
49 do_test (void)
51 int retval = 0;
53 __nss_configure_lookup ("passwd", "test1");
55 /* This must match the pwd_table above. */
56 static const unsigned int pwdids[] = { 100, 30, 200, 60, 20000 };
57 #define npwdids (sizeof (pwdids) / sizeof (pwdids[0]))
59 setpwent ();
61 const unsigned int *np = pwdids;
62 for (struct passwd *p = getpwent (); p != NULL; ++np, p = getpwent ())
64 retval += compare_passwds (np-pwdids, p, & pwd_table[np-pwdids]);
66 if (p->pw_uid != *np || strncmp (p->pw_name, "name", 4) != 0
67 || atol (p->pw_name + 4) != *np)
69 printf ("FAIL: passwd entry %td wrong (%s, %u)\n",
70 np - pwdids, p->pw_name, p->pw_uid);
71 retval = 1;
72 break;
76 endpwent ();
78 for (int i = npwdids - 1; i >= 0; --i)
80 char buf[30];
81 snprintf (buf, sizeof (buf), "name%u", pwdids[i]);
83 struct passwd *p = getpwnam (buf);
84 if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
86 printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
87 retval = 1;
90 p = getpwuid (pwdids[i]);
91 if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
93 printf ("FAIL: passwd entry %u wrong\n", pwdids[i]);
94 retval = 1;
97 snprintf (buf, sizeof (buf), "name%u", pwdids[i] + 1);
99 p = getpwnam (buf);
100 if (p != NULL)
102 printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
103 retval = 1;
106 p = getpwuid (pwdids[i] + 1);
107 if (p != NULL)
109 printf ("FAIL: passwd entry %u wrong\n", pwdids[i] + 1);
110 retval = 1;
114 if (!hook_called)
116 retval = 1;
117 printf("FAIL: init hook never called\n");
120 return retval;
123 #define TEST_FUNCTION do_test ()
124 #include "../test-skeleton.c"