aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/lluri_tut.cpp
blob: 949d77df56d8858e2712e8eaaea08b0b39b9ebb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
 * @file   lluri_tut.cpp
 * @brief  LLURI unit tests
 * @date   September 2006
 *
 * $LicenseInfo:firstyear=2006&license=viewergpl$
 * 
 * Copyright (c) 2006-2008, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "linden_common.h"
#include "lltut.h"
#include "llsd.h"
#include "lluri.h"
#include "llhost.h"

namespace tut
{
	struct URITestData {
		void checkParts(const LLURI& u,
				const char* expectedScheme,
				const char* expectedOpaque,
				const char* expectedAuthority,
				const char* expectedPath,
				const char* expectedQuery = "")
		{
			ensure_equals("scheme",		u.scheme(),		expectedScheme);
			ensure_equals("opaque",		u.opaque(),		expectedOpaque);
			ensure_equals("authority",	u.authority(),	expectedAuthority);
			ensure_equals("path",		u.path(),		expectedPath);
			ensure_equals("query",		u.query(),		expectedQuery);
		}
	};
	
	typedef test_group<URITestData>	URITestGroup;
	typedef URITestGroup::object	URITestObject;

	URITestGroup uriTestGroup("LLURI");
	
	template<> template<>
	void URITestObject::test<1>()
	{
		LLURI u("http://abc.com/def/ghi?x=37&y=hello");

		ensure_equals("scheme",		u.scheme(),		"http");
		ensure_equals("authority",	u.authority(),	"abc.com");
		ensure_equals("path",		u.path(),		"/def/ghi");
		ensure_equals("query",		u.query(),		"x=37&y=hello");

		ensure_equals("host name", u.hostName(), "abc.com");
		ensure_equals("host port", u.hostPort(), 80);

		LLSD query = u.queryMap();
		ensure_equals("query x", query["x"].asInteger(), 37);
		ensure_equals("query y", query["y"].asString(), "hello");

		query = LLURI::queryMap("x=22.23&y=https://lindenlab.com/");
		ensure_equals("query x", query["x"].asReal(), 22.23);
		ensure_equals("query y", query["y"].asURI().asString(), "https://lindenlab.com/");
	}

	template<> template<>
	void URITestObject::test<2>()
	{
		// empty string
		checkParts(LLURI(""), "", "", "", "");
	}
	
	template<> template<>
	void URITestObject::test<3>()
	{
		// no scheme
		checkParts(LLURI("foo"), "", "foo", "", "");
		checkParts(LLURI("foo%3A"), "", "foo:", "", "");
	}

	template<> template<>
	void URITestObject::test<4>()
	{
		// scheme w/o paths
		checkParts(LLURI("mailto:zero@ll.com"),
			"mailto", "zero@ll.com", "", "");
		checkParts(LLURI("silly://abc/def?foo"),
			"silly", "//abc/def?foo", "", "");
	}

	template<> template<>
	void URITestObject::test<5>()
	{
		// authority section
		checkParts(LLURI("http:///"),
			"http", "///", "", "/");
			
		checkParts(LLURI("http://abc"),
			"http", "//abc", "abc", "");
			
		checkParts(LLURI("http://a%2Fb/cd"),
			"http", "//a/b/cd", "a/b", "/cd");
			
		checkParts(LLURI("http://host?"),
			"http", "//host?", "host", "");
	}

	template<> template<>
	void URITestObject::test<6>()
	{		
		// path section
		checkParts(LLURI("http://host/a/b/"),
				"http", "//host/a/b/", "host", "/a/b/");
				
		checkParts(LLURI("http://host/a%3Fb/"),
				"http", "//host/a?b/", "host", "/a?b/");
				
		checkParts(LLURI("http://host/a:b/"),
				"http", "//host/a:b/", "host", "/a:b/");
	}

