xref: /netbsd-src/tests/fs/kernfs/t_basic.c (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
1 /*	$NetBSD: t_basic.c,v 1.2 2010/04/26 23:47:25 pooka Exp $	*/
2 
3 #include <sys/types.h>
4 #include <sys/mount.h>
5 #include <sys/module.h>
6 #include <sys/dirent.h>
7 #include <sys/sysctl.h>
8 
9 #include <atf-c.h>
10 #include <err.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <stdlib.h>
17 
18 #include <rump/rump.h>
19 #include <rump/rump_syscalls.h>
20 
21 #include <miscfs/kernfs/kernfs.h>
22 
23 #define USE_ATF
24 #include "../../h_macros.h"
25 
26 #ifdef USE_ATF
27 ATF_TC(getdents);
28 ATF_TC_HEAD(getdents, tc)
29 {
30 
31 	atf_tc_set_md_var(tc, "descr", "kernfs directory contains files");
32 }
33 #else
34 #define atf_tc_fail(...) errx(1, __VA_ARGS__)
35 #endif
36 
37 static void
38 mountkernfs(void)
39 {
40 
41 	rump_init();
42 
43 	if (rump_sys_mkdir("/kern", 0777) == -1)
44 		atf_tc_fail_errno("mkdir /kern");
45 	if (rump_sys_mount(MOUNT_KERNFS, "/kern", 0, NULL, 0) == -1)
46 		atf_tc_fail_errno("could not mount kernfs");
47 }
48 
49 #ifdef USE_ATF
50 ATF_TC_BODY(getdents, tc)
51 #else
52 int main(int argc, char *argv[])
53 #endif
54 {
55 	struct dirent *dent;
56 	char buf[8192];
57 	int dfd;
58 
59 	mountkernfs();
60 
61 	if ((dfd = rump_sys_open("/kern", O_RDONLY)) == -1)
62 		atf_tc_fail_errno("can't open directory");
63 	if (rump_sys_getdents(dfd, buf, sizeof(buf)) == -1)
64 		atf_tc_fail_errno("getdents");
65 
66 	/*
67 	 * Check that we get the first three values (., .., boottime).
68 	 * Make more complete by autogenerating list from kernfs_vnops.c?
69 	 */
70 	dent = (void *)buf;
71 	ATF_REQUIRE_STREQ(dent->d_name, ".");
72 	dent = _DIRENT_NEXT(dent);
73 	ATF_REQUIRE_STREQ(dent->d_name, "..");
74 	dent = _DIRENT_NEXT(dent);
75 	ATF_REQUIRE_STREQ(dent->d_name, "boottime");
76 
77 	/* done */
78 }
79 
80 #ifdef USE_ATF
81 ATF_TC(hostname);
82 ATF_TC_HEAD(hostname, tc)
83 {
84 
85 	atf_tc_set_md_var(tc, "descr", "/kern/hostname changes hostname");
86 }
87 
88 static char *
89 getthehost(void)
90 {
91 	static char buf[8192];
92 	int mib[2];
93 	size_t blen;
94 
95 	mib[0] = CTL_KERN;
96 	mib[1] = KERN_HOSTNAME;
97 	blen = sizeof(buf);
98 	if (rump_sys___sysctl(mib, 2, buf, &blen, NULL, 0) == -1)
99 		atf_tc_fail_errno("sysctl gethostname");
100 
101 	return buf;
102 }
103 
104 #define NEWHOSTNAME "turboton roos-berg"
105 ATF_TC_BODY(hostname, tc)
106 {
107 	char buf[8192];
108 	char *shost, *p;
109 	int fd;
110 
111 	mountkernfs();
112 	if ((fd = rump_sys_open("/kern/hostname", O_RDWR)) == -1)
113 		atf_tc_fail_errno("open hostname");
114 
115 	/* check initial match */
116 	shost = getthehost();
117 	buf[0] = '\0';
118 	if (rump_sys_read(fd, buf, sizeof(buf)) == -1)
119 		atf_tc_fail_errno("read hostname");
120 	p = strchr(buf, '\n');
121 	if (p)
122 		 *p = '\0';
123 	ATF_REQUIRE_STREQ_MSG(buf, shost, "initial hostname mismatch");
124 
125 	/* check changing hostname works */
126 	if (rump_sys_pwrite(fd, NEWHOSTNAME, strlen(NEWHOSTNAME), 0)
127 	    != strlen(NEWHOSTNAME)) {
128 		atf_tc_fail_errno("write new hostname");
129 	}
130 
131 	shost = getthehost();
132 	ATF_REQUIRE_STREQ_MSG(NEWHOSTNAME, shost, "modified hostname mismatch");
133 
134 	/* done */
135 }
136 
137 ATF_TP_ADD_TCS(tp)
138 {
139 	ATF_TP_ADD_TC(tp, hostname);
140 	ATF_TP_ADD_TC(tp, getdents);
141 
142 	return atf_no_error();
143 }
144 #endif
145