NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
uri.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the WebSocket++ Project nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 //#define BOOST_TEST_DYN_LINK
28 #define BOOST_TEST_MODULE uri
29 #include <boost/test/unit_test.hpp>
30 
31 #include <iostream>
32 #include <string>
33 
34 #include <websocketpp/uri.hpp>
35 
36 // Test a regular valid ws URI
37 BOOST_AUTO_TEST_CASE( uri_valid ) {
38  websocketpp::uri uri("ws://localhost:9000/chat");
39 
40  BOOST_CHECK( uri.get_valid() );
41  BOOST_CHECK( !uri.get_secure() );
42  BOOST_CHECK_EQUAL( uri.get_scheme(), "ws");
43  BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
44  BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
45  BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
46  BOOST_CHECK_EQUAL( uri.get_query(), "" );
47 }
48 
49 // Test a regular valid ws URI
50 BOOST_AUTO_TEST_CASE( uri_valid_no_port_unsecure ) {
51  websocketpp::uri uri("ws://localhost/chat");
52 
53  BOOST_CHECK( uri.get_valid() );
54  BOOST_CHECK( !uri.get_secure() );
55  BOOST_CHECK_EQUAL( uri.get_scheme(), "ws");
56  BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
57  BOOST_CHECK_EQUAL( uri.get_port(), 80 );
58  BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
59 }
60 
61 // Valid URI with no port (secure)
62 BOOST_AUTO_TEST_CASE( uri_valid_no_port_secure ) {
63  websocketpp::uri uri("wss://localhost/chat");
64 
65  BOOST_CHECK( uri.get_valid() );
66  BOOST_CHECK( uri.get_secure() );
67  BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
68  BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
69  BOOST_CHECK_EQUAL( uri.get_port(), 443 );
70  BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
71 }
72 
73 // Valid URI with no resource
74 BOOST_AUTO_TEST_CASE( uri_valid_no_resource ) {
75  websocketpp::uri uri("wss://localhost:9000");
76 
77  BOOST_CHECK( uri.get_valid() );
78  BOOST_CHECK( uri.get_secure() );
79  BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
80  BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
81  BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
82  BOOST_CHECK_EQUAL( uri.get_resource(), "/" );
83 }
84 
85 // Valid URI IPv6 Literal
86 BOOST_AUTO_TEST_CASE( uri_valid_ipv6_literal ) {
87  websocketpp::uri uri("wss://[::1]:9000/chat");
88 
89  BOOST_CHECK( uri.get_valid() );
90  BOOST_CHECK( uri.get_secure() );
91  BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
92  BOOST_CHECK_EQUAL( uri.get_host(), "::1");
93  BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
94  BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
95 }
96 
97 // Valid URI with more complicated host
98 BOOST_AUTO_TEST_CASE( uri_valid_2 ) {
99  websocketpp::uri uri("wss://thor-websocket.zaphoyd.net:88/");
100 
101  BOOST_CHECK( uri.get_valid() );
102  BOOST_CHECK( uri.get_secure() );
103  BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
104  BOOST_CHECK_EQUAL( uri.get_host(), "thor-websocket.zaphoyd.net");
105  BOOST_CHECK_EQUAL( uri.get_port(), 88 );
106  BOOST_CHECK_EQUAL( uri.get_resource(), "/" );
107 }
108 
109 
110 // Invalid URI (port too long)
111 BOOST_AUTO_TEST_CASE( uri_invalid_long_port ) {
112  websocketpp::uri uri("wss://localhost:900000/chat");
113 
114  BOOST_CHECK( !uri.get_valid() );
115 }
116 
117 // Invalid URI (bogus scheme method)
118 BOOST_AUTO_TEST_CASE( uri_invalid_scheme ) {
119  websocketpp::uri uri("foo://localhost:9000/chat");
120 
121  BOOST_CHECK( !uri.get_valid() );
122 }
123 
124 // Valid URI (http method)
125 BOOST_AUTO_TEST_CASE( uri_http_scheme ) {
126  websocketpp::uri uri("http://localhost:9000/chat");
127 
128  BOOST_CHECK( uri.get_valid() );
129  BOOST_CHECK( !uri.get_secure() );
130  BOOST_CHECK_EQUAL( uri.get_scheme(), "http");
131  BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
132  BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
133  BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
134 }
135 
136 // Valid URI IPv4 literal
137 BOOST_AUTO_TEST_CASE( uri_valid_ipv4_literal ) {
138  websocketpp::uri uri("wss://127.0.0.1:9000/chat");
139 
140  BOOST_CHECK( uri.get_valid() );
141  BOOST_CHECK( uri.get_secure() );
142  BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
143  BOOST_CHECK_EQUAL( uri.get_host(), "127.0.0.1");
144  BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
145  BOOST_CHECK_EQUAL( uri.get_resource(), "/chat" );
146 }
147 
148 // Valid URI complicated resource path
149 BOOST_AUTO_TEST_CASE( uri_valid_3 ) {
150  websocketpp::uri uri("wss://localhost:9000/chat/foo/bar");
151 
152  BOOST_CHECK( uri.get_valid() );
153  BOOST_CHECK( uri.get_secure() );
154  BOOST_CHECK_EQUAL( uri.get_scheme(), "wss");
155  BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
156  BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
157  BOOST_CHECK_EQUAL( uri.get_resource(), "/chat/foo/bar" );
158 }
159 
160 // Invalid URI broken method separator
161 BOOST_AUTO_TEST_CASE( uri_invalid_method_separator ) {
162  websocketpp::uri uri("wss:/localhost:9000/chat");
163 
164  BOOST_CHECK( !uri.get_valid() );
165 }
166 
167 // Invalid URI port > 65535
168 BOOST_AUTO_TEST_CASE( uri_invalid_gt_16_bit_port ) {
169  websocketpp::uri uri("wss:/localhost:70000/chat");
170 
171  BOOST_CHECK( !uri.get_valid() );
172 }
173 
174 // Invalid URI includes uri fragment
175 BOOST_AUTO_TEST_CASE( uri_invalid_fragment ) {
176  websocketpp::uri uri("wss:/localhost:70000/chat#foo");
177 
178  BOOST_CHECK( !uri.get_valid() );
179 }
180 
181 // Invalid URI with no brackets around IPv6 literal
182 BOOST_AUTO_TEST_CASE( uri_invalid_bad_v6_literal_1 ) {
183  websocketpp::uri uri("wss://::1/chat");
184 
185  BOOST_CHECK( !uri.get_valid() );
186 }
187 
188 // Invalid URI with port and no brackets around IPv6 literal
189 BOOST_AUTO_TEST_CASE( uri_invalid_bad_v6_literal_2 ) {
190  websocketpp::uri uri("wss://::1:2009/chat");
191 
192  BOOST_CHECK( !uri.get_valid() );
193 }
194 
195 // Valid URI complicated resource path with query
196 BOOST_AUTO_TEST_CASE( uri_valid_4 ) {
197  websocketpp::uri uri("wss://localhost:9000/chat/foo/bar?foo=bar");
198 
199  BOOST_CHECK( uri.get_valid() );
200  BOOST_CHECK( uri.get_secure() );
201  BOOST_CHECK_EQUAL( uri.get_scheme(), "wss" );
202  BOOST_CHECK_EQUAL( uri.get_host(), "localhost");
203  BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
204  BOOST_CHECK_EQUAL( uri.get_resource(), "/chat/foo/bar?foo=bar" );
205  BOOST_CHECK_EQUAL( uri.get_query(), "foo=bar" );
206 }
207 
208 // Valid URI with a mapped v4 ipv6 literal
209 BOOST_AUTO_TEST_CASE( uri_valid_v4_mapped ) {
210  websocketpp::uri uri("wss://[0000:0000:0000:0000:0000:0000:192.168.1.1]:9000/");
211 
212  BOOST_CHECK( uri.get_valid() );
213  BOOST_CHECK( uri.get_secure() );
214  BOOST_CHECK_EQUAL( uri.get_scheme(), "wss" );
215  BOOST_CHECK_EQUAL( uri.get_host(), "0000:0000:0000:0000:0000:0000:192.168.1.1");
216  BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
217  BOOST_CHECK_EQUAL( uri.get_resource(), "/" );
218 }
219 
220 // Valid URI with a v6 address with mixed case
221 BOOST_AUTO_TEST_CASE( uri_valid_v6_mixed_case ) {
222  websocketpp::uri uri("wss://[::10aB]:9000/");
223 
224  BOOST_CHECK( uri.get_valid() == true );
225  BOOST_CHECK( uri.get_secure() == true );
226  BOOST_CHECK_EQUAL( uri.get_scheme(), "wss" );
227  BOOST_CHECK_EQUAL( uri.get_host(), "::10aB");
228  BOOST_CHECK_EQUAL( uri.get_port(), 9000 );
229  BOOST_CHECK_EQUAL( uri.get_resource(), "/" );
230 }
231 
232 // Valid URI with a v6 address with mixed case
233 BOOST_AUTO_TEST_CASE( uri_invalid_no_scheme ) {
234  websocketpp::uri uri("myserver.com");
235 
236  BOOST_CHECK( !uri.get_valid() );
237 }
238 
239 // Invalid IPv6 literal
240 /*BOOST_AUTO_TEST_CASE( uri_invalid_v6_nonhex ) {
241  websocketpp::uri uri("wss://[g::1]:9000/");
242 
243  BOOST_CHECK( uri.get_valid() == false );
244 }*/
245 
246 // TODO: tests for the other two constructors
std::string const & get_resource() const
Definition: uri.hpp:269
std::string const & get_host() const
Definition: uri.hpp:239
uint16_t get_port() const
Definition: uri.hpp:259
std::string get_query() const
Return the query portion.
Definition: uri.hpp:293
bool get_valid() const
Definition: uri.hpp:227
bool get_secure() const
Definition: uri.hpp:231
std::string const & get_scheme() const
Definition: uri.hpp:235
BOOST_AUTO_TEST_CASE(uri_valid)
Definition: uri.cpp:37