xref: /minix3/external/bsd/atf/dist/atf-c++/detail/application_test.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
111be35a1SLionel Sambuc //
211be35a1SLionel Sambuc // Automated Testing Framework (atf)
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Copyright (c) 2009 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc // All rights reserved.
611be35a1SLionel Sambuc //
711be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc // are met:
1011be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc //    documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc //
1611be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc //
2911be35a1SLionel Sambuc 
3011be35a1SLionel Sambuc extern "C" {
3111be35a1SLionel Sambuc #include <unistd.h>
3211be35a1SLionel Sambuc }
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include "application.hpp"
3511be35a1SLionel Sambuc 
3611be35a1SLionel Sambuc #include "../macros.hpp"
3711be35a1SLionel Sambuc 
3811be35a1SLionel Sambuc class getopt_app : public atf::application::app {
3911be35a1SLionel Sambuc public:
getopt_app(void)40*0a6a1f1dSLionel Sambuc     getopt_app(void) : app("description", "manpage") {}
4111be35a1SLionel Sambuc 
main(void)4211be35a1SLionel Sambuc     int main(void)
4311be35a1SLionel Sambuc     {
4411be35a1SLionel Sambuc         // Provide an option that is unknown to the application driver and
4511be35a1SLionel Sambuc         // one that is, together with an argument that would be swallowed by
4611be35a1SLionel Sambuc         // the test program option if it were recognized.
4711be35a1SLionel Sambuc         int argc = 4;
4811be35a1SLionel Sambuc         char arg1[] = "progname";
4911be35a1SLionel Sambuc         char arg2[] = "-Z";
5011be35a1SLionel Sambuc         char arg3[] = "-s";
5111be35a1SLionel Sambuc         char arg4[] = "foo";
5211be35a1SLionel Sambuc         char *const argv[] = { arg1, arg2, arg3, arg4, NULL };
5311be35a1SLionel Sambuc 
5411be35a1SLionel Sambuc         int ch;
5511be35a1SLionel Sambuc         bool zflag;
5611be35a1SLionel Sambuc 
5711be35a1SLionel Sambuc         // Given that this obviously is an application, and that we used the
5811be35a1SLionel Sambuc         // same driver to start, we can test getopt(3) right here without doing
5911be35a1SLionel Sambuc         // any fancy stuff.
6011be35a1SLionel Sambuc         zflag = false;
6111be35a1SLionel Sambuc         while ((ch = ::getopt(argc, argv, ":Z")) != -1) {
6211be35a1SLionel Sambuc             switch (ch) {
6311be35a1SLionel Sambuc             case 'Z':
6411be35a1SLionel Sambuc                 zflag = true;
6511be35a1SLionel Sambuc                 break;
6611be35a1SLionel Sambuc 
6711be35a1SLionel Sambuc             case '?':
6811be35a1SLionel Sambuc             default:
6911be35a1SLionel Sambuc                 if (optopt != 's')
7011be35a1SLionel Sambuc                     ATF_FAIL("Unexpected unknown option found");
7111be35a1SLionel Sambuc             }
7211be35a1SLionel Sambuc         }
7311be35a1SLionel Sambuc 
7411be35a1SLionel Sambuc         ATF_REQUIRE(zflag);
7511be35a1SLionel Sambuc         ATF_REQUIRE_EQ(1, argc - optind);
7611be35a1SLionel Sambuc         ATF_REQUIRE_EQ(std::string("foo"), argv[optind]);
7711be35a1SLionel Sambuc 
7811be35a1SLionel Sambuc         return 0;
7911be35a1SLionel Sambuc     }
8011be35a1SLionel Sambuc };
8111be35a1SLionel Sambuc 
8211be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(getopt);
ATF_TEST_CASE_BODY(getopt)8311be35a1SLionel Sambuc ATF_TEST_CASE_BODY(getopt)
8411be35a1SLionel Sambuc {
8511be35a1SLionel Sambuc     int argc = 1;
8611be35a1SLionel Sambuc     char arg1[] = "progname";
8711be35a1SLionel Sambuc     char *const argv[] = { arg1, NULL };
8811be35a1SLionel Sambuc     ATF_REQUIRE_EQ(0, getopt_app().run(argc, argv));
8911be35a1SLionel Sambuc }
9011be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)9111be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
9211be35a1SLionel Sambuc {
9311be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, getopt);
9411be35a1SLionel Sambuc }
95