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 "libkmod-util.h"
36 #include "testsuite.h"
38 static const char *ANSI_HIGHLIGHT_GREEN_ON
= "\x1B[1;32m";
39 static const char *ANSI_HIGHLIGHT_RED_ON
= "\x1B[1;31m";
40 static const char *ANSI_HIGHLIGHT_OFF
= "\x1B[0m";
42 static const char *progname
;
43 static int oneshot
= 0;
44 static const char options_short
[] = "lhn";
45 static const struct option options
[] = {
46 { "list", no_argument
, 0, 'l' },
47 { "help", no_argument
, 0, 'h' },
51 #define OVERRIDE_LIBDIR ABS_TOP_BUILDDIR "/testsuite/.libs/"
55 const char *ldpreload
;
56 } env_config
[_TC_LAST
] = {
57 [TC_UNAME_R
] = { S_TC_UNAME_R
, OVERRIDE_LIBDIR
"uname.so" },
58 [TC_ROOTFS
] = { S_TC_ROOTFS
, OVERRIDE_LIBDIR
"path.so" },
59 [TC_INIT_MODULE_RETCODES
] = { S_TC_INIT_MODULE_RETCODES
, OVERRIDE_LIBDIR
"init_module.so" },
60 [TC_DELETE_MODULE_RETCODES
] = { S_TC_DELETE_MODULE_RETCODES
, OVERRIDE_LIBDIR
"delete_module.so" },
63 #define USEC_PER_SEC 1000000ULL
64 #define USEC_PER_MSEC 1000ULL
65 #define TEST_TIMEOUT_USEC 2 * USEC_PER_SEC
66 static unsigned long long now_usec(void)
70 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) != 0)
76 static void help(void)
78 const struct option
*itr
;
79 const char *itr_short
;
82 "\t%s [options] <test>\n"
83 "Options:\n", basename(progname
));
85 for (itr
= options
, itr_short
= options_short
;
86 itr
->name
!= NULL
; itr
++, itr_short
++)
87 printf("\t-%c, --%s\n", *itr_short
, itr
->name
);
90 static void test_list(const struct test
*tests
[])
94 printf("Available tests:\n");
95 for (i
= 0; tests
[i
] != NULL
; i
++)
96 printf("\t%s, %s\n", tests
[i
]->name
, tests
[i
]->description
);
99 int test_init(int argc
, char *const argv
[], const struct test
*tests
[])
105 c
= getopt_long(argc
, argv
, options_short
, options
, &idx
);
121 ERR("unexpected getopt_long() value %c\n", c
);
126 if (isatty(STDOUT_FILENO
) == 0) {
127 ANSI_HIGHLIGHT_OFF
= "";
128 ANSI_HIGHLIGHT_RED_ON
= "";
129 ANSI_HIGHLIGHT_GREEN_ON
= "";
135 const struct test
*test_find(const struct test
*tests
[], const char *name
)
139 for (i
= 0; tests
[i
] != NULL
; i
++) {
140 if (strcmp(tests
[i
]->name
, name
) == 0)
147 static int test_spawn_test(const struct test
*t
)
149 const char *const args
[] = { progname
, "-n", t
->name
, NULL
};
151 execv(progname
, (char *const *) args
);
153 ERR("failed to spawn %s for %s: %m\n", progname
, t
->name
);
157 static int test_run_spawned(const struct test
*t
)
159 int err
= t
->func(t
);
165 int test_spawn_prog(const char *prog
, const char *const args
[])
167 execv(prog
, (char *const *) args
);
169 ERR("failed to spawn %s\n", prog
);
170 ERR("did you forget to build tools?\n");
174 static void test_export_environ(const struct test
*t
)
176 char *preload
= NULL
;
177 size_t preloadlen
= 0;
179 const struct keyval
*env
;
181 unsetenv("LD_PRELOAD");
183 for (i
= 0; i
< _TC_LAST
; i
++) {
184 const char *ldpreload
;
188 if (t
->config
[i
] == NULL
)
191 setenv(env_config
[i
].key
, t
->config
[i
], 1);
193 ldpreload
= env_config
[i
].ldpreload
;
194 ldpreloadlen
= strlen(ldpreload
);
195 tmp
= realloc(preload
, preloadlen
+ 2 + ldpreloadlen
);
197 ERR("oom: test_export_environ()\n");
203 preload
[preloadlen
++] = ' ';
204 memcpy(preload
+ preloadlen
, ldpreload
, ldpreloadlen
);
205 preloadlen
+= ldpreloadlen
;
206 preload
[preloadlen
] = '\0';
210 setenv("LD_PRELOAD", preload
, 1);
214 for (env
= t
->env_vars
; env
&& env
->key
; env
++)
215 setenv(env
->key
, env
->val
, 1);
218 static inline int test_run_child(const struct test
*t
, int fdout
[2],
219 int fderr
[2], int fdmonitor
[2])
221 /* kill child if parent dies */
222 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
224 test_export_environ(t
);
226 /* Close read-fds and redirect std{out,err} to the write-fds */
227 if (t
->output
.out
!= NULL
) {
229 if (dup2(fdout
[1], STDOUT_FILENO
) < 0) {
230 ERR("could not redirect stdout to pipe: %m\n");
235 if (t
->output
.err
!= NULL
) {
237 if (dup2(fderr
[1], STDERR_FILENO
) < 0) {
238 ERR("could not redirect stderr to pipe: %m\n");
245 if (t
->config
[TC_ROOTFS
] != NULL
) {
246 const char *stamp
= TESTSUITE_ROOTFS
"../stamp-rootfs";
247 const char *rootfs
= t
->config
[TC_ROOTFS
];
248 struct stat rootfsst
, stampst
;
250 if (stat(stamp
, &stampst
) != 0) {
251 ERR("could not stat %s\n - %m", stamp
);
255 if (stat(rootfs
, &rootfsst
) != 0) {
256 ERR("could not stat %s\n - %m", rootfs
);
260 if (stat_mstamp(&rootfsst
) > stat_mstamp(&stampst
)) {
261 ERR("rootfs %s is dirty, please run 'make rootfs' before runnning this test\n",
268 return test_spawn_test(t
);
270 return test_run_spawned(t
);
273 static inline bool test_run_parent_check_outputs(const struct test
*t
,
274 int fdout
, int fderr
, int fdmonitor
, pid_t child
)
276 struct epoll_event ep_outpipe
, ep_errpipe
, ep_monitor
;
277 int err
, fd_ep
, fd_matchout
= -1, fd_matcherr
= -1;
278 unsigned long long end_usec
, start_usec
;
280 fd_ep
= epoll_create1(EPOLL_CLOEXEC
);
282 ERR("could not create epoll fd: %m\n");
286 if (t
->output
.out
!= NULL
) {
287 fd_matchout
= open(t
->output
.out
, O_RDONLY
);
288 if (fd_matchout
< 0) {
290 ERR("could not open %s for read: %m\n",
294 memset(&ep_outpipe
, 0, sizeof(struct epoll_event
));
295 ep_outpipe
.events
= EPOLLIN
;
296 ep_outpipe
.data
.ptr
= &fdout
;
297 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fdout
, &ep_outpipe
) < 0) {
299 ERR("could not add fd to epoll: %m\n");
305 if (t
->output
.err
!= NULL
) {
306 fd_matcherr
= open(t
->output
.err
, O_RDONLY
);
307 if (fd_matcherr
< 0) {
309 ERR("could not open %s for read: %m\n",
314 memset(&ep_errpipe
, 0, sizeof(struct epoll_event
));
315 ep_errpipe
.events
= EPOLLIN
;
316 ep_errpipe
.data
.ptr
= &fderr
;
317 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fderr
, &ep_errpipe
) < 0) {
319 ERR("could not add fd to epoll: %m\n");
325 memset(&ep_monitor
, 0, sizeof(struct epoll_event
));
326 ep_monitor
.events
= EPOLLHUP
;
327 ep_monitor
.data
.ptr
= &fdmonitor
;
328 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fdmonitor
, &ep_monitor
) < 0) {
330 ERR("could not add monitor fd to epoll: %m\n");
334 start_usec
= now_usec();
335 end_usec
= start_usec
+ TEST_TIMEOUT_USEC
;
337 for (err
= 0; fdmonitor
>= 0 || fdout
>= 0 || fderr
>= 0;) {
338 int fdcount
, i
, timeout
;
339 struct epoll_event ev
[4];
340 unsigned long long curr_usec
= now_usec();
342 if (curr_usec
> end_usec
)
345 timeout
= (end_usec
- curr_usec
) / USEC_PER_MSEC
;
346 fdcount
= epoll_wait(fd_ep
, ev
, 4, timeout
);
351 ERR("could not poll: %m\n");
355 for (i
= 0; i
< fdcount
; i
++) {
356 int *fd
= ev
[i
].data
.ptr
;
358 if (ev
[i
].events
& EPOLLIN
) {
365 * compare the output from child with the one
369 r
= read(*fd
, buf
, sizeof(buf
) - 1);
374 fd_match
= fd_matchout
;
375 else if (*fd
== fderr
)
376 fd_match
= fd_matcherr
;
378 ERR("Unexpected activity on monitor pipe\n");
384 int rmatch
= read(fd_match
,
385 bufmatch
+ done
, r
- done
);
393 ERR("could not read match fd %d\n",
403 if (strcmp(buf
, bufmatch
) != 0) {
404 ERR("Outputs do not match on %s:\n",
405 fd_match
== fd_matchout
? "stdout" : "stderr");
406 ERR("correct:\n%s\n", bufmatch
);
407 ERR("wrong:\n%s\n", buf
);
411 } else if (ev
[i
].events
& EPOLLHUP
) {
412 if (epoll_ctl(fd_ep
, EPOLL_CTL_DEL
,
414 ERR("could not remove fd %d from epoll: %m\n",
422 if (err
== 0 && fdmonitor
>= 0) {
424 ERR("Test '%s' timed out, killing %d\n", t
->name
, child
);
425 kill(child
, SIGKILL
);
429 if (fd_matchout
>= 0)
431 if (fd_matcherr
>= 0)
438 static inline int safe_read(int fd
, void *buf
, size_t count
)
443 r
= read(fd
, buf
, count
);
444 if (r
== -1 && errno
== EINTR
)
452 static bool check_generated_files(const struct test
*t
)
454 const struct keyval
*k
;
456 /* This is not meant to be a diff replacement, just stupidly check if
457 * the files match. Bear in mind they can be binary files */
458 for (k
= t
->output
.files
; k
&& k
->key
; k
++) {
459 struct stat sta
, stb
;
460 int fda
= -1, fdb
= -1;
464 fda
= open(k
->key
, O_RDONLY
);
466 ERR("could not open %s\n - %m\n", k
->key
);
470 fdb
= open(k
->val
, O_RDONLY
);
472 ERR("could not open %s\n - %m\n", k
->val
);
476 if (fstat(fda
, &sta
) != 0) {
477 ERR("could not fstat %d %s\n - %m\n", fda
, k
->key
);
481 if (fstat(fdb
, &stb
) != 0) {
482 ERR("could not fstat %d %s\n - %m\n", fdb
, k
->key
);
486 if (sta
.st_size
!= stb
.st_size
) {
487 ERR("sizes do not match %s %s\n", k
->key
, k
->val
);
494 r
= safe_read(fda
, bufa
, sizeof(bufa
));
499 /* size is already checked, go to next file */
502 for (done
= 0; done
< r
;) {
503 int r2
= safe_read(fdb
, bufb
+ done
, r
- done
);
511 if (memcmp(bufa
, bufb
, r
) != 0)
532 static int cmp_modnames(const void *m1
, const void *m2
)
534 const char *s1
= *(char *const *)m1
;
535 const char *s2
= *(char *const *)m2
;
538 for (i
= 0; s1
[i
] || s2
[i
]; i
++) {
539 char c1
= s1
[i
], c2
= s2
[i
];
551 * Store the expected module names in buf and return a list of pointers to
554 static const char **read_expected_modules(const struct test
*t
,
555 char **buf
, int *count
)
562 if (t
->modules_loaded
[0] == '\0') {
567 *buf
= strdup(t
->modules_loaded
);
573 for (p
= *buf
; *p
; p
++)
576 res
= malloc(sizeof(char *) * len
);
586 for (p
= *buf
; i
< len
; p
++)
595 static char **read_loaded_modules(const struct test
*t
, char **buf
, int *count
)
597 char dirname
[PATH_MAX
];
599 struct dirent
*dirent
;
604 const char *rootfs
= t
->config
[TC_ROOTFS
] ? t
->config
[TC_ROOTFS
] : "";
606 /* Store the entries in /sys/module to res */
607 if (snprintf(dirname
, sizeof(dirname
), "%s/sys/module", rootfs
)
608 >= (int)sizeof(dirname
)) {
609 ERR("rootfs path too long: %s\n", rootfs
);
614 dir
= opendir(dirname
);
615 /* not an error, simply return empty list */
621 while ((dirent
= readdir(dir
))) {
622 if (dirent
->d_name
[0] == '.')
625 bufsz
+= strlen(dirent
->d_name
) + 1;
627 res
= malloc(sizeof(char *) * len
);
633 *buf
= malloc(bufsz
);
644 while ((dirent
= readdir(dir
))) {
647 if (dirent
->d_name
[0] == '.')
649 size
= strlen(dirent
->d_name
) + 1;
650 memcpy(p
, dirent
->d_name
, size
);
661 static int check_loaded_modules(const struct test
*t
)
669 a1
= read_expected_modules(t
, &buf1
, &l1
);
672 a2
= read_loaded_modules(t
, &buf2
, &l2
);
675 qsort(a1
, l1
, sizeof(char *), cmp_modnames
);
676 qsort(a2
, l2
, sizeof(char *), cmp_modnames
);
679 while (i1
< l1
|| i2
< l2
) {
687 cmp
= cmp_modnames(&a1
[i1
], &a2
[i2
]);
691 } else if (cmp
< 0) {
693 ERR("module %s not loaded\n", a1
[i1
]);
697 ERR("module %s is loaded but should not be \n", a2
[i2
]);
709 static inline int test_run_parent(const struct test
*t
, int fdout
[2],
710 int fderr
[2], int fdmonitor
[2], pid_t child
)
714 bool matchout
, match_modules
;
716 /* Close write-fds */
717 if (t
->output
.out
!= NULL
)
719 if (t
->output
.err
!= NULL
)
723 matchout
= test_run_parent_check_outputs(t
, fdout
[0], fderr
[0],
724 fdmonitor
[0], child
);
727 * break pipe on the other end: either child already closed or we want
730 if (t
->output
.out
!= NULL
)
732 if (t
->output
.err
!= NULL
)
739 ERR("error waitpid(): %m\n");
742 } while (!WIFEXITED(err
) && !WIFSIGNALED(err
));
744 if (WIFEXITED(err
)) {
745 if (WEXITSTATUS(err
) != 0)
746 ERR("'%s' [%u] exited with return code %d\n",
747 t
->name
, pid
, WEXITSTATUS(err
));
749 LOG("'%s' [%u] exited with return code %d\n",
750 t
->name
, pid
, WEXITSTATUS(err
));
751 } else if (WIFSIGNALED(err
)) {
752 ERR("'%s' [%u] terminated by signal %d (%s)\n", t
->name
, pid
,
753 WTERMSIG(err
), strsignal(WTERMSIG(err
)));
754 return t
->expected_fail
? EXIT_SUCCESS
: EXIT_FAILURE
;
758 matchout
= check_generated_files(t
);
759 if (t
->modules_loaded
)
760 match_modules
= check_loaded_modules(t
);
762 match_modules
= true;
764 if (t
->expected_fail
== false) {
766 if (matchout
&& match_modules
)
767 LOG("%sPASSED%s: %s\n",
768 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
771 ERR("%sFAILED%s: exit ok but %s do not match: %s\n",
772 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
773 matchout
? "loaded modules" : "outputs",
778 ERR("%sFAILED%s: %s\n",
779 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
785 LOG("%sUNEXPECTED PASS%s: %s\n",
786 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
790 LOG("%sEXPECTED FAIL%s: exit ok but outputs do not match: %s\n",
791 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
794 ERR("%sEXPECTED FAIL%s: %s\n",
795 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
804 static int prepend_path(const char *extra
)
806 char *oldpath
, *newpath
;
812 oldpath
= getenv("PATH");
814 return setenv("PATH", extra
, 1);
816 if (asprintf(&newpath
, "%s:%s", extra
, oldpath
) < 0) {
817 ERR("failed to allocate memory to new PATH\n");
821 r
= setenv("PATH", newpath
, 1);
827 int test_run(const struct test
*t
)
834 if (t
->need_spawn
&& oneshot
)
837 if (t
->output
.out
!= NULL
) {
838 if (pipe(fdout
) != 0) {
839 ERR("could not create out pipe for %s\n", t
->name
);
844 if (t
->output
.err
!= NULL
) {
845 if (pipe(fderr
) != 0) {
846 ERR("could not create err pipe for %s\n", t
->name
);
851 if (pipe(fdmonitor
) != 0) {
852 ERR("could not create monitor pipe for %s\n", t
->name
);
856 if (prepend_path(t
->path
) < 0) {
857 ERR("failed to prepend '%s' to PATH\n", t
->path
);
861 LOG("running %s, in forked context\n", t
->name
);
865 ERR("could not fork(): %m\n");
866 LOG("FAILED: %s\n", t
->name
);
871 return test_run_parent(t
, fdout
, fderr
, fdmonitor
, pid
);
873 return test_run_child(t
, fdout
, fderr
, fdmonitor
);