roken: Improve test-getuserinfo

This commit is contained in:
Nicolas Williams
2021-09-15 16:05:30 -05:00
parent 0e8323ccee
commit 2f57f31cd7

View File

@@ -36,6 +36,12 @@
#endif #endif
#include "roken.h" #include "roken.h"
static void
print1(const char *name, const char *tabs, const char *s2)
{
(void) printf("%s:%s%s\n", name, tabs, s2 ? s2 : "<NULL>");
}
int int
main(void) main(void)
{ {
@@ -44,20 +50,38 @@ main(void)
char buf2[MAX_PATH * 2]; char buf2[MAX_PATH * 2];
int ret = 0; int ret = 0;
if (!issuid() && getuid() != 0) { if (!issuid() && getuid() != 0) {
const char *s, *s2;
if (getenv("USER") != NULL && strlen(getenv("USER")) != 0 && if (getenv("USER") != NULL && strlen(getenv("USER")) != 0 &&
strcmp(getenv("USER"), (s = roken_get_username(buf, sizeof(buf))) == NULL) {
roken_get_username(buf, sizeof(buf))) != 0) { warnx("roken_get_username() returned NULL but $USER is set");
ret++;
}
if (getenv("USER") != NULL && strlen(getenv("USER")) != 0 && s &&
strcmp(getenv("USER"), s) != 0) {
warnx("roken_get_username() != getenv(\"USER\")"); warnx("roken_get_username() != getenv(\"USER\")");
ret++; ret++;
} }
if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 && if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 &&
strcmp(getenv("HOME"), roken_get_homedir(buf, sizeof(buf))) != 0) { (s = roken_get_homedir(buf, sizeof(buf))) == NULL) {
warnx("roken_get_homedir() returned NULL but $HOME is set");
ret++;
}
if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 && s &&
strcmp(getenv("HOME"), s) != 0) {
warnx("roken_get_homedir() != getenv(\"HOME\")"); warnx("roken_get_homedir() != getenv(\"HOME\")");
ret++; ret++;
} }
if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 && s &&
(s2 = roken_get_appdatadir(buf, sizeof(buf))) == NULL) {
warnx("roken_get_appdatadir() returned NULL but $HOME is set "
"and roken_get_homedir() returned not-NULL");
ret++;
}
if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 && if (getenv("HOME") != NULL && strlen(getenv("HOME")) != 0 &&
strcmp(roken_get_appdatadir(buf, sizeof(buf)), s && s2 && strcmp(s, s2) != 0) {
roken_get_homedir(buf2, sizeof(buf2))) != 0) {
warnx("roken_get_homedir() != roken_get_appdatadir()"); warnx("roken_get_homedir() != roken_get_appdatadir()");
ret++; ret++;
} }
@@ -68,37 +92,40 @@ main(void)
} }
} }
#endif #endif
printf("Username:\t%s\n", roken_get_username(buf, sizeof(buf)));
printf("Loginname:\t%s\n", roken_get_loginname(buf, sizeof(buf))); print1("Username", "\t", roken_get_username(buf, sizeof(buf)));
printf("Home:\t\t%s\n", roken_get_homedir(buf, sizeof(buf))); print1("Loginname", "\t", roken_get_loginname(buf, sizeof(buf)));
printf("Appdatadir:\t%s\n", roken_get_appdatadir(buf, sizeof(buf))); print1("Home", "\t\t", roken_get_homedir(buf, sizeof(buf)));
printf("Shell:\t\t%s\n", roken_get_shell(buf, sizeof(buf))); print1("Appdatadir", "\t", roken_get_appdatadir(buf, sizeof(buf)));
print1("Shell", "\t\t", roken_get_shell(buf, sizeof(buf)));
#ifndef WIN32 #ifndef WIN32
if (!issuid() && getuid() != 0) { if (!issuid() && getuid() != 0) {
const char *s, *s2;
putenv("USER=h5lfoouser"); putenv("USER=h5lfoouser");
putenv("HOME=/no/such/dir/h5lfoouser"); putenv("HOME=/no/such/dir/h5lfoouser");
putenv("SHELL=/no/such/shell"); putenv("SHELL=/no/such/shell");
if (strcmp("h5lfoouser", roken_get_username(buf, sizeof(buf))) != 0) { if ((s = roken_get_username(buf, sizeof(buf))) == NULL ||
warnx("roken_get_username() (%s) did not honor $USER", strcmp("h5lfoouser", s) != 0) {
roken_get_username(buf, sizeof(buf))); warnx("roken_get_username() (%s) did not honor $USER", s);
ret++; ret++;
} }
if (strcmp("/no/such/dir/h5lfoouser", if ((s = roken_get_homedir(buf, sizeof(buf))) == NULL ||
roken_get_homedir(buf, sizeof(buf))) != 0) { strcmp("/no/such/dir/h5lfoouser", s) != 0) {
warnx("roken_get_homedir() (%s) did not honor $HOME", warnx("roken_get_homedir() (%s) did not honor $HOME", s);
roken_get_homedir(buf, sizeof(buf)));
ret++; ret++;
} }
if (strcmp(roken_get_appdatadir(buf, sizeof(buf)), s = roken_get_homedir(buf, sizeof(buf));
roken_get_homedir(buf2, sizeof(buf2))) != 0) { s2 = roken_get_appdatadir(buf2, sizeof(buf2));
if (strcmp(s, s2) != 0) {
warnx("roken_get_homedir() != roken_get_appdatadir() (%s)", warnx("roken_get_homedir() != roken_get_appdatadir() (%s)",
roken_get_appdatadir(buf, sizeof(buf))); roken_get_appdatadir(buf, sizeof(buf)));
ret++; ret++;
} }
if (strcmp("/no/such/shell", roken_get_shell(buf, sizeof(buf))) != 0) { if ((s = roken_get_shell(buf, sizeof(buf))) == NULL ||
warnx("roken_get_shell() (%s) did not honor $SHELL", strcmp("/no/such/shell", s) != 0) {
roken_get_shell(buf, sizeof(buf))); warnx("roken_get_shell() (%s) did not honor $SHELL", s);
ret++; ret++;
} }
} }