t_vfsops.c revision 1.3
1/*	$NetBSD: t_vfsops.c,v 1.3 2010/07/16 17:49:38 njoly 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 CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/stat.h>
30#include <sys/statvfs.h>
31
32#include <atf-c.h>
33#include <fcntl.h>
34#include <unistd.h>
35
36#include <rump/rump_syscalls.h>
37#include <rump/rump.h>
38
39#include "../common/h_fsmacros.h"
40#include "../../h_macros.h"
41
42static void
43tmount(const atf_tc_t *tc, const char *path)
44{
45
46	return;
47}
48
49static void
50tstatvfs(const atf_tc_t *tc, const char *path)
51{
52	const char *fstype = atf_tc_get_md_var(tc, "X-fs.type");
53	struct statvfs svb;
54
55	if (rump_sys_statvfs1(path, &svb, ST_WAIT) == -1)
56		atf_tc_fail_errno("statvfs");
57
58	ATF_REQUIRE(svb.f_namemax > 0 && svb.f_namemax <= MAXNAMLEN);
59	if (!FSTYPE_PUFFS(tc))
60		ATF_REQUIRE_STREQ(svb.f_fstypename, fstype);
61	ATF_REQUIRE_STREQ(svb.f_mntonname, path);
62}
63
64static void
65tsync(const atf_tc_t *tc, const char *path)
66{
67
68	rump_sys_sync();
69}
70
71#define MAGICSTR "just a string, I like A"
72static void
73tfilehandle(const atf_tc_t *tc, const char *path)
74{
75	char fpath[MAXPATHLEN];
76	char buf[sizeof(MAGICSTR)];
77	size_t fhsize;
78	void *fhp;
79	int fd;
80
81	sprintf(fpath, "%s/file", path);
82	fd = rump_sys_open(fpath, O_RDWR | O_CREAT, 0777);
83	if (fd == -1)
84		atf_tc_fail_errno("open");
85
86	if (rump_sys_write(fd, MAGICSTR, sizeof(MAGICSTR)) != sizeof(MAGICSTR))
87		atf_tc_fail("write to file");
88	rump_sys_close(fd);
89
90	/*
91	 * Get file handle size.
92	 * This also weeds out unsupported file systems.
93	 */
94	fhsize = 0;
95	if (rump_sys_getfh(fpath, NULL, &fhsize) == -1) {
96		if (errno == EOPNOTSUPP) {
97			atf_tc_skip("file handles not supported");
98		} else if (errno != E2BIG) {
99			atf_tc_fail_errno("getfh size");
100		}
101	}
102
103	fhp = malloc(fhsize);
104	if (rump_sys_getfh(fpath, fhp, &fhsize) == -1)
105		atf_tc_fail_errno("getfh");
106
107	/* open file based on file handle */
108	fd = rump_sys_fhopen(fhp, fhsize, O_RDONLY);
109	if (FSTYPE_TMPFS(tc)) {
110		atf_tc_expect_fail("PR kern/43605");
111		if (fd != -1 || errno != EINVAL)
112			atf_tc_expect_pass();
113	}
114	if (fd == -1) {
115		atf_tc_fail_errno("fhopen");
116	}
117
118	/* check that we got the same file */
119	if (rump_sys_read(fd, buf, sizeof(buf)) != sizeof(MAGICSTR))
120		atf_tc_fail("read fhopened file");
121
122	ATF_REQUIRE_STREQ(buf, MAGICSTR);
123
124	rump_sys_close(fd);
125}
126
127ATF_TC_FSAPPLY(tmount, "mount/unmount");
128ATF_TC_FSAPPLY(tstatvfs, "statvfs");
129ATF_TC_FSAPPLY(tsync, "sync");
130ATF_TC_FSAPPLY(tfilehandle, "file handles");
131
132ATF_TP_ADD_TCS(tp)
133{
134
135	ATF_TP_FSAPPLY(tmount);
136	ATF_TP_FSAPPLY(tstatvfs);
137	ATF_TP_FSAPPLY(tsync);
138	ATF_TP_FSAPPLY(tfilehandle);
139
140	return atf_no_error();
141}
142