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