1*11be35a1SLionel Sambuc /* $NetBSD: t_ro.c,v 1.5 2011/02/22 21:23:19 yamt Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc */
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include <sys/stat.h>
30*11be35a1SLionel Sambuc #include <sys/statvfs.h>
31*11be35a1SLionel Sambuc
32*11be35a1SLionel Sambuc #include <atf-c.h>
33*11be35a1SLionel Sambuc #include <fcntl.h>
34*11be35a1SLionel Sambuc #include <libgen.h>
35*11be35a1SLionel Sambuc #include <stdlib.h>
36*11be35a1SLionel Sambuc #include <unistd.h>
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
39*11be35a1SLionel Sambuc #include <rump/rump.h>
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc #include "../common/h_fsmacros.h"
42*11be35a1SLionel Sambuc #include "../../h_macros.h"
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc #define AFILE "testfile"
45*11be35a1SLionel Sambuc #define ADIR "testdir"
46*11be35a1SLionel Sambuc #define AFIFO "testfifo"
47*11be35a1SLionel Sambuc #define ASYMLINK "testsymlink"
48*11be35a1SLionel Sambuc #define ALINK "testlink"
49*11be35a1SLionel Sambuc #define FUNTEXT "this is some non-humppa text"
50*11be35a1SLionel Sambuc #define FUNSIZE (sizeof(FUNTEXT)-1)
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc static void
nullgen(const atf_tc_t * tc,const char * mp)53*11be35a1SLionel Sambuc nullgen(const atf_tc_t *tc, const char *mp)
54*11be35a1SLionel Sambuc {
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc return;
57*11be35a1SLionel Sambuc }
58*11be35a1SLionel Sambuc
59*11be35a1SLionel Sambuc static void
filegen(const atf_tc_t * tc,const char * mp)60*11be35a1SLionel Sambuc filegen(const atf_tc_t *tc, const char *mp)
61*11be35a1SLionel Sambuc {
62*11be35a1SLionel Sambuc int fd;
63*11be35a1SLionel Sambuc
64*11be35a1SLionel Sambuc FSTEST_ENTER();
65*11be35a1SLionel Sambuc RL(fd = rump_sys_open(AFILE, O_CREAT | O_RDWR, 0777));
66*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(rump_sys_write(fd, FUNTEXT, FUNSIZE), FUNSIZE);
67*11be35a1SLionel Sambuc RL(rump_sys_close(fd));
68*11be35a1SLionel Sambuc FSTEST_EXIT();
69*11be35a1SLionel Sambuc }
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc /*
72*11be35a1SLionel Sambuc *
73*11be35a1SLionel Sambuc * BEGIN tests
74*11be35a1SLionel Sambuc *
75*11be35a1SLionel Sambuc */
76*11be35a1SLionel Sambuc
77*11be35a1SLionel Sambuc static void
create(const atf_tc_t * tc,const char * mp)78*11be35a1SLionel Sambuc create(const atf_tc_t *tc, const char *mp)
79*11be35a1SLionel Sambuc {
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc FSTEST_ENTER();
82*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_open(AFILE, O_CREAT|O_RDONLY) == -1);
83*11be35a1SLionel Sambuc FSTEST_EXIT();
84*11be35a1SLionel Sambuc }
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc static void
rmfile(const atf_tc_t * tc,const char * mp)87*11be35a1SLionel Sambuc rmfile(const atf_tc_t *tc, const char *mp)
88*11be35a1SLionel Sambuc {
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc FSTEST_ENTER();
91*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_unlink(AFILE) == -1);
92*11be35a1SLionel Sambuc FSTEST_EXIT();
93*11be35a1SLionel Sambuc }
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc static void
fileio(const atf_tc_t * tc,const char * mp)96*11be35a1SLionel Sambuc fileio(const atf_tc_t *tc, const char *mp)
97*11be35a1SLionel Sambuc {
98*11be35a1SLionel Sambuc int fd;
99*11be35a1SLionel Sambuc char buf[FUNSIZE+1];
100*11be35a1SLionel Sambuc int expected;
101*11be35a1SLionel Sambuc
102*11be35a1SLionel Sambuc if (FSTYPE_NFSRO(tc))
103*11be35a1SLionel Sambuc expected = EACCES;
104*11be35a1SLionel Sambuc else
105*11be35a1SLionel Sambuc expected = EROFS;
106*11be35a1SLionel Sambuc
107*11be35a1SLionel Sambuc FSTEST_ENTER();
108*11be35a1SLionel Sambuc RL(fd = rump_sys_open(AFILE, O_RDONLY));
109*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(rump_sys_read(fd, buf, FUNSIZE), FUNSIZE);
110*11be35a1SLionel Sambuc buf[FUNSIZE] = '\0';
111*11be35a1SLionel Sambuc ATF_REQUIRE_STREQ(buf, FUNTEXT);
112*11be35a1SLionel Sambuc RL(rump_sys_close(fd));
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(expected, rump_sys_open(AFILE, O_WRONLY) == -1);
115*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(expected, rump_sys_open(AFILE, O_RDWR) == -1);
116*11be35a1SLionel Sambuc FSTEST_EXIT();
117*11be35a1SLionel Sambuc }
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc static void
attrs(const atf_tc_t * tc,const char * mp)120*11be35a1SLionel Sambuc attrs(const atf_tc_t *tc, const char *mp)
121*11be35a1SLionel Sambuc {
122*11be35a1SLionel Sambuc struct timeval sometvs[2];
123*11be35a1SLionel Sambuc struct stat sb;
124*11be35a1SLionel Sambuc int fd;
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc FSTEST_ENTER();
127*11be35a1SLionel Sambuc
128*11be35a1SLionel Sambuc RL(rump_sys_stat(AFILE, &sb));
129*11be35a1SLionel Sambuc
130*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_chmod(AFILE, 0775) == -1);
131*11be35a1SLionel Sambuc if (!FSTYPE_MSDOS(tc))
132*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_chown(AFILE, 1, 1) == -1);
133*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_utimes(AFILE, sometvs) == -1);
134*11be35a1SLionel Sambuc
135*11be35a1SLionel Sambuc RL(fd = rump_sys_open(AFILE, O_RDONLY));
136*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_fchmod(fd, 0775) == -1);
137*11be35a1SLionel Sambuc if (!FSTYPE_MSDOS(tc))
138*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_fchown(fd, 1, 1) == -1);
139*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_futimes(fd, sometvs) == -1);
140*11be35a1SLionel Sambuc RL(rump_sys_close(fd));
141*11be35a1SLionel Sambuc
142*11be35a1SLionel Sambuc FSTEST_EXIT();
143*11be35a1SLionel Sambuc }
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc static void
createdir(const atf_tc_t * tc,const char * mp)146*11be35a1SLionel Sambuc createdir(const atf_tc_t *tc, const char *mp)
147*11be35a1SLionel Sambuc {
148*11be35a1SLionel Sambuc
149*11be35a1SLionel Sambuc FSTEST_ENTER();
150*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_mkdir(ADIR, 0775) == -1);
151*11be35a1SLionel Sambuc FSTEST_EXIT();
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc
154*11be35a1SLionel Sambuc static void
createfifo(const atf_tc_t * tc,const char * mp)155*11be35a1SLionel Sambuc createfifo(const atf_tc_t *tc, const char *mp)
156*11be35a1SLionel Sambuc {
157*11be35a1SLionel Sambuc
158*11be35a1SLionel Sambuc FSTEST_ENTER();
159*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_mkfifo(AFIFO, 0775) == -1);
160*11be35a1SLionel Sambuc FSTEST_EXIT();
161*11be35a1SLionel Sambuc }
162*11be35a1SLionel Sambuc
163*11be35a1SLionel Sambuc static void
createsymlink(const atf_tc_t * tc,const char * mp)164*11be35a1SLionel Sambuc createsymlink(const atf_tc_t *tc, const char *mp)
165*11be35a1SLionel Sambuc {
166*11be35a1SLionel Sambuc
167*11be35a1SLionel Sambuc FSTEST_ENTER();
168*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_symlink("hoge", ASYMLINK) == -1);
169*11be35a1SLionel Sambuc FSTEST_EXIT();
170*11be35a1SLionel Sambuc }
171*11be35a1SLionel Sambuc
172*11be35a1SLionel Sambuc static void
createlink(const atf_tc_t * tc,const char * mp)173*11be35a1SLionel Sambuc createlink(const atf_tc_t *tc, const char *mp)
174*11be35a1SLionel Sambuc {
175*11be35a1SLionel Sambuc
176*11be35a1SLionel Sambuc FSTEST_ENTER();
177*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EROFS, rump_sys_link(AFILE, ALINK) == -1);
178*11be35a1SLionel Sambuc FSTEST_EXIT();
179*11be35a1SLionel Sambuc }
180*11be35a1SLionel Sambuc
181*11be35a1SLionel Sambuc ATF_TC_FSAPPLY_RO(create, "create file on r/o mount", nullgen);
182*11be35a1SLionel Sambuc ATF_TC_FSAPPLY_RO(rmfile, "remove file from r/o mount", filegen);
183*11be35a1SLionel Sambuc ATF_TC_FSAPPLY_RO(fileio, "can read a file but not write it", filegen);
184*11be35a1SLionel Sambuc ATF_TC_FSAPPLY_RO(attrs, "can query but not change attributes", filegen);
185*11be35a1SLionel Sambuc ATF_TC_FSAPPLY_RO(createdir, "create directory on r/o mount", nullgen);
186*11be35a1SLionel Sambuc ATF_TC_FSAPPLY_RO(createfifo, "create fifo on r/o mount", nullgen);
187*11be35a1SLionel Sambuc ATF_TC_FSAPPLY_RO(createsymlink, "create symlink on r/o mount", nullgen);
188*11be35a1SLionel Sambuc ATF_TC_FSAPPLY_RO(createlink, "create hardlink on r/o mount", filegen);
189*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)190*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
191*11be35a1SLionel Sambuc {
192*11be35a1SLionel Sambuc
193*11be35a1SLionel Sambuc ATF_TP_FSAPPLY_RO(create);
194*11be35a1SLionel Sambuc ATF_TP_FSAPPLY_RO(rmfile);
195*11be35a1SLionel Sambuc ATF_TP_FSAPPLY_RO(fileio);
196*11be35a1SLionel Sambuc ATF_TP_FSAPPLY_RO(attrs);
197*11be35a1SLionel Sambuc ATF_TP_FSAPPLY_RO(createdir);
198*11be35a1SLionel Sambuc ATF_TP_FSAPPLY_RO(createfifo);
199*11be35a1SLionel Sambuc ATF_TP_FSAPPLY_RO(createsymlink);
200*11be35a1SLionel Sambuc ATF_TP_FSAPPLY_RO(createlink);
201*11be35a1SLionel Sambuc
202*11be35a1SLionel Sambuc return atf_no_error();
203*11be35a1SLionel Sambuc }
204