xref: /netbsd-src/external/bsd/kyua-testers/dist/common_inttest.h (revision 754f425fc237c181450c91977727274098801c74)
1*754f425fSjmmv // Copyright 2012 Google Inc.
2*754f425fSjmmv // All rights reserved.
3*754f425fSjmmv //
4*754f425fSjmmv // Redistribution and use in source and binary forms, with or without
5*754f425fSjmmv // modification, are permitted provided that the following conditions are
6*754f425fSjmmv // met:
7*754f425fSjmmv //
8*754f425fSjmmv // * Redistributions of source code must retain the above copyright
9*754f425fSjmmv //   notice, this list of conditions and the following disclaimer.
10*754f425fSjmmv // * Redistributions in binary form must reproduce the above copyright
11*754f425fSjmmv //   notice, this list of conditions and the following disclaimer in the
12*754f425fSjmmv //   documentation and/or other materials provided with the distribution.
13*754f425fSjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*754f425fSjmmv //   may be used to endorse or promote products derived from this software
15*754f425fSjmmv //   without specific prior written permission.
16*754f425fSjmmv //
17*754f425fSjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*754f425fSjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*754f425fSjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*754f425fSjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*754f425fSjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*754f425fSjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*754f425fSjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*754f425fSjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*754f425fSjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*754f425fSjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*754f425fSjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*754f425fSjmmv 
29*754f425fSjmmv /// \file common_inttest.h
30*754f425fSjmmv /// Common integration tests for the tester binaries.
31*754f425fSjmmv 
32*754f425fSjmmv #if defined(KYUA_COMMON_INTTEST_H)
33*754f425fSjmmv #   error "common_inttest.h can only be defined once"
34*754f425fSjmmv #endif
35*754f425fSjmmv /// Include guard.
36*754f425fSjmmv #define KYUA_COMMON_INTTEST_H
37*754f425fSjmmv 
38*754f425fSjmmv #if !defined(INTERFACE)
39*754f425fSjmmv #   error "Must define INTERFACE to the name of the tester interface"
40*754f425fSjmmv #endif
41*754f425fSjmmv 
42*754f425fSjmmv #include <err.h>
43*754f425fSjmmv #include <stdarg.h>
44*754f425fSjmmv #include <stdbool.h>
45*754f425fSjmmv #include <stdio.h>
46*754f425fSjmmv #include <stdlib.h>
47*754f425fSjmmv 
48*754f425fSjmmv #include <atf-c.h>
49*754f425fSjmmv 
50*754f425fSjmmv #include "cli.h"  // For the EXIT_* constants only.
51*754f425fSjmmv #include "defs.h"
52*754f425fSjmmv 
53*754f425fSjmmv 
54*754f425fSjmmv /// Path to the installed testers.
55*754f425fSjmmv static const char* default_testersdir = TESTERSDIR;
56*754f425fSjmmv 
57*754f425fSjmmv 
58*754f425fSjmmv /// Returns the name of the current tester.
59*754f425fSjmmv #define TESTER_BIN "kyua-" INTERFACE "-tester"
60*754f425fSjmmv 
61*754f425fSjmmv 
62*754f425fSjmmv /// Returns the path to the helpers.
63*754f425fSjmmv ///
64*754f425fSjmmv /// \param tc Pointer to the caller test case, to obtain the srcdir property.
65*754f425fSjmmv ///
66*754f425fSjmmv /// \return A dynamically-allocated string; must be released with free(3).
67*754f425fSjmmv static char*
helpers_path(const atf_tc_t * tc)68*754f425fSjmmv helpers_path(const atf_tc_t* tc)
69*754f425fSjmmv {
70*754f425fSjmmv     const char* srcdir = atf_tc_get_config_var(tc, "srcdir");
71*754f425fSjmmv     const char* name = INTERFACE "_helpers";
72*754f425fSjmmv 
73*754f425fSjmmv     const size_t length = strlen(srcdir) + 1 + strlen(name) + 1;
74*754f425fSjmmv     char* buffer = (char*)malloc(length);
75*754f425fSjmmv     (void)snprintf(buffer, length, "%s/%s", srcdir, name);
76*754f425fSjmmv     return buffer;
77*754f425fSjmmv }
78*754f425fSjmmv 
79*754f425fSjmmv 
80*754f425fSjmmv /// Returns the path to the tester.
81*754f425fSjmmv ///
82*754f425fSjmmv /// \return A dynamically-allocated string; must be released with free(3).
83*754f425fSjmmv static char*
tester_path(void)84*754f425fSjmmv tester_path(void)
85*754f425fSjmmv {
86*754f425fSjmmv     const char* testersdir = getenv("TESTERSDIR");
87*754f425fSjmmv     if (testersdir == NULL)
88*754f425fSjmmv         testersdir = default_testersdir;
89*754f425fSjmmv     const char* name = TESTER_BIN;
90*754f425fSjmmv 
91*754f425fSjmmv     const size_t length = strlen(testersdir) + 1 + strlen(name) + 1;
92*754f425fSjmmv     char* buffer = (char*)malloc(length);
93*754f425fSjmmv     ATF_REQUIRE(buffer != NULL);
94*754f425fSjmmv     (void)snprintf(buffer, length, "%s/%s", testersdir, name);
95*754f425fSjmmv     return buffer;
96*754f425fSjmmv }
97*754f425fSjmmv 
98*754f425fSjmmv 
99*754f425fSjmmv /// Initializes the test case metadata and the helpers.
100*754f425fSjmmv ///
101*754f425fSjmmv /// \param [in,out] tc The test case in which to set the property.
102*754f425fSjmmv /// \param uses_helpers Whether the test uses the helpers or not.
103*754f425fSjmmv static void
setup(atf_tc_t * tc,const bool uses_helpers)104*754f425fSjmmv setup(atf_tc_t* tc, const bool uses_helpers)
105*754f425fSjmmv {
106*754f425fSjmmv     char* tester = tester_path();
107*754f425fSjmmv     if (uses_helpers) {
108*754f425fSjmmv         char* helpers = helpers_path(tc);
109*754f425fSjmmv         atf_tc_set_md_var(tc, "require.progs", "%s %s", tester, helpers);
110*754f425fSjmmv         free(helpers);
111*754f425fSjmmv     } else {
112*754f425fSjmmv         atf_tc_set_md_var(tc, "require.progs", "%s", tester);
113*754f425fSjmmv     }
114*754f425fSjmmv     free(tester);
115*754f425fSjmmv }
116*754f425fSjmmv 
117*754f425fSjmmv 
118*754f425fSjmmv static void execute(va_list ap) KYUA_DEFS_NORETURN;
119*754f425fSjmmv 
120*754f425fSjmmv 
121*754f425fSjmmv /// Executes the tester with the given set of variable arguments.
122*754f425fSjmmv ///
123*754f425fSjmmv /// \param ap List of arguments to the tester.
124*754f425fSjmmv static void
execute(va_list ap)125*754f425fSjmmv execute(va_list ap)
126*754f425fSjmmv {
127*754f425fSjmmv     const char* args[16];
128*754f425fSjmmv 
129*754f425fSjmmv     const char** current_arg = &args[0];
130*754f425fSjmmv     *current_arg = TESTER_BIN;
131*754f425fSjmmv     ++current_arg;
132*754f425fSjmmv     while ((*current_arg = va_arg(ap, const char*)) != NULL)
133*754f425fSjmmv         ++current_arg;
134*754f425fSjmmv 
135*754f425fSjmmv     char* program = tester_path();
136*754f425fSjmmv     (void)execv(program, KYUA_DEFS_UNCONST(args));
137*754f425fSjmmv     free(program);
138*754f425fSjmmv     err(111, "Failed to execute %s", program);
139*754f425fSjmmv }
140*754f425fSjmmv 
141*754f425fSjmmv 
142*754f425fSjmmv /// Executes the tester and validates its output.
143*754f425fSjmmv ///
144*754f425fSjmmv /// \param expected_exit_status Expected exit status of the subprocess.
145*754f425fSjmmv /// \param expected_stdout Expected contents of stdout.
146*754f425fSjmmv /// \param expected_stderr Expected contents of stderr.
147*754f425fSjmmv /// \param ... Arguments to the tester, not including the program name.
148*754f425fSjmmv static void
check(const int expected_exit_status,const char * expected_stdout,const char * expected_stderr,...)149*754f425fSjmmv check(const int expected_exit_status, const char* expected_stdout,
150*754f425fSjmmv       const char* expected_stderr, ...)
151*754f425fSjmmv {
152*754f425fSjmmv     const pid_t pid = atf_utils_fork();
153*754f425fSjmmv     if (pid == 0) {
154*754f425fSjmmv         va_list ap;
155*754f425fSjmmv         va_start(ap, expected_stderr);
156*754f425fSjmmv         execute(ap);
157*754f425fSjmmv         va_end(ap);
158*754f425fSjmmv     } else {
159*754f425fSjmmv         atf_utils_wait(pid, expected_exit_status, expected_stdout,
160*754f425fSjmmv                        expected_stderr);
161*754f425fSjmmv     }
162*754f425fSjmmv }
163*754f425fSjmmv 
164*754f425fSjmmv 
165*754f425fSjmmv ATF_TC(top__missing_command);
ATF_TC_HEAD(top__missing_command,tc)166*754f425fSjmmv ATF_TC_HEAD(top__missing_command, tc) { setup(tc, false); }
ATF_TC_BODY(top__missing_command,tc)167*754f425fSjmmv ATF_TC_BODY(top__missing_command, tc)
168*754f425fSjmmv {
169*754f425fSjmmv     check(EXIT_USAGE_ERROR, "", TESTER_BIN": Must provide a command\n",
170*754f425fSjmmv           NULL);
171*754f425fSjmmv }
172*754f425fSjmmv 
173*754f425fSjmmv 
174*754f425fSjmmv ATF_TC(top__unknown_command);
ATF_TC_HEAD(top__unknown_command,tc)175*754f425fSjmmv ATF_TC_HEAD(top__unknown_command, tc) { setup(tc, false); }
ATF_TC_BODY(top__unknown_command,tc)176*754f425fSjmmv ATF_TC_BODY(top__unknown_command, tc)
177*754f425fSjmmv {
178*754f425fSjmmv     check(EXIT_USAGE_ERROR, "", TESTER_BIN": Unknown command 'foo'\n",
179*754f425fSjmmv           "foo", NULL);
180*754f425fSjmmv }
181