]>
Commit | Line | Data |
---|---|---|
e701e381 | 1 | /* |
e6b0e49b | 2 | * Copyright (C) 2012-2013 ProFUSION embedded systems |
e701e381 | 3 | * |
e1b1ab24 LDM |
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. | |
e701e381 LDM |
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 | |
e1b1ab24 LDM |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. | |
e701e381 | 13 | * |
e1b1ab24 LDM |
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 | |
e701e381 LDM |
17 | */ |
18 | ||
ab253110 LDM |
19 | #include <stdio.h> |
20 | #include <stdlib.h> | |
21 | #include <stddef.h> | |
22 | #include <string.h> | |
4e36cb18 | 23 | #include <dirent.h> |
ab253110 LDM |
24 | #include <errno.h> |
25 | #include <unistd.h> | |
1426a613 LDM |
26 | #include <sys/stat.h> |
27 | #include <sys/types.h> | |
ab253110 LDM |
28 | #include <sys/utsname.h> |
29 | #include <libkmod.h> | |
30 | ||
31 | #include "testsuite.h" | |
32 | ||
33 | ||
34 | #define TEST_UNAME "4.0.20-kmod" | |
d96ca9c4 | 35 | static noreturn int testsuite_uname(const struct test *t) |
ab253110 LDM |
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 | } | |
c5d81989 | 52 | static DEFINE_TEST(testsuite_uname, |
ab253110 | 53 | .description = "test if trap to uname() works", |
ab253110 LDM |
54 | .config = { |
55 | [TC_UNAME_R] = TEST_UNAME, | |
56 | }, | |
c5d81989 | 57 | .need_spawn = true); |
ab253110 | 58 | |
6afc9cd6 LDM |
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 | } | |
c5d81989 | 78 | static DEFINE_TEST(testsuite_rootfs_fopen, |
6afc9cd6 | 79 | .description = "test if rootfs works - fopen()", |
6afc9cd6 LDM |
80 | .config = { |
81 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", | |
82 | }, | |
c5d81989 | 83 | .need_spawn = true); |
6afc9cd6 | 84 | |
7fa8c2d2 LDM |
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 | } | |
c5d81989 | 111 | static DEFINE_TEST(testsuite_rootfs_open, |
7fa8c2d2 | 112 | .description = "test if rootfs works - open()", |
7fa8c2d2 LDM |
113 | .config = { |
114 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", | |
115 | }, | |
c5d81989 | 116 | .need_spawn = true); |
7fa8c2d2 | 117 | |
1426a613 LDM |
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 | } | |
c5d81989 | 134 | static DEFINE_TEST(testsuite_rootfs_stat_access, |
1426a613 | 135 | .description = "test if rootfs works - stat() and access()", |
1426a613 LDM |
136 | .config = { |
137 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", | |
138 | }, | |
c5d81989 | 139 | .need_spawn = true); |
1426a613 | 140 | |
4e36cb18 LDM |
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 | } | |
c5d81989 | 154 | static DEFINE_TEST(testsuite_rootfs_opendir, |
4e36cb18 | 155 | .description = "test if rootfs works - opendir()", |
4e36cb18 LDM |
156 | .config = { |
157 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", | |
158 | }, | |
c5d81989 | 159 | .need_spawn = true); |
4e36cb18 | 160 | |
ab253110 LDM |
161 | static const struct test *tests[] = { |
162 | &stestsuite_uname, | |
6afc9cd6 | 163 | &stestsuite_rootfs_fopen, |
7fa8c2d2 | 164 | &stestsuite_rootfs_open, |
1426a613 | 165 | &stestsuite_rootfs_stat_access, |
4e36cb18 | 166 | &stestsuite_rootfs_opendir, |
ab253110 LDM |
167 | NULL, |
168 | }; | |
169 | ||
e9fa9de3 | 170 | TESTSUITE_MAIN(tests); |