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