]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-testsuite.c
testsuite: create additional pipe to monitor child
[thirdparty/kmod.git] / testsuite / test-testsuite.c
1 /*
2 * Copyright (C) 2012 ProFUSION embedded systems
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stddef.h>
21 #include <string.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/utsname.h>
28 #include <libkmod.h>
29
30 #include "testsuite.h"
31
32
33 #define TEST_UNAME "4.0.20-kmod"
34 static __noreturn int testsuite_uname(const struct test *t)
35 {
36 struct utsname u;
37 int err = uname(&u);
38
39 if (err < 0)
40 exit(EXIT_FAILURE);
41
42 if (strcmp(u.release, TEST_UNAME) != 0) {
43 char *ldpreload = getenv("LD_PRELOAD");
44 ERR("u.release=%s should be %s\n", u.release, TEST_UNAME);
45 ERR("LD_PRELOAD=%s\n", ldpreload);
46 exit(EXIT_FAILURE);
47 }
48
49 exit(EXIT_SUCCESS);
50 }
51 static DEFINE_TEST(testsuite_uname,
52 .description = "test if trap to uname() works",
53 .config = {
54 [TC_UNAME_R] = TEST_UNAME,
55 },
56 .need_spawn = true);
57
58 static int testsuite_rootfs_fopen(const struct test *t)
59 {
60 FILE *fp;
61 char s[100];
62 int n;
63
64 fp = fopen("/lib/modules/a", "r");
65 if (fp == NULL)
66 return EXIT_FAILURE;;
67
68 n = fscanf(fp, "%s", s);
69 if (n != 1)
70 return EXIT_FAILURE;
71
72 if (strcmp(s, "kmod-test-chroot-works") != 0)
73 return EXIT_FAILURE;
74
75 return EXIT_SUCCESS;
76 }
77 static DEFINE_TEST(testsuite_rootfs_fopen,
78 .description = "test if rootfs works - fopen()",
79 .config = {
80 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
81 },
82 .need_spawn = true);
83
84 static int testsuite_rootfs_open(const struct test *t)
85 {
86 char buf[100];
87 int fd, done;
88
89 fd = open("/lib/modules/a", O_RDONLY);
90 if (fd < 0)
91 return EXIT_FAILURE;
92
93 for (done = 0;;) {
94 int r = read(fd, buf + done, sizeof(buf) - 1 - done);
95 if (r == 0)
96 break;
97 if (r == -EWOULDBLOCK || r == -EAGAIN)
98 continue;
99
100 done += r;
101 }
102
103 buf[done] = '\0';
104
105 if (strcmp(buf, "kmod-test-chroot-works\n") != 0)
106 return EXIT_FAILURE;
107
108 return EXIT_SUCCESS;
109 }
110 static DEFINE_TEST(testsuite_rootfs_open,
111 .description = "test if rootfs works - open()",
112 .config = {
113 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
114 },
115 .need_spawn = true);
116
117 static int testsuite_rootfs_stat_access(const struct test *t)
118 {
119 struct stat st;
120
121 if (access("/lib/modules/a", F_OK) < 0) {
122 ERR("access failed: %m\n");
123 return EXIT_FAILURE;
124 }
125
126 if (stat("/lib/modules/a", &st) < 0) {
127 ERR("stat failed: %m\n");
128 return EXIT_FAILURE;
129 }
130
131 return EXIT_SUCCESS;
132 }
133 static DEFINE_TEST(testsuite_rootfs_stat_access,
134 .description = "test if rootfs works - stat() and access()",
135 .config = {
136 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
137 },
138 .need_spawn = true);
139
140 static int testsuite_rootfs_opendir(const struct test *t)
141 {
142 DIR *d;
143
144 d = opendir("/testdir");
145 if (d == NULL) {
146 ERR("opendir failed: %m\n");
147 return EXIT_FAILURE;
148 }
149
150 closedir(d);
151 return EXIT_SUCCESS;
152 }
153 static DEFINE_TEST(testsuite_rootfs_opendir,
154 .description = "test if rootfs works - opendir()",
155 .config = {
156 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
157 },
158 .need_spawn = true);
159
160 static const struct test *tests[] = {
161 &stestsuite_uname,
162 &stestsuite_rootfs_fopen,
163 &stestsuite_rootfs_open,
164 &stestsuite_rootfs_stat_access,
165 &stestsuite_rootfs_opendir,
166 NULL,
167 };
168
169 TESTSUITE_MAIN(tests);