	template<> template<>
	void URITestObject::test<7>()
	{		
		// query string
		checkParts(LLURI("http://host/?"),
				"http", "//host/?", "host", "/", "");
				
		checkParts(LLURI("http://host/?x"),
				"http", "//host/?x", "host", "/", "x");
				
		checkParts(LLURI("http://host/??"),
				"http", "//host/??", "host", "/", "?");
				
		checkParts(LLURI("http://host/?%3F"),
				"http", "//host/??", "host", "/", "?");
	}

	template<> template<>
	void URITestObject::test<8>()
	{
		LLSD path;
		path.append("x");
		path.append("123");
		checkParts(LLURI::buildHTTP("host", path),
			"http", "//host/x/123", "host", "/x/123");
		
		LLSD query;
		query["123"] = "12";
		query["abcd"] = "abc";
		checkParts(LLURI::buildHTTP("host", path, query),
			"http", "//host/x/123?123=12&abcd=abc",
			"host", "/x/123", "123=12&abcd=abc");
	}

	template<> template<>
	void URITestObject::test<9>()
	{
		// test unescaped path components
		LLSD path;
		path.append("x@*//*$&^");
		path.append("123");
		checkParts(LLURI::buildHTTP("host", path),
			"http", "//host/x@*//*$&^/123", "host", "/x@*//*$&^/123");
	}

	template<> template<>
	void URITestObject::test<10>()
	{
		// test unescaped query components
		LLSD path;
		path.append("x");
		path.append("123");
		LLSD query;
		query["123"] = "?&*#//";
		query["**@&?//"] = "abc";
		checkParts(LLURI::buildHTTP("host", path, query),
			"http", "//host/x/123?**@&?//=abc&123=?&*#//",
			"host", "/x/123", "**@&?//=abc&123=?&*#//");
	}

	template<> template<>
	void URITestObject::test<11>()
	{
		// test unescaped host components
		LLSD path;
		path.append("x");
		path.append("123");
		LLSD query;
		query["123"] = "12";
		query["abcd"] = "abc";
		checkParts(LLURI::buildHTTP("hi123*33--}{:portstuffs", path, query),
			"http", "//hi123*33--}{:portstuffs/x/123?123=12&abcd=abc",
			"hi123*33--}{:portstuffs", "/x/123", "123=12&abcd=abc");
	}
	
	template<> template<>
	void URITestObject::test<12>()
	{
		// test funky host_port values that are actually prefixes
		
		checkParts(LLURI::buildHTTP("http://example.com:8080", LLSD()),
			"http", "//example.com:8080",
			"example.com:8080", "");
			
		checkParts(LLURI::buildHTTP("http://example.com:8080/", LLSD()),
			"http", "//example.com:8080/",
			"example.com:8080", "/");

		checkParts(LLURI::buildHTTP("http://example.com:8080/a/b", LLSD()),
			"http", "//example.com:8080/a/b",
			"example.com:8080", "/a/b");
	}

