tp_test.c revision 240116
1240116Smarcel/*
2240116Smarcel * Automated Testing Framework (atf)
3240116Smarcel *
4240116Smarcel * Copyright (c) 2010 The NetBSD Foundation, Inc.
5240116Smarcel * All rights reserved.
6240116Smarcel *
7240116Smarcel * Redistribution and use in source and binary forms, with or without
8240116Smarcel * modification, are permitted provided that the following conditions
9240116Smarcel * are met:
10240116Smarcel * 1. Redistributions of source code must retain the above copyright
11240116Smarcel *    notice, this list of conditions and the following disclaimer.
12240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel *    notice, this list of conditions and the following disclaimer in the
14240116Smarcel *    documentation and/or other materials provided with the distribution.
15240116Smarcel *
16240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel */
29240116Smarcel
30240116Smarcel#include <string.h>
31240116Smarcel#include <unistd.h>
32240116Smarcel
33240116Smarcel#include <atf-c.h>
34240116Smarcel
35240116Smarcel#include "detail/test_helpers.h"
36240116Smarcel
37240116SmarcelATF_TC(getopt);
38240116SmarcelATF_TC_HEAD(getopt, tc)
39240116Smarcel{
40240116Smarcel    atf_tc_set_md_var(tc, "descr", "Checks if getopt(3) global state is "
41240116Smarcel        "reset by the test program driver so that test cases can use "
42240116Smarcel        "getopt(3) again");
43240116Smarcel}
44240116SmarcelATF_TC_BODY(getopt, tc)
45240116Smarcel{
46240116Smarcel    /* Provide an option that is unknown to the test program driver and
47240116Smarcel     * one that is, together with an argument that would be swallowed by
48240116Smarcel     * the test program option if it were recognized. */
49240116Smarcel    int argc = 4;
50240116Smarcel    char arg1[] = "progname";
51240116Smarcel    char arg2[] = "-Z";
52240116Smarcel    char arg3[] = "-s";
53240116Smarcel    char arg4[] = "foo";
54240116Smarcel    char *const argv[] = { arg1, arg2, arg3, arg4, NULL };
55240116Smarcel
56240116Smarcel    int ch;
57240116Smarcel    bool zflag;
58240116Smarcel
59240116Smarcel    /* Given that this obviously is a test program, and that we used the
60240116Smarcel     * same driver to start, we can test getopt(3) right here without doing
61240116Smarcel     * any fancy stuff. */
62240116Smarcel    zflag = false;
63240116Smarcel    while ((ch = getopt(argc, argv, ":Z")) != -1) {
64240116Smarcel        switch (ch) {
65240116Smarcel        case 'Z':
66240116Smarcel            zflag = true;
67240116Smarcel            break;
68240116Smarcel
69240116Smarcel        case '?':
70240116Smarcel        default:
71240116Smarcel            if (optopt != 's')
72240116Smarcel                atf_tc_fail("Unexpected unknown option -%c found", optopt);
73240116Smarcel        }
74240116Smarcel    }
75240116Smarcel
76240116Smarcel    ATF_REQUIRE(zflag);
77240116Smarcel    ATF_REQUIRE_EQ_MSG(1, argc - optind, "Invalid number of arguments left "
78240116Smarcel        "after the call to getopt(3)");
79240116Smarcel    ATF_CHECK_STREQ_MSG("foo", argv[optind], "The non-option argument is "
80240116Smarcel        "invalid");
81240116Smarcel}
82240116Smarcel
83240116Smarcel/* ---------------------------------------------------------------------
84240116Smarcel * Tests cases for the header file.
85240116Smarcel * --------------------------------------------------------------------- */
86240116Smarcel
87240116SmarcelHEADER_TC(include, "atf-c/tp.h");
88240116Smarcel
89240116Smarcel/* ---------------------------------------------------------------------
90240116Smarcel * Main.
91240116Smarcel * --------------------------------------------------------------------- */
92240116Smarcel
93240116SmarcelATF_TP_ADD_TCS(tp)
94240116Smarcel{
95240116Smarcel    ATF_TP_ADD_TC(tp, getopt);
96240116Smarcel
97240116Smarcel    /* Add the test cases for the header file. */
98240116Smarcel    ATF_TP_ADD_TC(tp, include);
99240116Smarcel
100240116Smarcel    return atf_no_error();
101240116Smarcel}
102