xref: /freebsd/lib/libutil/tests/grp_test.c (revision 069ac184)
1 /*-
2  * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 #include <errno.h>
29 #include <grp.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include <libutil.h>
35 
36 
37 /*
38  * Static values for building and testing an artificial group.
39  */
40 static char grpName[] = "groupName";
41 static char grpPasswd[] = "groupPwd";
42 static gid_t grpGID = 1234;
43 static char *grpMems[] = { "mem1", "mem2", "mem3", NULL };
44 static const char *origStrGrp = "groupName:groupPwd:1234:mem1,mem2,mem3";
45 
46 
47 /*
48  * Build a group to test against without depending on a real group to be found
49  * within /etc/group.
50  */
51 static void
52 build_grp(struct group *grp)
53 {
54 	grp->gr_name = grpName;
55 	grp->gr_passwd = grpPasswd;
56 	grp->gr_gid = grpGID;
57 	grp->gr_mem = grpMems;
58 
59 	return;
60 }
61 
62 
63 int
64 main(void)
65 {
66 	char *strGrp;
67 	int testNdx;
68 	struct group *dupGrp;
69 	struct group *scanGrp;
70 	struct group origGrp;
71 
72 	/* Setup. */
73 	printf("1..4\n");
74 	testNdx = 0;
75 
76 	/* Manually build a group using static values. */
77 	build_grp(&origGrp);
78 
79 	/* Copy the group. */
80 	testNdx++;
81 	if ((dupGrp = gr_dup(&origGrp)) == NULL)
82 		printf("not ");
83 	printf("ok %d - %s\n", testNdx, "gr_dup");
84 
85 	/* Compare the original and duplicate groups. */
86 	testNdx++;
87 	if (! gr_equal(&origGrp, dupGrp))
88 		printf("not ");
89 	printf("ok %d - %s\n", testNdx, "gr_equal");
90 
91 	/* Create group string from the duplicate group structure. */
92 	testNdx++;
93 	strGrp = gr_make(dupGrp);
94 	if (strcmp(strGrp, origStrGrp) != 0)
95 		printf("not ");
96 	printf("ok %d - %s\n", testNdx, "gr_make");
97 
98 	/*
99 	 * Create group structure from string and compare it to the original
100 	 * group structure.
101 	 */
102 	testNdx++;
103 	if ((scanGrp = gr_scan(strGrp)) == NULL || ! gr_equal(&origGrp,
104 	    scanGrp))
105 		printf("not ");
106 	printf("ok %d - %s\n", testNdx, "gr_scan");
107 
108 	/* Clean up. */
109 	free(scanGrp);
110 	free(strGrp);
111 	free(dupGrp);
112 
113 	exit(EXIT_SUCCESS);
114 }
115