blob: 56e73609f2048e158d9ba84ba8b1aa748d4c2483 [file] [log] [blame]
Lucas De Marchie701e382012-01-26 17:01:41 -02001/*
Lucas De Marchie6b0e492013-01-16 11:27:21 -02002 * Copyright (C) 2012-2013 ProFUSION embedded systems
Lucas De Marchie701e382012-01-26 17:01:41 -02003 *
Lucas De Marchie1b1ab22012-07-10 09:42:24 -03004 * 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 Marchie701e382012-01-26 17:01:41 -02008 *
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 Marchie1b1ab22012-07-10 09:42:24 -030011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
Lucas De Marchie701e382012-01-26 17:01:41 -020013 *
Lucas De Marchie1b1ab22012-07-10 09:42:24 -030014 * You should have received a copy of the GNU Lesser General Public
Lucas De Marchidea2dfe2014-12-25 23:32:03 -020015 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Lucas De Marchie701e382012-01-26 17:01:41 -020016 */
17
Lucas De Marchi4e36cb12012-01-25 12:42:13 -020018#include <dirent.h>
Lucas De Marchiab253112012-01-24 23:35:18 -020019#include <errno.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030020#include <stddef.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Lucas De Marchiab253112012-01-24 23:35:18 -020024#include <unistd.h>
Lucas De Marchi1426a612012-01-25 12:22:50 -020025#include <sys/stat.h>
26#include <sys/types.h>
Lucas De Marchiab253112012-01-24 23:35:18 -020027#include <sys/utsname.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030028
Lucas De Marchi5c42c5f2015-01-14 14:22:09 -020029#include <shared/util.h>
30
Lucas De Marchif3578662015-01-02 12:38:27 -020031#include <libkmod/libkmod.h>
Lucas De Marchiab253112012-01-24 23:35:18 -020032
33#include "testsuite.h"
34
35
36#define TEST_UNAME "4.0.20-kmod"
Lucas De Marchid96ca9c2013-12-17 19:10:16 -020037static noreturn int testsuite_uname(const struct test *t)
Lucas De Marchiab253112012-01-24 23:35:18 -020038{
39 struct utsname u;
40 int err = uname(&u);
41
42 if (err < 0)
43 exit(EXIT_FAILURE);
44
Lucas De Marchi5c42c5f2015-01-14 14:22:09 -020045 if (!streq(u.release, TEST_UNAME)) {
Lucas De Marchiab253112012-01-24 23:35:18 -020046 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 Marchif1155c12014-10-09 13:00:30 -030054DEFINE_TEST(testsuite_uname,
Lucas De Marchiab253112012-01-24 23:35:18 -020055 .description = "test if trap to uname() works",
Lucas De Marchiab253112012-01-24 23:35:18 -020056 .config = {
57 [TC_UNAME_R] = TEST_UNAME,
58 },
Lucas De Marchic5d81982012-02-07 10:46:46 -020059 .need_spawn = true);
Lucas De Marchiab253112012-01-24 23:35:18 -020060
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -020061static 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 Marchi5c42c5f2015-01-14 14:22:09 -020075 if (!streq(s, "kmod-test-chroot-works"))
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -020076 return EXIT_FAILURE;
77
78 return EXIT_SUCCESS;
79}
Lucas De Marchif1155c12014-10-09 13:00:30 -030080DEFINE_TEST(testsuite_rootfs_fopen,
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -020081 .description = "test if rootfs works - fopen()",
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -020082 .config = {
83 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
84 },
Lucas De Marchic5d81982012-02-07 10:46:46 -020085 .need_spawn = true);
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -020086
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -020087static 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 Marchia6ede6c2016-08-10 12:42:12 -0300100 if (r == -EAGAIN)
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -0200101 continue;
102
103 done += r;
104 }
105
106 buf[done] = '\0';
107
Lucas De Marchi5c42c5f2015-01-14 14:22:09 -0200108 if (!streq(buf, "kmod-test-chroot-works\n"))
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -0200109 return EXIT_FAILURE;
110
111 return EXIT_SUCCESS;
112}
Lucas De Marchif1155c12014-10-09 13:00:30 -0300113DEFINE_TEST(testsuite_rootfs_open,
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -0200114 .description = "test if rootfs works - open()",
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -0200115 .config = {
116 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
117 },
Lucas De Marchic5d81982012-02-07 10:46:46 -0200118 .need_spawn = true);
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -0200119
Lucas De Marchi1426a612012-01-25 12:22:50 -0200120static 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 Marchif1155c12014-10-09 13:00:30 -0300136DEFINE_TEST(testsuite_rootfs_stat_access,
Lucas De Marchi1426a612012-01-25 12:22:50 -0200137 .description = "test if rootfs works - stat() and access()",
Lucas De Marchi1426a612012-01-25 12:22:50 -0200138 .config = {
139 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
140 },
Lucas De Marchic5d81982012-02-07 10:46:46 -0200141 .need_spawn = true);
Lucas De Marchi1426a612012-01-25 12:22:50 -0200142
Lucas De Marchi4e36cb12012-01-25 12:42:13 -0200143static 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 Marchif1155c12014-10-09 13:00:30 -0300156DEFINE_TEST(testsuite_rootfs_opendir,
Lucas De Marchi4e36cb12012-01-25 12:42:13 -0200157 .description = "test if rootfs works - opendir()",
Lucas De Marchi4e36cb12012-01-25 12:42:13 -0200158 .config = {
159 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
160 },
Lucas De Marchic5d81982012-02-07 10:46:46 -0200161 .need_spawn = true);
Lucas De Marchi4e36cb12012-01-25 12:42:13 -0200162
Lucas De Marchi43289822014-10-09 14:29:04 -0300163TESTSUITE_MAIN();