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