1240116Smarcel// Copyright (c) 2009 The NetBSD Foundation, Inc.
2240116Smarcel// All rights reserved.
3240116Smarcel//
4240116Smarcel// Redistribution and use in source and binary forms, with or without
5240116Smarcel// modification, are permitted provided that the following conditions
6240116Smarcel// are met:
7240116Smarcel// 1. Redistributions of source code must retain the above copyright
8240116Smarcel//    notice, this list of conditions and the following disclaimer.
9240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright
10240116Smarcel//    notice, this list of conditions and the following disclaimer in the
11240116Smarcel//    documentation and/or other materials provided with the distribution.
12240116Smarcel//
13240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25240116Smarcel
26273929Sjmmv#include "atf-c++/detail/application.hpp"
27273929Sjmmv
28240116Smarcelextern "C" {
29240116Smarcel#include <unistd.h>
30240116Smarcel}
31240116Smarcel
32273929Sjmmv#include <atf-c++.hpp>
33240116Smarcel
34240116Smarcelclass getopt_app : public atf::application::app {
35240116Smarcelpublic:
36261897Sjmmv    getopt_app(void) : app("description", "manpage") {}
37240116Smarcel
38240116Smarcel    int main(void)
39240116Smarcel    {
40240116Smarcel        // Provide an option that is unknown to the application driver and
41240116Smarcel        // one that is, together with an argument that would be swallowed by
42240116Smarcel        // the test program option if it were recognized.
43240116Smarcel        int argc = 4;
44240116Smarcel        char arg1[] = "progname";
45240116Smarcel        char arg2[] = "-Z";
46240116Smarcel        char arg3[] = "-s";
47240116Smarcel        char arg4[] = "foo";
48240116Smarcel        char *const argv[] = { arg1, arg2, arg3, arg4, NULL };
49240116Smarcel
50240116Smarcel        int ch;
51240116Smarcel        bool zflag;
52240116Smarcel
53240116Smarcel        // Given that this obviously is an application, and that we used the
54240116Smarcel        // same driver to start, we can test getopt(3) right here without doing
55240116Smarcel        // any fancy stuff.
56240116Smarcel        zflag = false;
57240116Smarcel        while ((ch = ::getopt(argc, argv, ":Z")) != -1) {
58240116Smarcel            switch (ch) {
59240116Smarcel            case 'Z':
60240116Smarcel                zflag = true;
61240116Smarcel                break;
62240116Smarcel
63240116Smarcel            case '?':
64240116Smarcel            default:
65240116Smarcel                if (optopt != 's')
66240116Smarcel                    ATF_FAIL("Unexpected unknown option found");
67240116Smarcel            }
68240116Smarcel        }
69240116Smarcel
70240116Smarcel        ATF_REQUIRE(zflag);
71240116Smarcel        ATF_REQUIRE_EQ(1, argc - optind);
72240116Smarcel        ATF_REQUIRE_EQ(std::string("foo"), argv[optind]);
73240116Smarcel
74240116Smarcel        return 0;
75240116Smarcel    }
76240116Smarcel};
77240116Smarcel
78240116SmarcelATF_TEST_CASE_WITHOUT_HEAD(getopt);
79240116SmarcelATF_TEST_CASE_BODY(getopt)
80240116Smarcel{
81240116Smarcel    int argc = 1;
82240116Smarcel    char arg1[] = "progname";
83240116Smarcel    char *const argv[] = { arg1, NULL };
84240116Smarcel    ATF_REQUIRE_EQ(0, getopt_app().run(argc, argv));
85240116Smarcel}
86240116Smarcel
87240116SmarcelATF_INIT_TEST_CASES(tcs)
88240116Smarcel{
89240116Smarcel    ATF_ADD_TEST_CASE(tcs, getopt);
90240116Smarcel}
91