Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 1 | /* |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 2 | * Copyright (C) 2012-2013 ProFUSION embedded systems |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 3 | * |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 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. |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 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 |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 13 | * |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 14 | * You should have received a copy of the GNU Lesser General Public |
Lucas De Marchi | dea2dfe | 2014-12-25 23:32:03 -0200 | [diff] [blame] | 15 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 16 | */ |
| 17 | |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 18 | #include <dirent.h> |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 19 | #include <errno.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 20 | #include <stddef.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 24 | #include <unistd.h> |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 27 | #include <sys/utsname.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 28 | |
Lucas De Marchi | 5c42c5f | 2015-01-14 14:22:09 -0200 | [diff] [blame] | 29 | #include <shared/util.h> |
| 30 | |
Lucas De Marchi | f357866 | 2015-01-02 12:38:27 -0200 | [diff] [blame] | 31 | #include <libkmod/libkmod.h> |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 32 | |
| 33 | #include "testsuite.h" |
| 34 | |
| 35 | |
| 36 | #define TEST_UNAME "4.0.20-kmod" |
Lucas De Marchi | d96ca9c | 2013-12-17 19:10:16 -0200 | [diff] [blame] | 37 | static noreturn int testsuite_uname(const struct test *t) |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 38 | { |
| 39 | struct utsname u; |
| 40 | int err = uname(&u); |
| 41 | |
| 42 | if (err < 0) |
| 43 | exit(EXIT_FAILURE); |
| 44 | |
Lucas De Marchi | 5c42c5f | 2015-01-14 14:22:09 -0200 | [diff] [blame] | 45 | if (!streq(u.release, TEST_UNAME)) { |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 46 | char *ldpreload = getenv("LD_PRELOAD"); |
| 47 | ERR("u.release=%s should be %s\n", u.release, TEST_UNAME); |
| 48 | ERR("LD_PRELOAD=%s\n", ldpreload); |
| 49 | exit(EXIT_FAILURE); |
| 50 | } |
| 51 | |
| 52 | exit(EXIT_SUCCESS); |
| 53 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 54 | DEFINE_TEST(testsuite_uname, |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 55 | .description = "test if trap to uname() works", |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 56 | .config = { |
| 57 | [TC_UNAME_R] = TEST_UNAME, |
| 58 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 59 | .need_spawn = true); |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 60 | |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 61 | static int testsuite_rootfs_fopen(const struct test *t) |
| 62 | { |
| 63 | FILE *fp; |
| 64 | char s[100]; |
| 65 | int n; |
| 66 | |
| 67 | fp = fopen("/lib/modules/a", "r"); |
| 68 | if (fp == NULL) |
| 69 | return EXIT_FAILURE;; |
| 70 | |
| 71 | n = fscanf(fp, "%s", s); |
| 72 | if (n != 1) |
| 73 | return EXIT_FAILURE; |
| 74 | |
Lucas De Marchi | 5c42c5f | 2015-01-14 14:22:09 -0200 | [diff] [blame] | 75 | if (!streq(s, "kmod-test-chroot-works")) |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 76 | return EXIT_FAILURE; |
| 77 | |
| 78 | return EXIT_SUCCESS; |
| 79 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 80 | DEFINE_TEST(testsuite_rootfs_fopen, |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 81 | .description = "test if rootfs works - fopen()", |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 82 | .config = { |
| 83 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", |
| 84 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 85 | .need_spawn = true); |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 86 | |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 87 | static int testsuite_rootfs_open(const struct test *t) |
| 88 | { |
| 89 | char buf[100]; |
| 90 | int fd, done; |
| 91 | |
| 92 | fd = open("/lib/modules/a", O_RDONLY); |
| 93 | if (fd < 0) |
| 94 | return EXIT_FAILURE; |
| 95 | |
| 96 | for (done = 0;;) { |
| 97 | int r = read(fd, buf + done, sizeof(buf) - 1 - done); |
| 98 | if (r == 0) |
| 99 | break; |
Lucas De Marchi | a6ede6c | 2016-08-10 12:42:12 -0300 | [diff] [blame] | 100 | if (r == -EAGAIN) |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 101 | continue; |
| 102 | |
| 103 | done += r; |
| 104 | } |
| 105 | |
| 106 | buf[done] = '\0'; |
| 107 | |
Lucas De Marchi | 5c42c5f | 2015-01-14 14:22:09 -0200 | [diff] [blame] | 108 | if (!streq(buf, "kmod-test-chroot-works\n")) |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 109 | return EXIT_FAILURE; |
| 110 | |
| 111 | return EXIT_SUCCESS; |
| 112 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 113 | DEFINE_TEST(testsuite_rootfs_open, |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 114 | .description = "test if rootfs works - open()", |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 115 | .config = { |
| 116 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", |
| 117 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 118 | .need_spawn = true); |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 119 | |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 120 | static int testsuite_rootfs_stat_access(const struct test *t) |
| 121 | { |
| 122 | struct stat st; |
| 123 | |
| 124 | if (access("/lib/modules/a", F_OK) < 0) { |
| 125 | ERR("access failed: %m\n"); |
| 126 | return EXIT_FAILURE; |
| 127 | } |
| 128 | |
| 129 | if (stat("/lib/modules/a", &st) < 0) { |
| 130 | ERR("stat failed: %m\n"); |
| 131 | return EXIT_FAILURE; |
| 132 | } |
| 133 | |
| 134 | return EXIT_SUCCESS; |
| 135 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 136 | DEFINE_TEST(testsuite_rootfs_stat_access, |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 137 | .description = "test if rootfs works - stat() and access()", |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 138 | .config = { |
| 139 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", |
| 140 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 141 | .need_spawn = true); |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 142 | |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 143 | static int testsuite_rootfs_opendir(const struct test *t) |
| 144 | { |
| 145 | DIR *d; |
| 146 | |
| 147 | d = opendir("/testdir"); |
| 148 | if (d == NULL) { |
| 149 | ERR("opendir failed: %m\n"); |
| 150 | return EXIT_FAILURE; |
| 151 | } |
| 152 | |
| 153 | closedir(d); |
| 154 | return EXIT_SUCCESS; |
| 155 | } |
Lucas De Marchi | f1155c1 | 2014-10-09 13:00:30 -0300 | [diff] [blame] | 156 | DEFINE_TEST(testsuite_rootfs_opendir, |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 157 | .description = "test if rootfs works - opendir()", |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 158 | .config = { |
| 159 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", |
| 160 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 161 | .need_spawn = true); |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 162 | |
Lucas De Marchi | 4328982 | 2014-10-09 14:29:04 -0300 | [diff] [blame] | 163 | TESTSUITE_MAIN(); |