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