/indra/llprimitive/tests/llmediaentry_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 502 lines · 406 code · 47 blank · 49 comment · 28 complexity · f9d206f2e84828a2ad12b0b9e5abeccf MD5 · raw file

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