Home | History | Annotate | Line # | Download | only in rumpvfs
t_etfs.c revision 1.5
      1 /*	$NetBSD: t_etfs.c,v 1.5 2010/06/20 17:43:33 pooka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/types.h>
     31 #include <sys/mount.h>
     32 #include <sys/sysctl.h>
     33 
     34 #include <rump/rump.h>
     35 #include <rump/rump_syscalls.h>
     36 
     37 #include <atf-c.h>
     38 #include <fcntl.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <unistd.h>
     42 
     43 #include "../../h_macros.h"
     44 
     45 ATF_TC(reregister_reg);
     46 ATF_TC_HEAD(reregister_reg, tc)
     47 {
     48 
     49 	atf_tc_set_md_var(tc, "descr", "Tests register/unregister/register "
     50 	    "for a regular file");
     51 	atf_tc_set_md_var(tc, "use.fs", "true");
     52 }
     53 
     54 #define TESTSTR1 "hi, it's me again!"
     55 #define TESTSTR1SZ (sizeof(TESTSTR1)-1)
     56 
     57 #define TESTSTR2 "what about the old vulcan proverb?"
     58 #define TESTSTR2SZ (sizeof(TESTSTR2)-1)
     59 
     60 #define TESTPATH1 "/trip/to/the/moon"
     61 #define TESTPATH2 "/but/not/the/dark/size"
     62 ATF_TC_BODY(reregister_reg, tc)
     63 {
     64 	char buf[1024];
     65 	int localfd, etcfd;
     66 	ssize_t n;
     67 	int tfd;
     68 
     69 	etcfd = open("/etc/passwd", O_RDONLY);
     70 	ATF_REQUIRE(etcfd != -1);
     71 
     72 	localfd = open("./testfile", O_RDWR | O_CREAT, 0666);
     73 	ATF_REQUIRE(localfd != -1);
     74 
     75 	ATF_REQUIRE_EQ(write(localfd, TESTSTR1, TESTSTR1SZ), TESTSTR1SZ);
     76 	/* testfile now contains test string */
     77 
     78 	rump_init();
     79 
     80 	ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1, "/etc/passwd",
     81 	    RUMP_ETFS_REG), 0);
     82 	tfd = rump_sys_open(TESTPATH1, O_RDONLY);
     83 	ATF_REQUIRE(tfd != -1);
     84 	ATF_REQUIRE(rump_sys_read(tfd, buf, sizeof(buf)) > 0);
     85 	rump_sys_close(tfd);
     86 	rump_pub_etfs_remove(TESTPATH1);
     87 
     88 	ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2, "./testfile",
     89 	    RUMP_ETFS_REG), 0);
     90 	tfd = rump_sys_open(TESTPATH2, O_RDWR);
     91 	ATF_REQUIRE(tfd != -1);
     92 	memset(buf, 0, sizeof(buf));
     93 	ATF_REQUIRE((n = rump_sys_read(tfd, buf, sizeof(buf))) > 0);
     94 
     95 	/* check that we have what we expected */
     96 	ATF_REQUIRE_STREQ(buf, TESTSTR1);
     97 
     98 	/* ... while here, check that writing works too */
     99 	ATF_REQUIRE_EQ(rump_sys_lseek(tfd, 0, SEEK_SET), 0);
    100 	ATF_REQUIRE(TESTSTR1SZ <= TESTSTR2SZ);
    101 	ATF_REQUIRE_EQ(rump_sys_write(tfd, TESTSTR2, TESTSTR2SZ), TESTSTR2SZ);
    102 
    103 	memset(buf, 0, sizeof(buf));
    104 	ATF_REQUIRE_EQ(lseek(localfd, 0, SEEK_SET), 0);
    105 	ATF_REQUIRE(read(localfd, buf, sizeof(buf)) > 0);
    106 	ATF_REQUIRE_STREQ(buf, TESTSTR2);
    107 }
    108 
    109 ATF_TC(reregister_blk);
    110 ATF_TC_HEAD(reregister_blk, tc)
    111 {
    112 
    113 	atf_tc_set_md_var(tc, "descr", "Tests register/unregister/register "
    114 	    "for a block device");
    115 	atf_tc_set_md_var(tc, "use.fs", "true");
    116 }
    117 
    118 ATF_TC_BODY(reregister_blk, tc)
    119 {
    120 	char buf[512 * 128];
    121 	char cmpbuf[512 * 128];
    122 	int rv, tfd;
    123 
    124 	/* first, create some image files */
    125 	rv = system("dd if=/dev/zero bs=512 count=64 "
    126 	    "| tr '\\0' '\\1' > disk1.img");
    127 	ATF_REQUIRE_EQ(rv, 0);
    128 
    129 	rv = system("dd if=/dev/zero bs=512 count=128 "
    130 	    "| tr '\\0' '\\2' > disk2.img");
    131 	ATF_REQUIRE_EQ(rv, 0);
    132 
    133 	rump_init();
    134 
    135 	ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1, "./disk1.img",
    136 	    RUMP_ETFS_BLK), 0);
    137 	tfd = rump_sys_open(TESTPATH1, O_RDONLY);
    138 	ATF_REQUIRE(tfd != -1);
    139 	ATF_REQUIRE_EQ(rump_sys_read(tfd, buf, sizeof(buf)), 64*512);
    140 	memset(cmpbuf, 1, sizeof(cmpbuf));
    141 	ATF_REQUIRE_EQ(memcmp(buf, cmpbuf, 64*512), 0);
    142 	ATF_REQUIRE_EQ(rump_sys_close(tfd), 0);
    143 	ATF_REQUIRE_EQ(rump_pub_etfs_remove(TESTPATH1), 0);
    144 
    145 	ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2, "./disk2.img",
    146 	    RUMP_ETFS_BLK), 0);
    147 	tfd = rump_sys_open(TESTPATH2, O_RDONLY);
    148 	ATF_REQUIRE(tfd != -1);
    149 	ATF_REQUIRE_EQ(rump_sys_read(tfd, buf, sizeof(buf)), 128*512);
    150 	memset(cmpbuf, 2, sizeof(cmpbuf));
    151 	ATF_REQUIRE_EQ(memcmp(buf, cmpbuf, 128*512), 0);
    152 	ATF_REQUIRE_EQ(rump_sys_close(tfd), 0);
    153 	ATF_REQUIRE_EQ(rump_pub_etfs_remove(TESTPATH2), 0);
    154 }
    155 
    156 ATF_TC_WITH_CLEANUP(large_blk);
    157 ATF_TC_HEAD(large_blk, tc)
    158 {
    159 
    160 	atf_tc_set_md_var(tc, "descr", "Check etfs block devices work for "
    161 	    ">2TB images");
    162 	atf_tc_set_md_var(tc, "use.fs", "true");
    163 }
    164 
    165 #define IMG_ON_MFS "mfsdir/disk.img"
    166 ATF_TC_BODY(large_blk, tc)
    167 {
    168 	char buf[128];
    169 	char cmpbuf[128];
    170 	ssize_t n;
    171 	int rv, tfd;
    172 
    173 	/*
    174 	 * mount mfs.  it would be nice if this would not be required,
    175 	 * but a) tmpfs doesn't "support" sparse files b) we don't really
    176 	 * know what fs atf workdir is on anyway.
    177 	 */
    178 	if (mkdir("mfsdir", 0777) == -1)
    179 		atf_tc_fail_errno("mkdir failed");
    180 	if (system("mount_mfs -s 64m -o nosuid,nodev mfs mfsdir") != 0)
    181 		atf_tc_skip("could not mount mfs");
    182 
    183 	/* create a 8TB sparse file */
    184 	rv = system("dd if=/dev/zero of=" IMG_ON_MFS " bs=1 count=1 seek=8t");
    185 	ATF_REQUIRE_EQ(rv, 0);
    186 
    187 	/*
    188 	 * map it and issue write at 6TB, then unmap+remap and check
    189 	 * we get the same stuff back
    190 	 */
    191 
    192 	rump_init();
    193 	ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1, IMG_ON_MFS,
    194 	    RUMP_ETFS_BLK), 0);
    195 	tfd = rump_sys_open(TESTPATH1, O_RDWR);
    196 	ATF_REQUIRE(tfd != -1);
    197 	memset(buf, 12, sizeof(buf));
    198 	n = rump_sys_pwrite(tfd, buf, sizeof(buf), 6*1024*1024*1024ULL*1024ULL);
    199 	ATF_REQUIRE_EQ(n, sizeof(buf));
    200 	ATF_REQUIRE_EQ(rump_sys_close(tfd), 0);
    201 	ATF_REQUIRE_EQ(rump_pub_etfs_remove(TESTPATH1), 0);
    202 
    203 	ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2, IMG_ON_MFS,
    204 	    RUMP_ETFS_BLK), 0);
    205 	tfd = rump_sys_open(TESTPATH2, O_RDWR);
    206 	ATF_REQUIRE(tfd != -1);
    207 	memset(buf, 0, sizeof(buf));
    208 	n = rump_sys_pread(tfd, buf, sizeof(buf), 6*1024*1024*1024ULL*1024ULL);
    209 	ATF_REQUIRE_EQ(n, sizeof(buf));
    210 
    211 	memset(cmpbuf, 12, sizeof(cmpbuf));
    212 	ATF_REQUIRE_EQ(memcmp(cmpbuf, buf, 128), 0);
    213 }
    214 
    215 ATF_TC_CLEANUP(large_blk, tc)
    216 {
    217 
    218 	system("umount mfsdir");
    219 }
    220 
    221 ATF_TP_ADD_TCS(tp)
    222 {
    223 
    224 	ATF_TP_ADD_TC(tp, reregister_reg);
    225 	ATF_TP_ADD_TC(tp, reregister_blk);
    226 	ATF_TP_ADD_TC(tp, large_blk);
    227 
    228 	return atf_no_error();
    229 }
    230