blob: 8518431ea9474fc7b1360058a6f7d59a0e7b8fcb [file] [log] [blame]
Lucas De Marchie701e382012-01-26 17:01:41 -02001/*
2 * Copyright (C) 2012 ProFUSION embedded systems
3 *
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
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 Marchie701e382012-01-26 17:01:41 -020017 */
18
Lucas De Marchiab253112012-01-24 23:35:18 -020019#include <stdio.h>
20#include <stdlib.h>
21#include <stddef.h>
22#include <string.h>
Lucas De Marchi4e36cb12012-01-25 12:42:13 -020023#include <dirent.h>
Lucas De Marchiab253112012-01-24 23:35:18 -020024#include <errno.h>
25#include <unistd.h>
Lucas De Marchi1426a612012-01-25 12:22:50 -020026#include <sys/stat.h>
27#include <sys/types.h>
Lucas De Marchiab253112012-01-24 23:35:18 -020028#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 Marchi32d29b32012-02-08 20:32:31 -020035static __noreturn int testsuite_uname(const struct test *t)
Lucas De Marchiab253112012-01-24 23:35:18 -020036{
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 Marchic5d81982012-02-07 10:46:46 -020052static DEFINE_TEST(testsuite_uname,
Lucas De Marchiab253112012-01-24 23:35:18 -020053 .description = "test if trap to uname() works",
Lucas De Marchiab253112012-01-24 23:35:18 -020054 .config = {
55 [TC_UNAME_R] = TEST_UNAME,
56 },
Lucas De Marchic5d81982012-02-07 10:46:46 -020057 .need_spawn = true);
Lucas De Marchiab253112012-01-24 23:35:18 -020058
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -020059static 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 Marchic5d81982012-02-07 10:46:46 -020078static DEFINE_TEST(testsuite_rootfs_fopen,
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -020079 .description = "test if rootfs works - fopen()",
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -020080 .config = {
81 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
82 },
Lucas De Marchic5d81982012-02-07 10:46:46 -020083 .need_spawn = true);
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -020084
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -020085static 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 Marchic5d81982012-02-07 10:46:46 -0200111static DEFINE_TEST(testsuite_rootfs_open,
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -0200112 .description = "test if rootfs works - open()",
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -0200113 .config = {
114 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
115 },
Lucas De Marchic5d81982012-02-07 10:46:46 -0200116 .need_spawn = true);
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -0200117
Lucas De Marchi1426a612012-01-25 12:22:50 -0200118static 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 Marchic5d81982012-02-07 10:46:46 -0200134static DEFINE_TEST(testsuite_rootfs_stat_access,
Lucas De Marchi1426a612012-01-25 12:22:50 -0200135 .description = "test if rootfs works - stat() and access()",
Lucas De Marchi1426a612012-01-25 12:22:50 -0200136 .config = {
137 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
138 },
Lucas De Marchic5d81982012-02-07 10:46:46 -0200139 .need_spawn = true);
Lucas De Marchi1426a612012-01-25 12:22:50 -0200140
Lucas De Marchi4e36cb12012-01-25 12:42:13 -0200141static 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 Marchic5d81982012-02-07 10:46:46 -0200154static DEFINE_TEST(testsuite_rootfs_opendir,
Lucas De Marchi4e36cb12012-01-25 12:42:13 -0200155 .description = "test if rootfs works - opendir()",
Lucas De Marchi4e36cb12012-01-25 12:42:13 -0200156 .config = {
157 [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
158 },
Lucas De Marchic5d81982012-02-07 10:46:46 -0200159 .need_spawn = true);
Lucas De Marchi4e36cb12012-01-25 12:42:13 -0200160
Lucas De Marchiab253112012-01-24 23:35:18 -0200161static const struct test *tests[] = {
162 &stestsuite_uname,
Lucas De Marchi6afc9cd2012-01-25 02:44:45 -0200163 &stestsuite_rootfs_fopen,
Lucas De Marchi7fa8c2d2012-01-25 11:36:28 -0200164 &stestsuite_rootfs_open,
Lucas De Marchi1426a612012-01-25 12:22:50 -0200165 &stestsuite_rootfs_stat_access,
Lucas De Marchi4e36cb12012-01-25 12:42:13 -0200166 &stestsuite_rootfs_opendir,
Lucas De Marchiab253112012-01-24 23:35:18 -0200167 NULL,
168};
169
Lucas De Marchie9fa9de2012-02-07 10:09:20 -0200170TESTSUITE_MAIN(tests);