1178431Sscf/*-
2178431Sscf * Copyright (c) 2008 Sean C. Farley <scf@FreeBSD.org>
3178431Sscf * All rights reserved.
4178431Sscf *
5178431Sscf * Redistribution and use in source and binary forms, with or without
6178431Sscf * modification, are permitted provided that the following conditions
7178431Sscf * are met:
8178431Sscf * 1. Redistributions of source code must retain the above copyright
9178431Sscf *    notice, this list of conditions and the following disclaimer,
10178431Sscf *    without modification, immediately at the beginning of the file.
11178431Sscf * 2. Redistributions in binary form must reproduce the above copyright
12178431Sscf *    notice, this list of conditions and the following disclaimer in the
13178431Sscf *    documentation and/or other materials provided with the distribution.
14178431Sscf *
15178431Sscf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16178431Sscf * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17178431Sscf * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18178431Sscf * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19178431Sscf * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20178431Sscf * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21178431Sscf * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22178431Sscf * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23178431Sscf * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24178431Sscf * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25178431Sscf */
26178431Sscf
27178431Sscf#include <sys/cdefs.h>
28178431Sscf__FBSDID("$FreeBSD: releng/10.3/lib/libutil/tests/grp_test.c 269904 2014-08-13 04:56:27Z ngie $");
29178431Sscf
30178431Sscf#include <sys/types.h>
31178431Sscf#include <errno.h>
32178431Sscf#include <grp.h>
33178431Sscf#include <stdio.h>
34178431Sscf#include <stdlib.h>
35178431Sscf#include <string.h>
36178431Sscf
37178431Sscf#include <libutil.h>
38178431Sscf
39178431Sscf
40178431Sscf/*
41178431Sscf * Static values for building and testing an artificial group.
42178431Sscf */
43178431Sscfstatic char grpName[] = "groupName";
44178431Sscfstatic char grpPasswd[] = "groupPwd";
45178431Sscfstatic gid_t grpGID = 1234;
46178431Sscfstatic char *grpMems[] = { "mem1", "mem2", "mem3", NULL };
47178431Sscfstatic const char *origStrGrp = "groupName:groupPwd:1234:mem1,mem2,mem3";
48178431Sscf
49178431Sscf
50178431Sscf/*
51178431Sscf * Build a group to test against without depending on a real group to be found
52178431Sscf * within /etc/group.
53178431Sscf */
54178431Sscfstatic void
55178431Sscfbuild_grp(struct group *grp)
56178431Sscf{
57178431Sscf	grp->gr_name = grpName;
58178431Sscf	grp->gr_passwd = grpPasswd;
59178431Sscf	grp->gr_gid = grpGID;
60178431Sscf	grp->gr_mem = grpMems;
61178431Sscf
62178431Sscf	return;
63178431Sscf}
64178431Sscf
65178431Sscf
66178431Sscfint
67199211Sdesmain(void)
68178431Sscf{
69178431Sscf	char *strGrp;
70178431Sscf	int testNdx;
71178431Sscf	struct group *dupGrp;
72178431Sscf	struct group *scanGrp;
73178431Sscf	struct group origGrp;
74178431Sscf
75178431Sscf	/* Setup. */
76178431Sscf	printf("1..4\n");
77178431Sscf	testNdx = 0;
78178431Sscf
79178431Sscf	/* Manually build a group using static values. */
80178431Sscf	build_grp(&origGrp);
81178431Sscf
82178431Sscf	/* Copy the group. */
83178431Sscf	testNdx++;
84178431Sscf	if ((dupGrp = gr_dup(&origGrp)) == NULL)
85178431Sscf		printf("not ");
86178431Sscf	printf("ok %d - %s\n", testNdx, "gr_dup");
87178431Sscf
88178431Sscf	/* Compare the original and duplicate groups. */
89178431Sscf	testNdx++;
90178431Sscf	if (! gr_equal(&origGrp, dupGrp))
91178431Sscf		printf("not ");
92178431Sscf	printf("ok %d - %s\n", testNdx, "gr_equal");
93178431Sscf
94178431Sscf	/* Create group string from the duplicate group structure. */
95178431Sscf	testNdx++;
96178431Sscf	strGrp = gr_make(dupGrp);
97178431Sscf	if (strcmp(strGrp, origStrGrp) != 0)
98178431Sscf		printf("not ");
99178431Sscf	printf("ok %d - %s\n", testNdx, "gr_make");
100178431Sscf
101178431Sscf	/*
102178431Sscf	 * Create group structure from string and compare it to the original
103178431Sscf	 * group structure.
104178431Sscf	 */
105178431Sscf	testNdx++;
106178431Sscf	if ((scanGrp = gr_scan(strGrp)) == NULL || ! gr_equal(&origGrp,
107178431Sscf	    scanGrp))
108178431Sscf		printf("not ");
109178431Sscf	printf("ok %d - %s\n", testNdx, "gr_scan");
110178431Sscf
111178431Sscf	/* Clean up. */
112178431Sscf	free(scanGrp);
113178431Sscf	free(strGrp);
114178431Sscf	free(dupGrp);
115178431Sscf
116178431Sscf	exit(EXIT_SUCCESS);
117178431Sscf}
118