	template<> template<>
	void URITestObject::test<13>()
	{
		const std::string unreserved =   
			"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
			"0123456789"
			"-._~";
		// test escape
		ensure_equals("escaping", LLURI::escape("abcdefg", "abcdef"), "abcdef%67");
		ensure_equals("escaping", LLURI::escape("|/&\\+-_!@", ""), "%7C%2F%26%5C%2B%2D%5F%21%40");
		ensure_equals("escaping as query variable", 
					  LLURI::escape("http://10.0.1.4:12032/agent/god/agent-id/map/layer/?resume=http://station3.ll.com:12032/agent/203ad6df-b522-491d-ba48-4e24eb57aeff/send-postcard", unreserved + ":@!$'()*+,="), 
					  "http:%2F%2F10.0.1.4:12032%2Fagent%2Fgod%2Fagent-id%2Fmap%2Flayer%2F%3Fresume=http:%2F%2Fstation3.ll.com:12032%2Fagent%2F203ad6df-b522-491d-ba48-4e24eb57aeff%2Fsend-postcard");
		// French cedilla (C with squiggle, like in the word Francais) is UTF-8 C3 A7
		std::string cedilla;
		cedilla.push_back( 0xC3 );
		cedilla.push_back( 0xA7 );
		ensure_equals("escape UTF8", LLURI::escape( cedilla, unreserved), "%C3%A7");
	}
	

#if LL_ENABLE_JANKY_DEPRECATED_WEB_SERVICE_CALLS
	template<> template<>
	void URITestObject::test<14>()
	{
		// test various build utilities
		
		LLUUID id("11111111-2222-3333-4444-5566778899aa");
		
		
		checkParts(LLURI::buildAgentSessionURI(id, NULL),
			"http", "//localhost:12040/agent/11111111-2222-3333-4444-5566778899aa/session",
			"localhost:12040", "/agent/11111111-2222-3333-4444-5566778899aa/session");

		checkParts(LLURI::buildAgentLoginInfoURI(id, "datasever:12345"),
			"http", "//datasever:12345/agent/11111111-2222-3333-4444-5566778899aa/logininfo",
			"datasever:12345", "/agent/11111111-2222-3333-4444-5566778899aa/logininfo");
	}
#endif // LL_ENABLE_JANKY_DEPRECATED_WEB_SERVICE_CALLS

	 
	template<> template<>
	void URITestObject::test<15>()
	{
		LLURI u("secondlife:///app/login?first_name=Testert4&last_name=Tester&web_login_key=test");
		// if secondlife is the scheme, LLURI should parse /app/login as path, with no authority 
		ensure_equals("scheme",		u.scheme(),		"secondlife");
		ensure_equals("authority",	u.authority(),	"");
		ensure_equals("path",		u.path(),		"/app/login");
		ensure_equals("pathmap",	u.pathArray()[0].asString(),	"app");
		ensure_equals("pathmap",	u.pathArray()[1].asString(),	"login");
		ensure_equals("query",		u.query(),		"first_name=Testert4&last_name=Tester&web_login_key=test");
		ensure_equals("query map element", u.queryMap()["last_name"].asString(), "Tester");
		
		u = LLURI("secondlife://Da Boom/128/128/128");
		// if secondlife is the scheme, LLURI should parse /128/128/128 as path, with Da Boom as authority
		ensure_equals("scheme",		u.scheme(),		"secondlife");
		ensure_equals("authority",	u.authority(),	"Da Boom");
		ensure_equals("path",		u.path(),		"/128/128/128");
		ensure_equals("pathmap",	u.pathArray()[0].asString(),	"128");
		ensure_equals("pathmap",	u.pathArray()[1].asString(),	"128");
		ensure_equals("pathmap",	u.pathArray()[2].asString(),	"128");
		ensure_equals("query",		u.query(),		"");
	}

	template<> template<>
	void URITestObject::test<16>()
	{
		// Parse about: schemes
		LLURI u("about:blank?redirect-http-hack=secondlife%3A%2F%2F%2Fapp%2Flogin%3Ffirst_name%3DCallum%26last_name%3DLinden%26location%3Dspecify%26grid%3Dvaak%26region%3D%2FMorris%2F128%2F128%26web_login_key%3Defaa4795-c2aa-4c58-8966-763c27931e78");
		ensure_equals("scheme",		u.scheme(),		"about");
		ensure_equals("authority",	u.authority(),	"");
		ensure_equals("path",		u.path(),		"blank");
		ensure_equals("pathmap",	u.pathArray()[0].asString(),	"blank");
		ensure_equals("query",		u.query(),		"redirect-http-hack=secondlife:///app/login?first_name=Callum&last_name=Linden&location=specify&grid=vaak&region=/Morris/128/128&web_login_key=efaa4795-c2aa-4c58-8966-763c27931e78");
		ensure_equals("query map element", u.queryMap()["redirect-http-hack"].asString(), "secondlife:///app/login?first_name=Callum&last_name=Linden&location=specify&grid=vaak&region=/Morris/128/128&web_login_key=efaa4795-c2aa-4c58-8966-763c27931e78");
	}
}