xref: /minix3/tests/fs/umapfs/t_basic.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: t_basic.c,v 1.4 2010/07/19 15:35:39 pooka Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc #include <sys/types.h>
4*11be35a1SLionel Sambuc #include <sys/param.h>
5*11be35a1SLionel Sambuc #include <sys/mount.h>
6*11be35a1SLionel Sambuc 
7*11be35a1SLionel Sambuc #include <atf-c.h>
8*11be35a1SLionel Sambuc #include <err.h>
9*11be35a1SLionel Sambuc #include <errno.h>
10*11be35a1SLionel Sambuc #include <fcntl.h>
11*11be35a1SLionel Sambuc #include <stdio.h>
12*11be35a1SLionel Sambuc #include <unistd.h>
13*11be35a1SLionel Sambuc #include <string.h>
14*11be35a1SLionel Sambuc #include <stdlib.h>
15*11be35a1SLionel Sambuc 
16*11be35a1SLionel Sambuc #include <rump/rump.h>
17*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
18*11be35a1SLionel Sambuc #include <rump/rumpvfs_if_pub.h>
19*11be35a1SLionel Sambuc 
20*11be35a1SLionel Sambuc #include <fs/tmpfs/tmpfs_args.h>
21*11be35a1SLionel Sambuc #include <miscfs/umapfs/umap.h>
22*11be35a1SLionel Sambuc 
23*11be35a1SLionel Sambuc #include "../../h_macros.h"
24*11be35a1SLionel Sambuc 
25*11be35a1SLionel Sambuc ATF_TC(basic);
ATF_TC_HEAD(basic,tc)26*11be35a1SLionel Sambuc ATF_TC_HEAD(basic, tc)
27*11be35a1SLionel Sambuc {
28*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "basic umapfs mapping");
29*11be35a1SLionel Sambuc }
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc static void
xtouch(const char * path)32*11be35a1SLionel Sambuc xtouch(const char *path)
33*11be35a1SLionel Sambuc {
34*11be35a1SLionel Sambuc 	int fd;
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc 	fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777);
37*11be35a1SLionel Sambuc 	if (fd == -1)
38*11be35a1SLionel Sambuc 		atf_tc_fail_errno("create %s", path);
39*11be35a1SLionel Sambuc 	rump_sys_close(fd);
40*11be35a1SLionel Sambuc }
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc static void
xchown(const char * path,uid_t uid,gid_t gid)43*11be35a1SLionel Sambuc xchown(const char *path, uid_t uid, gid_t gid)
44*11be35a1SLionel Sambuc {
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc 	if (rump_sys_chown(path, uid, gid) == -1)
47*11be35a1SLionel Sambuc 		atf_tc_fail_errno("chown %s failed", path);
48*11be35a1SLionel Sambuc }
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc static void
testuidgid(const char * path,uid_t uid,gid_t gid)51*11be35a1SLionel Sambuc testuidgid(const char *path, uid_t uid, gid_t gid)
52*11be35a1SLionel Sambuc {
53*11be35a1SLionel Sambuc 	struct stat sb;
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc 	if (rump_sys_stat(path, &sb) == -1)
56*11be35a1SLionel Sambuc 		atf_tc_fail_errno("stat %s", path);
57*11be35a1SLionel Sambuc 	if (uid != (uid_t)-1) {
58*11be35a1SLionel Sambuc 		if (sb.st_uid != uid)
59*11be35a1SLionel Sambuc 			atf_tc_fail("%s: expected uid %d, got %d",
60*11be35a1SLionel Sambuc 			    path, uid, sb.st_uid);
61*11be35a1SLionel Sambuc 	}
62*11be35a1SLionel Sambuc 	if (gid != (gid_t)-1) {
63*11be35a1SLionel Sambuc 		if (sb.st_gid != gid)
64*11be35a1SLionel Sambuc 			atf_tc_fail("%s: expected gid %d, got %d",
65*11be35a1SLionel Sambuc 			    path, gid, sb.st_gid);
66*11be35a1SLionel Sambuc 	}
67*11be35a1SLionel Sambuc }
68*11be35a1SLionel Sambuc 
ATF_TC_BODY(basic,tc)69*11be35a1SLionel Sambuc ATF_TC_BODY(basic, tc)
70*11be35a1SLionel Sambuc {
71*11be35a1SLionel Sambuc 	struct umap_args umargs;
72*11be35a1SLionel Sambuc 	struct tmpfs_args targs;
73*11be35a1SLionel Sambuc 	u_long umaps[2][2];
74*11be35a1SLionel Sambuc 	u_long gmaps[2][2];
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc 	rump_init();
77*11be35a1SLionel Sambuc 	if (rump_sys_mkdir("/td1", 0777) == -1)
78*11be35a1SLionel Sambuc 		atf_tc_fail_errno("mp1");
79*11be35a1SLionel Sambuc 	if (rump_sys_mkdir("/td2", 0777) == -1)
80*11be35a1SLionel Sambuc 		atf_tc_fail_errno("mp1");
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc 	/* use tmpfs because rumpfs doesn't support ownership */
83*11be35a1SLionel Sambuc 	memset(&targs, 0, sizeof(targs));
84*11be35a1SLionel Sambuc 	targs.ta_version = TMPFS_ARGS_VERSION;
85*11be35a1SLionel Sambuc 	targs.ta_root_mode = 0777;
86*11be35a1SLionel Sambuc 	if (rump_sys_mount(MOUNT_TMPFS, "/td1", 0, &targs, sizeof(targs)) == -1)
87*11be35a1SLionel Sambuc 		atf_tc_fail_errno("could not mount tmpfs td1");
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc 	memset(&umargs, 0, sizeof(umargs));
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc 	/*
92*11be35a1SLionel Sambuc 	 * Map td1 uid 555 to td2 uid 777 (yes, IMHO the umapfs
93*11be35a1SLionel Sambuc 	 * mapping format is counter-intuitive).
94*11be35a1SLionel Sambuc 	 */
95*11be35a1SLionel Sambuc 	umaps[0][0] = 777;
96*11be35a1SLionel Sambuc 	umaps[0][1] = 555;
97*11be35a1SLionel Sambuc 	umaps[1][0] = 0;
98*11be35a1SLionel Sambuc 	umaps[1][1] = 0;
99*11be35a1SLionel Sambuc 	gmaps[0][0] = 4321;
100*11be35a1SLionel Sambuc 	gmaps[0][1] = 1234;
101*11be35a1SLionel Sambuc 	gmaps[1][0] = 0;
102*11be35a1SLionel Sambuc 	gmaps[1][1] = 0;
103*11be35a1SLionel Sambuc 
104*11be35a1SLionel Sambuc 	umargs.umap_target = __UNCONST("/td1");
105*11be35a1SLionel Sambuc 	umargs.nentries = 2;
106*11be35a1SLionel Sambuc 	umargs.gnentries = 2;
107*11be35a1SLionel Sambuc 	umargs.mapdata = umaps;
108*11be35a1SLionel Sambuc 	umargs.gmapdata = gmaps;
109*11be35a1SLionel Sambuc 
110*11be35a1SLionel Sambuc 	if (rump_sys_mount(MOUNT_UMAP, "/td2", 0, &umargs,sizeof(umargs)) == -1)
111*11be35a1SLionel Sambuc 		atf_tc_fail_errno("could not mount umapfs");
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc 	xtouch("/td1/noch");
114*11be35a1SLionel Sambuc 	testuidgid("/td1/noch", 0, 0);
115*11be35a1SLionel Sambuc 	testuidgid("/td2/noch", 0, 0);
116*11be35a1SLionel Sambuc 
117*11be35a1SLionel Sambuc 	xtouch("/td1/nomap");
118*11be35a1SLionel Sambuc 	xchown("/td1/nomap", 1, 2);
119*11be35a1SLionel Sambuc 	testuidgid("/td1/nomap", 1, 2);
120*11be35a1SLionel Sambuc 	testuidgid("/td2/nomap", -1, -1);
121*11be35a1SLionel Sambuc 
122*11be35a1SLionel Sambuc 	xtouch("/td1/forwmap");
123*11be35a1SLionel Sambuc 	xchown("/td1/forwmap", 555, 1234);
124*11be35a1SLionel Sambuc 	testuidgid("/td1/forwmap", 555, 1234);
125*11be35a1SLionel Sambuc 	testuidgid("/td2/forwmap", 777, 4321);
126*11be35a1SLionel Sambuc 
127*11be35a1SLionel Sambuc 	/*
128*11be35a1SLionel Sambuc 	 * this *CANNOT* be correct???
129*11be35a1SLionel Sambuc 	 */
130*11be35a1SLionel Sambuc 	xtouch("/td1/revmap");
131*11be35a1SLionel Sambuc 	/*
132*11be35a1SLionel Sambuc 	 * should be 777 / 4321 (?), but makes first test fail since
133*11be35a1SLionel Sambuc 	 * it gets 777 / 4321, i.e. unmapped results.
134*11be35a1SLionel Sambuc 	 */
135*11be35a1SLionel Sambuc 	xchown("/td2/revmap", 555, 1234);
136*11be35a1SLionel Sambuc 	testuidgid("/td1/revmap", 555, 1234);
137*11be35a1SLionel Sambuc 	testuidgid("/td2/revmap", 777, 4321);
138*11be35a1SLionel Sambuc 
139*11be35a1SLionel Sambuc }
140*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)141*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
142*11be35a1SLionel Sambuc {
143*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, basic);
144*11be35a1SLionel Sambuc 	return 0; /*XXX?*/
145*11be35a1SLionel Sambuc }
146