summaryrefslogtreecommitdiff
path: root/src/tests/test-switch.c
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/tests/test-switch.c
parent42c3adc8841167026bb8409cb5e4390a32bff49a (diff)
downloadnsss-3cb4106b13aa883f3b704aa4026b78fd36db3756.tar.xz
Add test framework, fix some bugs
Diffstat (limited to 'src/tests/test-switch.c')
-rw-r--r--src/tests/test-switch.c83
1 files changed, 83 insertions, 0 deletions
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 ;
+}