1314817Sngie/*	$NetBSD: t_basic.c,v 1.5 2017/01/13 21:30:40 christos Exp $	*/
2272343Sngie
3272343Sngie#include <sys/types.h>
4272343Sngie#include <sys/param.h>
5272343Sngie#include <sys/mount.h>
6272343Sngie
7272343Sngie#include <atf-c.h>
8272343Sngie#include <err.h>
9272343Sngie#include <errno.h>
10272343Sngie#include <fcntl.h>
11272343Sngie#include <stdio.h>
12272343Sngie#include <unistd.h>
13272343Sngie#include <string.h>
14272343Sngie#include <stdlib.h>
15272343Sngie
16272343Sngie#include <rump/rump.h>
17272343Sngie#include <rump/rump_syscalls.h>
18272343Sngie#include <rump/rumpvfs_if_pub.h>
19272343Sngie
20272343Sngie#include <fs/tmpfs/tmpfs_args.h>
21272343Sngie#include <miscfs/umapfs/umap.h>
22272343Sngie
23314817Sngie#include "h_macros.h"
24272343Sngie
25272343SngieATF_TC(basic);
26272343SngieATF_TC_HEAD(basic, tc)
27272343Sngie{
28272343Sngie	atf_tc_set_md_var(tc, "descr", "basic umapfs mapping");
29272343Sngie}
30272343Sngie
31272343Sngiestatic void
32272343Sngiextouch(const char *path)
33272343Sngie{
34272343Sngie	int fd;
35272343Sngie
36272343Sngie	fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777);
37272343Sngie	if (fd == -1)
38272343Sngie		atf_tc_fail_errno("create %s", path);
39272343Sngie	rump_sys_close(fd);
40272343Sngie}
41272343Sngie
42272343Sngiestatic void
43272343Sngiexchown(const char *path, uid_t uid, gid_t gid)
44272343Sngie{
45272343Sngie
46272343Sngie	if (rump_sys_chown(path, uid, gid) == -1)
47272343Sngie		atf_tc_fail_errno("chown %s failed", path);
48272343Sngie}
49272343Sngie
50272343Sngiestatic void
51272343Sngietestuidgid(const char *path, uid_t uid, gid_t gid)
52272343Sngie{
53272343Sngie	struct stat sb;
54272343Sngie
55272343Sngie	if (rump_sys_stat(path, &sb) == -1)
56272343Sngie		atf_tc_fail_errno("stat %s", path);
57272343Sngie	if (uid != (uid_t)-1) {
58272343Sngie		if (sb.st_uid != uid)
59272343Sngie			atf_tc_fail("%s: expected uid %d, got %d",
60272343Sngie			    path, uid, sb.st_uid);
61272343Sngie	}
62272343Sngie	if (gid != (gid_t)-1) {
63272343Sngie		if (sb.st_gid != gid)
64272343Sngie			atf_tc_fail("%s: expected gid %d, got %d",
65272343Sngie			    path, gid, sb.st_gid);
66272343Sngie	}
67272343Sngie}
68272343Sngie
69272343SngieATF_TC_BODY(basic, tc)
70272343Sngie{
71272343Sngie	struct umap_args umargs;
72272343Sngie	struct tmpfs_args targs;
73272343Sngie	u_long umaps[2][2];
74272343Sngie	u_long gmaps[2][2];
75272343Sngie
76272343Sngie	rump_init();
77272343Sngie	if (rump_sys_mkdir("/td1", 0777) == -1)
78272343Sngie		atf_tc_fail_errno("mp1");
79272343Sngie	if (rump_sys_mkdir("/td2", 0777) == -1)
80272343Sngie		atf_tc_fail_errno("mp1");
81272343Sngie
82272343Sngie	/* use tmpfs because rumpfs doesn't support ownership */
83272343Sngie	memset(&targs, 0, sizeof(targs));
84272343Sngie	targs.ta_version = TMPFS_ARGS_VERSION;
85272343Sngie	targs.ta_root_mode = 0777;
86272343Sngie	if (rump_sys_mount(MOUNT_TMPFS, "/td1", 0, &targs, sizeof(targs)) == -1)
87272343Sngie		atf_tc_fail_errno("could not mount tmpfs td1");
88272343Sngie
89272343Sngie	memset(&umargs, 0, sizeof(umargs));
90272343Sngie
91272343Sngie	/*
92272343Sngie	 * Map td1 uid 555 to td2 uid 777 (yes, IMHO the umapfs
93272343Sngie	 * mapping format is counter-intuitive).
94272343Sngie	 */
95272343Sngie	umaps[0][0] = 777;
96272343Sngie	umaps[0][1] = 555;
97272343Sngie	umaps[1][0] = 0;
98272343Sngie	umaps[1][1] = 0;
99272343Sngie	gmaps[0][0] = 4321;
100272343Sngie	gmaps[0][1] = 1234;
101272343Sngie	gmaps[1][0] = 0;
102272343Sngie	gmaps[1][1] = 0;
103272343Sngie
104272343Sngie	umargs.umap_target = __UNCONST("/td1");
105272343Sngie	umargs.nentries = 2;
106272343Sngie	umargs.gnentries = 2;
107272343Sngie	umargs.mapdata = umaps;
108272343Sngie	umargs.gmapdata = gmaps;
109272343Sngie
110272343Sngie	if (rump_sys_mount(MOUNT_UMAP, "/td2", 0, &umargs,sizeof(umargs)) == -1)
111272343Sngie		atf_tc_fail_errno("could not mount umapfs");
112272343Sngie
113272343Sngie	xtouch("/td1/noch");
114272343Sngie	testuidgid("/td1/noch", 0, 0);
115272343Sngie	testuidgid("/td2/noch", 0, 0);
116272343Sngie
117272343Sngie	xtouch("/td1/nomap");
118272343Sngie	xchown("/td1/nomap", 1, 2);
119272343Sngie	testuidgid("/td1/nomap", 1, 2);
120272343Sngie	testuidgid("/td2/nomap", -1, -1);
121272343Sngie
122272343Sngie	xtouch("/td1/forwmap");
123272343Sngie	xchown("/td1/forwmap", 555, 1234);
124272343Sngie	testuidgid("/td1/forwmap", 555, 1234);
125272343Sngie	testuidgid("/td2/forwmap", 777, 4321);
126272343Sngie
127272343Sngie	/*
128272343Sngie	 * this *CANNOT* be correct???
129272343Sngie	 */
130272343Sngie	xtouch("/td1/revmap");
131272343Sngie	/*
132272343Sngie	 * should be 777 / 4321 (?), but makes first test fail since
133272343Sngie	 * it gets 777 / 4321, i.e. unmapped results.
134272343Sngie	 */
135272343Sngie	xchown("/td2/revmap", 555, 1234);
136272343Sngie	testuidgid("/td1/revmap", 555, 1234);
137272343Sngie	testuidgid("/td2/revmap", 777, 4321);
138272343Sngie
139272343Sngie}
140272343Sngie
141272343SngieATF_TP_ADD_TCS(tp)
142272343Sngie{
143272343Sngie	ATF_TP_ADD_TC(tp, basic);
144272343Sngie	return 0; /*XXX?*/
145272343Sngie}
146