Line data Source code
1 : // Copyright (c) 2011-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/edhttp
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : /** \file
20 : * \brief Verify the validator_uri class.
21 : *
22 : * This file implements tests to verify that the validator_uri
23 : * class functions as expected.
24 : */
25 :
26 : // self
27 : //
28 : #include "catch_main.h"
29 :
30 :
31 : // edhttp
32 : //
33 : #include <edhttp/validator_uri.h>
34 :
35 :
36 :
37 11 : CATCH_TEST_CASE("validator_uri", "[domain]")
38 : {
39 18 : CATCH_START_SECTION("validator_uri: validate valid and invalid URIs (default)")
40 : {
41 2 : advgetopt::validator::pointer_t v(advgetopt::validator::create("uri"));
42 :
43 1 : CATCH_REQUIRE(v != nullptr);
44 1 : CATCH_REQUIRE(v->name() == "uri");
45 :
46 1 : CATCH_REQUIRE(v->validate("http://snap.website/"));
47 1 : CATCH_REQUIRE(v->validate("https://snap.website/with/path"));
48 1 : CATCH_REQUIRE(v->validate("gopher://snap.website/?with=param"));
49 1 : CATCH_REQUIRE(v->validate("cd://snap.website/#with-anchor"));
50 :
51 1 : CATCH_REQUIRE_FALSE(v->validate("://bar.uri/"));
52 1 : CATCH_REQUIRE_FALSE(v->validate("bad:too"));
53 1 : CATCH_REQUIRE_FALSE(v->validate("---really bad---"));
54 :
55 1 : CATCH_REQUIRE_FALSE(v->validate("http://127.0.0.1/refused-by-default"));
56 1 : CATCH_REQUIRE_FALSE(v->validate("cdu:/refused/by/default"));
57 : }
58 : CATCH_END_SECTION()
59 :
60 18 : CATCH_START_SECTION("validator_uri: validate valid and invalid URIs (refuse all)")
61 : {
62 2 : advgetopt::validator::pointer_t v(advgetopt::validator::create("uri(refuse-ip, refuse-path)"));
63 :
64 1 : CATCH_REQUIRE(v != nullptr);
65 1 : CATCH_REQUIRE(v->name() == "uri");
66 :
67 1 : CATCH_REQUIRE(v->validate("http://snap.website/"));
68 1 : CATCH_REQUIRE(v->validate("https://snap.website/with/path"));
69 1 : CATCH_REQUIRE(v->validate("gopher://snap.website/?with=param"));
70 1 : CATCH_REQUIRE(v->validate("cd://snap.website/#with-anchor"));
71 :
72 1 : CATCH_REQUIRE_FALSE(v->validate("://bar.uri/"));
73 1 : CATCH_REQUIRE_FALSE(v->validate("bad:too"));
74 1 : CATCH_REQUIRE_FALSE(v->validate("---really bad---"));
75 :
76 1 : CATCH_REQUIRE_FALSE(v->validate("http://127.0.0.1/refused-by-default"));
77 1 : CATCH_REQUIRE_FALSE(v->validate("cdu:/refused/by/default"));
78 : }
79 : CATCH_END_SECTION()
80 :
81 18 : CATCH_START_SECTION("validator_uri: validate valid and invalid URIs (refuse ip)")
82 : {
83 2 : advgetopt::validator::pointer_t v(advgetopt::validator::create("uri(refuse_ip)"));
84 :
85 1 : CATCH_REQUIRE(v != nullptr);
86 1 : CATCH_REQUIRE(v->name() == "uri");
87 :
88 1 : CATCH_REQUIRE(v->validate("http://snap.website/"));
89 1 : CATCH_REQUIRE(v->validate("https://snap.website/with/path"));
90 1 : CATCH_REQUIRE(v->validate("gopher://snap.website/?with=param"));
91 1 : CATCH_REQUIRE(v->validate("cd://snap.website/#with-anchor"));
92 :
93 1 : CATCH_REQUIRE_FALSE(v->validate("://bar.uri/"));
94 1 : CATCH_REQUIRE_FALSE(v->validate("bad:too"));
95 1 : CATCH_REQUIRE_FALSE(v->validate("---really bad---"));
96 :
97 1 : CATCH_REQUIRE_FALSE(v->validate("http://127.0.0.1/refused-by-default"));
98 1 : CATCH_REQUIRE_FALSE(v->validate("cdu:/refused/by/default"));
99 : }
100 : CATCH_END_SECTION()
101 :
102 18 : CATCH_START_SECTION("validator_uri: validate valid and invalid URIs (refuse path)")
103 : {
104 2 : advgetopt::validator::pointer_t v(advgetopt::validator::create("uri(refuse_path)"));
105 :
106 1 : CATCH_REQUIRE(v != nullptr);
107 1 : CATCH_REQUIRE(v->name() == "uri");
108 :
109 1 : CATCH_REQUIRE(v->validate("http://snap.website/"));
110 1 : CATCH_REQUIRE(v->validate("https://snap.website/with/path"));
111 1 : CATCH_REQUIRE(v->validate("gopher://snap.website/?with=param"));
112 1 : CATCH_REQUIRE(v->validate("cd://snap.website/#with-anchor"));
113 :
114 1 : CATCH_REQUIRE_FALSE(v->validate("://bar.uri/"));
115 1 : CATCH_REQUIRE_FALSE(v->validate("bad:too"));
116 1 : CATCH_REQUIRE_FALSE(v->validate("---really bad---"));
117 :
118 1 : CATCH_REQUIRE_FALSE(v->validate("http://127.0.0.1/refused-by-default"));
119 1 : CATCH_REQUIRE_FALSE(v->validate("cdu:/refused/by/default"));
120 : }
121 : CATCH_END_SECTION()
122 :
123 18 : CATCH_START_SECTION("validator_uri: validate valid and invalid URIs (accept IP, dash)")
124 : {
125 2 : advgetopt::validator::pointer_t v(advgetopt::validator::create("uri(accept-ip)"));
126 :
127 1 : CATCH_REQUIRE(v != nullptr);
128 1 : CATCH_REQUIRE(v->name() == "uri");
129 :
130 1 : CATCH_REQUIRE(v->validate("http://snap.website/"));
131 1 : CATCH_REQUIRE(v->validate("https://snap.website/with/path"));
132 1 : CATCH_REQUIRE(v->validate("gopher://snap.website/?with=param"));
133 1 : CATCH_REQUIRE(v->validate("cd://snap.website/#with-anchor"));
134 1 : CATCH_REQUIRE(v->validate("http://127.0.0.1/accepted?this#one"));
135 :
136 1 : CATCH_REQUIRE_FALSE(v->validate("://bar.uri/"));
137 1 : CATCH_REQUIRE_FALSE(v->validate("bad:too"));
138 1 : CATCH_REQUIRE_FALSE(v->validate("---really bad---"));
139 :
140 1 : CATCH_REQUIRE_FALSE(v->validate("/refused/by/default"));
141 : }
142 : CATCH_END_SECTION()
143 :
144 18 : CATCH_START_SECTION("validator_uri: validate valid and invalid URIs (accept IP, underscore)")
145 : {
146 2 : advgetopt::validator::pointer_t v(advgetopt::validator::create("uri(accept_ip)"));
147 :
148 1 : CATCH_REQUIRE(v != nullptr);
149 1 : CATCH_REQUIRE(v->name() == "uri");
150 :
151 1 : CATCH_REQUIRE(v->validate("http://snap.website/"));
152 1 : CATCH_REQUIRE(v->validate("https://snap.website/with/path"));
153 1 : CATCH_REQUIRE(v->validate("gopher://snap.website/?with=param"));
154 1 : CATCH_REQUIRE(v->validate("cd://snap.website/#with-anchor"));
155 1 : CATCH_REQUIRE(v->validate("http://127.0.0.1/accepted?this#one"));
156 :
157 1 : CATCH_REQUIRE_FALSE(v->validate("://bar.uri/"));
158 1 : CATCH_REQUIRE_FALSE(v->validate("bad:too"));
159 1 : CATCH_REQUIRE_FALSE(v->validate("---really bad---"));
160 :
161 1 : CATCH_REQUIRE_FALSE(v->validate("/refused/by/default"));
162 : }
163 : CATCH_END_SECTION()
164 :
165 18 : CATCH_START_SECTION("validator_uri: validate valid and invalid URIs (accept Unix path, dash)")
166 : {
167 2 : advgetopt::validator::pointer_t v(advgetopt::validator::create("uri(accept-path)"));
168 :
169 1 : CATCH_REQUIRE(v != nullptr);
170 1 : CATCH_REQUIRE(v->name() == "uri");
171 :
172 1 : CATCH_REQUIRE(v->validate("http://snap.website/"));
173 1 : CATCH_REQUIRE(v->validate("https://snap.website/with/path"));
174 1 : CATCH_REQUIRE(v->validate("gopher://snap.website/?with=param"));
175 1 : CATCH_REQUIRE(v->validate("cd://snap.website/#with-anchor"));
176 1 : CATCH_REQUIRE(v->validate("cdu:///refused/by/default"));
177 :
178 1 : CATCH_REQUIRE_FALSE(v->validate("://bar.uri/"));
179 1 : CATCH_REQUIRE_FALSE(v->validate("bad:too"));
180 1 : CATCH_REQUIRE_FALSE(v->validate("---really bad---"));
181 :
182 1 : CATCH_REQUIRE_FALSE(v->validate("http://127.0.0.1/refused-by-default"));
183 : }
184 : CATCH_END_SECTION()
185 :
186 18 : CATCH_START_SECTION("validator_uri: validate valid and invalid URIs (accept Unix path, underscore)")
187 : {
188 2 : advgetopt::validator::pointer_t v(advgetopt::validator::create("uri(accept_path)"));
189 :
190 1 : CATCH_REQUIRE(v != nullptr);
191 1 : CATCH_REQUIRE(v->name() == "uri");
192 :
193 1 : CATCH_REQUIRE(v->validate("http://snap.website/"));
194 1 : CATCH_REQUIRE(v->validate("https://snap.website/with/path"));
195 1 : CATCH_REQUIRE(v->validate("gopher://snap.website/?with=param"));
196 1 : CATCH_REQUIRE(v->validate("cd://snap.website/#with-anchor"));
197 1 : CATCH_REQUIRE(v->validate("cdu:///refused/by/default"));
198 :
199 1 : CATCH_REQUIRE_FALSE(v->validate("://bar.uri/"));
200 1 : CATCH_REQUIRE_FALSE(v->validate("bad:too"));
201 1 : CATCH_REQUIRE_FALSE(v->validate("---really bad---"));
202 :
203 1 : CATCH_REQUIRE_FALSE(v->validate("http://127.0.0.1/refused-by-default"));
204 : }
205 : CATCH_END_SECTION()
206 :
207 18 : CATCH_START_SECTION("validator_uri: validate valid and invalid URIs (invalid parameter)")
208 : {
209 : // TODO: verify error message
210 2 : advgetopt::validator::pointer_t v(advgetopt::validator::create("uri(allow_path)"));
211 :
212 1 : CATCH_REQUIRE(v != nullptr);
213 1 : CATCH_REQUIRE(v->name() == "uri");
214 :
215 1 : CATCH_REQUIRE(v->validate("http://snap.website/"));
216 1 : CATCH_REQUIRE(v->validate("https://snap.website/with/path"));
217 1 : CATCH_REQUIRE(v->validate("gopher://snap.website/?with=param"));
218 1 : CATCH_REQUIRE(v->validate("cd://snap.website/#with-anchor"));
219 :
220 1 : CATCH_REQUIRE_FALSE(v->validate("://bar.uri/"));
221 1 : CATCH_REQUIRE_FALSE(v->validate("bad:too"));
222 1 : CATCH_REQUIRE_FALSE(v->validate("---really bad---"));
223 :
224 1 : CATCH_REQUIRE_FALSE(v->validate("http://127.0.0.1/refused-by-default"));
225 1 : CATCH_REQUIRE_FALSE(v->validate("cdu:///refused/by/default"));
226 : }
227 : CATCH_END_SECTION()
228 15 : }
229 :
230 :
231 : // vim: ts=4 sw=4 et
|