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