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