t_etfs.c revision 1.2 1 1.2 pooka /* $NetBSD: t_etfs.c,v 1.2 2010/06/19 13:40:09 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 pooka * All rights reserved.
6 1.1 pooka *
7 1.1 pooka * Redistribution and use in source and binary forms, with or without
8 1.1 pooka * modification, are permitted provided that the following conditions
9 1.1 pooka * are met:
10 1.1 pooka * 1. Redistributions of source code must retain the above copyright
11 1.1 pooka * notice, this list of conditions and the following disclaimer.
12 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pooka * notice, this list of conditions and the following disclaimer in the
14 1.1 pooka * documentation and/or other materials provided with the distribution.
15 1.1 pooka *
16 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 1.1 pooka * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 1.1 pooka * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 1.1 pooka * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 pooka * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 1.1 pooka * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 1.1 pooka * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 1.1 pooka * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 1.1 pooka * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 1.1 pooka * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 pooka */
29 1.1 pooka
30 1.1 pooka #include <sys/types.h>
31 1.1 pooka #include <sys/mount.h>
32 1.1 pooka #include <sys/sysctl.h>
33 1.1 pooka
34 1.1 pooka #include <rump/rump.h>
35 1.1 pooka #include <rump/rump_syscalls.h>
36 1.1 pooka
37 1.1 pooka #include <atf-c.h>
38 1.1 pooka #include <fcntl.h>
39 1.1 pooka #include <stdio.h>
40 1.1 pooka #include <stdlib.h>
41 1.1 pooka #include <unistd.h>
42 1.1 pooka
43 1.1 pooka #include "../../h_macros.h"
44 1.1 pooka
45 1.1 pooka ATF_TC(reregister_reg);
46 1.1 pooka ATF_TC_HEAD(reregister_reg, tc)
47 1.1 pooka {
48 1.1 pooka
49 1.1 pooka atf_tc_set_md_var(tc, "descr", "Tests register/unregister/register "
50 1.1 pooka "for a regular file");
51 1.1 pooka atf_tc_set_md_var(tc, "use.fs", "true");
52 1.1 pooka }
53 1.1 pooka
54 1.1 pooka #define TESTSTR1 "hi, it's me again!"
55 1.1 pooka #define TESTSTR1SZ (sizeof(TESTSTR1)-1)
56 1.1 pooka
57 1.1 pooka #define TESTSTR2 "what about the old vulcan proverb?"
58 1.1 pooka #define TESTSTR2SZ (sizeof(TESTSTR2)-1)
59 1.1 pooka
60 1.1 pooka #define TESTPATH1 "/trip/to/the/moon"
61 1.1 pooka #define TESTPATH2 "/but/not/the/dark/size"
62 1.1 pooka ATF_TC_BODY(reregister_reg, tc)
63 1.1 pooka {
64 1.1 pooka char buf[1024];
65 1.1 pooka int localfd, etcfd;
66 1.1 pooka ssize_t n;
67 1.1 pooka int tfd;
68 1.1 pooka
69 1.1 pooka etcfd = open("/etc/passwd", O_RDONLY);
70 1.1 pooka ATF_REQUIRE(etcfd != -1);
71 1.1 pooka
72 1.1 pooka localfd = open("./testfile", O_RDWR | O_CREAT, 0666);
73 1.1 pooka ATF_REQUIRE(localfd != -1);
74 1.1 pooka
75 1.1 pooka ATF_REQUIRE_EQ(write(localfd, TESTSTR1, TESTSTR1SZ), TESTSTR1SZ);
76 1.1 pooka /* testfile now contains test string */
77 1.1 pooka
78 1.1 pooka rump_init();
79 1.1 pooka
80 1.1 pooka ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1, "/etc/passwd",
81 1.1 pooka RUMP_ETFS_REG), 0);
82 1.1 pooka tfd = rump_sys_open(TESTPATH1, O_RDONLY);
83 1.1 pooka ATF_REQUIRE(tfd != -1);
84 1.1 pooka ATF_REQUIRE(rump_sys_read(tfd, buf, sizeof(buf)) > 0);
85 1.1 pooka rump_sys_close(tfd);
86 1.1 pooka rump_pub_etfs_remove(TESTPATH1);
87 1.1 pooka
88 1.1 pooka ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2, "./testfile",
89 1.1 pooka RUMP_ETFS_REG), 0);
90 1.1 pooka tfd = rump_sys_open(TESTPATH2, O_RDWR);
91 1.1 pooka ATF_REQUIRE(tfd != -1);
92 1.1 pooka memset(buf, 0, sizeof(buf));
93 1.1 pooka ATF_REQUIRE((n = rump_sys_read(tfd, buf, sizeof(buf))) > 0);
94 1.1 pooka
95 1.1 pooka /* check that we have what we expected */
96 1.1 pooka ATF_REQUIRE_STREQ(buf, TESTSTR1);
97 1.1 pooka
98 1.1 pooka /* ... while here, check that writing works too */
99 1.1 pooka ATF_REQUIRE_EQ(rump_sys_lseek(tfd, 0, SEEK_SET), 0);
100 1.1 pooka ATF_REQUIRE(TESTSTR1SZ <= TESTSTR2SZ);
101 1.1 pooka ATF_REQUIRE_EQ(rump_sys_write(tfd, TESTSTR2, TESTSTR2SZ), TESTSTR2SZ);
102 1.1 pooka
103 1.1 pooka memset(buf, 0, sizeof(buf));
104 1.1 pooka ATF_REQUIRE_EQ(lseek(localfd, 0, SEEK_SET), 0);
105 1.1 pooka ATF_REQUIRE(read(localfd, buf, sizeof(buf)) > 0);
106 1.1 pooka ATF_REQUIRE_STREQ(buf, TESTSTR2);
107 1.1 pooka }
108 1.1 pooka
109 1.2 pooka ATF_TC(reregister_blk);
110 1.2 pooka ATF_TC_HEAD(reregister_blk, tc)
111 1.2 pooka {
112 1.2 pooka
113 1.2 pooka atf_tc_set_md_var(tc, "descr", "Tests register/unregister/register "
114 1.2 pooka "for a block device");
115 1.2 pooka atf_tc_set_md_var(tc, "use.fs", "true");
116 1.2 pooka }
117 1.2 pooka
118 1.2 pooka ATF_TC_BODY(reregister_blk, tc)
119 1.2 pooka {
120 1.2 pooka char buf[512 * 128];
121 1.2 pooka char cmpbuf[512 * 128];
122 1.2 pooka int rv, tfd;
123 1.2 pooka
124 1.2 pooka /* first, create some image files */
125 1.2 pooka rv = system("dd if=/dev/zero bs=512 count=64 "
126 1.2 pooka "| tr '\\0' '\\1' > disk1.img");
127 1.2 pooka ATF_REQUIRE_EQ(rv, 0);
128 1.2 pooka
129 1.2 pooka rv = system("dd if=/dev/zero bs=512 count=128 "
130 1.2 pooka "| tr '\\0' '\\2' > disk2.img");
131 1.2 pooka ATF_REQUIRE_EQ(rv, 0);
132 1.2 pooka
133 1.2 pooka rump_init();
134 1.2 pooka
135 1.2 pooka ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1, "./disk1.img",
136 1.2 pooka RUMP_ETFS_BLK), 0);
137 1.2 pooka tfd = rump_sys_open(TESTPATH1, O_RDONLY);
138 1.2 pooka ATF_REQUIRE(tfd != -1);
139 1.2 pooka ATF_REQUIRE_EQ(rump_sys_read(tfd, buf, sizeof(buf)), 64*512);
140 1.2 pooka memset(cmpbuf, 1, sizeof(cmpbuf));
141 1.2 pooka ATF_REQUIRE_EQ(memcmp(buf, cmpbuf, 64*512), 0);
142 1.2 pooka rump_sys_close(tfd);
143 1.2 pooka rump_pub_etfs_remove(TESTPATH1);
144 1.2 pooka
145 1.2 pooka ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2, "./disk2.img",
146 1.2 pooka RUMP_ETFS_BLK), 0);
147 1.2 pooka tfd = rump_sys_open(TESTPATH2, O_RDONLY);
148 1.2 pooka ATF_REQUIRE(tfd != -1);
149 1.2 pooka ATF_REQUIRE_EQ(rump_sys_read(tfd, buf, sizeof(buf)), 128*512);
150 1.2 pooka memset(cmpbuf, 2, sizeof(cmpbuf));
151 1.2 pooka ATF_REQUIRE_EQ(memcmp(buf, cmpbuf, 128*512), 0);
152 1.2 pooka rump_sys_close(tfd);
153 1.2 pooka rump_pub_etfs_remove(TESTPATH1);
154 1.2 pooka
155 1.2 pooka rump_sys_reboot(RUMP_RB_DUMP, NULL);
156 1.2 pooka }
157 1.2 pooka
158 1.1 pooka ATF_TP_ADD_TCS(tp)
159 1.1 pooka {
160 1.1 pooka
161 1.1 pooka ATF_TP_ADD_TC(tp, reregister_reg);
162 1.2 pooka ATF_TP_ADD_TC(tp, reregister_blk);
163 1.1 pooka
164 1.1 pooka return atf_no_error();
165 1.1 pooka }
166