2 * Copyright (C) 2012-2013 ProFUSION embedded systems
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.
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.
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
30 #include <sys/epoll.h>
31 #include <sys/prctl.h>
35 #include <shared/util.h>
37 #include <libkmod-util.h>
39 #include "testsuite.h"
41 static const char *ANSI_HIGHLIGHT_GREEN_ON
= "\x1B[1;32m";
42 static const char *ANSI_HIGHLIGHT_RED_ON
= "\x1B[1;31m";
43 static const char *ANSI_HIGHLIGHT_OFF
= "\x1B[0m";
45 static const char *progname
;
46 static int oneshot
= 0;
47 static const char options_short
[] = "lhn";
48 static const struct option options
[] = {
49 { "list", no_argument
, 0, 'l' },
50 { "help", no_argument
, 0, 'h' },
54 #define OVERRIDE_LIBDIR ABS_TOP_BUILDDIR "/testsuite/.libs/"
58 const char *ldpreload
;
59 } env_config
[_TC_LAST
] = {
60 [TC_UNAME_R
] = { S_TC_UNAME_R
, OVERRIDE_LIBDIR
"uname.so" },
61 [TC_ROOTFS
] = { S_TC_ROOTFS
, OVERRIDE_LIBDIR
"path.so" },
62 [TC_INIT_MODULE_RETCODES
] = { S_TC_INIT_MODULE_RETCODES
, OVERRIDE_LIBDIR
"init_module.so" },
63 [TC_DELETE_MODULE_RETCODES
] = { S_TC_DELETE_MODULE_RETCODES
, OVERRIDE_LIBDIR
"delete_module.so" },
66 #define USEC_PER_SEC 1000000ULL
67 #define USEC_PER_MSEC 1000ULL
68 #define TEST_TIMEOUT_USEC 2 * USEC_PER_SEC
69 static unsigned long long now_usec(void)
73 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) != 0)
79 static void help(void)
81 const struct option
*itr
;
82 const char *itr_short
;
85 "\t%s [options] <test>\n"
86 "Options:\n", basename(progname
));
88 for (itr
= options
, itr_short
= options_short
;
89 itr
->name
!= NULL
; itr
++, itr_short
++)
90 printf("\t-%c, --%s\n", *itr_short
, itr
->name
);
93 static void test_list(const struct test
*tests
[])
97 printf("Available tests:\n");
98 for (i
= 0; tests
[i
] != NULL
; i
++)
99 printf("\t%s, %s\n", tests
[i
]->name
, tests
[i
]->description
);
102 int test_init(int argc
, char *const argv
[], const struct test
*tests
[])
108 c
= getopt_long(argc
, argv
, options_short
, options
, &idx
);
124 ERR("unexpected getopt_long() value %c\n", c
);
129 if (isatty(STDOUT_FILENO
) == 0) {
130 ANSI_HIGHLIGHT_OFF
= "";
131 ANSI_HIGHLIGHT_RED_ON
= "";
132 ANSI_HIGHLIGHT_GREEN_ON
= "";
138 const struct test
*test_find(const struct test
*tests
[], const char *name
)
142 for (i
= 0; tests
[i
] != NULL
; i
++) {
143 if (strcmp(tests
[i
]->name
, name
) == 0)
150 static int test_spawn_test(const struct test
*t
)
152 const char *const args
[] = { progname
, "-n", t
->name
, NULL
};
154 execv(progname
, (char *const *) args
);
156 ERR("failed to spawn %s for %s: %m\n", progname
, t
->name
);
160 static int test_run_spawned(const struct test
*t
)
162 int err
= t
->func(t
);
168 int test_spawn_prog(const char *prog
, const char *const args
[])
170 execv(prog
, (char *const *) args
);
172 ERR("failed to spawn %s\n", prog
);
173 ERR("did you forget to build tools?\n");
177 static void test_export_environ(const struct test
*t
)
179 char *preload
= NULL
;
180 size_t preloadlen
= 0;
182 const struct keyval
*env
;
184 unsetenv("LD_PRELOAD");
186 for (i
= 0; i
< _TC_LAST
; i
++) {
187 const char *ldpreload
;
191 if (t
->config
[i
] == NULL
)
194 setenv(env_config
[i
].key
, t
->config
[i
], 1);
196 ldpreload
= env_config
[i
].ldpreload
;
197 ldpreloadlen
= strlen(ldpreload
);
198 tmp
= realloc(preload
, preloadlen
+ 2 + ldpreloadlen
);
200 ERR("oom: test_export_environ()\n");
206 preload
[preloadlen
++] = ' ';
207 memcpy(preload
+ preloadlen
, ldpreload
, ldpreloadlen
);
208 preloadlen
+= ldpreloadlen
;
209 preload
[preloadlen
] = '\0';
213 setenv("LD_PRELOAD", preload
, 1);
217 for (env
= t
->env_vars
; env
&& env
->key
; env
++)
218 setenv(env
->key
, env
->val
, 1);
221 static inline int test_run_child(const struct test
*t
, int fdout
[2],
222 int fderr
[2], int fdmonitor
[2])
224 /* kill child if parent dies */
225 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
227 test_export_environ(t
);
229 /* Close read-fds and redirect std{out,err} to the write-fds */
230 if (t
->output
.out
!= NULL
) {
232 if (dup2(fdout
[1], STDOUT_FILENO
) < 0) {
233 ERR("could not redirect stdout to pipe: %m\n");
238 if (t
->output
.err
!= NULL
) {
240 if (dup2(fderr
[1], STDERR_FILENO
) < 0) {
241 ERR("could not redirect stderr to pipe: %m\n");
248 if (t
->config
[TC_ROOTFS
] != NULL
) {
249 const char *stamp
= TESTSUITE_ROOTFS
"../stamp-rootfs";
250 const char *rootfs
= t
->config
[TC_ROOTFS
];
251 struct stat rootfsst
, stampst
;
253 if (stat(stamp
, &stampst
) != 0) {
254 ERR("could not stat %s\n - %m", stamp
);
258 if (stat(rootfs
, &rootfsst
) != 0) {
259 ERR("could not stat %s\n - %m", rootfs
);
263 if (stat_mstamp(&rootfsst
) > stat_mstamp(&stampst
)) {
264 ERR("rootfs %s is dirty, please run 'make rootfs' before runnning this test\n",
271 return test_spawn_test(t
);
273 return test_run_spawned(t
);
276 static inline bool test_run_parent_check_outputs(const struct test
*t
,
277 int fdout
, int fderr
, int fdmonitor
, pid_t child
)
279 struct epoll_event ep_outpipe
, ep_errpipe
, ep_monitor
;
280 int err
, fd_ep
, fd_matchout
= -1, fd_matcherr
= -1;
281 unsigned long long end_usec
, start_usec
;
283 fd_ep
= epoll_create1(EPOLL_CLOEXEC
);
285 ERR("could not create epoll fd: %m\n");
289 if (t
->output
.out
!= NULL
) {
290 fd_matchout
= open(t
->output
.out
, O_RDONLY
);
291 if (fd_matchout
< 0) {
293 ERR("could not open %s for read: %m\n",
297 memset(&ep_outpipe
, 0, sizeof(struct epoll_event
));
298 ep_outpipe
.events
= EPOLLIN
;
299 ep_outpipe
.data
.ptr
= &fdout
;
300 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fdout
, &ep_outpipe
) < 0) {
302 ERR("could not add fd to epoll: %m\n");
308 if (t
->output
.err
!= NULL
) {
309 fd_matcherr
= open(t
->output
.err
, O_RDONLY
);
310 if (fd_matcherr
< 0) {
312 ERR("could not open %s for read: %m\n",
317 memset(&ep_errpipe
, 0, sizeof(struct epoll_event
));
318 ep_errpipe
.events
= EPOLLIN
;
319 ep_errpipe
.data
.ptr
= &fderr
;
320 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fderr
, &ep_errpipe
) < 0) {
322 ERR("could not add fd to epoll: %m\n");
328 memset(&ep_monitor
, 0, sizeof(struct epoll_event
));
329 ep_monitor
.events
= EPOLLHUP
;
330 ep_monitor
.data
.ptr
= &fdmonitor
;
331 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fdmonitor
, &ep_monitor
) < 0) {
333 ERR("could not add monitor fd to epoll: %m\n");
337 start_usec
= now_usec();
338 end_usec
= start_usec
+ TEST_TIMEOUT_USEC
;
340 for (err
= 0; fdmonitor
>= 0 || fdout
>= 0 || fderr
>= 0;) {
341 int fdcount
, i
, timeout
;
342 struct epoll_event ev
[4];
343 unsigned long long curr_usec
= now_usec();
345 if (curr_usec
> end_usec
)
348 timeout
= (end_usec
- curr_usec
) / USEC_PER_MSEC
;
349 fdcount
= epoll_wait(fd_ep
, ev
, 4, timeout
);
354 ERR("could not poll: %m\n");
358 for (i
= 0; i
< fdcount
; i
++) {
359 int *fd
= ev
[i
].data
.ptr
;
361 if (ev
[i
].events
& EPOLLIN
) {
368 * compare the output from child with the one
372 r
= read(*fd
, buf
, sizeof(buf
) - 1);
377 fd_match
= fd_matchout
;
378 else if (*fd
== fderr
)
379 fd_match
= fd_matcherr
;
381 ERR("Unexpected activity on monitor pipe\n");
387 int rmatch
= read(fd_match
,
388 bufmatch
+ done
, r
- done
);
396 ERR("could not read match fd %d\n",
406 if (strcmp(buf
, bufmatch
) != 0) {
407 ERR("Outputs do not match on %s:\n",
408 fd_match
== fd_matchout
? "stdout" : "stderr");
409 ERR("correct:\n%s\n", bufmatch
);
410 ERR("wrong:\n%s\n", buf
);
414 } else if (ev
[i
].events
& EPOLLHUP
) {
415 if (epoll_ctl(fd_ep
, EPOLL_CTL_DEL
,
417 ERR("could not remove fd %d from epoll: %m\n",
425 if (err
== 0 && fdmonitor
>= 0) {
427 ERR("Test '%s' timed out, killing %d\n", t
->name
, child
);
428 kill(child
, SIGKILL
);
432 if (fd_matchout
>= 0)
434 if (fd_matcherr
>= 0)
441 static inline int safe_read(int fd
, void *buf
, size_t count
)
446 r
= read(fd
, buf
, count
);
447 if (r
== -1 && errno
== EINTR
)
455 static bool check_generated_files(const struct test
*t
)
457 const struct keyval
*k
;
459 /* This is not meant to be a diff replacement, just stupidly check if
460 * the files match. Bear in mind they can be binary files */
461 for (k
= t
->output
.files
; k
&& k
->key
; k
++) {
462 struct stat sta
, stb
;
463 int fda
= -1, fdb
= -1;
467 fda
= open(k
->key
, O_RDONLY
);
469 ERR("could not open %s\n - %m\n", k
->key
);
473 fdb
= open(k
->val
, O_RDONLY
);
475 ERR("could not open %s\n - %m\n", k
->val
);
479 if (fstat(fda
, &sta
) != 0) {
480 ERR("could not fstat %d %s\n - %m\n", fda
, k
->key
);
484 if (fstat(fdb
, &stb
) != 0) {
485 ERR("could not fstat %d %s\n - %m\n", fdb
, k
->key
);
489 if (sta
.st_size
!= stb
.st_size
) {
490 ERR("sizes do not match %s %s\n", k
->key
, k
->val
);
497 r
= safe_read(fda
, bufa
, sizeof(bufa
));
502 /* size is already checked, go to next file */
505 for (done
= 0; done
< r
;) {
506 int r2
= safe_read(fdb
, bufb
+ done
, r
- done
);
514 if (memcmp(bufa
, bufb
, r
) != 0)
535 static int cmp_modnames(const void *m1
, const void *m2
)
537 const char *s1
= *(char *const *)m1
;
538 const char *s2
= *(char *const *)m2
;
541 for (i
= 0; s1
[i
] || s2
[i
]; i
++) {
542 char c1
= s1
[i
], c2
= s2
[i
];
554 * Store the expected module names in buf and return a list of pointers to
557 static const char **read_expected_modules(const struct test
*t
,
558 char **buf
, int *count
)
565 if (t
->modules_loaded
[0] == '\0') {
570 *buf
= strdup(t
->modules_loaded
);
576 for (p
= *buf
; *p
; p
++)
579 res
= malloc(sizeof(char *) * len
);
589 for (p
= *buf
; i
< len
; p
++)
598 static char **read_loaded_modules(const struct test
*t
, char **buf
, int *count
)
600 char dirname
[PATH_MAX
];
602 struct dirent
*dirent
;
607 const char *rootfs
= t
->config
[TC_ROOTFS
] ? t
->config
[TC_ROOTFS
] : "";
609 /* Store the entries in /sys/module to res */
610 if (snprintf(dirname
, sizeof(dirname
), "%s/sys/module", rootfs
)
611 >= (int)sizeof(dirname
)) {
612 ERR("rootfs path too long: %s\n", rootfs
);
617 dir
= opendir(dirname
);
618 /* not an error, simply return empty list */
624 while ((dirent
= readdir(dir
))) {
625 if (dirent
->d_name
[0] == '.')
628 bufsz
+= strlen(dirent
->d_name
) + 1;
630 res
= malloc(sizeof(char *) * len
);
636 *buf
= malloc(bufsz
);
647 while ((dirent
= readdir(dir
))) {
650 if (dirent
->d_name
[0] == '.')
652 size
= strlen(dirent
->d_name
) + 1;
653 memcpy(p
, dirent
->d_name
, size
);
664 static int check_loaded_modules(const struct test
*t
)
672 a1
= read_expected_modules(t
, &buf1
, &l1
);
675 a2
= read_loaded_modules(t
, &buf2
, &l2
);
678 qsort(a1
, l1
, sizeof(char *), cmp_modnames
);
679 qsort(a2
, l2
, sizeof(char *), cmp_modnames
);
682 while (i1
< l1
|| i2
< l2
) {
690 cmp
= cmp_modnames(&a1
[i1
], &a2
[i2
]);
694 } else if (cmp
< 0) {
696 ERR("module %s not loaded\n", a1
[i1
]);
700 ERR("module %s is loaded but should not be \n", a2
[i2
]);
712 static inline int test_run_parent(const struct test
*t
, int fdout
[2],
713 int fderr
[2], int fdmonitor
[2], pid_t child
)
717 bool matchout
, match_modules
;
719 /* Close write-fds */
720 if (t
->output
.out
!= NULL
)
722 if (t
->output
.err
!= NULL
)
726 matchout
= test_run_parent_check_outputs(t
, fdout
[0], fderr
[0],
727 fdmonitor
[0], child
);
730 * break pipe on the other end: either child already closed or we want
733 if (t
->output
.out
!= NULL
)
735 if (t
->output
.err
!= NULL
)
742 ERR("error waitpid(): %m\n");
746 } while (!WIFEXITED(err
) && !WIFSIGNALED(err
));
748 if (WIFEXITED(err
)) {
749 if (WEXITSTATUS(err
) != 0)
750 ERR("'%s' [%u] exited with return code %d\n",
751 t
->name
, pid
, WEXITSTATUS(err
));
753 LOG("'%s' [%u] exited with return code %d\n",
754 t
->name
, pid
, WEXITSTATUS(err
));
755 } else if (WIFSIGNALED(err
)) {
756 ERR("'%s' [%u] terminated by signal %d (%s)\n", t
->name
, pid
,
757 WTERMSIG(err
), strsignal(WTERMSIG(err
)));
758 err
= t
->expected_fail
? EXIT_SUCCESS
: EXIT_FAILURE
;
763 matchout
= check_generated_files(t
);
764 if (t
->modules_loaded
)
765 match_modules
= check_loaded_modules(t
);
767 match_modules
= true;
769 if (t
->expected_fail
== false) {
771 if (matchout
&& match_modules
)
772 LOG("%sPASSED%s: %s\n",
773 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
776 ERR("%sFAILED%s: exit ok but %s do not match: %s\n",
777 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
778 matchout
? "loaded modules" : "outputs",
783 ERR("%sFAILED%s: %s\n",
784 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
790 ERR("%sUNEXPECTED PASS%s: exit with 0: %s\n",
791 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
795 ERR("%sUNEXPECTED PASS%s: exit with 0 and outputs do not match: %s\n",
796 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
802 LOG("%sEXPECTED FAIL%s: %s\n",
803 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
807 LOG("%sEXPECTED FAIL%s: exit with %d but outputs do not match: %s\n",
808 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
809 WEXITSTATUS(err
), t
->name
);
820 static int prepend_path(const char *extra
)
822 char *oldpath
, *newpath
;
828 oldpath
= getenv("PATH");
830 return setenv("PATH", extra
, 1);
832 if (asprintf(&newpath
, "%s:%s", extra
, oldpath
) < 0) {
833 ERR("failed to allocate memory to new PATH\n");
837 r
= setenv("PATH", newpath
, 1);
843 int test_run(const struct test
*t
)
850 if (t
->need_spawn
&& oneshot
)
853 if (t
->output
.out
!= NULL
) {
854 if (pipe(fdout
) != 0) {
855 ERR("could not create out pipe for %s\n", t
->name
);
860 if (t
->output
.err
!= NULL
) {
861 if (pipe(fderr
) != 0) {
862 ERR("could not create err pipe for %s\n", t
->name
);
867 if (pipe(fdmonitor
) != 0) {
868 ERR("could not create monitor pipe for %s\n", t
->name
);
872 if (prepend_path(t
->path
) < 0) {
873 ERR("failed to prepend '%s' to PATH\n", t
->path
);
877 LOG("running %s, in forked context\n", t
->name
);
881 ERR("could not fork(): %m\n");
882 LOG("FAILED: %s\n", t
->name
);
887 return test_run_parent(t
, fdout
, fderr
, fdmonitor
, pid
);
889 return test_run_child(t
, fdout
, fderr
, fdmonitor
);