Line data Source code
1 : // Copyright (c) 2011-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/libaddr
4 : // contact@m2osw.com
5 : //
6 : // Permission is hereby granted, free of charge, to any
7 : // person obtaining a copy of this software and
8 : // associated documentation files (the "Software"), to
9 : // deal in the Software without restriction, including
10 : // without limitation the rights to use, copy, modify,
11 : // merge, publish, distribute, sublicense, and/or sell
12 : // copies of the Software, and to permit persons to whom
13 : // the Software is furnished to do so, subject to the
14 : // following conditions:
15 : //
16 : // The above copyright notice and this permission notice
17 : // shall be included in all copies or substantial
18 : // portions of the Software.
19 : //
20 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
21 : // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
22 : // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
23 : // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
24 : // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 : // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 : // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 : // ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 : // SOFTWARE.
30 :
31 :
32 : /** \file
33 : * \brief Test the Address Validator.
34 : *
35 : * The advgetopt library comes with a way to validate command line options.
36 : * The validator_address class extends that feature by offering programmers
37 : * a way to validate input data as an IP address.
38 : */
39 :
40 : // addr
41 : //
42 : #include <libaddr/validator_address.h>
43 :
44 :
45 : // self
46 : //
47 : #include "catch_main.h"
48 :
49 :
50 : // last include
51 : //
52 : #include <snapdev/poison.h>
53 :
54 :
55 :
56 :
57 5 : CATCH_TEST_CASE("validator", "[validator]")
58 : {
59 6 : CATCH_START_SECTION("validator: default validation")
60 : {
61 2 : advgetopt::validator::pointer_t address_validator(advgetopt::validator::create("address", advgetopt::string_list_t()));
62 :
63 1 : CATCH_REQUIRE(address_validator != nullptr);
64 1 : CATCH_REQUIRE(address_validator->name() == "address");
65 :
66 1 : CATCH_REQUIRE(address_validator->validate("192.168.1.1"));
67 1 : CATCH_REQUIRE(address_validator->validate("10.0.0.10:5434"));
68 1 : CATCH_REQUIRE_FALSE(address_validator->validate("10.0.0.10:5434/24"));
69 1 : CATCH_REQUIRE(address_validator->validate("::"));
70 1 : CATCH_REQUIRE(address_validator->validate("[::]:307"));
71 1 : CATCH_REQUIRE(address_validator->validate("f801::5"));
72 : }
73 : CATCH_END_SECTION()
74 :
75 6 : CATCH_START_SECTION("validator: allow all")
76 : {
77 1 : advgetopt::string_list_t const flags{
78 : "address=commas spaces newlines range lookup",
79 : "port=yes",
80 : "mask=address",
81 : "comment",
82 : "defaults=192.168.2.1:4040/24",
83 2 : };
84 2 : advgetopt::validator::pointer_t address_validator(advgetopt::validator::create("address", flags));
85 :
86 1 : CATCH_REQUIRE(address_validator != nullptr);
87 1 : CATCH_REQUIRE(address_validator->name() == "address");
88 :
89 1 : CATCH_REQUIRE(address_validator->validate("192.168.1.1"));
90 1 : CATCH_REQUIRE(address_validator->validate("10.0.0.10:5434"));
91 1 : CATCH_REQUIRE(address_validator->validate("10.0.0.10:5434/24"));
92 1 : CATCH_REQUIRE(address_validator->validate(":5/255.255.255.0"));
93 1 : CATCH_REQUIRE(address_validator->validate("::"));
94 1 : CATCH_REQUIRE(address_validator->validate("f801::5/48"));
95 : }
96 : CATCH_END_SECTION()
97 :
98 6 : CATCH_START_SECTION("validator: unknown option")
99 : {
100 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: \"coment=\" is not a known option for the address validator.");
101 1 : advgetopt::string_list_t const flags{
102 : "", // ignored
103 : "coment=", // "comment" mispelled
104 : "", // ignored
105 2 : };
106 2 : advgetopt::validator::pointer_t address_validator(advgetopt::validator::create("address", flags));
107 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
108 :
109 1 : CATCH_REQUIRE(address_validator != nullptr);
110 1 : CATCH_REQUIRE(address_validator->name() == "address");
111 :
112 1 : CATCH_REQUIRE(address_validator->validate("5.6.7.8"));
113 : }
114 : CATCH_END_SECTION()
115 9 : }
116 :
117 :
118 :
119 : // vim: ts=4 sw=4 et
|