1146773Ssam// Copyright 2012 Google Inc. 2146773Ssam// All rights reserved. 3146773Ssam// 4146773Ssam// Redistribution and use in source and binary forms, with or without 5146773Ssam// modification, are permitted provided that the following conditions are 6146773Ssam// met: 7146773Ssam// 8146773Ssam// * Redistributions of source code must retain the above copyright 9146773Ssam// notice, this list of conditions and the following disclaimer. 10146773Ssam// * Redistributions in binary form must reproduce the above copyright 11146773Ssam// notice, this list of conditions and the following disclaimer in the 12146773Ssam// documentation and/or other materials provided with the distribution. 13146773Ssam// * Neither the name of Google Inc. nor the names of its contributors 14146773Ssam// may be used to endorse or promote products derived from this software 15146773Ssam// without specific prior written permission. 16146773Ssam// 17146773Ssam// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18146773Ssam// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19190207Srpaulo// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20146773Ssam// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21146773Ssam// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22146773Ssam// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23146773Ssam// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24146773Ssam// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25146773Ssam// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26146773Ssam// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27146773Ssam// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28146773Ssam 29146773Ssam#include <err.h> 30146773Ssam#include <stdio.h> 31146773Ssam#include <stdlib.h> 32146773Ssam#include <string.h> 33146773Ssam#include <unistd.h> 34146773Ssam 35146773Ssam#include "defs.h" 36146773Ssam 37146773Ssam 38146773Ssam/// Exit code to report internal, unexpected errors. 39146773Ssamstatic const int EXIT_BOGUS = 123; 40146773Ssam 41146773Ssam 42146773Ssam/// Test case that always fails. 43146773Ssamstatic int 44146773Ssamfail_helper(void) 45146773Ssam{ 46146773Ssam fprintf(stdout, "First line to stdout\n"); 47146773Ssam fprintf(stderr, "First line to stderr\n"); 48146773Ssam return 78; 49146773Ssam} 50146773Ssam 51146773Ssam 52146773Ssam/// Test case that always passes. 53146773Ssamstatic int 54146773Ssampass_helper(void) 55146773Ssam{ 56146773Ssam fprintf(stdout, "First line to stdout\n"); 57146773Ssam fprintf(stdout, "Second line to stdout\n"); 58146773Ssam fprintf(stderr, "First line to stderr\n"); 59146773Ssam fprintf(stderr, "Second line to stderr\n"); 60146773Ssam return EXIT_SUCCESS; 61146773Ssam} 62146773Ssam 63146773Ssam 64146773Ssam/// Test case that always dies due to a signal and dumps core. 65146773Ssamstatic int 66146773Ssamsignal_helper(void) 67146773Ssam{ 68146773Ssam fprintf(stderr, "About to die due to SIGABRT!\n"); 69146773Ssam abort(); 70146773Ssam} 71146773Ssam 72146773Ssam 73146773Ssam/// Test case that sleeps for a long time. 74146773Ssamstatic int 75146773Ssamsleep_helper(void) 76146773Ssam{ 77146773Ssam sleep(300); 78146773Ssam return EXIT_FAILURE; 79146773Ssam} 80146773Ssam 81146773Ssam 82146773Ssam/// Dispatcher for individual testers based on the HELPER environment variable. 83146773Ssam/// 84146773Ssam/// \param argc Number of arguments to the command. 85146773Ssam/// \param unused_argv Arguments to the command. 86146773Ssam/// 87146773Ssam/// \return An exit code. 88146773Ssamint 89146773Ssammain(const int argc, char* const* const KYUA_DEFS_UNUSED_PARAM(argv)) 90146773Ssam{ 91146773Ssam if (argc != 1) 92146773Ssam errx(EXIT_BOGUS, "No arguments allowed"); 93146773Ssam 94146773Ssam const char* helper_name = getenv("HELPER"); 95146773Ssam if (helper_name == NULL) { 96146773Ssam errx(EXIT_BOGUS, "Must set HELPER to the name of the desired helper"); 97146773Ssam } 98146773Ssam 99146773Ssam if (strcmp(helper_name, "fail") == 0) { 100146773Ssam return fail_helper(); 101146773Ssam } else if (strcmp(helper_name, "pass") == 0) { 102146773Ssam return pass_helper(); 103146773Ssam } else if (strcmp(helper_name, "signal") == 0) { 104146773Ssam return signal_helper(); 105146773Ssam } else if (strcmp(helper_name, "sleep") == 0) { 106146773Ssam return sleep_helper(); 107146773Ssam } else { 108146773Ssam errx(EXIT_BOGUS, "Unknown helper '%s'", helper_name); 109146773Ssam } 110146773Ssam} 111146773Ssam