t_basic.c revision 1.2
1/*	$NetBSD: t_basic.c,v 1.2 2010/05/31 23:44:54 pooka Exp $	*/
2
3#include <sys/types.h>
4#include <sys/mount.h>
5
6#include <atf-c.h>
7#include <err.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <stdio.h>
11#include <unistd.h>
12#include <string.h>
13#include <stdlib.h>
14
15#include <rump/rump.h>
16#include <rump/rump_syscalls.h>
17
18#include <miscfs/nullfs/null.h>
19#include <fs/tmpfs/tmpfs_args.h>
20
21#include "../../h_macros.h"
22
23ATF_TC(basic);
24ATF_TC_HEAD(basic, tc)
25{
26	atf_tc_set_md_var(tc, "descr", "basic nullfs functionality");
27}
28
29#define MSTR "magic bus"
30
31static void
32xput_tfile(const char *path, const char *mstr)
33{
34	int fd;
35
36	fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777);
37	if (fd == -1)
38		atf_tc_fail_errno("create %s", path);
39	if (rump_sys_write(fd, MSTR, sizeof(MSTR)) != sizeof(MSTR))
40		atf_tc_fail_errno("write to testfile");
41	rump_sys_close(fd);
42}
43
44static int
45xread_tfile(const char *path, const char *mstr)
46{
47	char buf[128];
48	int fd;
49
50	fd = rump_sys_open(path, O_RDONLY);
51	if (fd == -1)
52		return errno;
53	if (rump_sys_read(fd, buf, sizeof(buf)) == -1)
54		atf_tc_fail_errno("read tfile");
55	rump_sys_close(fd);
56	if (strcmp(buf, MSTR) == 0)
57		return 0;
58	return EPROGMISMATCH;
59}
60
61ATF_TC_BODY(basic, tc)
62{
63	struct null_args nargs;
64	struct tmpfs_args targs;
65	struct stat sb;
66	int error;
67
68	rump_init();
69	if (rump_sys_mkdir("/td1", 0777) == -1)
70		atf_tc_fail_errno("mp1");
71	if (rump_sys_mkdir("/td2", 0777) == -1)
72		atf_tc_fail_errno("mp1");
73
74	/* use tmpfs because rumpfs doesn't support regular files */
75	memset(&targs, 0, sizeof(targs));
76	targs.ta_version = TMPFS_ARGS_VERSION;
77	targs.ta_root_mode = 0777;
78	if (rump_sys_mount(MOUNT_TMPFS, "/td1", 0, &targs, sizeof(targs)) == -1)
79		atf_tc_fail_errno("could not mount tmpfs td1");
80
81	memset(&nargs, 0, sizeof(nargs));
82	nargs.nulla_target = __UNCONST("/td1");
83	if (rump_sys_mount(MOUNT_NULL, "/td2", 0, &nargs, sizeof(nargs)) == -1)
84		atf_tc_fail_errno("could not mount nullfs");
85
86	/* test unnull -> null */
87	xput_tfile("/td1/tensti", "jeppe");
88	error = xread_tfile("/td2/tensti", "jeppe");
89	if (error != 0)
90		atf_tc_fail("null compare failed: %d (%s)",
91		    error, strerror(error));
92
93	/* test null -> unnull */
94	xput_tfile("/td2/kiekko", "keppi");
95	error = xread_tfile("/td1/kiekko", "keppi");
96	if (error != 0)
97		atf_tc_fail("unnull compare failed: %d (%s)",
98		    error, strerror(error));
99
100	/* test unnull -> null overwrite */
101	xput_tfile("/td1/tensti", "se oolannin sota");
102	error = xread_tfile("/td2/tensti", "se oolannin sota");
103	if (error != 0)
104		atf_tc_fail("unnull compare failed: %d (%s)",
105		    error, strerror(error));
106
107	/* test that /td2 is unaffected in "real life" */
108	if (rump_sys_unmount("/td2", 0) == -1)
109		atf_tc_fail_errno("cannot unmount nullfs");
110	if ((error = rump_sys_stat("/td2/tensti", &sb)) != -1
111	    || errno != ENOENT) {
112		atf_tc_fail("stat tensti should return ENOENT, got %d", error);
113	}
114	if ((error = rump_sys_stat("/td2/kiekko", &sb)) != -1
115	    || errno != ENOENT) {
116		atf_tc_fail("stat kiekko should return ENOENT, got %d", error);
117	}
118
119	/* done */
120}
121
122ATF_TP_ADD_TCS(tp)
123{
124	ATF_TP_ADD_TC(tp, basic);
125	return 0; /*XXX?*/
126}
127