summaryrefslogtreecommitdiff
path: root/nss/tst-nss-test1.c
blob: 4af3599bc1574984415a2d10595540df44983a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Basic test of passwd database handling.
   Copyright (C) 2017-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <nss.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <support/support.h>

#include "nss_test.h"

static int hook_called = 0;

/* Note: the values chosen here are arbitrary; they need only be
   unique within the table.  However, they do need to match the
   "pwdids" array further down.  */
static struct passwd pwd_table[] = {
    PWD (100),
    PWD (30),
    PWD (200),
    PWD (60),
    PWD (20000),
    PWD_LAST ()
  };

void
_nss_test1_init_hook(test_tables *t)
{
  hook_called = 1;
  t->pwd_table = pwd_table;
}

static int
do_test (void)
{
  int retval = 0;

  __nss_configure_lookup ("passwd", "test1");

  /* This must match the pwd_table above.  */
  static const unsigned int pwdids[] = { 100, 30, 200, 60, 20000 };
#define npwdids (sizeof (pwdids) / sizeof (pwdids[0]))

  setpwent ();

  const unsigned int *np = pwdids;
  for (struct passwd *p = getpwent (); p != NULL; ++np, p = getpwent ())
    {
      retval += compare_passwds (np-pwdids, p, & pwd_table[np-pwdids]);

      if (p->pw_uid != *np || strncmp (p->pw_name, "name", 4) != 0
	  || atol (p->pw_name + 4) != *np)
	{
	  printf ("FAIL: passwd entry %td wrong (%s, %u)\n",
		  np - pwdids, p->pw_name, p->pw_uid);
	  retval = 1;
	  break;
	}
    }

  endpwent ();

  for (int i = npwdids - 1; i >= 0; --i)
    {
      char buf[30];
      snprintf (buf, sizeof (buf), "name%u", pwdids[i]);

      struct passwd *p = getpwnam (buf);
      if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
	{
	  printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
	  retval = 1;
	}

      p = getpwuid (pwdids[i]);
      if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
	{
	  printf ("FAIL: passwd entry %u wrong\n", pwdids[i]);
	  retval = 1;
	}

      snprintf (buf, sizeof (buf), "name%u", pwdids[i] + 1);

      p = getpwnam (buf);
      if (p != NULL)
	{
	  printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
	  retval = 1;
	}

      p = getpwuid (pwdids[i] + 1);
      if (p != NULL)
	{
	  printf ("FAIL: passwd entry %u wrong\n", pwdids[i] + 1);
	  retval = 1;
	}
    }

  if (!hook_called)
    {
      retval = 1;
      printf("FAIL: init hook never called\n");
    }

  return retval;
}

#include <support/test-driver.c>