1c243e490SMarcel Moolenaar // Copyright (c) 2009 The NetBSD Foundation, Inc. 2c243e490SMarcel Moolenaar // All rights reserved. 3c243e490SMarcel Moolenaar // 4c243e490SMarcel Moolenaar // Redistribution and use in source and binary forms, with or without 5c243e490SMarcel Moolenaar // modification, are permitted provided that the following conditions 6c243e490SMarcel Moolenaar // are met: 7c243e490SMarcel Moolenaar // 1. Redistributions of source code must retain the above copyright 8c243e490SMarcel Moolenaar // notice, this list of conditions and the following disclaimer. 9c243e490SMarcel Moolenaar // 2. Redistributions in binary form must reproduce the above copyright 10c243e490SMarcel Moolenaar // notice, this list of conditions and the following disclaimer in the 11c243e490SMarcel Moolenaar // documentation and/or other materials provided with the distribution. 12c243e490SMarcel Moolenaar // 13c243e490SMarcel Moolenaar // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 14c243e490SMarcel Moolenaar // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 15c243e490SMarcel Moolenaar // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16c243e490SMarcel Moolenaar // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17c243e490SMarcel Moolenaar // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 18c243e490SMarcel Moolenaar // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19c243e490SMarcel Moolenaar // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 20c243e490SMarcel Moolenaar // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21c243e490SMarcel Moolenaar // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 22c243e490SMarcel Moolenaar // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23c243e490SMarcel Moolenaar // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 24c243e490SMarcel Moolenaar // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*0677dfd1SJulio Merino 26*0677dfd1SJulio Merino #include "atf-c++/detail/application.hpp" 27c243e490SMarcel Moolenaar 28c243e490SMarcel Moolenaar extern "C" { 29c243e490SMarcel Moolenaar #include <unistd.h> 30c243e490SMarcel Moolenaar } 31c243e490SMarcel Moolenaar 32*0677dfd1SJulio Merino #include <atf-c++.hpp> 33c243e490SMarcel Moolenaar 34c243e490SMarcel Moolenaar class getopt_app : public atf::application::app { 35c243e490SMarcel Moolenaar public: 361a61beb0SJulio Merino getopt_app(void) : app("description", "manpage") {} 37c243e490SMarcel Moolenaar 38c243e490SMarcel Moolenaar int main(void) 39c243e490SMarcel Moolenaar { 40c243e490SMarcel Moolenaar // Provide an option that is unknown to the application driver and 41c243e490SMarcel Moolenaar // one that is, together with an argument that would be swallowed by 42c243e490SMarcel Moolenaar // the test program option if it were recognized. 43c243e490SMarcel Moolenaar int argc = 4; 44c243e490SMarcel Moolenaar char arg1[] = "progname"; 45c243e490SMarcel Moolenaar char arg2[] = "-Z"; 46c243e490SMarcel Moolenaar char arg3[] = "-s"; 47c243e490SMarcel Moolenaar char arg4[] = "foo"; 48c243e490SMarcel Moolenaar char *const argv[] = { arg1, arg2, arg3, arg4, NULL }; 49c243e490SMarcel Moolenaar 50c243e490SMarcel Moolenaar int ch; 51c243e490SMarcel Moolenaar bool zflag; 52c243e490SMarcel Moolenaar 53c243e490SMarcel Moolenaar // Given that this obviously is an application, and that we used the 54c243e490SMarcel Moolenaar // same driver to start, we can test getopt(3) right here without doing 55c243e490SMarcel Moolenaar // any fancy stuff. 56c243e490SMarcel Moolenaar zflag = false; 57c243e490SMarcel Moolenaar while ((ch = ::getopt(argc, argv, ":Z")) != -1) { 58c243e490SMarcel Moolenaar switch (ch) { 59c243e490SMarcel Moolenaar case 'Z': 60c243e490SMarcel Moolenaar zflag = true; 61c243e490SMarcel Moolenaar break; 62c243e490SMarcel Moolenaar 63c243e490SMarcel Moolenaar case '?': 64c243e490SMarcel Moolenaar default: 65c243e490SMarcel Moolenaar if (optopt != 's') 66c243e490SMarcel Moolenaar ATF_FAIL("Unexpected unknown option found"); 67c243e490SMarcel Moolenaar } 68c243e490SMarcel Moolenaar } 69c243e490SMarcel Moolenaar 70c243e490SMarcel Moolenaar ATF_REQUIRE(zflag); 71c243e490SMarcel Moolenaar ATF_REQUIRE_EQ(1, argc - optind); 72c243e490SMarcel Moolenaar ATF_REQUIRE_EQ(std::string("foo"), argv[optind]); 73c243e490SMarcel Moolenaar 74c243e490SMarcel Moolenaar return 0; 75c243e490SMarcel Moolenaar } 76c243e490SMarcel Moolenaar }; 77c243e490SMarcel Moolenaar 78c243e490SMarcel Moolenaar ATF_TEST_CASE_WITHOUT_HEAD(getopt); 79c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(getopt) 80c243e490SMarcel Moolenaar { 81c243e490SMarcel Moolenaar int argc = 1; 82c243e490SMarcel Moolenaar char arg1[] = "progname"; 83c243e490SMarcel Moolenaar char *const argv[] = { arg1, NULL }; 84c243e490SMarcel Moolenaar ATF_REQUIRE_EQ(0, getopt_app().run(argc, argv)); 85c243e490SMarcel Moolenaar } 86c243e490SMarcel Moolenaar 87c243e490SMarcel Moolenaar ATF_INIT_TEST_CASES(tcs) 88c243e490SMarcel Moolenaar { 89c243e490SMarcel Moolenaar ATF_ADD_TEST_CASE(tcs, getopt); 90c243e490SMarcel Moolenaar } 91