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, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include <dirent.h> 19 #include <errno.h> 20 #include <stddef.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <unistd.h> 25 #include <sys/stat.h> 26 #include <sys/types.h> 27 #include <sys/utsname.h> 28 29 #include <shared/util.h> 30 31 #include <libkmod/libkmod.h> 32 33 #include "testsuite.h" 34 35 36 #define TEST_UNAME "4.0.20-kmod" 37 static noreturn int testsuite_uname(const struct test *t) 38 { 39 struct utsname u; 40 int err = uname(&u); 41 42 if (err < 0) 43 exit(EXIT_FAILURE); 44 45 if (!streq(u.release, TEST_UNAME)) { 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 } 54 DEFINE_TEST(testsuite_uname, 55 .description = "test if trap to uname() works", 56 .config = { 57 [TC_UNAME_R] = TEST_UNAME, 58 }, 59 .need_spawn = true); 60 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 75 if (!streq(s, "kmod-test-chroot-works")) 76 return EXIT_FAILURE; 77 78 return EXIT_SUCCESS; 79 } 80 DEFINE_TEST(testsuite_rootfs_fopen, 81 .description = "test if rootfs works - fopen()", 82 .config = { 83 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", 84 }, 85 .need_spawn = true); 86 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; 100 if (r == -EAGAIN) 101 continue; 102 103 done += r; 104 } 105 106 buf[done] = '\0'; 107 108 if (!streq(buf, "kmod-test-chroot-works\n")) 109 return EXIT_FAILURE; 110 111 return EXIT_SUCCESS; 112 } 113 DEFINE_TEST(testsuite_rootfs_open, 114 .description = "test if rootfs works - open()", 115 .config = { 116 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", 117 }, 118 .need_spawn = true); 119 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 } 136 DEFINE_TEST(testsuite_rootfs_stat_access, 137 .description = "test if rootfs works - stat() and access()", 138 .config = { 139 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", 140 }, 141 .need_spawn = true); 142 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 } 156 DEFINE_TEST(testsuite_rootfs_opendir, 157 .description = "test if rootfs works - opendir()", 158 .config = { 159 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", 160 }, 161 .need_spawn = true); 162 163 TESTSUITE_MAIN(); 164