Home | History | Annotate | Line # | Download | only in rumpvfs
t_etfs.c revision 1.3
      1 /*	$NetBSD: t_etfs.c,v 1.3 2010/06/19 13:43:29 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 	rump_sys_reboot(RUMP_RB_DUMP, NULL);
    156 }
    157 
    158 ATF_TP_ADD_TCS(tp)
    159 {
    160 
    161 	ATF_TP_ADD_TC(tp, reregister_reg);
    162 	ATF_TP_ADD_TC(tp, reregister_blk);
    163 
    164 	return atf_no_error();
    165 }
    166