1//
2// Automated Testing Framework (atf)
3//
4// Copyright (c) 2008 The NetBSD Foundation, Inc.
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10// 1. Redistributions of source code must retain the above copyright
11//    notice, this list of conditions and the following disclaimer.
12// 2. Redistributions in binary form must reproduce the above copyright
13//    notice, this list of conditions and the following disclaimer in the
14//    documentation and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
30extern "C" {
31#include <sys/types.h>
32#include <sys/wait.h>
33
34#include <limits.h>
35#include <signal.h>
36#include <unistd.h>
37}
38
39#include <cerrno>
40#include <cstdlib>
41#include <cstring>
42#include <fstream>
43#include <ios>
44#include <iostream>
45#include <iterator>
46#include <list>
47#include <memory>
48#include <utility>
49
50#include "atf-c++/check.hpp"
51#include "atf-c++/config.hpp"
52#include "atf-c++/utils.hpp"
53
54#include "atf-c++/detail/application.hpp"
55#include "atf-c++/detail/exceptions.hpp"
56#include "atf-c++/detail/fs.hpp"
57#include "atf-c++/detail/process.hpp"
58#include "atf-c++/detail/sanity.hpp"
59#include "atf-c++/detail/text.hpp"
60
61// ------------------------------------------------------------------------
62// Auxiliary functions.
63// ------------------------------------------------------------------------
64
65namespace {
66
67enum status_check_t {
68    sc_exit,
69    sc_ignore,
70    sc_signal,
71};
72
73struct status_check {
74    status_check_t type;
75    bool negated;
76    int value;
77
78    status_check(const status_check_t& p_type, const bool p_negated,
79                 const int p_value) :
80        type(p_type),
81        negated(p_negated),
82        value(p_value)
83    {
84    }
85};
86
87enum output_check_t {
88    oc_ignore,
89    oc_inline,
90    oc_file,
91    oc_empty,
92    oc_match,
93    oc_save
94};
95
96struct output_check {
97    output_check_t type;
98    bool negated;
99    std::string value;
100
101    output_check(const output_check_t& p_type, const bool p_negated,
102                 const std::string& p_value) :
103        type(p_type),
104        negated(p_negated),
105        value(p_value)
106    {
107    }
108};
109
110class temp_file : public std::ostream {
111    std::auto_ptr< atf::fs::path > m_path;
112    int m_fd;
113
114public:
115    temp_file(const atf::fs::path& p) :
116        std::ostream(NULL),
117        m_fd(-1)
118    {
119        atf::utils::auto_array< char > buf(new char[p.str().length() + 1]);
120        std::strcpy(buf.get(), p.c_str());
121
122        m_fd = ::mkstemp(buf.get());
123        if (m_fd == -1)
124            throw atf::system_error("atf_check::temp_file::temp_file(" +
125                                    p.str() + ")", "mkstemp(3) failed",
126                                    errno);
127
128        m_path.reset(new atf::fs::path(buf.get()));
129    }
130
131    ~temp_file(void)
132    {
133        close();
134        try {
135            remove(*m_path);
136        } catch (const atf::system_error&) {
137            // Ignore deletion errors.
138        }
139    }
140
141    const atf::fs::path&
142    get_path(void) const
143    {
144        return *m_path;
145    }
146
147    void
148    write(const std::string& text)
149    {
150        if (::write(m_fd, text.c_str(), text.size()) == -1)
151            throw atf::system_error("atf_check", "write(2) failed", errno);
152    }
153
154    void
155    close(void)
156    {
157        if (m_fd != -1) {
158            flush();
159            ::close(m_fd);
160            m_fd = -1;
161        }
162    }
163};
164
165} // anonymous namespace
166
167static int
168parse_exit_code(const std::string& str)
169{
170    try {
171        const int value = atf::text::to_type< int >(str);
172        if (value < 0 || value > 255)
173            throw std::runtime_error("Unused reason");
174        return value;
175    } catch (const std::runtime_error&) {
176        throw atf::application::usage_error("Invalid exit code for -s option; "
177            "must be an integer in range 0-255");
178    }
179}
180
181static struct name_number {
182    const char *name;
183    int signo;
184} signal_names_to_numbers[] = {
185    { "hup", SIGHUP },
186    { "int", SIGINT },
187    { "quit", SIGQUIT },
188    { "trap", SIGTRAP },
189    { "abrt", SIGABRT },
190    { "kill", SIGKILL },
191    { "segv", SIGSEGV },
192    { "pipe", SIGPIPE },
193    { "alrm", SIGALRM },
194    { "term", SIGTERM },
195    { "usr1", SIGUSR1 },
196    { "usr2", SIGUSR2 },
197    { NULL, INT_MIN },
198};
199
200static int
201signal_name_to_number(const std::string& str)
202{
203    struct name_number* iter = signal_names_to_numbers;
204    int signo = INT_MIN;
205    while (signo == INT_MIN && iter->name != NULL) {
206        if (str == iter->name || str == std::string("sig") + iter->name)
207            signo = iter->signo;
208        else
209            iter++;
210    }
211    return signo;
212}
213
214static int
215parse_signal(const std::string& str)
216{
217    const int signo = signal_name_to_number(str);
218    if (signo == INT_MIN) {
219        try {
220            return atf::text::to_type< int >(str);
221        } catch (std::runtime_error) {
222            throw atf::application::usage_error("Invalid signal name or number "
223                "in -s option");
224        }
225    }
226    INV(signo != INT_MIN);
227    return signo;
228}
229
230static status_check
231parse_status_check_arg(const std::string& arg)
232{
233    const std::string::size_type delimiter = arg.find(':');
234    bool negated = (arg.compare(0, 4, "not-") == 0);
235    const std::string action_str = arg.substr(0, delimiter);
236    const std::string action = negated ? action_str.substr(4) : action_str;
237    const std::string value_str = (
238        delimiter == std::string::npos ? "" : arg.substr(delimiter + 1));
239    int value;
240
241    status_check_t type;
242    if (action == "eq") {
243        // Deprecated; use exit instead.  TODO: Remove after 0.10.
244        type = sc_exit;
245        if (negated)
246            throw atf::application::usage_error("Cannot negate eq checker");
247        negated = false;
248        value = parse_exit_code(value_str);
249    } else if (action == "exit") {
250        type = sc_exit;
251        if (value_str.empty())
252            value = INT_MIN;
253        else
254            value = parse_exit_code(value_str);
255    } else if (action == "ignore") {
256        if (negated)
257            throw atf::application::usage_error("Cannot negate ignore checker");
258        type = sc_ignore;
259        value = INT_MIN;
260    } else if (action == "ne") {
261        // Deprecated; use not-exit instead.  TODO: Remove after 0.10.
262        type = sc_exit;
263        if (negated)
264            throw atf::application::usage_error("Cannot negate ne checker");
265        negated = true;
266        value = parse_exit_code(value_str);
267    } else if (action == "signal") {
268        type = sc_signal;
269        if (value_str.empty())
270            value = INT_MIN;
271        else
272            value = parse_signal(value_str);
273    } else
274        throw atf::application::usage_error("Invalid status checker");
275
276    return status_check(type, negated, value);
277}
278
279static
280output_check
281parse_output_check_arg(const std::string& arg)
282{
283    const std::string::size_type delimiter = arg.find(':');
284    const bool negated = (arg.compare(0, 4, "not-") == 0);
285    const std::string action_str = arg.substr(0, delimiter);
286    const std::string action = negated ? action_str.substr(4) : action_str;
287
288    output_check_t type;
289    if (action == "empty")
290        type = oc_empty;
291    else if (action == "file")
292        type = oc_file;
293    else if (action == "ignore") {
294        if (negated)
295            throw atf::application::usage_error("Cannot negate ignore checker");
296        type = oc_ignore;
297    } else if (action == "inline")
298        type = oc_inline;
299    else if (action == "match")
300        type = oc_match;
301    else if (action == "save") {
302        if (negated)
303            throw atf::application::usage_error("Cannot negate save checker");
304        type = oc_save;
305    } else
306        throw atf::application::usage_error("Invalid output checker");
307
308    return output_check(type, negated, arg.substr(delimiter + 1));
309}
310
311static
312std::string
313flatten_argv(char* const* argv)
314{
315    std::string cmdline;
316
317    char* const* arg = &argv[0];
318    while (*arg != NULL) {
319        if (arg != &argv[0])
320            cmdline += ' ';
321
322        cmdline += *arg;
323
324        arg++;
325    }
326
327    return cmdline;
328}
329
330static
331std::auto_ptr< atf::check::check_result >
332execute(const char* const* argv)
333{
334    std::cout << "Executing command [ ";
335    for (int i = 0; argv[i] != NULL; ++i)
336        std::cout << argv[i] << " ";
337    std::cout << "]\n";
338
339    atf::process::argv_array argva(argv);
340    return atf::check::exec(argva);
341}
342
343static
344std::auto_ptr< atf::check::check_result >
345execute_with_shell(char* const* argv)
346{
347    const std::string cmd = flatten_argv(argv);
348
349    const char* sh_argv[4];
350    sh_argv[0] = atf::config::get("atf_shell").c_str();
351    sh_argv[1] = "-c";
352    sh_argv[2] = cmd.c_str();
353    sh_argv[3] = NULL;
354    return execute(sh_argv);
355}
356
357static
358void
359cat_file(const atf::fs::path& path)
360{
361    std::ifstream stream(path.c_str());
362    if (!stream)
363        throw std::runtime_error("Failed to open " + path.str());
364
365    stream >> std::noskipws;
366    std::istream_iterator< char > begin(stream), end;
367    std::ostream_iterator< char > out(std::cerr);
368    std::copy(begin, end, out);
369
370    stream.close();
371}
372
373static
374bool
375grep_file(const atf::fs::path& path, const std::string& regexp)
376{
377    std::ifstream stream(path.c_str());
378    if (!stream)
379        throw std::runtime_error("Failed to open " + path.str());
380
381    bool found = false;
382
383    std::string line;
384    while (!found && !std::getline(stream, line).fail()) {
385        if (atf::text::match(line, regexp))
386            found = true;
387    }
388
389    stream.close();
390
391    return found;
392}
393
394static
395bool
396file_empty(const atf::fs::path& p)
397{
398    atf::fs::file_info f(p);
399
400    return (f.get_size() == 0);
401}
402
403static bool
404compare_files(const atf::fs::path& p1, const atf::fs::path& p2)
405{
406    bool equal = false;
407
408    std::ifstream f1(p1.c_str());
409    if (!f1)
410        throw std::runtime_error("Failed to open " + p1.str());
411
412    std::ifstream f2(p2.c_str());
413    if (!f2)
414        throw std::runtime_error("Failed to open " + p1.str());
415
416    for (;;) {
417        char buf1[512], buf2[512];
418
419        f1.read(buf1, sizeof(buf1));
420        if (f1.bad())
421            throw std::runtime_error("Failed to read from " + p1.str());
422
423        f2.read(buf2, sizeof(buf2));
424        if (f2.bad())
425            throw std::runtime_error("Failed to read from " + p1.str());
426
427        if ((f1.gcount() == 0) && (f2.gcount() == 0)) {
428            equal = true;
429            break;
430        }
431
432        if ((f1.gcount() != f2.gcount()) ||
433            (std::memcmp(buf1, buf2, f1.gcount()) != 0)) {
434            break;
435        }
436    }
437
438    return equal;
439}
440
441static
442void
443print_diff(const atf::fs::path& p1, const atf::fs::path& p2)
444{
445    const atf::process::status s =
446        atf::process::exec(atf::fs::path("diff"),
447                           atf::process::argv_array("diff", "-u", p1.c_str(),
448                                                    p2.c_str(), NULL),
449                           atf::process::stream_connect(STDOUT_FILENO,
450                                                        STDERR_FILENO),
451                           atf::process::stream_inherit());
452
453    if (!s.exited())
454        std::cerr << "Failed to run diff(3)\n";
455
456    if (s.exitstatus() != 1)
457        std::cerr << "Error while running diff(3)\n";
458}
459
460static
461std::string
462decode(const std::string& s)
463{
464    size_t i;
465    std::string res;
466
467    res.reserve(s.length());
468
469    i = 0;
470    while (i < s.length()) {
471        char c = s[i++];
472
473        if (c == '\\') {
474            switch (s[i++]) {
475            case 'a': c = '\a'; break;
476            case 'b': c = '\b'; break;
477            case 'c': break;
478            case 'e': c = 033; break;
479            case 'f': c = '\f'; break;
480            case 'n': c = '\n'; break;
481            case 'r': c = '\r'; break;
482            case 't': c = '\t'; break;
483            case 'v': c = '\v'; break;
484            case '\\': break;
485            case '0':
486                {
487                    int count = 3;
488                    c = 0;
489                    while (--count >= 0 && (unsigned)(s[i] - '0') < 8)
490                        c = (c << 3) + (s[i++] - '0');
491                    break;
492                }
493            default:
494                --i;
495                break;
496            }
497        }
498
499        res.push_back(c);
500    }
501
502    return res;
503}
504
505static
506bool
507run_status_check(const status_check& sc, const atf::check::check_result& cr)
508{
509    bool result;
510
511    if (sc.type == sc_exit) {
512        if (cr.exited() && sc.value != INT_MIN) {
513            const int status = cr.exitcode();
514
515            if (!sc.negated && sc.value != status) {
516                std::cerr << "Fail: incorrect exit status: "
517                          << status << ", expected: "
518                          << sc.value << "\n";
519                result = false;
520            } else if (sc.negated && sc.value == status) {
521                std::cerr << "Fail: incorrect exit status: "
522                          << status << ", expected: "
523                          << "anything else\n";
524                result = false;
525            } else
526                result = true;
527        } else if (cr.exited() && sc.value == INT_MIN) {
528            result = true;
529        } else {
530            std::cerr << "Fail: program did not exit cleanly\n";
531            result = false;
532        }
533    } else if (sc.type == sc_ignore) {
534        result = true;
535    } else if (sc.type == sc_signal) {
536        if (cr.signaled() && sc.value != INT_MIN) {
537            const int status = cr.termsig();
538
539            if (!sc.negated && sc.value != status) {
540                std::cerr << "Fail: incorrect signal received: "
541                          << status << ", expected: " << sc.value << "\n";
542                result = false;
543            } else if (sc.negated && sc.value == status) {
544                std::cerr << "Fail: incorrect signal received: "
545                          << status << ", expected: "
546                          << "anything else\n";
547                result = false;
548            } else
549                result = true;
550        } else if (cr.signaled() && sc.value == INT_MIN) {
551            result = true;
552        } else {
553            std::cerr << "Fail: program did not receive a signal\n";
554            result = false;
555        }
556    } else {
557        UNREACHABLE;
558        result = false;
559    }
560
561    if (result == false) {
562        std::cerr << "stdout:\n";
563        cat_file(atf::fs::path(cr.stdout_path()));
564        std::cerr << "\n";
565
566        std::cerr << "stderr:\n";
567        cat_file(atf::fs::path(cr.stderr_path()));
568        std::cerr << "\n";
569    }
570
571    return result;
572}
573
574static
575bool
576run_status_checks(const std::vector< status_check >& checks,
577                  const atf::check::check_result& result)
578{
579    bool ok = false;
580
581    for (std::vector< status_check >::const_iterator iter = checks.begin();
582         !ok && iter != checks.end(); iter++) {
583         ok |= run_status_check(*iter, result);
584    }
585
586    return ok;
587}
588
589static
590bool
591run_output_check(const output_check oc, const atf::fs::path& path,
592                 const std::string& stdxxx)
593{
594    bool result;
595
596    if (oc.type == oc_empty) {
597        const bool is_empty = file_empty(path);
598        if (!oc.negated && !is_empty) {
599            std::cerr << "Fail: " << stdxxx << " not empty\n";
600            print_diff(atf::fs::path("/dev/null"), path);
601            result = false;
602        } else if (oc.negated && is_empty) {
603            std::cerr << "Fail: " << stdxxx << " is empty\n";
604            result = false;
605        } else
606            result = true;
607    } else if (oc.type == oc_file) {
608        const bool equals = compare_files(path, atf::fs::path(oc.value));
609        if (!oc.negated && !equals) {
610            std::cerr << "Fail: " << stdxxx << " does not match golden "
611                "output\n";
612            print_diff(atf::fs::path(oc.value), path);
613            result = false;
614        } else if (oc.negated && equals) {
615            std::cerr << "Fail: " << stdxxx << " matches golden output\n";
616            cat_file(atf::fs::path(oc.value));
617            result = false;
618        } else
619            result = true;
620    } else if (oc.type == oc_ignore) {
621        result = true;
622    } else if (oc.type == oc_inline) {
623        atf::fs::path path2 = atf::fs::path(atf::config::get("atf_workdir"))
624                              / "inline.XXXXXX";
625        temp_file temp(path2);
626        temp.write(decode(oc.value));
627        temp.close();
628
629        const bool equals = compare_files(path, temp.get_path());
630        if (!oc.negated && !equals) {
631            std::cerr << "Fail: " << stdxxx << " does not match expected "
632                "value\n";
633            print_diff(temp.get_path(), path);
634            result = false;
635        } else if (oc.negated && equals) {
636            std::cerr << "Fail: " << stdxxx << " matches expected value\n";
637            cat_file(temp.get_path());
638            result = false;
639        } else
640            result = true;
641    } else if (oc.type == oc_match) {
642        const bool matches = grep_file(path, oc.value);
643        if (!oc.negated && !matches) {
644            std::cerr << "Fail: regexp " + oc.value + " not in " << stdxxx
645                      << "\n";
646            cat_file(path);
647            result = false;
648        } else if (oc.negated && matches) {
649            std::cerr << "Fail: regexp " + oc.value + " is in " << stdxxx
650                      << "\n";
651            cat_file(path);
652            result = false;
653        } else
654            result = true;
655    } else if (oc.type == oc_save) {
656        INV(!oc.negated);
657        std::ifstream ifs(path.c_str(), std::fstream::binary);
658        ifs >> std::noskipws;
659        std::istream_iterator< char > begin(ifs), end;
660
661        std::ofstream ofs(oc.value.c_str(), std::fstream::binary
662                                     | std::fstream::trunc);
663        std::ostream_iterator <char> obegin(ofs);
664
665        std::copy(begin, end, obegin);
666        result = true;
667    } else {
668        UNREACHABLE;
669        result = false;
670    }
671
672    return result;
673}
674
675static
676bool
677run_output_checks(const std::vector< output_check >& checks,
678                  const atf::fs::path& path, const std::string& stdxxx)
679{
680    bool ok = true;
681
682    for (std::vector< output_check >::const_iterator iter = checks.begin();
683         iter != checks.end(); iter++) {
684         ok &= run_output_check(*iter, path, stdxxx);
685    }
686
687    return ok;
688}
689
690// ------------------------------------------------------------------------
691// The "atf_check" application.
692// ------------------------------------------------------------------------
693
694namespace {
695
696class atf_check : public atf::application::app {
697    bool m_xflag;
698
699    std::vector< status_check > m_status_checks;
700    std::vector< output_check > m_stdout_checks;
701    std::vector< output_check > m_stderr_checks;
702
703    static const char* m_description;
704
705    bool run_output_checks(const atf::check::check_result&,
706                           const std::string&) const;
707
708    std::string specific_args(void) const;
709    options_set specific_options(void) const;
710    void process_option(int, const char*);
711    void process_option_s(const std::string&);
712
713public:
714    atf_check(void);
715    int main(void);
716};
717
718} // anonymous namespace
719
720const char* atf_check::m_description =
721    "atf-check executes given command and analyzes its results.";
722
723atf_check::atf_check(void) :
724    app(m_description, "atf-check(1)", "atf(7)"),
725    m_xflag(false)
726{
727}
728
729bool
730atf_check::run_output_checks(const atf::check::check_result& r,
731                             const std::string& stdxxx)
732    const
733{
734    if (stdxxx == "stdout") {
735        return ::run_output_checks(m_stdout_checks,
736            atf::fs::path(r.stdout_path()), "stdout");
737    } else if (stdxxx == "stderr") {
738        return ::run_output_checks(m_stderr_checks,
739            atf::fs::path(r.stderr_path()), "stderr");
740    } else {
741        UNREACHABLE;
742        return false;
743    }
744}
745
746std::string
747atf_check::specific_args(void)
748    const
749{
750    return "<command>";
751}
752
753atf_check::options_set
754atf_check::specific_options(void)
755    const
756{
757    using atf::application::option;
758    options_set opts;
759
760    opts.insert(option('s', "qual:value", "Handle status. Qualifier "
761                "must be one of: ignore exit:<num> signal:<name|num>"));
762    opts.insert(option('o', "action:arg", "Handle stdout. Action must be "
763                "one of: empty ignore file:<path> inline:<val> match:regexp "
764                "save:<path>"));
765    opts.insert(option('e', "action:arg", "Handle stderr. Action must be "
766                "one of: empty ignore file:<path> inline:<val> match:regexp "
767                "save:<path>"));
768    opts.insert(option('x', "", "Execute command as a shell command"));
769
770    return opts;
771}
772
773void
774atf_check::process_option(int ch, const char* arg)
775{
776    switch (ch) {
777    case 's':
778        m_status_checks.push_back(parse_status_check_arg(arg));
779        break;
780
781    case 'o':
782        m_stdout_checks.push_back(parse_output_check_arg(arg));
783        break;
784
785    case 'e':
786        m_stderr_checks.push_back(parse_output_check_arg(arg));
787        break;
788
789    case 'x':
790        m_xflag = true;
791        break;
792
793    default:
794        UNREACHABLE;
795    }
796}
797
798int
799atf_check::main(void)
800{
801    if (m_argc < 1)
802        throw atf::application::usage_error("No command specified");
803
804    int status = EXIT_FAILURE;
805
806    std::auto_ptr< atf::check::check_result > r =
807        m_xflag ? execute_with_shell(m_argv) : execute(m_argv);
808
809    if (m_status_checks.empty())
810        m_status_checks.push_back(status_check(sc_exit, false, EXIT_SUCCESS));
811    else if (m_status_checks.size() > 1) {
812        // TODO: Remove this restriction.
813        throw atf::application::usage_error("Cannot specify -s more than once");
814    }
815
816    if (m_stdout_checks.empty())
817        m_stdout_checks.push_back(output_check(oc_empty, false, ""));
818    if (m_stderr_checks.empty())
819        m_stderr_checks.push_back(output_check(oc_empty, false, ""));
820
821    if ((run_status_checks(m_status_checks, *r) == false) ||
822        (run_output_checks(*r, "stderr") == false) ||
823        (run_output_checks(*r, "stdout") == false))
824        status = EXIT_FAILURE;
825    else
826        status = EXIT_SUCCESS;
827
828    return status;
829}
830
831int
832main(int argc, char* const* argv)
833{
834    return atf_check().run(argc, argv);
835}
836