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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/* Common code for NSS test cases.
Copyright (C) 2017-2025 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/>. */
/* There are two (or more) NSS test modules named nss_test1,
nss_test2, etc. Each one will call a function IN THE TEST CASE
called _nss_test1_init_hook(test_tables *) (or _nss_test2_*, etc).
In your copy of the hook function, you may change the *_table
pointers in the passed struct to point to static tables in your
test case, and the test modules will use that table instead.
Your tables MUST end with an entry that has a *_LAST() macro.
Use the *_ISLAST() macro to test for end of list.
Use __nss_configure_lookup("passwd", "test1 test2") (for example) to
configure NSS to use the test modules. */
#include <pwd.h>
#include <grp.h>
#include <shadow.h>
#include <netdb.h>
typedef struct test_tables {
struct passwd *pwd_table;
struct group *grp_table;
struct spwd *spwd_table;
struct hostent *host_table;
} test_tables;
extern void _nss_test1_init_hook (test_tables *) __attribute__((weak));
extern void _nss_test2_init_hook (test_tables *) __attribute__((weak));
#define PWD_LAST() { .pw_name = NULL, .pw_uid = 0 }
#define GRP_LAST() { .gr_name = NULL, .gr_gid = 0 }
#define SPWD_LAST() { .sp_namp = NULL, .sp_pwdp = NULL }
#define HOST_LAST() { .h_name = NULL, .h_aliases = NULL, .h_length = 0, .h_addr_list = NULL }
#define PWD_ISLAST(p) ((p)->pw_name == NULL && (p)->pw_uid == 0)
#define GRP_ISLAST(g) ((g)->gr_name == NULL && (g)->gr_gid == 0)
#define SPWD_ISLAST(s) ((s)->sp_namp == NULL && (s)->sp_pwdp == 0)
#define HOST_ISLAST(h) ((h)->h_name == NULL && (h)->h_length == 0)
/* Macros to fill in the tables easily. */
/* Note that the "unparameterized" fields are not magic; they're just
arbitrary values. Tests which need to verify those fields should
fill them in explicitly. */
#define PWD(u) \
{ .pw_name = (char *) "name" #u, .pw_passwd = (char *) "*", .pw_uid = u, \
.pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*", \
.pw_shell = (char *) "*" }
#define PWD_N(u,n) \
{ .pw_name = (char *) n, .pw_passwd = (char *) "*", .pw_uid = u, \
.pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*", \
.pw_shell = (char *) "*" }
#define GRP(u) \
{ .gr_name = (char *) "name" #u, .gr_passwd = (char *) "*", .gr_gid = u, \
.gr_mem = (char **) group_##u }
#define GRP_N(u,n,m) \
{ .gr_name = (char *) n, .gr_passwd = (char *) "*", .gr_gid = u, \
.gr_mem = (char **) m }
#define SPWD(u) \
{ .sp_namp = (char *) "name" #u, .sp_pwdp = (char *) "passwd" #u }
#define HOST(u) \
{ .h_name = (char *) "name" #u, .h_aliases = NULL, .h_addrtype = u, \
.h_length = 4, \
.h_addr_list = (char **) hostaddr_##u }
/*------------------------------------------------------------*/
/* Helper functions for testing passwd entries. Call
compare_passwds() passing a test index, the passwd entry you got,
and the expected passwd entry. The function will return the number
of mismatches, or zero of the two records are the same. */
static void __attribute__((used))
print_passwd (struct passwd *p)
{
printf (" passwd %u.%s (%s) :", p->pw_uid, p->pw_name, p->pw_passwd);
printf (" %u, %s, %s, %s\n", p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
printf ("\n");
}
static int __attribute__((used))
compare_passwd_field (int i, struct passwd *p, const char *got,
const char *exp, const char *name)
{
/* Does the entry have a value? */
if (got == NULL)
{
printf ("[%d] passwd %s for %u.%s was (null)\n",
i, name,
p->pw_uid, p->pw_name);
return 1;
}
/* Does the entry have an unexpected name? */
else if (exp == NULL)
{
printf ("[%d] passwd %s for %u.(null) was %s\n",
i, name,
p->pw_uid, got);
return 1;
}
/* And is it correct? */
else if (got && strcmp (got, exp) != 0)
{
printf("[%d] passwd entry %u.%s had %s \"%s\" (expected \"%s\") \n",
i,
p->pw_uid, p->pw_name, name,
got, exp);
return 1;
}
return 0;
}
#define COMPARE_PWD_FIELD(f) \
retval += compare_passwd_field (i, e, p->f, e->f, #f)
/* Compare passwd to expected passwd, return number of "problems".
"I" is the index into the testcase data. */
static int __attribute__((used))
compare_passwds (int i, struct passwd *p, struct passwd *e)
{
int retval = 0;
/* Did we get the expected uid? */
if (p->pw_uid != e->pw_uid)
{
printf("[%d] passwd entry %u.%s had uid %u\n", i,
e->pw_uid, e->pw_name,
p->pw_uid);
++retval;
}
/* Did we get the expected gid? */
if (p->pw_gid != e->pw_gid)
{
printf("[%d] passwd entry %u.%s had gid %u (expected %u)\n", i,
e->pw_uid, e->pw_name,
p->pw_gid, e->pw_gid);
++retval;
}
COMPARE_PWD_FIELD (pw_name);
COMPARE_PWD_FIELD (pw_passwd);
COMPARE_PWD_FIELD (pw_gecos);
COMPARE_PWD_FIELD (pw_dir);
COMPARE_PWD_FIELD (pw_shell);
if (retval > 0)
{
/* Left in for debugging later, if needed. */
print_passwd (p);
print_passwd (e);
}
return retval;
}
/*------------------------------------------------------------*/
/* Helpers for checking group entries. See passwd helper comment
above for details. */
static void __attribute__((used))
print_group (struct group *g)
{
int j;
printf (" group %u.%s (%s) :", g->gr_gid, g->gr_name, g->gr_passwd);
if (g->gr_mem)
for (j=0; g->gr_mem[j]; j++)
printf ("%s%s", j==0 ? " " : ", ", g->gr_mem[j]);
printf ("\n");
}
/* Compare group to expected group, return number of "problems". "I"
is the index into the testcase data. */
static int __attribute__((used))
compare_groups (int i, struct group *g, struct group *e)
{
int j;
int retval = 0;
/* Did we get the expected gid? */
if (g->gr_gid != e->gr_gid)
{
printf("[%d] group entry %u.%s had gid %u\n", i,
e->gr_gid, e->gr_name,
g->gr_gid);
++retval;
}
/* Does the entry have a name? */
if (g->gr_name == NULL)
{
printf ("[%d] group name for %u.%s was (null)\n", i,
e->gr_gid, e->gr_name);
++retval;
}
/* Does the entry have an unexpected name? */
else if (e->gr_name == NULL)
{
printf ("[%d] group name for %u.(null) was %s\n", i,
e->gr_gid, g->gr_name);
++retval;
}
/* And is it correct? */
else if (strcmp (g->gr_name, e->gr_name) != 0)
{
printf("[%d] group entry %u.%s had name \"%s\"\n", i,
e->gr_gid, e->gr_name,
g->gr_name);
++retval;
}
/* Does the entry have a password? */
if (g->gr_passwd == NULL && e->gr_passwd != NULL)
{
printf ("[%d] group password for %u.%s was NULL\n", i,
e->gr_gid, e->gr_name);
++retval;
}
else if (g->gr_passwd != NULL && e->gr_passwd == NULL)
{
printf ("[%d] group password for %u.%s was not NULL\n", i,
e->gr_gid, e->gr_name);
++retval;
}
/* And is it correct? */
else if (g->gr_passwd && strcmp (g->gr_passwd, e->gr_passwd) != 0)
{
printf("[%d] group entry %u.%s had password \"%s\" (not \"%s\")\n", i,
e->gr_gid, e->gr_name,
g->gr_passwd, e->gr_passwd);
++retval;
}
/* Now compare group members... */
if (e->gr_mem != NULL && g->gr_mem == NULL)
{
printf("[%d] group entry %u.%s missing member list\n", i,
e->gr_gid, e->gr_name);
++retval;
}
else if (e->gr_mem == NULL && g->gr_mem != NULL)
{
printf("[%d] group entry %u.%s has unexpected member list\n", i,
e->gr_gid, e->gr_name);
++retval;
}
else if (e->gr_mem == NULL && g->gr_mem == NULL)
{
/* This case is OK. */
}
else
{
/* Compare two existing lists. */
j = 0;
for (;;)
{
if (g->gr_mem[j] == NULL && e->gr_mem[j] == NULL)
{
/* Matching end-of-lists. */
break;
}
if (g->gr_mem[j] == NULL)
{
printf ("[%d] group member list for %u.%s is too short.\n", i,
e->gr_gid, e->gr_name);
++retval;
break;
}
if (e->gr_mem[j] == NULL)
{
printf ("[%d] group member list for %u.%s is too long.\n", i,
e->gr_gid, e->gr_name);
++retval;
break;
}
if (strcmp (g->gr_mem[j], e->gr_mem[j]) != 0)
{
printf ("[%d] group member list for %u.%s differs: %s vs %s.\n", i,
e->gr_gid, e->gr_name,
e->gr_mem[j], g->gr_mem[j]);
++retval;
}
j++;
}
}
if (retval > 0)
{
/* Left in for debugging later, if needed. */
print_group (g);
print_group (e);
}
return retval;
}
|