diff options
| author | DJ Delorie <dj@delorie.com> | 2017-07-17 15:50:43 -0400 |
|---|---|---|
| committer | DJ Delorie <dj@delorie.com> | 2017-07-17 15:52:44 -0400 |
| commit | ae5c498d93d049d9574d3f8f18e62cac64cbdf5c (patch) | |
| tree | 986e51e120bd42c02fe2ee5a4b04ca13951600e3 | |
| parent | 48145e1c7dabaeab7ad8346d14f910cdeb6bc910 (diff) | |
| download | glibc-ae5c498d93d049d9574d3f8f18e62cac64cbdf5c.tar.xz glibc-ae5c498d93d049d9574d3f8f18e62cac64cbdf5c.zip | |
Extend NSS test suite
* nss/nss_test.h: New.
* nss/nss_test1.h: Rewrite to use test-provided data. Add group
tests. Parameterize to allow multiple instances.
* nss/nss_test2.h: New. Second instance.
* nss/nss_test.ver: New.
* nss/nss_test1.c: Update to use new framework.
* nss/nss_test2.c: New.
* nss/nss_test3.c: New.
* nss/nss_test4.c: New.
* nss/nss_test5.c: New.
* nss/Makefile: Build new tests.
* shlib-versions: Add libnss_test2.
| -rw-r--r-- | ChangeLog | 15 | ||||
| -rw-r--r-- | nss/Makefile | 27 | ||||
| -rw-r--r-- | nss/nss_test.h | 308 | ||||
| -rw-r--r-- | nss/nss_test.ver | 4 | ||||
| -rw-r--r-- | nss/nss_test1.c | 311 | ||||
| -rw-r--r-- | nss/nss_test2.c | 20 | ||||
| -rw-r--r-- | nss/tst-nss-test1.c | 76 | ||||
| -rw-r--r-- | nss/tst-nss-test2.c | 136 | ||||
| -rw-r--r-- | nss/tst-nss-test3.c | 150 | ||||
| -rw-r--r-- | nss/tst-nss-test4.c | 137 | ||||
| -rw-r--r-- | nss/tst-nss-test5.c | 108 | ||||
| -rw-r--r-- | shlib-versions | 1 |
12 files changed, 1201 insertions, 92 deletions
@@ -1,3 +1,18 @@ +2017-07-17 DJ Delorie <dj@redhat.com> + + * nss/nss_test.h: New. + * nss/nss_test1.h: Rewrite to use test-provided data. Add group + tests. Parameterize to allow multiple instances. + * nss/nss_test2.h: New. Second instance. + * nss/nss_test.ver: New. + * nss/nss_test1.c: Update to use new framework. + * nss/nss_test2.c: New. + * nss/nss_test3.c: New. + * nss/nss_test4.c: New. + * nss/nss_test5.c: New. + * nss/Makefile: Build new tests. + * shlib-versions: Add libnss_test2. + 2017-07-17 Adhemerval Zanella <adhemerval.zanella@linaro.org> [BZ #21512] diff --git a/nss/Makefile b/nss/Makefile index 430be8726f..d9f6d41181 100644 --- a/nss/Makefile +++ b/nss/Makefile @@ -50,8 +50,12 @@ extra-objs += $(makedb-modules:=.o) tests-static = tst-field tests-internal = tst-field -tests = test-netdb tst-nss-test1 test-digits-dots \ - tst-nss-getpwent bug17079 +tests = test-netdb test-digits-dots tst-nss-getpwent bug17079 \ + tst-nss-test1 \ + tst-nss-test2 \ + tst-nss-test3 \ + tst-nss-test4 \ + tst-nss-test5 xtests = bug-erange # If we have a thread library then we can test cancellation against @@ -94,7 +98,7 @@ routines += $(libnss_files-routines) static-only-routines += $(libnss_files-routines) tests-static += tst-nss-static endif -extra-test-objs += nss_test1.os +extra-test-objs += nss_test1.os nss_test2.os include ../Rules @@ -123,14 +127,29 @@ $(objpfx)makedb: $(makedb-modules:%=$(objpfx)%.o) $(inst_vardbdir)/Makefile: db-Makefile $(+force) $(do-install) +libnss_test1.so-no-z-defs = 1 +libnss_test2.so-no-z-defs = 1 + +rtld-tests-LDFLAGS += -Wl,--dynamic-list=nss_test.ver + libof-nss_test1 = extramodules +libof-nss_test2 = extramodules $(objpfx)/libnss_test1.so: $(objpfx)nss_test1.os $(link-libc-deps) $(build-module) +$(objpfx)/libnss_test2.so: $(objpfx)nss_test2.os $(link-libc-deps) + $(build-module) +$(objpfx)nss_test2.os : nss_test1.c ifdef libnss_test1.so-version $(objpfx)/libnss_test1.so$(libnss_test1.so-version): $(objpfx)/libnss_test1.so $(make-link) endif -$(objpfx)tst-nss-test1.out: $(objpfx)/libnss_test1.so$(libnss_test1.so-version) +ifdef libnss_test2.so-version +$(objpfx)/libnss_test2.so$(libnss_test2.so-version): $(objpfx)/libnss_test2.so + $(make-link) +endif +$(patsubst %,$(objpfx)%.out,$(tests)) : \ + $(objpfx)/libnss_test1.so$(libnss_test1.so-version) \ + $(objpfx)/libnss_test2.so$(libnss_test2.so-version) ifeq (yes,$(have-thread-library)) $(objpfx)tst-cancel-getpwuid_r: $(shared-thread-library) diff --git a/nss/nss_test.h b/nss/nss_test.h new file mode 100644 index 0000000000..0a0e00b4e6 --- /dev/null +++ b/nss/nss_test.h @@ -0,0 +1,308 @@ +/* Common code for NSS test cases. + Copyright (C) 2017 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 + <http://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> + +typedef struct test_tables { + struct passwd *pwd_table; + struct group *grp_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 PWD_ISLAST(p) ((p)->pw_name == NULL && (p)->pw_uid == 0) +#define GRP_ISLAST(g) ((g)->gr_name == NULL && (g)->gr_gid == 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 } + +/*------------------------------------------------------------*/ + +/* 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; +} diff --git a/nss/nss_test.ver b/nss/nss_test.ver new file mode 100644 index 0000000000..2e21176b40 --- /dev/null +++ b/nss/nss_test.ver @@ -0,0 +1,4 @@ +{ + _nss_test1_init_hook; + _nss_test2_init_hook; +}; diff --git a/nss/nss_test1.c b/nss/nss_test1.c index 3beb488fcf..b728e418a3 100644 --- a/nss/nss_test1.c +++ b/nss/nss_test1.c @@ -1,84 +1,168 @@ +/* Template generic NSS service provider. See nss_test.h for usage. + Copyright (C) 2017 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 + <http://www.gnu.org/licenses/>. */ + #include <errno.h> #include <nss.h> #include <pthread.h> #include <string.h> +#include <stdio.h> +#include <alloc_buffer.h> -#define COPY_IF_ROOM(s) \ - ({ size_t len_ = strlen (s) + 1; \ - char *start_ = cp; \ - buflen - (cp - buffer) < len_ \ - ? NULL \ - : (cp = mempcpy (cp, s, len_), start_); }) +/* We need to be able to handle NULLs "properly" within the testsuite, + to test known bad data. */ +#define alloc_buffer_maybe_copy_string(b,s) s ? alloc_buffer_copy_string (b, s) : NULL; +/* This file is the master template. Other instances of this test + module should define NAME(x) to have their name instead of "test1", + then include this file. +*/ +#define NAME_(x,n) _nss_##n##_##x +#ifndef NAME +#define NAME(x) NAME_(x,test1) +#endif +#define NAMESTR__(x) #x +#define NAMESTR_(x) NAMESTR__(x) +#define NAMESTR(x) NAMESTR_(NAME(x)) -/* Password handling. */ -#include <pwd.h> +#include "nss_test.h" + +/* -------------------------------------------------- */ +/* Default Data. */ -static struct passwd pwd_data[] = +static struct passwd default_pwd_data[] = { #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 *) "*" } - PWD (100), PWD (30), + PWD (100), PWD (200), PWD (60), PWD (20000) }; -#define npwd_data (sizeof (pwd_data) / sizeof (pwd_data[0])) +#define default_npwd_data (sizeof (pwd_data) / sizeof (pwd_data[0])) + +static struct passwd *pwd_data = default_pwd_data; +static int npwd_data = default_npwd_data; + +static struct group *grp_data = NULL; +static int ngrp_data = 0; + +/* This function will get called, and once per session, look back into + the test case's executable for an init hook function, and call + it. */ + +static int initted = 0; +static void +init(void) +{ + test_tables t; + int i; + + if (initted) + return; + if (NAME(init_hook)) + { + memset (&t, 0, sizeof(t)); + NAME(init_hook)(&t); + + if (t.pwd_table) + { + pwd_data = t.pwd_table; + for (i=0; ! PWD_ISLAST(& pwd_data[i]); i++) + ; + npwd_data = i; + } + + if (t.grp_table) + { + grp_data = t.grp_table; + for (i=0; ! GRP_ISLAST(& grp_data[i]); i++) + ; + ngrp_data = i; + } + } + initted = 1; +} + +/* -------------------------------------------------- */ +/* Password handling. */ static size_t pwd_iter; #define CURPWD pwd_data[pwd_iter] static pthread_mutex_t pwd_lock = PTHREAD_MUTEX_INITIALIZER; - enum nss_status -_nss_test1_setpwent (int stayopen) +NAME(setpwent) (int stayopen) { + init(); pwd_iter = 0; return NSS_STATUS_SUCCESS; } enum nss_status -_nss_test1_endpwent (void) +NAME(endpwent) (void) { + init(); return NSS_STATUS_SUCCESS; } +static enum nss_status +copy_passwd (struct passwd *result, struct passwd *local, + char *buffer, size_t buflen, int *errnop) +{ + struct alloc_buffer buf = alloc_buffer_create (buffer, buflen); + + result->pw_name = alloc_buffer_maybe_copy_string (&buf, local->pw_name); + result->pw_passwd = alloc_buffer_maybe_copy_string (&buf, local->pw_passwd); + result->pw_uid = local->pw_uid; + result->pw_gid = local->pw_gid; + result->pw_gecos = alloc_buffer_maybe_copy_string (&buf, local->pw_gecos); + result->pw_dir = alloc_buffer_maybe_copy_string (&buf, local->pw_dir); + result->pw_shell = alloc_buffer_maybe_copy_string (&buf, local->pw_shell); + + if (alloc_buffer_has_failed (&buf)) + { + *errnop = ERANGE; + return NSS_STATUS_TRYAGAIN; + } + + return NSS_STATUS_SUCCESS; +} enum nss_status -_nss_test1_getpwent_r (struct passwd *result, char *buffer, size_t buflen, +NAME(getpwent_r) (struct passwd *result, char *buffer, size_t buflen, int *errnop) { - char *cp = buffer; int res = NSS_STATUS_SUCCESS; + init(); pthread_mutex_lock (&pwd_lock); if (pwd_iter >= npwd_data) res = NSS_STATUS_NOTFOUND; else { - result->pw_name = COPY_IF_ROOM (CURPWD.pw_name); - result->pw_passwd = COPY_IF_ROOM (CURPWD.pw_passwd); - result->pw_uid = CURPWD.pw_uid; - result->pw_gid = CURPWD.pw_gid; - result->pw_gecos = COPY_IF_ROOM (CURPWD.pw_gecos); - result->pw_dir = COPY_IF_ROOM (CURPWD.pw_dir); - result->pw_shell = COPY_IF_ROOM (CURPWD.pw_shell); - - if (result->pw_name == NULL || result->pw_passwd == NULL - || result->pw_gecos == NULL || result->pw_dir == NULL - || result->pw_shell == NULL) - { - *errnop = ERANGE; - res = NSS_STATUS_TRYAGAIN; - } - + res = copy_passwd (result, &CURPWD, buffer, buflen, errnop); ++pwd_iter; } @@ -89,65 +173,140 @@ _nss_test1_getpwent_r (struct passwd *result, char *buffer, size_t buflen, enum nss_status -_nss_test1_getpwuid_r (uid_t uid, struct passwd *result, char *buffer, +NAME(getpwuid_r) (uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop) { + init(); for (size_t idx = 0; idx < npwd_data; ++idx) if (pwd_data[idx].pw_uid == uid) - { - char *cp = buffer; - int res = NSS_STATUS_SUCCESS; - - result->pw_name = COPY_IF_ROOM (pwd_data[idx].pw_name); - result->pw_passwd = COPY_IF_ROOM (pwd_data[idx].pw_passwd); - result->pw_uid = pwd_data[idx].pw_uid; - result->pw_gid = pwd_data[idx].pw_gid; - result->pw_gecos = COPY_IF_ROOM (pwd_data[idx].pw_gecos); - result->pw_dir = COPY_IF_ROOM (pwd_data[idx].pw_dir); - result->pw_shell = COPY_IF_ROOM (pwd_data[idx].pw_shell); - - if (result->pw_name == NULL || result->pw_passwd == NULL - || result->pw_gecos == NULL || result->pw_dir == NULL - || result->pw_shell == NULL) - { - *errnop = ERANGE; - res = NSS_STATUS_TRYAGAIN; - } - - return res; - } + return copy_passwd (result, &pwd_data[idx], buffer, buflen, errnop); return NSS_STATUS_NOTFOUND; } enum nss_status -_nss_test1_getpwnam_r (const char *name, struct passwd *result, char *buffer, +NAME(getpwnam_r) (const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop) { + init(); for (size_t idx = 0; idx < npwd_data; |
