1 /*-
2 * Copyright (c) 2008 Sean C. Farley <[email protected]>
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/cdefs.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <grp.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <libutil.h>
36
37
38 /*
39 * Static values for building and testing an artificial group.
40 */
41 static char grpName[] = "groupName";
42 static char grpPasswd[] = "groupPwd";
43 static gid_t grpGID = 1234;
44 static char *grpMems[] = { "mem1", "mem2", "mem3", NULL };
45 static const char *origStrGrp = "groupName:groupPwd:1234:mem1,mem2,mem3";
46
47
48 /*
49 * Build a group to test against without depending on a real group to be found
50 * within /etc/group.
51 */
52 static void
build_grp(struct group * grp)53 build_grp(struct group *grp)
54 {
55 grp->gr_name = grpName;
56 grp->gr_passwd = grpPasswd;
57 grp->gr_gid = grpGID;
58 grp->gr_mem = grpMems;
59
60 return;
61 }
62
63
64 int
main(void)65 main(void)
66 {
67 char *strGrp;
68 int testNdx;
69 struct group *dupGrp;
70 struct group *scanGrp;
71 struct group origGrp;
72
73 /* Setup. */
74 printf("1..4\n");
75 testNdx = 0;
76
77 /* Manually build a group using static values. */
78 build_grp(&origGrp);
79
80 /* Copy the group. */
81 testNdx++;
82 if ((dupGrp = gr_dup(&origGrp)) == NULL)
83 printf("not ");
84 printf("ok %d - %s\n", testNdx, "gr_dup");
85
86 /* Compare the original and duplicate groups. */
87 testNdx++;
88 if (! gr_equal(&origGrp, dupGrp))
89 printf("not ");
90 printf("ok %d - %s\n", testNdx, "gr_equal");
91
92 /* Create group string from the duplicate group structure. */
93 testNdx++;
94 strGrp = gr_make(dupGrp);
95 if (strcmp(strGrp, origStrGrp) != 0)
96 printf("not ");
97 printf("ok %d - %s\n", testNdx, "gr_make");
98
99 /*
100 * Create group structure from string and compare it to the original
101 * group structure.
102 */
103 testNdx++;
104 if ((scanGrp = gr_scan(strGrp)) == NULL || ! gr_equal(&origGrp,
105 scanGrp))
106 printf("not ");
107 printf("ok %d - %s\n", testNdx, "gr_scan");
108
109 /* Clean up. */
110 free(scanGrp);
111 free(strGrp);
112 free(dupGrp);
113
114 exit(EXIT_SUCCESS);
115 }
116