more dl* -> sys_dl* for sco
[kai/samba.git] / source3 / torture / nsstest.c
index 83a9fbfbb03535774c95aca0e566114ab4934491..c9b068aa046a9c92622832e6a9ec2bceff4d0c67 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 3.0
+   Unix SMB/CIFS implementation.
    nss tester for winbindd
    Copyright (C) Andrew Tridgell 2001
    
@@ -24,6 +23,8 @@
 static char *so_path = "/lib/libnss_winbind.so";
 static char *nss_name = "winbind";
 static int nss_errno;
+static NSS_STATUS last_error;
+static int total_errors;
 
 static void *find_fn(const char *name)
 {
@@ -34,13 +35,13 @@ static void *find_fn(const char *name)
        snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
 
        if (!h) {
-               h = dlopen(so_path, RTLD_LAZY);
+               h = sys_dlopen(so_path, RTLD_LAZY);
        }
        if (!h) {
                printf("Can't open shared library %s\n", so_path);
                exit(1);
        }
-       res = dlsym(h, s);
+       res = sys_dlsym(h, s);
        if (!res) {
                printf("Can't find function %s\n", s);
                return NULL;
@@ -50,7 +51,10 @@ static void *find_fn(const char *name)
 
 static void report_nss_error(const char *who, NSS_STATUS status)
 {
-       printf("ERROR %s: NSS_STATUS=%d  %d\n", who, status, NSS_STATUS_SUCCESS);
+       last_error = status;
+       total_errors++;
+       printf("ERROR %s: NSS_STATUS=%d  %d (nss_errno=%d)\n", 
+              who, status, NSS_STATUS_SUCCESS, nss_errno);
 }
 
 static struct passwd *nss_getpwent(void)
@@ -136,10 +140,19 @@ static struct group *nss_getgrent(void)
        NSS_STATUS (*_nss_getgrent_r)(struct group *, char *, 
                                      size_t , int *) = find_fn("getgrent_r");
        static struct group grp;
-       static char buf[1000];
+       static char *buf;
+       static int buflen = 1024;
        NSS_STATUS status;
-       
-       status = _nss_getgrent_r(&grp, buf, sizeof(buf), &nss_errno);
+
+       if (!buf) buf = malloc(buflen);
+
+again: 
+       status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
+       if (status == NSS_STATUS_TRYAGAIN) {
+               buflen *= 2;
+               buf = realloc(buf, buflen);
+               goto again;
+       }
        if (status == NSS_STATUS_NOTFOUND) {
                return NULL;
        }
@@ -155,10 +168,18 @@ static struct group *nss_getgrnam(const char *name)
        NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *, 
                                      size_t , int *) = find_fn("getgrnam_r");
        static struct group grp;
-       static char buf[1000];
+       static char *buf;
+       static int buflen = 1000;
        NSS_STATUS status;
-       
-       status = _nss_getgrnam_r(name, &grp, buf, sizeof(buf), &nss_errno);
+
+       if (!buf) buf = malloc(buflen);
+again: 
+       status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
+       if (status == NSS_STATUS_TRYAGAIN) {
+               buflen *= 2;
+               buf = realloc(buf, buflen);
+               goto again;
+       }
        if (status == NSS_STATUS_NOTFOUND) {
                return NULL;
        }
@@ -174,10 +195,18 @@ static struct group *nss_getgrgid(gid_t gid)
        NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *, 
                                      size_t , int *) = find_fn("getgrgid_r");
        static struct group grp;
-       static char buf[1000];
+       static char *buf;
+       static int buflen = 1000;
        NSS_STATUS status;
        
-       status = _nss_getgrgid_r(gid, &grp, buf, sizeof(buf), &nss_errno);
+       if (!buf) buf = malloc(buflen);
+again: 
+       status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
+       if (status == NSS_STATUS_TRYAGAIN) {
+               buflen *= 2;
+               buf = realloc(buf, buflen);
+               goto again;
+       }
        if (status == NSS_STATUS_NOTFOUND) {
                return NULL;
        }
@@ -289,8 +318,18 @@ static void nss_test_users(void)
                printf("Testing user %s\n", pwd->pw_name);
                printf("getpwent:   "); print_passwd(pwd);
                pwd = nss_getpwuid(pwd->pw_uid);
+               if (!pwd) {
+                       total_errors++;
+                       printf("ERROR: can't getpwuid\n");
+                       continue;
+               }
                printf("getpwuid:   "); print_passwd(pwd);
                pwd = nss_getpwnam(pwd->pw_name);
+               if (!pwd) {
+                       total_errors++;
+                       printf("ERROR: can't getpwnam\n");
+                       continue;
+               }
                printf("getpwnam:   "); print_passwd(pwd);
                printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
                printf("\n");
@@ -308,14 +347,53 @@ static void nss_test_groups(void)
                printf("Testing group %s\n", grp->gr_name);
                printf("getgrent: "); print_group(grp);
                grp = nss_getgrnam(grp->gr_name);
+               if (!grp) {
+                       total_errors++;
+                       printf("ERROR: can't getgrnam\n");
+                       continue;
+               }
                printf("getgrnam: "); print_group(grp);
                grp = nss_getgrgid(grp->gr_gid);
+               if (!grp) {
+                       total_errors++;
+                       printf("ERROR: can't getgrgid\n");
+                       continue;
+               }
                printf("getgrgid: "); print_group(grp);
                printf("\n");
        }
        nss_endgrent();
 }
 
+static void nss_test_errors(void)
+{
+       struct passwd *pwd;
+       struct group *grp;
+
+       pwd = getpwnam("nosuchname");
+       if (pwd || last_error != NSS_STATUS_NOTFOUND) {
+               total_errors++;
+               printf("ERROR Non existant user gave error %d\n", last_error);
+       }
+
+       pwd = getpwuid(0xFFF0);
+       if (pwd || last_error != NSS_STATUS_NOTFOUND) {
+               total_errors++;
+               printf("ERROR Non existant uid gave error %d\n", last_error);
+       }
+
+       grp = getgrnam("nosuchgroup");
+       if (grp || last_error != NSS_STATUS_NOTFOUND) {
+               total_errors++;
+               printf("ERROR Non existant group gave error %d\n", last_error);
+       }
+
+       grp = getgrgid(0xFFF0);
+       if (grp || last_error != NSS_STATUS_NOTFOUND) {
+               total_errors++;
+               printf("ERROR Non existant gid gave error %d\n", last_error);
+       }
+}
 
  int main(int argc, char *argv[])
 {      
@@ -324,6 +402,9 @@ static void nss_test_groups(void)
 
        nss_test_users();
        nss_test_groups();
+       nss_test_errors();
+
+       printf("total_errors=%d\n", total_errors);
 
-       return 0;
+       return total_errors;
 }