]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/test-testsuite.c
Move hash implementation to shared directory
[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 <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/utsname.h>
29 #include <libkmod.h>
30
31 #include "testsuite.h"
32
33
34 #define TEST_UNAME "4.0.20-kmod"
35 static noreturn int testsuite_uname(const struct test *t)
36 {
37 struct utsname u;
38 int err = uname(&u);
39
40 if (err < 0)
41 exit(EXIT_FAILURE);
42
43 if (strcmp(u.release, TEST_UNAME) != 0) {
44 char *ldpreload = getenv("LD_PRELOAD");
45 ERR("u.release=%s should be %s\n", u.release, TEST_UNAME);
46 ERR("LD_PRELOAD=%s\n", ldpreload);
47 exit(EXIT_FAILURE);
48 }
49
50 exit(EXIT_SUCCESS);
51 }
52 static DEFINE_TEST(testsuite_uname,
53 .description = "test if trap to uname() works",
54 .config = {
55 [TC_UNAME_R] = TEST_UNAME,
56 },
57 .need_spawn = true);
58
59 static int testsuite_rootfs_fopen(const struct test *t)
60 {
61 FILE *fp;
62 char s[100];
63 int n;
64
65 fp = fopen("/lib/modules/a", "r");
66 if (fp == NULL)
67 return EXIT_FAILURE;;
68
69 n = fscanf(fp, "%s", s);
70 if (n != 1)
71 return EXIT_FAILURE;
72
73 if (strcmp(s, "kmod-test-chroot-works") != 0)
74 return EXIT_FAILURE;
75
76 return EXIT_SUCCESS;
77 }
78 static DEFINE_TEST(testsuite_rootfs_fopen,
79 .description = "test if rootfs works - fopen()",
80 .config = {
81 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
82 },
83 .need_spawn = true);
84
85 static int testsuite_rootfs_open(const struct test *t)
86 {
87 char buf[100];
88 int fd, done;
89
90 fd = open("/lib/modules/a", O_RDONLY);
91 if (fd < 0)
92 return EXIT_FAILURE;
93
94 for (done = 0;;) {
95 int r = read(fd, buf + done, sizeof(buf) - 1 - done);
96 if (r == 0)
97 break;
98 if (r == -EWOULDBLOCK || r == -EAGAIN)
99 continue;
100
101 done += r;
102 }
103
104 buf[done] = '\0';
105
106 if (strcmp(buf, "kmod-test-chroot-works\n") != 0)
107 return EXIT_FAILURE;
108
109 return EXIT_SUCCESS;
110 }
111 static DEFINE_TEST(testsuite_rootfs_open,
112 .description = "test if rootfs works - open()",
113 .config = {
114 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
115 },
116 .need_spawn = true);
117
118 static int testsuite_rootfs_stat_access(const struct test *t)
119 {
120 struct stat st;
121
122 if (access("/lib/modules/a", F_OK) < 0) {
123 ERR("access failed: %m\n");
124 return EXIT_FAILURE;
125 }
126
127 if (stat("/lib/modules/a", &st) < 0) {
128 ERR("stat failed: %m\n");
129 return EXIT_FAILURE;
130 }
131
132 return EXIT_SUCCESS;
133 }
134 static DEFINE_TEST(testsuite_rootfs_stat_access,
135 .description = "test if rootfs works - stat() and access()",
136 .config = {
137 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
138 },
139 .need_spawn = true);
140
141 static int testsuite_rootfs_opendir(const struct test *t)
142 {
143 DIR *d;
144
145 d = opendir("/testdir");
146 if (d == NULL) {
147 ERR("opendir failed: %m\n");
148 return EXIT_FAILURE;
149 }
150
151 closedir(d);
152 return EXIT_SUCCESS;
153 }
154 static DEFINE_TEST(testsuite_rootfs_opendir,
155 .description = "test if rootfs works - opendir()",
156 .config = {
157 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
158 },
159 .need_spawn = true);
160
161 static const struct test *tests[] = {
162 &stestsuite_uname,
163 &stestsuite_rootfs_fopen,
164 &stestsuite_rootfs_open,
165 &stestsuite_rootfs_stat_access,
166 &stestsuite_rootfs_opendir,
167 NULL,
168 };
169
170 TESTSUITE_MAIN(tests);