aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llprimitive/tests/llmediaentry_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/llprimitive/tests/llmediaentry_test.cpp')
-rwxr-xr-xlinden/indra/llprimitive/tests/llmediaentry_test.cpp508
1 files changed, 508 insertions, 0 deletions
diff --git a/linden/indra/llprimitive/tests/llmediaentry_test.cpp b/linden/indra/llprimitive/tests/llmediaentry_test.cpp
new file mode 100755
index 0000000..89f778d
--- /dev/null
+++ b/linden/indra/llprimitive/tests/llmediaentry_test.cpp
@@ -0,0 +1,508 @@
1/**
2 * @file llmediaentry_test.cpp
3 * @brief llmediaentry unit tests
4 *
5 * $LicenseInfo:firstyear=2001&license=viewergpl$
6 *
7 * Copyright (c) 2001-2010, Linden Research, Inc.
8 *
9 * Second Life Viewer Source Code
10 * The source code in this file ("Source Code") is provided by Linden Lab
11 * to you under the terms of the GNU General Public License, version 2.0
12 * ("GPL"), unless you have obtained a separate licensing agreement
13 * ("Other License"), formally executed by you and Linden Lab. Terms of
14 * the GPL can be found in doc/GPL-license.txt in this distribution, or
15 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
16 *
17 * There are special exceptions to the terms and conditions of the GPL as
18 * it is applied to this Source Code. View the full text of the exception
19 * in the file doc/FLOSS-exception.txt in this software distribution, or
20 * online at
21 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
22 *
23 * By copying, modifying or distributing this software, you acknowledge
24 * that you have read and understood your obligations described above,
25 * and agree to abide by those obligations.
26 *
27 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
28 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
29 * COMPLETENESS OR PERFORMANCE.
30 * $/LicenseInfo$
31 */
32
33#include "linden_common.h"
34#include "lltut.h"
35#if LL_WINDOWS
36#pragma warning (push)
37#pragma warning (disable : 4702) // boost::lexical_cast generates this warning
38#endif
39#include <boost/lexical_cast.hpp>
40#if LL_WINDOWS
41#pragma warning (pop)
42#endif
43#include "llstring.h"
44#include "llsdutil.h"
45#include "llsdserialize.h"
46
47#include "../llmediaentry.h"
48#include "lllslconstants.h"
49
50#define DEFAULT_MEDIA_ENTRY "<llsd>\n\
51 <map>\n\
52 <key>alt_image_enable</key>\n\
53 <boolean>0</boolean>\n\
54 <key>auto_loop</key>\n\
55 <boolean>0</boolean>\n\
56 <key>auto_play</key>\n\
57 <boolean>0</boolean>\n\
58 <key>auto_scale</key>\n\
59 <boolean>0</boolean>\n\
60 <key>auto_zoom</key>\n\
61 <boolean>0</boolean>\n\
62 <key>controls</key>\n\
63 <integer>0</integer>\n\
64 <key>current_url</key>\n\
65 <string />\n\
66 <key>first_click_interact</key>\n\
67 <boolean>0</boolean>\n\
68 <key>height_pixels</key>\n\
69 <integer>0</integer>\n\
70 <key>home_url</key>\n\
71 <string />\n\
72 <key>perms_control</key>\n\
73 <integer>7</integer>\n\
74 <key>perms_interact</key>\n\
75 <integer>7</integer>\n\
76 <key>whitelist_enable</key>\n\
77 <boolean>0</boolean>\n\
78 <key>width_pixels</key>\n\
79 <integer>0</integer>\n\
80 </map>\n\
81 </llsd>"
82
83#define EMPTY_MEDIA_ENTRY "<llsd>\n\
84 <map>\n\
85 <key>alt_image_enable</key>\n\
86 <boolean>0</boolean>\n\
87 <key>auto_loop</key>\n\
88 <boolean>0</boolean>\n\
89 <key>auto_play</key>\n\
90 <boolean>0</boolean>\n\
91 <key>auto_scale</key>\n\
92 <boolean>0</boolean>\n\
93 <key>auto_zoom</key>\n\
94 <boolean>0</boolean>\n\
95 <key>controls</key>\n\
96 <integer>0</integer>\n\
97 <key>current_url</key>\n\
98 <string />\n\
99 <key>first_click_interact</key>\n\
100 <boolean>0</boolean>\n\
101 <key>height_pixels</key>\n\
102 <integer>0</integer>\n\
103 <key>home_url</key>\n\
104 <string />\n\
105 <key>perms_control</key>\n\
106 <integer>0</integer>\n\
107 <key>perms_interact</key>\n\
108 <integer>0</integer>\n\
109 <key>whitelist_enable</key>\n\
110 <boolean>0</boolean>\n\
111 <key>width_pixels</key>\n\
112 <integer>0</integer>\n\
113 </map>\n\
114 </llsd>"
115
116#define PARTIAL_MEDIA_ENTRY(CURRENT_URL) "<llsd>\n\
117 <map>\n\
118 <key>alt_image_enable</key>\n\
119 <boolean>0</boolean>\n\
120 <key>auto_loop</key>\n\
121 <boolean>0</boolean>\n\
122 <key>auto_play</key>\n\
123 <boolean>0</boolean>\n\
124 <key>auto_scale</key>\n\
125 <boolean>0</boolean>\n\
126 <key>auto_zoom</key>\n\
127 <boolean>0</boolean>\n\
128 <key>controls</key>\n\
129 <integer>0</integer>\n\
130 <key>current_url</key>\n\
131 <string>" CURRENT_URL "</string>\n\
132 <key>first_click_interact</key>\n\
133 <boolean>0</boolean>\n\
134 <key>height_pixels</key>\n\
135 <integer>0</integer>\n\
136 <key>home_url</key>\n\
137 <string />\n\
138 <key>perms_control</key>\n\
139 <integer>0</integer>\n\
140 <key>perms_interact</key>\n\
141 <integer>0</integer>\n\
142 <key>whitelist_enable</key>\n\
143 <boolean>0</boolean>\n\
144 <key>width_pixels</key>\n\
145 <integer>0</integer>\n\
146 </map>\n\
147 </llsd>"
148
149namespace tut
150{
151 // this is fixture data that gets created before each test and destroyed
152 // after each test. this is where we put all of the setup/takedown code
153 // and data needed for each test.
154 struct MediaEntry_test
155 {
156 MediaEntry_test() {
157 emptyMediaEntryStr = EMPTY_MEDIA_ENTRY;
158 std::istringstream e(EMPTY_MEDIA_ENTRY);
159 LLSDSerialize::fromXML(emptyMediaEntryLLSD, e);
160 defaultMediaEntryStr = DEFAULT_MEDIA_ENTRY;
161 std::istringstream d(DEFAULT_MEDIA_ENTRY);
162 LLSDSerialize::fromXML(defaultMediaEntryLLSD, d);
163 }
164 std::string emptyMediaEntryStr;
165 LLSD emptyMediaEntryLLSD;
166 std::string defaultMediaEntryStr;
167 LLSD defaultMediaEntryLLSD;
168 };
169
170 typedef test_group<MediaEntry_test, 55> factory;
171 typedef factory::object object;
172}
173
174
175namespace
176{
177 // this is for naming our tests to make pretty output
178 tut::factory tf("MediaEntry Test");
179}
180
181namespace tut
182{
183 void ensure_llsd_equals(const std::string& msg, const LLSD& expected, const LLSD& actual)
184 {
185 if (!llsd_equals(expected, actual))
186 {
187 std::string message = msg;
188 message += ": actual: ";
189 message += ll_pretty_print_sd(actual);
190 message += "\n expected: ";
191 message += ll_pretty_print_sd(expected);
192 message += "\n";
193 ensure(message, false);
194 }
195 }
196
197 void ensure_string_equals(const std::string& msg, const std::string& expected, const std::string& actual)
198 {
199 if ( expected != actual )
200 {
201 std::string message = msg;
202 message += ": actual: ";
203 message += actual;
204 message += "\n expected: ";
205 message += expected;
206 message += "\n";
207 ensure(message, false);
208 }
209 }
210
211 void set_whitelist(LLMediaEntry &entry, const char *str)
212 {
213 std::vector<std::string> tokens;
214 LLStringUtil::getTokens(std::string(str), tokens, ",");
215 entry.setWhiteList(tokens);
216 }
217
218 void whitelist_test(int num, bool enable, const char *whitelist, const char *candidate_url, bool expected_pass)
219 {
220 std::string message = "Whitelist test " + boost::lexical_cast<std::string>(num);
221 LLMediaEntry entry;
222 entry.setWhiteListEnable(enable);
223 set_whitelist(entry, whitelist);
224 bool passed_whitelist = entry.checkCandidateUrl(candidate_url);
225 if (passed_whitelist != expected_pass)
226 {
227 message += " failed: expected ";
228 message += (expected_pass) ? "" : "NOT ";
229 message += "to match\nwhitelist = ";
230 message += whitelist;
231 message += "\ncandidate_url = ";
232 message += candidate_url;
233 }
234 ensure(message, expected_pass == passed_whitelist);
235 }
236
237 void whitelist_test(int num, const char *whitelist, const char *candidate_url, bool expected_pass)
238 {
239 whitelist_test(num, true, whitelist, candidate_url, expected_pass);
240 }
241 void whitelist_test(int num, const char *whitelist, const char *candidate_url)
242 {
243 whitelist_test(num, true, whitelist, candidate_url, true);
244 }
245
246 template<> template<>
247 void object::test<1>()
248 {
249 set_test_name("Test LLMediaEntry Instantiation");
250 LLMediaEntry entry;
251 ensure_llsd_equals(get_test_name() + " failed", defaultMediaEntryLLSD, entry.asLLSD());
252 }
253
254 template<> template<>
255 void object::test<2>()
256 {
257 set_test_name("Test LLMediaEntry Instantiation from LLSD");
258 LLMediaEntry entry;
259 LLSD sd;
260 entry.fromLLSD(sd);
261 ensure_llsd_equals(get_test_name() + " failed", emptyMediaEntryLLSD, entry.asLLSD());
262 }
263
264 template<> template<>
265 void object::test<3>()
266 {
267 set_test_name("Test LLMediaEntry Partial Instantiation from LLSD");
268 LLMediaEntry entry;
269 LLSD sd;
270 sd[LLMediaEntry::CURRENT_URL_KEY] = "http://www.example.com";
271 entry.fromLLSD(sd);
272 LLSD golden;
273 std::istringstream p(PARTIAL_MEDIA_ENTRY("http://www.example.com"));
274 LLSDSerialize::fromXML(golden,p);
275 ensure_llsd_equals(get_test_name() + " failed", golden, entry.asLLSD());
276 }
277
278 template<> template<>
279 void object::test<4>()
280 {
281 set_test_name("Test LLMediaEntry::asLLSD()");
282 LLMediaEntry entry;
283 LLSD sd;
284 // Put some cruft in the LLSD
285 sd[LLMediaEntry::CURRENT_URL_KEY] = "http://www.example.com";
286 LLSD whitelist;
287 whitelist.append("*.example.com");
288 sd[LLMediaEntry::WHITELIST_KEY] = whitelist;
289 entry.asLLSD(sd);
290 ensure_llsd_equals(get_test_name() + " failed", defaultMediaEntryLLSD, sd);
291 }
292
293
294 template<> template<>
295 void object::test<5>()
296 {
297 set_test_name("Test LLMediaEntry::asLLSD() -> LLMediaEntry::fromLLSD()");
298 LLMediaEntry entry1, entry2;
299 // Add a whitelist to entry2
300 std::vector<std::string> whitelist;
301 whitelist.push_back("*.example.com");
302 entry2.setWhiteList(whitelist);
303 // Render entry1 (which has no whitelist) as an LLSD
304 LLSD sd;
305 entry1.asLLSD(sd);
306 // "read" that LLSD into entry 2
307 entry2.fromLLSD(sd);
308 ensure_llsd_equals(get_test_name() + " failed", defaultMediaEntryLLSD, entry2.asLLSD());
309 }
310
311 // limit tests
312 const char *URL_OK = "http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qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";
314
315 template<> template<>
316 void object::test<6>()
317 {
318 set_test_name("Test Limits on setting current URL");
319 LLMediaEntry entry;
320 U32 status = entry.setCurrentURL(URL_OK);
321 ensure(get_test_name() + " ok failed", status == LSL_STATUS_OK);
322 status = entry.setCurrentURL(URL_TOO_BIG);
323 ensure(get_test_name() + " ok failed", status == LSL_STATUS_BOUNDS_ERROR);
324 }
325
326 template<> template<>
327 void object::test<7>()
328 {
329 set_test_name("Test Limits on setting home URL");
330 LLMediaEntry entry;
331 U32 status = entry.setHomeURL(URL_OK);
332 ensure(get_test_name() + " ok failed", status == LSL_STATUS_OK);
333 status = entry.setHomeURL(URL_TOO_BIG);
334 ensure(get_test_name() + " ok failed", status == LSL_STATUS_BOUNDS_ERROR);
335 }
336
337 template<> template<>
338 void object::test<8>()
339 {
340 set_test_name("Test Limits on setting whitelist");
341
342 // Test a valid list
343 LLMediaEntry entry;
344 std::vector<std::string> whitelist;
345 whitelist.push_back(std::string(URL_OK));
346 S32 status = entry.setWhiteList(whitelist);
347 ensure(get_test_name() + " invalid result", status == LSL_STATUS_OK);
348 ensure(get_test_name() + " failed", whitelist == entry.getWhiteList());
349 }
350
351 template<> template<>
352 void object::test<9>()
353 {
354 set_test_name("Test Limits on setting whitelist too big");
355
356 // Test an invalid list
357 LLMediaEntry entry;
358 std::vector<std::string> whitelist, empty;
359 whitelist.push_back(std::string(URL_OK));
360 whitelist.push_back(std::string(URL_TOO_BIG));
361 S32 status = entry.setWhiteList(whitelist);
362 ensure(get_test_name() + " invalid result", status == LSL_STATUS_BOUNDS_ERROR);
363 ensure(get_test_name() + " failed", empty == entry.getWhiteList());
364 }
365
366 template<> template<>
367 void object::test<10>()
368 {
369 set_test_name("Test Limits on setting whitelist too many");
370
371 // Test an invalid list
372 LLMediaEntry entry;
373 std::vector<std::string> whitelist, empty;
374 for (int i=0; i < LLMediaEntry::MAX_WHITELIST_SIZE+1; i++) {
375 whitelist.push_back("Q");
376 }
377 S32 status = entry.setWhiteList(whitelist);
378 ensure(get_test_name() + " invalid result", status == LSL_STATUS_BOUNDS_ERROR);
379 ensure(get_test_name() + " failed", empty == entry.getWhiteList());
380 }
381
382 template<> template<>
383 void object::test<11>()
384 {
385 set_test_name("Test to make sure both setWhiteList() functions behave the same");
386
387 // Test a valid list
388 std::vector<std::string> whitelist, empty;
389 LLSD whitelist_llsd;
390 whitelist.push_back(std::string(URL_OK));
391 whitelist_llsd.append(std::string(URL_OK));
392 LLMediaEntry entry1, entry2;
393 ensure(get_test_name() + " setWhiteList(s) don't match",
394 entry1.setWhiteList(whitelist) == LSL_STATUS_OK &&
395 entry2.setWhiteList(whitelist_llsd)== LSL_STATUS_OK );
396 ensure(get_test_name() + " failed",
397 entry1.getWhiteList() == entry2.getWhiteList());
398 }
399
400 template<> template<>
401 void object::test<12>()
402 {
403 set_test_name("Test to make sure both setWhiteList() functions behave the same");
404
405 // Test an invalid list
406 std::vector<std::string> whitelist, empty;
407 LLSD whitelist_llsd;
408 whitelist.push_back(std::string(URL_OK));
409 whitelist.push_back(std::string(URL_TOO_BIG));
410 whitelist_llsd.append(std::string(URL_OK));
411 whitelist_llsd.append(std::string(URL_TOO_BIG));
412 LLMediaEntry entry1, entry2;
413 ensure(get_test_name() + " setWhiteList(s) don't match",
414 entry1.setWhiteList(whitelist) == LSL_STATUS_BOUNDS_ERROR &&
415 entry2.setWhiteList(whitelist_llsd) == LSL_STATUS_BOUNDS_ERROR);
416 ensure(get_test_name() + " failed",
417 empty == entry1.getWhiteList() &&
418 empty == entry2.getWhiteList());
419 }
420
421 template<> template<>
422 void object::test<13>()
423 {
424 set_test_name("Test to make sure both setWhiteList() functions behave the same");
425
426 // Test an invalid list, too many
427 std::vector<std::string> whitelist, empty;
428 LLSD whitelist_llsd;
429 for (int i=0; i < LLMediaEntry::MAX_WHITELIST_SIZE+1; i++) {
430 whitelist.push_back("Q");
431 whitelist_llsd.append("Q");
432 }
433 LLMediaEntry entry1, entry2;
434 ensure(get_test_name() + " invalid result",
435 entry1.setWhiteList(whitelist) == LSL_STATUS_BOUNDS_ERROR &&
436 entry2.setWhiteList(whitelist_llsd) == LSL_STATUS_BOUNDS_ERROR);
437 ensure(get_test_name() + " failed",
438 empty == entry1.getWhiteList() &&
439 empty == entry2.getWhiteList());
440 }
441
442 template<> template<>
443 void object::test<14>()
444 {
445 // Whitelist check tests
446 int n=0;
447
448 // Check the "empty whitelist" case
449 whitelist_test(++n, "", "http://www.example.com", true);
450
451 // Check the "missing scheme" case
452 whitelist_test(++n, "www.example.com", "http://www.example.com", true);
453
454 // Check the "exactly the same" case
455 whitelist_test(++n, "http://example.com", "http://example.com", true);
456
457 // Check the enable flag
458 whitelist_test(++n, false, "www.example.com", "http://www.secondlife.com", true);
459 whitelist_test(++n, true, "www.example.com", "http://www.secondlife.com", false);
460
461 // Check permutations of trailing slash:
462 whitelist_test(++n, "http://www.example.com", "http://www.example.com/", true);
463 whitelist_test(++n, "http://www.example.com/", "http://www.example.com/", true);
464 whitelist_test(++n, "http://www.example.com/", "http://www.example.com", false);
465 whitelist_test(++n, "http://www.example.com", "http://www.example.com/foobar", true);
466 whitelist_test(++n, "http://www.example.com/", "http://www.example.com/foobar", false);
467
468
469 // More cases...
470 whitelist_test(++n, "http://example.com", "http://example.com/wiki", true);
471 whitelist_test(++n, "www.example.com", "http://www.example.com/help", true);
472 whitelist_test(++n, "http://www.example.com", "http://wwwexample.com", false);
473 whitelist_test(++n, "http://www.example.com", "http://www.example.com/wiki", true);
474 whitelist_test(++n, "example.com", "http://wwwexample.com", false);
475 whitelist_test(++n, "http://www.example.com/", "http://www.amazon.com/wiki", false);
476 whitelist_test(++n, "www.example.com", "http://www.amazon.com", false);
477
478 // regexp cases
479 whitelist_test(++n, "*.example.com", "http://www.example.com", true);
480 whitelist_test(++n, "*.example.com", "http://www.amazon.com", false);
481 whitelist_test(++n, "*.example.com", "http://www.example.com/foo/bar", true);
482 whitelist_test(++n, "*.example.com", "http:/example.com/foo/bar", false);
483 whitelist_test(++n, "*example.com", "http://example.com/foo/bar", true);
484 whitelist_test(++n, "*example.com", "http://my.virus.com/foo/bar?example.com", false);
485 whitelist_test(++n, "example.com", "http://my.virus.com/foo/bar?example.com", false);
486 whitelist_test(++n, "*example.com", "http://my.virus.com/foo/bar?*example.com", false);
487 whitelist_test(++n, "http://*example.com", "http://www.example.com", true);
488 whitelist_test(++n, "http://*.example.com", "http://www.example.com", true);
489 whitelist_test(++n, "http://*.e$?^.com", "http://www.e$?^.com", true);
490 whitelist_test(++n, "*.example.com/foo/bar", "http://www.example.com/", false);
491 whitelist_test(++n, "*.example.com/foo/bar", "http://example.com/foo/bar", false);
492 whitelist_test(++n, "http://*.example.com/foo/bar", "http://www.example.com", false);
493 whitelist_test(++n, "http://*.example.com", "https://www.example.com", false);
494 whitelist_test(++n, "http*://*.example.com", "rtsp://www.example.com", false);
495 whitelist_test(++n, "http*://*.example.com", "https://www.example.com", true);
496 whitelist_test(++n, "example.com", "http://www.example.com", false);
497 whitelist_test(++n, "www.example.com", "http://www.example.com:80", false);
498 whitelist_test(++n, "www.example.com", "http://www.example.com", true);
499 whitelist_test(++n, "www.example.com/", "http://www.example.com", false);
500 whitelist_test(++n, "www.example.com/foo/bar/*", "http://www.example.com/foo/bar/baz", true);
501
502 // Path only
503 whitelist_test(++n, "/foo/*/baz", "http://www.example.com/foo/bar/baz", true);
504 whitelist_test(++n, "/foo/*/baz", "http://www.example.com/foo/bar/", false);
505 }
506
507}
508