Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 ProFUSION embedded systems |
| 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 |
| 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 |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 17 | */ |
| 18 | |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stddef.h> |
| 22 | #include <string.h> |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 23 | #include <dirent.h> |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | #include <unistd.h> |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 28 | #include <sys/utsname.h> |
| 29 | #include <libkmod.h> |
| 30 | |
| 31 | #include "testsuite.h" |
| 32 | |
| 33 | |
| 34 | #define TEST_UNAME "4.0.20-kmod" |
Lucas De Marchi | 32d29b3 | 2012-02-08 20:32:31 -0200 | [diff] [blame] | 35 | static __noreturn int testsuite_uname(const struct test *t) |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 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 | } |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 52 | static DEFINE_TEST(testsuite_uname, |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 53 | .description = "test if trap to uname() works", |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 54 | .config = { |
| 55 | [TC_UNAME_R] = TEST_UNAME, |
| 56 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 57 | .need_spawn = true); |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 58 | |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 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 | } |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 78 | static DEFINE_TEST(testsuite_rootfs_fopen, |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 79 | .description = "test if rootfs works - fopen()", |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 80 | .config = { |
| 81 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", |
| 82 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 83 | .need_spawn = true); |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 84 | |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 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 | } |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 111 | static DEFINE_TEST(testsuite_rootfs_open, |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 112 | .description = "test if rootfs works - open()", |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 113 | .config = { |
| 114 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", |
| 115 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 116 | .need_spawn = true); |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 117 | |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 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 | } |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 134 | static DEFINE_TEST(testsuite_rootfs_stat_access, |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 135 | .description = "test if rootfs works - stat() and access()", |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 136 | .config = { |
| 137 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", |
| 138 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 139 | .need_spawn = true); |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 140 | |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 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 | } |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 154 | static DEFINE_TEST(testsuite_rootfs_opendir, |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 155 | .description = "test if rootfs works - opendir()", |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 156 | .config = { |
| 157 | [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", |
| 158 | }, |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 159 | .need_spawn = true); |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 160 | |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 161 | static const struct test *tests[] = { |
| 162 | &stestsuite_uname, |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 163 | &stestsuite_rootfs_fopen, |
Lucas De Marchi | 7fa8c2d | 2012-01-25 11:36:28 -0200 | [diff] [blame] | 164 | &stestsuite_rootfs_open, |
Lucas De Marchi | 1426a61 | 2012-01-25 12:22:50 -0200 | [diff] [blame] | 165 | &stestsuite_rootfs_stat_access, |
Lucas De Marchi | 4e36cb1 | 2012-01-25 12:42:13 -0200 | [diff] [blame] | 166 | &stestsuite_rootfs_opendir, |
Lucas De Marchi | ab25311 | 2012-01-24 23:35:18 -0200 | [diff] [blame] | 167 | NULL, |
| 168 | }; |
| 169 | |
Lucas De Marchi | e9fa9de | 2012-02-07 10:09:20 -0200 | [diff] [blame] | 170 | TESTSUITE_MAIN(tests); |