Browse Source

Additional passwd and group tests

Eric Andersen 20 years ago
parent
commit
dae4f33e48
5 changed files with 157 additions and 46 deletions
  1. 4 7
      test/pwd_grp/.cvsignore
  2. 32 1
      test/pwd_grp/Makefile
  3. 108 0
      test/pwd_grp/getgroups.c
  4. 9 24
      test/pwd_grp/grcat.c
  5. 4 14
      test/pwd_grp/pwcat.c

+ 4 - 7
test/pwd_grp/.cvsignore

@@ -1,23 +1,20 @@
 test_grp
-test_grp.o
 test_grp.out
 test_grp_glibc
-test_grp_glibc.o
 test_grp_glibc.out
 test_pwd
 test_pwd.out
 test_pwd_glibc
 test_pwd_glibc.out
 pwcat
-pwcat.o
 pwcat.out
 pwcat_glibc
-pwcat_glibc.o
 pwcat_glibc.out
 grcat
-grcat.o
 grcat.out
 grcat_glibc
-grcat_glibc.o
 grcat_glibc.out
-
+getgroups
+getgroups.out
+getgroups_glibc
+getgroups_glibc.out

+ 32 - 1
test/pwd_grp/Makefile

@@ -24,7 +24,8 @@ TARGETS=test_pwd test_pwd_glibc
 TARGETS+=test_grp test_grp_glibc
 TARGETS+=pwcat pwcat_glibc
 TARGETS+=grcat grcat_glibc
-TARGETS+=test_pwd_diff test_grp_diff pwcat_diff grcat_diff
+TARGETS+=getgroups getgroups_glibc
+TARGETS+=test_pwd_diff test_grp_diff pwcat_diff grcat_diff getgroups_diff
 
 all: $(TARGETS)
 
@@ -116,6 +117,28 @@ grcat_glibc: grcat.c Makefile
 	-./$@ 2>&1 >grcat_glibc.out
 	-@ echo " "
 
+getgroups: getgroups.c Makefile $(TESTDIR)/Config $(TESTDIR)/Rules.mak $(CC)
+	-@ echo "-------"
+	-@ echo " "
+	-@ echo "Compiling vs uClibc: "
+	-@ echo " "
+	$(CC) $(CFLAGS) -c $< -o $@.o
+	$(CC) $(LDFLAGS) $@.o -o $@ $(EXTRA_LIBS)
+	$(STRIPTOOL) -x -R .note -R .comment $@
+	-./$@ 2>&1 >getgroups.out
+	-@ echo " "
+
+getgroups_glibc: getgroups.c Makefile
+	-@ echo "-------"
+	-@ echo " "
+	-@ echo "Compiling vs GNU libc: "
+	-@ echo " "
+	$(HOSTCC) $(GLIBC_CFLAGS) -c $< -o $@.o
+	$(HOSTCC) $(GLIBC_LDFLAGS) $@.o -o $@
+	$(STRIPTOOL) -x -R .note -R .comment $@
+	-./$@ 2>&1 >getgroups_glibc.out
+	-@ echo " "
+
 test_pwd_diff: test_pwd_glibc test_pwd
 	-@ echo "-------"
 	-@ echo " "
@@ -148,6 +171,14 @@ grcat_diff: grcat_glibc grcat
 	-diff -u grcat_glibc.out grcat.out
 	-@ echo " "
 
+getgroups_diff: getgroups_glibc getgroups
+	-@ echo "-------"
+	-@ echo " "
+	-@ echo "Diffing output: "
+	-@ echo " "
+	-diff -u getgroups_glibc.out getgroups.out
+	-@ echo " "
+
 clean:
 	$(RM) *.[oa] *~ core $(TARGETS) *.out
 

+ 108 - 0
test/pwd_grp/getgroups.c

