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