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