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