1273929Sjmmv/* Copyright (c) 2010 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
24273929Sjmmv * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25240116Smarcel
26273929Sjmmv#include "atf-c/tp.h"
27273929Sjmmv
28240116Smarcel#include <string.h>
29240116Smarcel#include <unistd.h>
30240116Smarcel
31240116Smarcel#include <atf-c.h>
32240116Smarcel
33240116SmarcelATF_TC(getopt);
34240116SmarcelATF_TC_HEAD(getopt, tc)
35240116Smarcel{
36240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks if getopt(3) global state is "
37240116Smarcel        "reset by the test program driver so that test cases can use "
38240116Smarcel        "getopt(3) again");
39240116Smarcel}
40240116SmarcelATF_TC_BODY(getopt, tc)
41240116Smarcel{
42240116Smarcel    /* Provide an option that is unknown to the test program driver and
43240116Smarcel     * one that is, together with an argument that would be swallowed by
44240116Smarcel     * the test program option if it were recognized. */
45240116Smarcel    int argc = 4;
46240116Smarcel    char arg1[] = "progname";
47240116Smarcel    char arg2[] = "-Z";
48240116Smarcel    char arg3[] = "-s";
49240116Smarcel    char arg4[] = "foo";
50240116Smarcel    char *const argv[] = { arg1, arg2, arg3, arg4, NULL };
51240116Smarcel
52240116Smarcel    int ch;
53240116Smarcel    bool zflag;
54240116Smarcel
55240116Smarcel    /* Given that this obviously is a test program, and that we used the
56240116Smarcel     * same driver to start, we can test getopt(3) right here without doing
57240116Smarcel     * any fancy stuff. */
58240116Smarcel    zflag = false;
59240116Smarcel    while ((ch = getopt(argc, argv, ":Z")) != -1) {
60240116Smarcel        switch (ch) {
61240116Smarcel        case 'Z':
62240116Smarcel            zflag = true;
63240116Smarcel            break;
64240116Smarcel
65240116Smarcel        case '?':
66240116Smarcel        default:
67240116Smarcel            if (optopt != 's')
68240116Smarcel                atf_tc_fail("Unexpected unknown option -%c found", optopt);
69240116Smarcel        }
70240116Smarcel    }
71240116Smarcel
72240116Smarcel    ATF_REQUIRE(zflag);
73240116Smarcel    ATF_REQUIRE_EQ_MSG(1, argc - optind, "Invalid number of arguments left "
74240116Smarcel        "after the call to getopt(3)");
75240116Smarcel    ATF_CHECK_STREQ_MSG("foo", argv[optind], "The non-option argument is "
76240116Smarcel        "invalid");
77240116Smarcel}
78240116Smarcel
79240116Smarcel/* ---------------------------------------------------------------------
80240116Smarcel * Main.
81240116Smarcel * --------------------------------------------------------------------- */
82240116Smarcel
83240116SmarcelATF_TP_ADD_TCS(tp)
84240116Smarcel{
85240116Smarcel    ATF_TP_ADD_TC(tp, getopt);
86240116Smarcel
87240116Smarcel    return atf_no_error();
88240116Smarcel}
89