Path: blob/main/contrib/atf/atf-c++/detail/application_test.cpp
39563 views
// Copyright (c) 2009 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#include "atf-c++/detail/application.hpp"2627extern "C" {28#include <unistd.h>29}3031#include <atf-c++.hpp>3233class getopt_app : public atf::application::app {34public:35getopt_app(void) : app("description", "manpage") {}3637int main(void)38{39// Provide an option that is unknown to the application driver and40// one that is, together with an argument that would be swallowed by41// the test program option if it were recognized.42int argc = 4;43char arg1[] = "progname";44char arg2[] = "-Z";45char arg3[] = "-s";46char arg4[] = "foo";47char *const argv[] = { arg1, arg2, arg3, arg4, NULL };4849int ch;50bool zflag;5152// Given that this obviously is an application, and that we used the53// same driver to start, we can test getopt(3) right here without doing54// any fancy stuff.55zflag = false;56while ((ch = ::getopt(argc, argv, ":Z")) != -1) {57switch (ch) {58case 'Z':59zflag = true;60break;6162case '?':63default:64if (optopt != 's')65ATF_FAIL("Unexpected unknown option found");66}67}6869ATF_REQUIRE(zflag);70ATF_REQUIRE_EQ(1, argc - optind);71ATF_REQUIRE_EQ(std::string("foo"), argv[optind]);7273return 0;74}75};7677ATF_TEST_CASE_WITHOUT_HEAD(getopt);78ATF_TEST_CASE_BODY(getopt)79{80int argc = 1;81char arg1[] = "progname";82char *const argv[] = { arg1, NULL };83ATF_REQUIRE_EQ(0, getopt_app().run(argc, argv));84}8586ATF_INIT_TEST_CASES(tcs)87{88ATF_ADD_TEST_CASE(tcs, getopt);89}909192