summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2018-07-02 12:38:33 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2018-07-02 12:38:33 +0000
commit3cb4106b13aa883f3b704aa4026b78fd36db3756 (patch)
tree7909fa214055aa344a71b515c8781c03db32ab79 /src
parent42c3adc8841167026bb8409cb5e4390a32bff49a (diff)
downloadnsss-3cb4106b13aa883f3b704aa4026b78fd36db3756.tar.xz
Add test framework, fix some bugs
Diffstat (limited to 'src')
-rw-r--r--src/libnsss/nsss_switch_grp_get.c8
-rw-r--r--src/libnsss/nsss_switch_grp_read.c3
-rw-r--r--src/nsssd/nsssd_main.c4
-rw-r--r--src/tests/deps-exe/test-all-fallback4
-rw-r--r--src/tests/deps-exe/test-switch4
-rw-r--r--src/tests/deps-exe/test-unix2
-rwxr-xr-xsrc/tests/test-all-fallback.baseline7
-rw-r--r--src/tests/test-all-fallback.c53
-rwxr-xr-xsrc/tests/test-switch.baseline7
-rw-r--r--src/tests/test-switch.c83
-rwxr-xr-xsrc/tests/test-switch.wrapper21
-rwxr-xr-xsrc/tests/test-unix.baseline7
-rw-r--r--src/tests/test-unix.c53
13 files changed, 249 insertions, 7 deletions
diff --git a/src/libnsss/nsss_switch_grp_get.c b/src/libnsss/nsss_switch_grp_get.c
index 4cfb67e..65da4e3 100644
--- a/src/libnsss/nsss_switch_grp_get.c
+++ b/src/libnsss/nsss_switch_grp_get.c
@@ -8,10 +8,10 @@
int nsss_switch_grp_get (nsss_switch_t *a, struct group *gr, stralloc *sa, genalloc *ga, tain_t const *deadline, tain_t *stamp)
{
- char c = NSSS_SWITCH_GRP_GET ;
- if (!ipc_timed_send(buffer_fd(&a->b), &c, 1, deadline, stamp)) return 0 ;
- if (!buffer_timed_get(&a->b, &c, 1, deadline, stamp)) return 0 ;
- if ((unsigned char)c == 255) return 0 ;
+ unsigned char c = NSSS_SWITCH_GRP_GET ;
+ if (!ipc_timed_send(buffer_fd(&a->b), (char *)&c, 1, deadline, stamp)) return 0 ;
+ if (!buffer_timed_get(&a->b, (char *)&c, 1, deadline, stamp)) return 0 ;
+ if (c == 255) return 0 ;
if (c) return (errno = c, 0) ;
return nsss_switch_grp_read(&a->b, gr, sa, ga, deadline, stamp) ;
}
diff --git a/src/libnsss/nsss_switch_grp_read.c b/src/libnsss/nsss_switch_grp_read.c
index 6152484..74b981b 100644
--- a/src/libnsss/nsss_switch_grp_read.c
+++ b/src/libnsss/nsss_switch_grp_read.c
@@ -1,5 +1,6 @@
/* ISC license. */
+
#include <stdint.h>
#include <string.h>
#include <errno.h>
@@ -33,7 +34,7 @@ int nsss_switch_grp_read (buffer *b, struct group *gr, stralloc *sa, genalloc *g
uint32_unpack_big(buf, &x) ; grtmp.gr_gid = x ;
uint32_unpack_big(buf + 4, &total) ;
uint32_unpack_big(buf + 8, &n) ;
- if (n >= 0x30000000u) return (errno = EPROTO, 0) ;
+ if (total < 2 || n >= 0x30000000u) return (errno = EPROTO, 0) ;
if (!stralloc_readyplus(sa, total)) return 0 ;
if (!genalloc_readyplus(char *, ga, n+1)) return 0 ;
if (!buffer_timed_get(b, sa->s + sa->len, total, deadline, stamp)) return 0 ;
diff --git a/src/nsssd/nsssd_main.c b/src/nsssd/nsssd_main.c
index 8673df4..585b995 100644
--- a/src/nsssd/nsssd_main.c
+++ b/src/nsssd/nsssd_main.c
@@ -92,8 +92,8 @@ static inline void print_gr (struct group const *gr)
if (len > 0xffffffffu || n > 0x30000000u) { answer(ENAMETOOLONG) ; return ; }
put1("", 1) ;
uint32_pack_big(pack, gr->gr_gid) ;
- uint32_pack_big(pack, len) ;
- uint32_pack_big(pack, n) ;
+ uint32_pack_big(pack + 4, len) ;
+ uint32_pack_big(pack + 8, n) ;
put1(pack, 12) ;
put1(gr->gr_name, namelen) ;
put1(gr->gr_passwd, passwdlen) ;
diff --git a/src/tests/deps-exe/test-all-fallback b/src/tests/deps-exe/test-all-fallback
new file mode 100644
index 0000000..704fc90
--- /dev/null
+++ b/src/tests/deps-exe/test-all-fallback
@@ -0,0 +1,4 @@
+${LIBNSSS}
+-lskarnet
+${SOCKET_LIB}
+${TAINNOW_LIB}
diff --git a/src/tests/deps-exe/test-switch b/src/tests/deps-exe/test-switch
new file mode 100644
index 0000000..704fc90
--- /dev/null
+++ b/src/tests/deps-exe/test-switch
@@ -0,0 +1,4 @@
+${LIBNSSS}
+-lskarnet
+${SOCKET_LIB}
+${TAINNOW_LIB}
diff --git a/src/tests/deps-exe/test-unix b/src/tests/deps-exe/test-unix
new file mode 100644
index 0000000..bbe01a8
--- /dev/null
+++ b/src/tests/deps-exe/test-unix
@@ -0,0 +1,2 @@
+${LIBNSSS}
+-lskarnet
diff --git a/src/tests/test-all-fallback.baseline b/src/tests/test-all-fallback.baseline
new file mode 100755
index 0000000..31fba7e
--- /dev/null
+++ b/src/tests/test-all-fallback.baseline
@@ -0,0 +1,7 @@
+#!/bin/sh -e
+
+cat /etc/passwd
+echo
+id -u root
+echo
+cat /etc/group
diff --git a/src/tests/test-all-fallback.c b/src/tests/test-all-fallback.c
new file mode 100644
index 0000000..666d2f7
--- /dev/null
+++ b/src/tests/test-all-fallback.c
@@ -0,0 +1,53 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <skalibs/buffer.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/lolstdio.h>
+#include <nsss/pwd-def.h>
+#include <nsss/grp-def.h>
+#include <nsss/nsss-all.h>
+
+int main (void)
+{
+ PROG = "test-all-fallback" ;
+ for (;;)
+ {
+ struct passwd *pw ;
+ errno = 0 ;
+ pw = nsss_all_getpwent() ;
+ if (!pw) break ;
+ lolprintf("%s:%s:%d:%d:%s:%s:%s\n", pw->pw_name, pw->pw_passwd, (int)pw->pw_uid, (int)pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell) ;
+ }
+ if (errno)
+ strerr_diefu1sys(111, "nsss_all_getpwent") ;
+ nsss_all_endpwent() ;
+ lolprintf("\n") ;
+
+ {
+ struct passwd *pw = nsss_all_getpwnam("root") ;
+ lolprintf("%u\n\n", (unsigned int)pw->pw_uid) ;
+ }
+
+ for (;;)
+ {
+ struct group *gr ;
+ char **p ;
+ errno = 0 ;
+ gr = nsss_all_getgrent() ;
+ if (!gr) break ;
+ p = gr->gr_mem ;
+ lolprintf("%s:%s:%d:", gr->gr_name, gr->gr_passwd, (int)gr->gr_gid) ;
+ if (*p)
+ {
+ while (*p) lolprintf("%s,", *p++) ;
+ buffer_unput(buffer_1, 1) ;
+ }
+ buffer_put(buffer_1, "\n", 1) ;
+ }
+ if (errno)
+ strerr_diefu1sys(111, "nsss_all_getgrent") ;
+ nsss_all_endgrent() ;
+ buffer_flush(buffer_1) ;
+ return 0 ;
+}
diff --git a/src/tests/test-switch.baseline b/src/tests/test-switch.baseline
new file mode 100755
index 0000000..31fba7e
--- /dev/null
+++ b/src/tests/test-switch.baseline
@@ -0,0 +1,7 @@
+#!/bin/sh -e
+
+cat /etc/passwd
+echo
+id -u root
+echo
+cat /etc/group
diff --git a/src/tests/test-switch.c b/src/tests/test-switch.c
new file mode 100644
index 0000000..0133777
--- /dev/null
+++ b/src/tests/test-switch.c
@@ -0,0 +1,83 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <skalibs/buffer.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/genalloc.h>
+#include <skalibs/lolstdio.h>
+#include <skalibs/tai.h>
+#include <nsss/pwd-def.h>
+#include <nsss/grp-def.h>
+#include <nsss/nsss-switch.h>
+
+#define S "./.test-switch-socket"
+
+int main (void)
+{
+ nsss_switch_t a = NSSS_SWITCH_ZERO ;
+ stralloc sa = STRALLOC_ZERO ;
+ genalloc ga = GENALLOC_ZERO ;
+ tain_t deadline ;
+ PROG = "test-switch" ;
+ tain_now_g() ;
+ tain_from_millisecs(&deadline, 10000) ;
+ tain_add_g(&deadline, &deadline) ;
+
+ if (!nsss_switch_start_g(&a, NSSS_SWITCH_PWD, S, &deadline))
+ strerr_diefu1sys(111, "nsss_switch_start") ;
+
+ for (;;)
+ {
+ struct passwd pw ;
+ errno = 0 ;
+ if (!nsss_switch_pwd_get_g(&a, &pw, &sa, &deadline)) break ;
+ lolprintf("%s:%s:%d:%d:%s:%s:%s\n", pw.pw_name, pw.pw_passwd, (int)pw.pw_uid, (int)pw.pw_gid, pw.pw_gecos, pw.pw_dir, pw.pw_shell) ;
+ sa.len = 0 ;
+ }
+ if (errno)
+ strerr_diefu1sys(111, "nsss_switch_pwd_get") ;
+ if (!nsss_switch_pwd_end_g(&a, &deadline))
+ strerr_diefu1sys(111, "nsss_switch_pwd_end") ;
+ lolprintf("\n") ;
+
+ {
+ struct passwd pw ;
+ if (!nsss_switch_pwd_getbyname_g(&a, &pw, &sa, "root", &deadline))
+ strerr_diefu1sys(111, "nsss_switch_pwd_getbyname") ;
+ lolprintf("%u\n\n", (unsigned int)pw.pw_uid) ;
+ sa.len = 0 ;
+ }
+
+ if (!nsss_switch_start_g(&a, NSSS_SWITCH_GRP, S, &deadline))
+ strerr_diefu1sys(111, "nsss_switch_start") ;
+ nsss_switch_end(&a, NSSS_SWITCH_PWD) ;
+ buffer_flush(buffer_1) ;
+
+ for (;;)
+ {
+ struct group gr ;
+ char **p ;
+ errno = 0 ;
+ if (!nsss_switch_grp_get_g(&a, &gr, &sa, &ga, &deadline)) break ;
+ p = gr.gr_mem ;
+ lolprintf("%s:%s:%d:", gr.gr_name, gr.gr_passwd, (int)gr.gr_gid) ;
+ buffer_flush(buffer_1) ;
+ if (*p)
+ {
+ while (*p) lolprintf("%s,", *p++) ;
+ buffer_unput(buffer_1, 1) ;
+ }
+ buffer_put(buffer_1, "\n", 1) ;
+ sa.len = 0 ;
+ genalloc_setlen(char *, &ga, 0) ;
+ }
+ if (errno)
+ strerr_diefu1sys(111, "nsss_switch_grp_get") ;
+ if (!nsss_switch_grp_end_g(&a, &deadline))
+ strerr_diefu1sys(111, "nsss_switch_grp_end") ;
+ nsss_switch_end(&a, NSSS_SWITCH_GRP) ;
+
+ buffer_flush(buffer_1) ;
+ return 0 ;
+}
diff --git a/src/tests/test-switch.wrapper b/src/tests/test-switch.wrapper
new file mode 100755
index 0000000..5117400
--- /dev/null
+++ b/src/tests/test-switch.wrapper
@@ -0,0 +1,21 @@
+#!/bin/sh -e
+
+S=./.test-switch-socket
+F=./.test-switch-fifo
+
+pid=0
+
+cleanup () {
+ kill $pid
+ rm -f $S
+}
+
+mkfifo $F
+head -n 1 < $F >/dev/null &
+pid=$!
+s6-ipcserver -1 -- $S ./nsssd-unix > $F &
+wait $pid
+pid=$!
+rm -f $F
+trap cleanup TERM INT EXIT
+./test-switch
diff --git a/src/tests/test-unix.baseline b/src/tests/test-unix.baseline
new file mode 100755
index 0000000..31fba7e
--- /dev/null
+++ b/src/tests/test-unix.baseline
@@ -0,0 +1,7 @@
+#!/bin/sh -e
+
+cat /etc/passwd
+echo
+id -u root
+echo
+cat /etc/group
diff --git a/src/tests/test-unix.c b/src/tests/test-unix.c
new file mode 100644
index 0000000..98b6ae8
--- /dev/null
+++ b/src/tests/test-unix.c
@@ -0,0 +1,53 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <skalibs/buffer.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/lolstdio.h>
+#include <nsss/pwd-def.h>
+#include <nsss/grp-def.h>
+#include <nsss/nsss-unix.h>
+
+int main (void)
+{
+ PROG = "test-unix" ;
+ for (;;)
+ {
+ struct passwd *pw ;
+ errno = 0 ;
+ pw = nsss_unix_getpwent() ;
+ if (!pw) break ;
+ lolprintf("%s:%s:%d:%d:%s:%s:%s\n", pw->pw_name, pw->pw_passwd, (int)pw->pw_uid, (int)pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell) ;
+ }
+ if (errno)
+ strerr_diefu1sys(111, "nsss_unix_getpwent") ;
+ nsss_unix_endpwent() ;
+ lolprintf("\n") ;
+
+ {
+ struct passwd *pw = nsss_unix_getpwnam("root") ;
+ lolprintf("%u\n\n", (unsigned int)pw->pw_uid) ;
+ }
+
+ for (;;)
+ {
+ struct group *gr ;
+ char **p ;
+ errno = 0 ;
+ gr = nsss_unix_getgrent() ;
+ if (!gr) break ;
+ p = gr->gr_mem ;
+ lolprintf("%s:%s:%d:", gr->gr_name, gr->gr_passwd, (int)gr->gr_gid) ;
+ if (*p)
+ {
+ while (*p) lolprintf("%s,", *p++) ;
+ buffer_unput(buffer_1, 1) ;
+ }
+ buffer_put(buffer_1, "\n", 1) ;
+ }
+ if (errno)
+ strerr_diefu1sys(111, "nsss_unix_getgrent") ;
+ nsss_unix_endgrent() ;
+ buffer_flush(buffer_1) ;
+ return 0 ;
+}