1 /* $NetBSD: t_etfs.c,v 1.10 2014/05/12 15:33:12 christos 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 } 52 53 #define TESTSTR1 "hi, it's me again!" 54 #define TESTSTR1SZ (sizeof(TESTSTR1)-1) 55 56 #define TESTSTR2 "what about the old vulcan proverb?" 57 #define TESTSTR2SZ (sizeof(TESTSTR2)-1) 58 59 #define TESTPATH1 "/trip/to/the/moon" 60 #define TESTPATH2 "/but/not/the/dark/size" 61 ATF_TC_BODY(reregister_reg, tc) 62 { 63 char buf[1024]; 64 int localfd, etcfd; 65 ssize_t n; 66 int tfd; 67 68 etcfd = open("/etc/passwd", O_RDONLY); 69 ATF_REQUIRE(etcfd != -1); 70 71 localfd = open("./testfile", O_RDWR | O_CREAT, 0666); 72 ATF_REQUIRE(localfd != -1); 73 74 ATF_REQUIRE_EQ(write(localfd, TESTSTR1, TESTSTR1SZ), TESTSTR1SZ); 75 /* testfile now contains test string */ 76 77 rump_init(); 78 79 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1, "/etc/passwd", 80 RUMP_ETFS_REG), 0); 81 tfd = rump_sys_open(TESTPATH1, O_RDONLY); 82 ATF_REQUIRE(tfd != -1); 83 ATF_REQUIRE(rump_sys_read(tfd, buf, sizeof(buf)) > 0); 84 rump_sys_close(tfd); 85 rump_pub_etfs_remove(TESTPATH1); 86 87 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2, "./testfile", 88 RUMP_ETFS_REG), 0); 89 tfd = rump_sys_open(TESTPATH2, O_RDWR); 90 ATF_REQUIRE(tfd != -1); 91 memset(buf, 0, sizeof(buf)); 92 ATF_REQUIRE((n = rump_sys_read(tfd, buf, sizeof(buf))) > 0); 93 94 /* check that we have what we expected */ 95 ATF_REQUIRE_STREQ(buf, TESTSTR1); 96 97 /* ... while here, check that writing works too */ 98 ATF_REQUIRE_EQ(rump_sys_lseek(tfd, 0, SEEK_SET), 0); 99 ATF_REQUIRE(TESTSTR1SZ <= TESTSTR2SZ); 100 ATF_REQUIRE_EQ(rump_sys_write(tfd, TESTSTR2, TESTSTR2SZ), TESTSTR2SZ); 101 102 memset(buf, 0, sizeof(buf)); 103 ATF_REQUIRE_EQ(lseek(localfd, 0, SEEK_SET), 0); 104 ATF_REQUIRE(read(localfd, buf, sizeof(buf)) > 0); 105 ATF_REQUIRE_STREQ(buf, TESTSTR2); 106 close(etcfd); 107 close(localfd); 108 } 109 110 ATF_TC(reregister_blk); 111 ATF_TC_HEAD(reregister_blk, tc) 112 { 113 114 atf_tc_set_md_var(tc, "descr", "Tests register/unregister/register " 115 "for a block device"); 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 } 163 164 #define IMG_ON_MFS "mfsdir/disk.img" 165 ATF_TC_BODY(large_blk, tc) 166 { 167 char buf[128]; 168 char cmpbuf[128]; 169 ssize_t n; 170 int rv, tfd; 171 172 /* 173 * mount mfs. it would be nice if this would not be required, 174 * but a) tmpfs doesn't "support" sparse files b) we don't really 175 * know what fs atf workdir is on anyway. 176 */ 177 if (mkdir("mfsdir", 0777) == -1) 178 atf_tc_fail_errno("mkdir failed"); 179 if (system("mount_mfs -s 64m -o nosuid,nodev mfs mfsdir") != 0) 180 atf_tc_skip("could not mount mfs"); 181 182 /* create a 8TB sparse file */ 183 rv = system("dd if=/dev/zero of=" IMG_ON_MFS " bs=1 count=1 seek=8t"); 184 ATF_REQUIRE_EQ(rv, 0); 185 186 /* 187 * map it and issue write at 6TB, then unmap+remap and check 188 * we get the same stuff back 189 */ 190 191 rump_init(); 192 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1, IMG_ON_MFS, 193 RUMP_ETFS_BLK), 0); 194 tfd = rump_sys_open(TESTPATH1, O_RDWR); 195 ATF_REQUIRE(tfd != -1); 196 memset(buf, 12, sizeof(buf)); 197 n = rump_sys_pwrite(tfd, buf, sizeof(buf), 6*1024*1024*1024ULL*1024ULL); 198 ATF_REQUIRE_EQ(n, sizeof(buf)); 199 ATF_REQUIRE_EQ(rump_sys_close(tfd), 0); 200 ATF_REQUIRE_EQ(rump_pub_etfs_remove(TESTPATH1), 0); 201 202 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2, IMG_ON_MFS, 203 RUMP_ETFS_BLK), 0); 204 tfd = rump_sys_open(TESTPATH2, O_RDWR); 205 ATF_REQUIRE(tfd != -1); 206 memset(buf, 0, sizeof(buf)); 207 n = rump_sys_pread(tfd, buf, sizeof(buf), 6*1024*1024*1024ULL*1024ULL); 208 ATF_REQUIRE_EQ(n, sizeof(buf)); 209 210 memset(cmpbuf, 12, sizeof(cmpbuf)); 211 ATF_REQUIRE_EQ(memcmp(cmpbuf, buf, 128), 0); 212 } 213 214 ATF_TC_CLEANUP(large_blk, tc) 215 { 216 217 system("umount mfsdir"); 218 } 219 220 ATF_TC(range_blk); 221 ATF_TC_HEAD(range_blk, tc) 222 { 223 224 atf_tc_set_md_var(tc, "descr", "Checks ranged (offset,size) mappings"); 225 } 226 227 ATF_TC_BODY(range_blk, tc) 228 { 229 char buf[32000]; 230 char cmpbuf[32000]; 231 ssize_t n; 232 int rv, tfd; 233 234 /* create a 64000 byte file with 16 1's at offset = 32000 */ 235 rv = system("dd if=/dev/zero of=disk.img bs=1000 count=64"); 236 ATF_REQUIRE_EQ(rv, 0); 237 rv = system("yes | tr '\\ny' '\\1' " 238 "| dd of=disk.img conv=notrunc bs=1 count=16 seek=32000"); 239 ATF_REQUIRE_EQ(rv, 0); 240 241 /* map the file at [16000,48000]. this puts our 1's at offset 16000 */ 242 rump_init(); 243 ATF_REQUIRE_EQ(rump_pub_etfs_register_withsize(TESTPATH1, "disk.img", 244 RUMP_ETFS_BLK, 16000, 32000), 0); 245 tfd = rump_sys_open(TESTPATH1, O_RDWR); 246 ATF_REQUIRE(tfd != -1); 247 n = rump_sys_read(tfd, buf, sizeof(buf)); 248 ATF_REQUIRE_EQ(n, sizeof(buf)); 249 ATF_REQUIRE_EQ(rump_sys_close(tfd), 0); 250 ATF_REQUIRE_EQ(rump_pub_etfs_remove(TESTPATH1), 0); 251 252 /* check that we got what is expected */ 253 memset(cmpbuf, 0, sizeof(cmpbuf)); 254 memset(cmpbuf+16000, 1, 16); 255 ATF_REQUIRE_EQ(memcmp(buf, cmpbuf, sizeof(buf)), 0); 256 } 257 258 ATF_TC(key); 259 ATF_TC_HEAD(key, tc) 260 { 261 262 atf_tc_set_md_var(tc, "descr", "Checks key format"); 263 } 264 265 ATF_TC_BODY(key, tc) 266 { 267 268 RZ(rump_init()); 269 270 RL(open("hostfile", O_RDWR | O_CREAT, 0777)); 271 272 RZ(rump_pub_etfs_register("/key", "hostfile", RUMP_ETFS_REG)); 273 ATF_REQUIRE_EQ(rump_pub_etfs_register("key", "hostfile", RUMP_ETFS_REG), 274 EINVAL); 275 276 RL(rump_sys_open("/key", O_RDONLY)); 277 RL(rump_sys_open("////////key", O_RDONLY)); 278 279 RZ(rump_pub_etfs_register("////key//with/slashes", "hostfile", 280 RUMP_ETFS_REG)); 281 282 RL(rump_sys_open("/key//with/slashes", O_RDONLY)); 283 RL(rump_sys_open("key//with/slashes", O_RDONLY)); 284 ATF_REQUIRE_ERRNO(ENOENT, 285 rump_sys_open("/key/with/slashes", O_RDONLY) == -1); 286 287 RL(rump_sys_mkdir("/a", 0777)); 288 ATF_REQUIRE_ERRNO(ENOENT, 289 rump_sys_open("/a/key//with/slashes", O_RDONLY) == -1); 290 } 291 292 ATF_TP_ADD_TCS(tp) 293 { 294 295 ATF_TP_ADD_TC(tp, reregister_reg); 296 ATF_TP_ADD_TC(tp, reregister_blk); 297 ATF_TP_ADD_TC(tp, large_blk); 298 ATF_TP_ADD_TC(tp, range_blk); 299 ATF_TP_ADD_TC(tp, key); 300 301 return atf_no_error(); 302 } 303