1// Copyright 2012 Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include <err.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35#include "defs.h"
36
37
38/// Exit code to report internal, unexpected errors.
39static const int EXIT_BOGUS = 123;
40
41
42/// Test case that always fails.
43static int
44fail_helper(void)
45{
46    fprintf(stdout, "First line to stdout\n");
47    fprintf(stderr, "First line to stderr\n");
48    return 78;
49}
50
51
52/// Test case that always passes.
53static int
54pass_helper(void)
55{
56    fprintf(stdout, "First line to stdout\n");
57    fprintf(stdout, "Second line to stdout\n");
58    fprintf(stderr, "First line to stderr\n");
59    fprintf(stderr, "Second line to stderr\n");
60    return EXIT_SUCCESS;
61}
62
63
64/// Test case that always dies due to a signal and dumps core.
65static int
66signal_helper(void)
67{
68    fprintf(stderr, "About to die due to SIGABRT!\n");
69    abort();
70}
71
72
73/// Test case that sleeps for a long time.
74static int
75sleep_helper(void)
76{
77    sleep(300);
78    return EXIT_FAILURE;
79}
80
81
82/// Dispatcher for individual testers based on the HELPER environment variable.
83///
84/// \param argc Number of arguments to the command.
85/// \param unused_argv Arguments to the command.
86///
87/// \return An exit code.
88int
89main(const int argc, char* const* const KYUA_DEFS_UNUSED_PARAM(argv))
90{
91    if (argc != 1)
92        errx(EXIT_BOGUS, "No arguments allowed");
93
94    const char* helper_name = getenv("HELPER");
95    if (helper_name == NULL) {
96        errx(EXIT_BOGUS, "Must set HELPER to the name of the desired helper");
97    }
98
99    if (strcmp(helper_name, "fail") == 0) {
100        return fail_helper();
101    } else if (strcmp(helper_name, "pass") == 0) {
102        return pass_helper();
103    } else if (strcmp(helper_name, "signal") == 0) {
104        return signal_helper();
105    } else if (strcmp(helper_name, "sleep") == 0) {
106        return sleep_helper();
107    } else {
108        errx(EXIT_BOGUS, "Unknown helper '%s'", helper_name);
109    }
110}
111