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 "testsuite.h"
39 static const char *ANSI_HIGHLIGHT_GREEN_ON
= "\x1B[1;32m";
40 static const char *ANSI_HIGHLIGHT_RED_ON
= "\x1B[1;31m";
41 static const char *ANSI_HIGHLIGHT_OFF
= "\x1B[0m";
43 static const char *progname
;
44 static int oneshot
= 0;
45 static const char options_short
[] = "lhn";
46 static const struct option options
[] = {
47 { "list", no_argument
, 0, 'l' },
48 { "help", no_argument
, 0, 'h' },
52 #define OVERRIDE_LIBDIR ABS_TOP_BUILDDIR "/testsuite/.libs/"
56 const char *ldpreload
;
57 } env_config
[_TC_LAST
] = {
58 [TC_UNAME_R
] = { S_TC_UNAME_R
, OVERRIDE_LIBDIR
"uname.so" },
59 [TC_ROOTFS
] = { S_TC_ROOTFS
, OVERRIDE_LIBDIR
"path.so" },
60 [TC_INIT_MODULE_RETCODES
] = { S_TC_INIT_MODULE_RETCODES
, OVERRIDE_LIBDIR
"init_module.so" },
61 [TC_DELETE_MODULE_RETCODES
] = { S_TC_DELETE_MODULE_RETCODES
, OVERRIDE_LIBDIR
"delete_module.so" },
64 #define USEC_PER_SEC 1000000ULL
65 #define USEC_PER_MSEC 1000ULL
66 #define TEST_TIMEOUT_USEC 2 * USEC_PER_SEC
67 static unsigned long long now_usec(void)
71 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) != 0)
77 static void help(void)
79 const struct option
*itr
;
80 const char *itr_short
;
83 "\t%s [options] <test>\n"
84 "Options:\n", basename(progname
));
86 for (itr
= options
, itr_short
= options_short
;
87 itr
->name
!= NULL
; itr
++, itr_short
++)
88 printf("\t-%c, --%s\n", *itr_short
, itr
->name
);
91 static void test_list(const struct test
*tests
[])
95 printf("Available tests:\n");
96 for (i
= 0; tests
[i
] != NULL
; i
++)
97 printf("\t%s, %s\n", tests
[i
]->name
, tests
[i
]->description
);
100 int test_init(int argc
, char *const argv
[], const struct test
*tests
[])
106 c
= getopt_long(argc
, argv
, options_short
, options
, &idx
);
122 ERR("unexpected getopt_long() value %c\n", c
);
127 if (isatty(STDOUT_FILENO
) == 0) {
128 ANSI_HIGHLIGHT_OFF
= "";
129 ANSI_HIGHLIGHT_RED_ON
= "";
130 ANSI_HIGHLIGHT_GREEN_ON
= "";
136 const struct test
*test_find(const struct test
*tests
[], const char *name
)
140 for (i
= 0; tests
[i
] != NULL
; i
++) {
141 if (strcmp(tests
[i
]->name
, name
) == 0)
148 static int test_spawn_test(const struct test
*t
)
150 const char *const args
[] = { progname
, "-n", t
->name
, NULL
};
152 execv(progname
, (char *const *) args
);
154 ERR("failed to spawn %s for %s: %m\n", progname
, t
->name
);
158 static int test_run_spawned(const struct test
*t
)
160 int err
= t
->func(t
);
166 int test_spawn_prog(const char *prog
, const char *const args
[])
168 execv(prog
, (char *const *) args
);
170 ERR("failed to spawn %s\n", prog
);
171 ERR("did you forget to build tools?\n");
175 static void test_export_environ(const struct test
*t
)
177 char *preload
= NULL
;
178 size_t preloadlen
= 0;
180 const struct keyval
*env
;
182 unsetenv("LD_PRELOAD");
184 for (i
= 0; i
< _TC_LAST
; i
++) {
185 const char *ldpreload
;
189 if (t
->config
[i
] == NULL
)
192 setenv(env_config
[i
].key
, t
->config
[i
], 1);
194 ldpreload
= env_config
[i
].ldpreload
;
195 ldpreloadlen
= strlen(ldpreload
);
196 tmp
= realloc(preload
, preloadlen
+ 2 + ldpreloadlen
);
198 ERR("oom: test_export_environ()\n");
204 preload
[preloadlen
++] = ' ';
205 memcpy(preload
+ preloadlen
, ldpreload
, ldpreloadlen
);
206 preloadlen
+= ldpreloadlen
;
207 preload
[preloadlen
] = '\0';
211 setenv("LD_PRELOAD", preload
, 1);
215 for (env
= t
->env_vars
; env
&& env
->key
; env
++)
216 setenv(env
->key
, env
->val
, 1);
219 static inline int test_run_child(const struct test
*t
, int fdout
[2],
220 int fderr
[2], int fdmonitor
[2])
222 /* kill child if parent dies */
223 prctl(PR_SET_PDEATHSIG
, SIGTERM
);
225 test_export_environ(t
);
227 /* Close read-fds and redirect std{out,err} to the write-fds */
228 if (t
->output
.out
!= NULL
) {
230 if (dup2(fdout
[1], STDOUT_FILENO
) < 0) {
231 ERR("could not redirect stdout to pipe: %m\n");
236 if (t
->output
.err
!= NULL
) {
238 if (dup2(fderr
[1], STDERR_FILENO
) < 0) {
239 ERR("could not redirect stderr to pipe: %m\n");
246 if (t
->config
[TC_ROOTFS
] != NULL
) {
247 const char *stamp
= TESTSUITE_ROOTFS
"../stamp-rootfs";
248 const char *rootfs
= t
->config
[TC_ROOTFS
];
249 struct stat rootfsst
, stampst
;
251 if (stat(stamp
, &stampst
) != 0) {
252 ERR("could not stat %s\n - %m", stamp
);
256 if (stat(rootfs
, &rootfsst
) != 0) {
257 ERR("could not stat %s\n - %m", rootfs
);
261 if (stat_mstamp(&rootfsst
) > stat_mstamp(&stampst
)) {
262 ERR("rootfs %s is dirty, please run 'make rootfs' before runnning this test\n",
269 return test_spawn_test(t
);
271 return test_run_spawned(t
);
274 static inline bool test_run_parent_check_outputs(const struct test
*t
,
275 int fdout
, int fderr
, int fdmonitor
, pid_t child
)
277 struct epoll_event ep_outpipe
, ep_errpipe
, ep_monitor
;
278 int err
, fd_ep
, fd_matchout
= -1, fd_matcherr
= -1;
279 unsigned long long end_usec
, start_usec
;
281 fd_ep
= epoll_create1(EPOLL_CLOEXEC
);
283 ERR("could not create epoll fd: %m\n");
287 if (t
->output
.out
!= NULL
) {
288 fd_matchout
= open(t
->output
.out
, O_RDONLY
);
289 if (fd_matchout
< 0) {
291 ERR("could not open %s for read: %m\n",
295 memset(&ep_outpipe
, 0, sizeof(struct epoll_event
));
296 ep_outpipe
.events
= EPOLLIN
;
297 ep_outpipe
.data
.ptr
= &fdout
;
298 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fdout
, &ep_outpipe
) < 0) {
300 ERR("could not add fd to epoll: %m\n");
306 if (t
->output
.err
!= NULL
) {
307 fd_matcherr
= open(t
->output
.err
, O_RDONLY
);
308 if (fd_matcherr
< 0) {
310 ERR("could not open %s for read: %m\n",
315 memset(&ep_errpipe
, 0, sizeof(struct epoll_event
));
316 ep_errpipe
.events
= EPOLLIN
;
317 ep_errpipe
.data
.ptr
= &fderr
;
318 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fderr
, &ep_errpipe
) < 0) {
320 ERR("could not add fd to epoll: %m\n");
326 memset(&ep_monitor
, 0, sizeof(struct epoll_event
));
327 ep_monitor
.events
= EPOLLHUP
;
328 ep_monitor
.data
.ptr
= &fdmonitor
;
329 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fdmonitor
, &ep_monitor
) < 0) {
331 ERR("could not add monitor fd to epoll: %m\n");
335 start_usec
= now_usec();
336 end_usec
= start_usec
+ TEST_TIMEOUT_USEC
;
338 for (err
= 0; fdmonitor
>= 0 || fdout
>= 0 || fderr
>= 0;) {
339 int fdcount
, i
, timeout
;
340 struct epoll_event ev
[4];
341 unsigned long long curr_usec
= now_usec();
343 if (curr_usec
> end_usec
)
346 timeout
= (end_usec
- curr_usec
) / USEC_PER_MSEC
;
347 fdcount
= epoll_wait(fd_ep
, ev
, 4, timeout
);
352 ERR("could not poll: %m\n");
356 for (i
= 0; i
< fdcount
; i
++) {
357 int *fd
= ev
[i
].data
.ptr
;
359 if (ev
[i
].events
& EPOLLIN
) {
366 * compare the output from child with the one
370 r
= read(*fd
, buf
, sizeof(buf
) - 1);
375 fd_match
= fd_matchout
;
376 else if (*fd
== fderr
)
377 fd_match
= fd_matcherr
;
379 ERR("Unexpected activity on monitor pipe\n");
385 int rmatch
= read(fd_match
,
386 bufmatch
+ done
, r
- done
);
394 ERR("could not read match fd %d\n",
404 if (strcmp(buf
, bufmatch
) != 0) {
405 ERR("Outputs do not match on %s:\n",
406 fd_match
== fd_matchout
? "stdout" : "stderr");
407 ERR("correct:\n%s\n", bufmatch
);
408 ERR("wrong:\n%s\n", buf
);
412 } else if (ev
[i
].events
& EPOLLHUP
) {
413 if (epoll_ctl(fd_ep
, EPOLL_CTL_DEL
,
415 ERR("could not remove fd %d from epoll: %m\n",
423 if (err
== 0 && fdmonitor
>= 0) {
425 ERR("Test '%s' timed out, killing %d\n", t
->name
, child
);
426 kill(child
, SIGKILL
);
430 if (fd_matchout
>= 0)
432 if (fd_matcherr
>= 0)
439 static inline int safe_read(int fd
, void *buf
, size_t count
)
444 r
= read(fd
, buf
, count
);
445 if (r
== -1 && errno
== EINTR
)
453 static bool check_generated_files(const struct test
*t
)
455 const struct keyval
*k
;
457 /* This is not meant to be a diff replacement, just stupidly check if
458 * the files match. Bear in mind they can be binary files */
459 for (k
= t
->output
.files
; k
&& k
->key
; k
++) {
460 struct stat sta
, stb
;
461 int fda
= -1, fdb
= -1;
465 fda
= open(k
->key
, O_RDONLY
);
467 ERR("could not open %s\n - %m\n", k
->key
);
471 fdb
= open(k
->val
, O_RDONLY
);
473 ERR("could not open %s\n - %m\n", k
->val
);
477 if (fstat(fda
, &sta
) != 0) {
478 ERR("could not fstat %d %s\n - %m\n", fda
, k
->key
);
482 if (fstat(fdb
, &stb
) != 0) {
483 ERR("could not fstat %d %s\n - %m\n", fdb
, k
->key
);
487 if (sta
.st_size
!= stb
.st_size
) {
488 ERR("sizes do not match %s %s\n", k
->key
, k
->val
);
495 r
= safe_read(fda
, bufa
, sizeof(bufa
));
500 /* size is already checked, go to next file */
503 for (done
= 0; done
< r
;) {
504 int r2
= safe_read(fdb
, bufb
+ done
, r
- done
);
512 if (memcmp(bufa
, bufb
, r
) != 0)
533 static int cmp_modnames(const void *m1
, const void *m2
)
535 const char *s1
= *(char *const *)m1
;
536 const char *s2
= *(char *const *)m2
;
539 for (i
= 0; s1
[i
] || s2
[i
]; i
++) {
540 char c1
= s1
[i
], c2
= s2
[i
];
552 * Store the expected module names in buf and return a list of pointers to
555 static const char **read_expected_modules(const struct test
*t
,
556 char **buf
, int *count
)
563 if (t
->modules_loaded
[0] == '\0') {
568 *buf
= strdup(t
->modules_loaded
);
574 for (p
= *buf
; *p
; p
++)
577 res
= malloc(sizeof(char *) * len
);
587 for (p
= *buf
; i
< len
; p
++)
596 static char **read_loaded_modules(const struct test
*t
, char **buf
, int *count
)
598 char dirname
[PATH_MAX
];
600 struct dirent
*dirent
;
605 const char *rootfs
= t
->config
[TC_ROOTFS
] ? t
->config
[TC_ROOTFS
] : "";
607 /* Store the entries in /sys/module to res */
608 if (snprintf(dirname
, sizeof(dirname
), "%s/sys/module", rootfs
)
609 >= (int)sizeof(dirname
)) {
610 ERR("rootfs path too long: %s\n", rootfs
);
615 dir
= opendir(dirname
);
616 /* not an error, simply return empty list */
622 while ((dirent
= readdir(dir
))) {
623 if (dirent
->d_name
[0] == '.')
626 bufsz
+= strlen(dirent
->d_name
) + 1;
628 res
= malloc(sizeof(char *) * len
);
634 *buf
= malloc(bufsz
);
645 while ((dirent
= readdir(dir
))) {
648 if (dirent
->d_name
[0] == '.')
650 size
= strlen(dirent
->d_name
) + 1;
651 memcpy(p
, dirent
->d_name
, size
);
662 static int check_loaded_modules(const struct test
*t
)
670 a1
= read_expected_modules(t
, &buf1
, &l1
);
673 a2
= read_loaded_modules(t
, &buf2
, &l2
);
676 qsort(a1
, l1
, sizeof(char *), cmp_modnames
);
677 qsort(a2
, l2
, sizeof(char *), cmp_modnames
);
680 while (i1
< l1
|| i2
< l2
) {
688 cmp
= cmp_modnames(&a1
[i1
], &a2
[i2
]);
692 } else if (cmp
< 0) {
694 ERR("module %s not loaded\n", a1
[i1
]);
698 ERR("module %s is loaded but should not be \n", a2
[i2
]);
710 static inline int test_run_parent(const struct test
*t
, int fdout
[2],
711 int fderr
[2], int fdmonitor
[2], pid_t child
)
715 bool matchout
, match_modules
;
717 /* Close write-fds */
718 if (t
->output
.out
!= NULL
)
720 if (t
->output
.err
!= NULL
)
724 matchout
= test_run_parent_check_outputs(t
, fdout
[0], fderr
[0],
725 fdmonitor
[0], child
);
728 * break pipe on the other end: either child already closed or we want
731 if (t
->output
.out
!= NULL
)
733 if (t
->output
.err
!= NULL
)
740 ERR("error waitpid(): %m\n");
744 } while (!WIFEXITED(err
) && !WIFSIGNALED(err
));
746 if (WIFEXITED(err
)) {
747 if (WEXITSTATUS(err
) != 0)
748 ERR("'%s' [%u] exited with return code %d\n",
749 t
->name
, pid
, WEXITSTATUS(err
));
751 LOG("'%s' [%u] exited with return code %d\n",
752 t
->name
, pid
, WEXITSTATUS(err
));
753 } else if (WIFSIGNALED(err
)) {
754 ERR("'%s' [%u] terminated by signal %d (%s)\n", t
->name
, pid
,
755 WTERMSIG(err
), strsignal(WTERMSIG(err
)));
756 err
= t
->expected_fail
? EXIT_SUCCESS
: EXIT_FAILURE
;
761 matchout
= check_generated_files(t
);
762 if (t
->modules_loaded
)
763 match_modules
= check_loaded_modules(t
);
765 match_modules
= true;
767 if (t
->expected_fail
== false) {
769 if (matchout
&& match_modules
)
770 LOG("%sPASSED%s: %s\n",
771 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
774 ERR("%sFAILED%s: exit ok but %s do not match: %s\n",
775 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
776 matchout
? "loaded modules" : "outputs",
781 ERR("%sFAILED%s: %s\n",
782 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
788 ERR("%sUNEXPECTED PASS%s: exit with 0: %s\n",
789 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
793 ERR("%sUNEXPECTED PASS%s: exit with 0 and outputs do not match: %s\n",
794 ANSI_HIGHLIGHT_RED_ON
, ANSI_HIGHLIGHT_OFF
,
800 LOG("%sEXPECTED FAIL%s: %s\n",
801 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
805 LOG("%sEXPECTED FAIL%s: exit with %d but outputs do not match: %s\n",
806 ANSI_HIGHLIGHT_GREEN_ON
, ANSI_HIGHLIGHT_OFF
,
807 WEXITSTATUS(err
), t
->name
);
818 static int prepend_path(const char *extra
)
820 char *oldpath
, *newpath
;
826 oldpath
= getenv("PATH");
828 return setenv("PATH", extra
, 1);
830 if (asprintf(&newpath
, "%s:%s", extra
, oldpath
) < 0) {
831 ERR("failed to allocate memory to new PATH\n");
835 r
= setenv("PATH", newpath
, 1);
841 int test_run(const struct test
*t
)
848 if (t
->need_spawn
&& oneshot
)
851 if (t
->output
.out
!= NULL
) {
852 if (pipe(fdout
) != 0) {
853 ERR("could not create out pipe for %s\n", t
->name
);
858 if (t
->output
.err
!= NULL
) {
859 if (pipe(fderr
) != 0) {
860 ERR("could not create err pipe for %s\n", t
->name
);
865 if (pipe(fdmonitor
) != 0) {
866 ERR("could not create monitor pipe for %s\n", t
->name
);
870 if (prepend_path(t
->path
) < 0) {
871 ERR("failed to prepend '%s' to PATH\n", t
->path
);
875 LOG("running %s, in forked context\n", t
->name
);
879 ERR("could not fork(): %m\n");
880 LOG("FAILED: %s\n", t
->name
);
885 return test_run_parent(t
, fdout
, fderr
, fdmonitor
, pid
);
887 return test_run_child(t
, fdout
, fderr
, fdmonitor
);