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"
38 #include "testsuite.h"
40 static const char *ANSI_HIGHLIGHT_GREEN_ON
= "\x1B[1;32m";
41 static const char *ANSI_HIGHLIGHT_RED_ON
= "\x1B[1;31m";
42 static const char *ANSI_HIGHLIGHT_OFF
= "\x1B[0m";
44 static const char *progname
;
45 static int oneshot
= 0;
46 static const char options_short
[] = "lhn";
47 static const struct option options
[] = {
48 { "list", no_argument
, 0, 'l' },
49 { "help", no_argument
, 0, 'h' },
53 #define OVERRIDE_LIBDIR ABS_TOP_BUILDDIR "/testsuite/.libs/"
57 const char *ldpreload
;
58 } env_config
[_TC_LAST
] = {
59 [TC_UNAME_R
] = { S_TC_UNAME_R
, OVERRIDE_LIBDIR
"uname.so" },
60 [TC_ROOTFS
] = { S_TC_ROOTFS
, OVERRIDE_LIBDIR
"path.so" },
61 [TC_INIT_MODULE_RETCODES
] = { S_TC_INIT_MODULE_RETCODES
, OVERRIDE_LIBDIR
"init_module.so" },
62 [TC_DELETE_MODULE_RETCODES
] = { S_TC_DELETE_MODULE_RETCODES
, OVERRIDE_LIBDIR
"delete_module.so" },
65 #define USEC_PER_SEC 1000000ULL
66 #define USEC_PER_MSEC 1000ULL
67 #define TEST_TIMEOUT_USEC 2 * USEC_PER_SEC
68 static unsigned long long now_usec(void)
72 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) != 0)
78 static void help(void)
80 const struct option
*itr
;
81 const char *itr_short
;
84 "\t%s [options] <test>\n"
85 "Options:\n", basename(progname
));
87 for (itr
= options
, itr_short
= options_short
;
88 itr
->name
!= NULL
; itr
++, itr_short
++)
89 printf("\t-%c, --%s\n", *itr_short
, itr
->name
);
92 static void test_list(const struct test
*tests
[])
96 printf("Available tests:\n");
97 for (i
= 0; tests
[i
] != NULL
; i
++)
98 printf("\t%s, %s\n", tests
[i
]->name
, tests
[i
]->description
);
101 int test_init(int argc
, char *const argv
[], const struct test
*tests
[])
107 c
= getopt_long(argc
, argv
, options_short
, options
, &idx
);
123 ERR("unexpected getopt_long() value %c\n", c
);
128 if (isatty(STDOUT_FILENO
) == 0) {
129 ANSI_HIGHLIGHT_OFF
= "";
130 ANSI_HIGHLIGHT_RED_ON
= "";
131 ANSI_HIGHLIGHT_GREEN_ON
= "";
137 const struct test
*test_find(const struct test
*tests
[], const char *name
)
141 for (i
= 0; tests
[i
] != NULL
; i
++) {
142 if (strcmp(tests
[i
]->name
, name
) == 0)
149 static int test_spawn_test(const struct test
*t
)
151 const char *const args
[] = { progname
, "-n", t
->name
, NULL
};
153 execv(progname
, (char *const *) args
);
155 ERR("failed to spawn %s for %s: %m\n", progname
, t
->name
);
159 static int test_run_spawned(const struct test
*t
)
161 int err
= t
->func(t
);
167 int test_spawn_prog(const char *prog
, const char *const args
[])
169 execv(prog
, (char *const *) args
);
171 ERR("failed to spawn %s\n", prog
);
172 ERR("did you forget to build tools?\n");
176 static void test_export_environ(const struct test
*t
)
178 char *preload
= NULL
;
179 size_t preloadlen
= 0;
181 const struct keyval
*env
;
183 unsetenv("LD_PRELOAD");
185 for (i
= 0; i
< _TC_LAST
; i
++) {
186 const char *ldpreload
;
190 if (t
->config
[i
] == NULL
)
193 setenv(env_config
[i
].key
, t
->config
[i
], 1);
195 ldpreload
= env_config
[i
].ldpreload
;
196 ldpreloadlen
= strlen(ldpreload
);
197 tmp
= realloc(preload
, preloadlen
+ 2 + ldpreloadlen
);
199 ERR("oom: test_export_environ()\n");
205 preload
[preloadlen
++] = ' ';
206 memcpy(preload
+ preloadlen
, ldpreload
, ldpreloadlen
);
207 preloadlen
+= ldpreloadlen
;
208 preload
[preloadlen
] = '\0';
212 setenv("LD_PRELOAD", preload
, 1);
216 for (env
= t
->env_vars
; env
&& env
->key
; env
++)
217 setenv(env
->key
, env
->val
, 1);
220 static inline int test_run_child(const struct test
*t
, int fdout
[2],
221 int fderr
[2], int fdmonitor
[2])
223 /* kill child if parent dies */
224 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
226 test_export_environ(t
);
228 /* Close read-fds and redirect std{out,err} to the write-fds */
229 if (t
->output
.out
!= NULL
) {
231 if (dup2(fdout
[1], STDOUT_FILENO
) < 0) {
232 ERR("could not redirect stdout to pipe: %m\n");
237 if (t
->output
.err
!= NULL
) {
239 if (dup2(fderr
[1], STDERR_FILENO
) < 0) {
240 ERR("could not redirect stderr to pipe: %m\n");
247 if (t
->config
[TC_ROOTFS
] != NULL
) {
248 const char *stamp
= TESTSUITE_ROOTFS
"../stamp-rootfs";
249 const char *rootfs
= t
->config
[TC_ROOTFS
];
250 struct stat rootfsst
, stampst
;
252 if (stat(stamp
, &stampst
) != 0) {
253 ERR("could not stat %s\n - %m", stamp
);
257 if (stat(rootfs
, &rootfsst
) != 0) {
258 ERR("could not stat %s\n - %m", rootfs
);
262 if (stat_mstamp(&rootfsst
) > stat_mstamp(&stampst
)) {
263 ERR("rootfs %s is dirty, please run 'make rootfs' before runnning this test\n",
270 return test_spawn_test(t
);
272 return test_run_spawned(t
);
275 static inline bool test_run_parent_check_outputs(const struct test
*t
,
276 int fdout
, int fderr
, int fdmonitor
, pid_t child
)
278 struct epoll_event ep_outpipe
, ep_errpipe
, ep_monitor
;
279 int err
, fd_ep
, fd_matchout
= -1, fd_matcherr
= -1;
280 unsigned long long end_usec
, start_usec
;
282 fd_ep
= epoll_create1(EPOLL_CLOEXEC
);
284 ERR("could not create epoll fd: %m\n");
288 if (t
->output
.out
!= NULL
) {
289 fd_matchout
= open(t
->output
.out
, O_RDONLY
);
290 if (fd_matchout
< 0) {
292 ERR("could not open %s for read: %m\n",
296 memset(&ep_outpipe
, 0, sizeof(struct epoll_event
));
297 ep_outpipe
.events
= EPOLLIN
;
298 ep_outpipe
.data
.ptr
= &fdout
;
299 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fdout
, &ep_outpipe
) < 0) {
301 ERR("could not add fd to epoll: %m\n");
307 if (t
->output
.err
!= NULL
) {
308 fd_matcherr
= open(t
->output
.err
, O_RDONLY
);
309 if (fd_matcherr
< 0) {
311 ERR("could not open %s for read: %m\n",
316 memset(&ep_errpipe
, 0, sizeof(struct epoll_event
));
317 ep_errpipe
.events
= EPOLLIN
;
318 ep_errpipe
.data
.ptr
= &fderr
;
319 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fderr
, &ep_errpipe
) < 0) {
321 ERR("could not add fd to epoll: %m\n");
327 memset(&ep_monitor
, 0, sizeof(struct epoll_event
));
328 ep_monitor
.events
= EPOLLHUP
;
329 ep_monitor
.data
.ptr
= &fdmonitor
;
330 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fdmonitor
, &ep_monitor
) < 0) {
332 ERR("could not add monitor fd to epoll: %m\n");
336 start_usec
= now_usec();
337 end_usec
= start_usec
+ TEST_TIMEOUT_USEC
;
339 for (err
= 0; fdmonitor
>= 0 || fdout
>= 0 || fderr
>= 0;) {
340 int fdcount
, i
, timeout
;
341 struct epoll_event ev
[4];
342 unsigned long long curr_usec
= now_usec();
344 if (curr_usec
> end_usec
)
347 timeout
= (end_usec
- curr_usec
) / USEC_PER_MSEC
;
348 fdcount
= epoll_wait(fd_ep
, ev
, 4, timeout
);
353 ERR("could not poll: %m\n");
357 for (i
= 0; i
< fdcount
; i
++) {
358 int *fd
= ev
[i
].data
.ptr
;
360 if (ev
[i
].events
& EPOLLIN
) {
367 * compare the output from child with the one
371 r
= read(*fd
, buf
, sizeof(buf
) - 1);
376 fd_match
= fd_matchout
;
377 else if (*fd
== fderr
)
378 fd_match
= fd_matcherr
;
380 ERR("Unexpected activity on monitor pipe\n");
386 int rmatch
= read(fd_match
,
387 bufmatch
+ done
, r
- done
);
395 ERR("could not read match fd %d\n",
405 if (strcmp(buf
, bufmatch
) != 0) {
406 ERR("Outputs do not match on %s:\n",
407 fd_match
== fd_matchout
? "stdout" : "stderr");
408 ERR("correct:\n%s\n", bufmatch
);
409 ERR("wrong:\n%s\n", buf
);
413 } else if (ev
[i
].events
& EPOLLHUP
) {
414 if (epoll_ctl(fd_ep
, EPOLL_CTL_DEL
,
416 ERR("could not remove fd %d from epoll: %m\n",
424 if (err
== 0 && fdmonitor
>= 0) {
426 ERR("Test '%s' timed out, killing %d\n", t
->name
, child
);
427 kill(child
, SIGKILL
);
431 if (fd_matchout
>= 0)
433 if (fd_matcherr
>= 0)
440 static inline int safe_read(int fd
, void *buf
, size_t count
)
445 r
= read(fd
, buf
, count
);
446 if (r
== -1 && errno
== EINTR
)
454 static bool check_generated_files(const struct test
*t
)
456 const struct keyval
*k
;
458 /* This is not meant to be a diff replacement, just stupidly check if
459 * the files match. Bear in mind they can be binary files */
460 for (k
= t
->output
.files
; k
&& k
->key
; k
++) {
461 struct stat sta
, stb
;
462 int fda
= -1, fdb
= -1;
466 fda
= open(k
->key
, O_RDONLY
);
468 ERR("could not open %s\n - %m\n", k
->key
);
472 fdb
= open(k
->val
, O_RDONLY
);
474 ERR("could not open %s\n - %m\n", k
->val
);
478 if (fstat(fda
, &sta
) != 0) {
479 ERR("could not fstat %d %s\n - %m\n", fda
, k
->key
);
483 if (fstat(fdb
, &stb
) != 0) {
484 ERR("could not fstat %d %s\n - %m\n", fdb
, k
->key
);
488 if (sta
.st_size
!= stb
.st_size
) {
489 ERR("sizes do not match %s %s\n", k
->key
, k
->val
);
496 r
= safe_read(fda
, bufa
, sizeof(bufa
));
501 /* size is already checked, go to next file */
504 for (done
= 0; done
< r
;) {
505 int r2
= safe_read(fdb
, bufb
+ done
, r
- done
);
513 if (memcmp(bufa
, bufb
, r
) != 0)
534 static int cmp_modnames(const void *m1
, const void *m2
)
536 const char *s1
= *(char *const *)m1
;
537 const char *s2
= *(char *const *)m2
;
540 for (i
= 0; s1
[i
] || s2
[i
]; i
++) {
541 char c1
= s1
[i
], c2
= s2
[i
];
553 * Store the expected module names in buf and return a list of pointers to
556 static const char **read_expected_modules(const struct test
*t
,
557 char **buf
, int *count
)
564 if (t
->modules_loaded
[0] == '\0') {
569 *buf
= strdup(t
->modules_loaded
);
575 for (p
= *buf
; *p
; p
++)
578 res
= malloc(sizeof(char *) * len
);
588 for (p
= *buf
; i
< len
; p
++)
597 static char **read_loaded_modules(const struct test
*t
, char **buf
, int *count
)
599 char dirname
[PATH_MAX
];
601 struct dirent
*dirent
;
606 const char *rootfs
= t
->config
[TC_ROOTFS
] ? t
->config
[TC_ROOTFS
] : "";
608 /* Store the entries in /sys/module to res */
609 if (snprintf(dirname
, sizeof(dirname
), "%s/sys/module", rootfs
)
610 >= (int)sizeof(dirname
)) {
611 ERR("rootfs path too long: %s\n", rootfs
);
616 dir
= opendir(dirname
);
617 /* not an error, simply return empty list */
623 while ((dirent
= readdir(dir
))) {
624 if (dirent
->d_name
[0] == '.')
627 bufsz
+= strlen(dirent
->d_name
) + 1;
629 res
= malloc(sizeof(char *) * len
);
635 *buf
= malloc(bufsz
);
646 while ((dirent
= readdir(dir
))) {
649 if (dirent
->d_name
[0] == '.')
651 size
= strlen(dirent
->d_name
) + 1;
652 memcpy(p
, dirent
->d_name
, size
);
663 static int check_loaded_modules(const struct test
*t
)
671 a1
= read_expected_modules(t
, &buf1
, &l1
);
674 a2
= read_loaded_modules(t
, &buf2
, &l2
);
677 qsort(a1
, l1
, sizeof(char *), cmp_modnames
);
678 qsort(a2
, l2
, sizeof(char *), cmp_modnames
);
681 while (i1
< l1
|| i2
< l2
) {
689 cmp
= cmp_modnames(&a1
[i1
], &a2
[i2
]);
693 } else if (cmp
< 0) {
695 ERR("module %s not loaded\n", a1
[i1
]);
699 ERR("module %s is loaded but should not be \n", a2
[i2
]);
711 static inline int test_run_parent(const struct test
*t
, int fdout
[2],
712 int fderr
[2], int fdmonitor
[2], pid_t child
)
716 bool matchout
, match_modules
;
718 /* Close write-fds */
719 if (t
->output
.out
!= NULL
)
721 if (t
->output
.err
!= NULL
)
725 matchout
= test_run_parent_check_outputs(t
, fdout
[0], fderr
[0],
726 fdmonitor
[0], child
);
729 * break pipe on the other end: either child already closed or we want
732 if (t
->output
.out
!= NULL
)
734 if (t
->output
.err
!= NULL
)
741 ERR("error waitpid(): %m\n");
745 } while (!WIFEXITED(err
) && !WIFSIGNALED(err
));
747 if (WIFEXITED(err
)) {
748 if (WEXITSTATUS(err
) != 0)
749 ERR("'%s' [%u] exited with return code %d\n",
750 t
->name
, pid
, WEXITSTATUS(err
));
752 LOG("'%s' [%u] exited with return code %d\n",
753 t
->name
, pid
, WEXITSTATUS(err
));
754 } else if (WIFSIGNALED(err
)) {
755 ERR("'%s' [%u] terminated by signal %d (%s)\n", t
->name
, pid
,
756 WTERMSIG(err
), strsignal(WTERMSIG(err
)));
757 err
= t
->expected_fail
? EXIT_SUCCESS
: EXIT_FAILURE
;
762 matchout
= check_generated_files(t
);
763 if (t
->modules_loaded
)
764 match_modules
= check_loaded_modules(t
);
766 match_modules
= true;
768 if (t
->expected_fail
== false) {
770 if (matchout
&& match_modules
)
771 LOG("%sPASSED%s: %s\n",
772 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
775 ERR("%sFAILED%s: exit ok but %s do not match: %s\n",
776 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
777 matchout
? "loaded modules" : "outputs",
782 ERR("%sFAILED%s: %s\n",
783 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
789 ERR("%sUNEXPECTED PASS%s: exit with 0: %s\n",
790 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
794 ERR("%sUNEXPECTED PASS%s: exit with 0 and outputs do not match: %s\n",
795 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
801 LOG("%sEXPECTED FAIL%s: %s\n",
802 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
806 LOG("%sEXPECTED FAIL%s: exit with %d but outputs do not match: %s\n",
807 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
808 WEXITSTATUS(err
), t
->name
);
819 static int prepend_path(const char *extra
)
821 char *oldpath
, *newpath
;
827 oldpath
= getenv("PATH");
829 return setenv("PATH", extra
, 1);
831 if (asprintf(&newpath
, "%s:%s", extra
, oldpath
) < 0) {
832 ERR("failed to allocate memory to new PATH\n");
836 r
= setenv("PATH", newpath
, 1);
842 int test_run(const struct test
*t
)
849 if (t
->need_spawn
&& oneshot
)
852 if (t
->output
.out
!= NULL
) {
853 if (pipe(fdout
) != 0) {
854 ERR("could not create out pipe for %s\n", t
->name
);
859 if (t
->output
.err
!= NULL
) {
860 if (pipe(fderr
) != 0) {
861 ERR("could not create err pipe for %s\n", t
->name
);
866 if (pipe(fdmonitor
) != 0) {
867 ERR("could not create monitor pipe for %s\n", t
->name
);
871 if (prepend_path(t
->path
) < 0) {
872 ERR("failed to prepend '%s' to PATH\n", t
->path
);
876 LOG("running %s, in forked context\n", t
->name
);
880 ERR("could not fork(): %m\n");
881 LOG("FAILED: %s\n", t
->name
);
886 return test_run_parent(t
, fdout
, fderr
, fdmonitor
, pid
);
888 return test_run_child(t
, fdout
, fderr
, fdmonitor
);