@@ -0,0 +1,108 @@
+/* This test was ripped out of GNU 'id' from coreutils-5.0
+ * by Erik Andersen.  
+ *
+ *
+ * id is Copyright (C) 1989-2003 Free Software Foundation, Inc.
+ * and licensed under the GPL v2 or later, and was written by 
+ * Arnold Robbins, with a major rewrite by David MacKenzie,
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
+#include <err.h>
+
+/* The number of errors encountered so far. */
+static int problems = 0;
+
+/* Print the name or value of group ID GID. */
+static void
+print_group (gid_t gid)
+{
+    struct group *grp = NULL;
+
+    grp = getgrgid (gid);
+    if (grp == NULL)
+    {
+	warn("cannot find name for group ID %u", gid);
+	problems++;
+    }
+
+    if (grp == NULL)
+	printf ("%u", (unsigned) gid);
+    else
+	printf ("%s", grp->gr_name);
+}
+
+static int
+xgetgroups (gid_t gid, int *n_groups, gid_t **groups)
+{
+    int max_n_groups;
+    int ng;
+    gid_t *g;
+    int fail = 0;
+
+    max_n_groups = getgroups (0, NULL);
+
+    /* Add 1 just in case max_n_groups is zero.  */
+    g = (gid_t *) malloc (max_n_groups * sizeof (gid_t) + 1);
+    if (g==NULL)
+	err(EXIT_FAILURE, "out of memory");
+    ng = getgroups (max_n_groups, g);
+
+    if (ng < 0)
+    {
+	warn("cannot get supplemental group list");
+	++fail;
+	free (groups);
+    }
+    if (!fail)
+    {
+	*n_groups = ng;
+	*groups = g;
+    }
+    return fail;
+}
+
+/* Print all of the distinct groups the user is in. */
+int main (int argc, char **argv)
+{
+    struct passwd *pwd;
+
+    pwd = getpwuid (getuid());
+    if (pwd == NULL)
+	problems++;
+
+    print_group (getgid());
+    if (getegid() != getgid())
+    {
+	putchar (' ');
+	print_group (getegid());
+    }
+
+    {
+	int n_groups;
+	gid_t *groups;
+	register int i;
+
+	if (xgetgroups ((pwd ? pwd->pw_gid : (gid_t) -1),
+		    &n_groups, &groups))
+	{
+	    return ++problems;
+	}
+
+	for (i = 0; i < n_groups; i++)
+	    if (groups[i] != getgid() && groups[i] != getegid())
+	    {
+		putchar (' ');
+		print_group (groups[i]);
+	    }
+	free (groups);
+    }
+    putchar('\n');
+    return (problems != 0);
+}
+

+ 9 - 24
test/pwd_grp/grcat.c

@@ -8,40 +8,25 @@
  * Public Domain
  */
 
-/* For OS/2, do nothing. */
-#if HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#if defined (STDC_HEADERS)
 #include <stdlib.h>
-#endif
-
-#ifndef HAVE_GETGRENT
-int main() { return 0; }
-#else
 #include <stdio.h>
 #include <grp.h>
 
-int
-main(argc, argv)
-int argc;
-char **argv;
+int main(int argc, char **argv)
 {
     struct group *g;
     int i;
 
     while ((g = getgrent()) != NULL) {
-        printf("%s:%s:%ld:", g->gr_name, g->gr_passwd,
-                                     (long) g->gr_gid);
-        for (i = 0; g->gr_mem[i] != NULL; i++) {
-            printf("%s", g->gr_mem[i]);
-            if (g->gr_mem[i+1] != NULL)
-                putchar(',');
-        }
-        putchar('\n');
+	printf("%s:%s:%ld:", g->gr_name, g->gr_passwd,
+		(long) g->gr_gid);
+	for (i = 0; g->gr_mem[i] != NULL; i++) {
+	    printf("%s", g->gr_mem[i]);
+	    if (g->gr_mem[i+1] != NULL)
+		putchar(',');
+	}
+	putchar('\n');
     }
     endgrent();
     return 0;
 }
-#endif /* HAVE_GETGRENT */

+ 4 - 14
test/pwd_grp/pwcat.c

@@ -8,28 +8,18 @@
  * Public Domain
  */
 
-#if HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include <stdio.h>
 #include <pwd.h>
-
-#if defined (STDC_HEADERS)
 #include <stdlib.h>
-#endif
 
-int
-main(argc, argv)
-int argc;
-char **argv;
+int main(int argc, char **argv)
 {
     struct passwd *p;
 
     while ((p = getpwent()) != NULL)
-        printf("%s:%s:%ld:%ld:%s:%s:%s\n",
-            p->pw_name, p->pw_passwd, (long) p->pw_uid,
-            (long) p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
+	printf("%s:%s:%ld:%ld:%s:%s:%s\n",
+		p->pw_name, p->pw_passwd, (long) p->pw_uid,
+		(long) p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
 
     endpwent();
     return 0;