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