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