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