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