1 /* $NetBSD: t_etfs.c,v 1.10 2014/05/12 15:33:12 christos Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/types.h>
31 #include <sys/mount.h>
32 #include <sys/sysctl.h>
34 #include <rump/rump.h>
35 #include <rump/rump_syscalls.h>
43 #include "../../h_macros.h"
45 ATF_TC(reregister_reg
);
46 ATF_TC_HEAD(reregister_reg
, tc
)
49 atf_tc_set_md_var(tc
, "descr", "Tests register/unregister/register "
50 "for a regular file");
53 #define TESTSTR1 "hi, it's me again!"
54 #define TESTSTR1SZ (sizeof(TESTSTR1)-1)
56 #define TESTSTR2 "what about the old vulcan proverb?"
57 #define TESTSTR2SZ (sizeof(TESTSTR2)-1)
59 #define TESTPATH1 "/trip/to/the/moon"
60 #define TESTPATH2 "/but/not/the/dark/size"
61 ATF_TC_BODY(reregister_reg
, tc
)
68 etcfd
= open("/etc/passwd", O_RDONLY
);
69 ATF_REQUIRE(etcfd
!= -1);
71 localfd
= open("./testfile", O_RDWR
| O_CREAT
, 0666);
72 ATF_REQUIRE(localfd
!= -1);
74 ATF_REQUIRE_EQ(write(localfd
, TESTSTR1
, TESTSTR1SZ
), TESTSTR1SZ
);
75 /* testfile now contains test string */
79 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1
, "/etc/passwd",
81 tfd
= rump_sys_open(TESTPATH1
, O_RDONLY
);
82 ATF_REQUIRE(tfd
!= -1);
83 ATF_REQUIRE(rump_sys_read(tfd
, buf
, sizeof(buf
)) > 0);
85 rump_pub_etfs_remove(TESTPATH1
);
87 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2
, "./testfile",
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);
94 /* check that we have what we expected */
95 ATF_REQUIRE_STREQ(buf
, TESTSTR1
);
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
);
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
);
110 ATF_TC(reregister_blk
);
111 ATF_TC_HEAD(reregister_blk
, tc
)
114 atf_tc_set_md_var(tc
, "descr", "Tests register/unregister/register "
115 "for a block device");
118 ATF_TC_BODY(reregister_blk
, tc
)
121 char cmpbuf
[512 * 128];
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);
129 rv
= system("dd if=/dev/zero bs=512 count=128 "
130 "| tr '\\0' '\\2' > disk2.img");
131 ATF_REQUIRE_EQ(rv
, 0);
135 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1
, "./disk1.img",
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);
145 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2
, "./disk2.img",
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);
156 ATF_TC_WITH_CLEANUP(large_blk
);
157 ATF_TC_HEAD(large_blk
, tc
)
160 atf_tc_set_md_var(tc
, "descr", "Check etfs block devices work for "
164 #define IMG_ON_MFS "mfsdir/disk.img"
165 ATF_TC_BODY(large_blk
, tc
)
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.
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");
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);
187 * map it and issue write at 6TB, then unmap+remap and check
188 * we get the same stuff back
192 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1
, IMG_ON_MFS
,
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);
202 ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2
, IMG_ON_MFS
,
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
));
210 memset(cmpbuf
, 12, sizeof(cmpbuf
));
211 ATF_REQUIRE_EQ(memcmp(cmpbuf
, buf
, 128), 0);
214 ATF_TC_CLEANUP(large_blk
, tc
)
217 system("umount mfsdir");
221 ATF_TC_HEAD(range_blk
, tc
)
224 atf_tc_set_md_var(tc
, "descr", "Checks ranged (offset,size) mappings");
227 ATF_TC_BODY(range_blk
, tc
)
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);
241 /* map the file at [16000,48000]. this puts our 1's at offset 16000 */
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);
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);
262 atf_tc_set_md_var(tc
, "descr", "Checks key format");
270 RL(open("hostfile", O_RDWR
| O_CREAT
, 0777));
272 RZ(rump_pub_etfs_register("/key", "hostfile", RUMP_ETFS_REG
));
273 ATF_REQUIRE_EQ(rump_pub_etfs_register("key", "hostfile", RUMP_ETFS_REG
),
276 RL(rump_sys_open("/key", O_RDONLY
));
277 RL(rump_sys_open("////////key", O_RDONLY
));
279 RZ(rump_pub_etfs_register("////key//with/slashes", "hostfile",
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);
287 RL(rump_sys_mkdir("/a", 0777));
288 ATF_REQUIRE_ERRNO(ENOENT
,
289 rump_sys_open("/a/key//with/slashes", O_RDONLY
) == -1);
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
);
301 return atf_no_error();