PageRenderTime 38ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llinventory/tests/inventorymisc_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 508 lines | 374 code | 97 blank | 37 comment | 12 complexity | 3993924831943bfabf0dedf731759c03 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file inventory.cpp
  3. * @author Phoenix
  4. * @date 2005-11-15
  5. * @brief Functions for inventory test framework
  6. *
  7. * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #include "linden_common.h"
  29. #include "llsd.h"
  30. #include "../llinventory.h"
  31. #include "../test/lltut.h"
  32. #if LL_WINDOWS
  33. // disable unreachable code warnings
  34. #pragma warning(disable: 4702)
  35. #endif
  36. LLPointer<LLInventoryItem> create_random_inventory_item()
  37. {
  38. LLUUID item_id;
  39. item_id.generate();
  40. LLUUID parent_id;
  41. parent_id.generate();
  42. LLPermissions perm;
  43. LLUUID creator_id;
  44. creator_id.generate();
  45. LLUUID owner_id;
  46. owner_id.generate();
  47. LLUUID last_owner_id;
  48. last_owner_id.generate();
  49. LLUUID group_id;
  50. group_id.generate();
  51. perm.init(creator_id, owner_id, last_owner_id, group_id);
  52. perm.initMasks(PERM_ALL, PERM_ALL, PERM_COPY, PERM_COPY, PERM_MODIFY | PERM_COPY);
  53. LLUUID asset_id;
  54. asset_id.generate();
  55. S32 price = rand();
  56. LLSaleInfo sale_info(LLSaleInfo::FS_COPY, price);
  57. U32 flags = rand();
  58. S32 creation = time(NULL);
  59. LLPointer<LLInventoryItem> item = new LLInventoryItem(
  60. item_id,
  61. parent_id,
  62. perm,
  63. asset_id,
  64. LLAssetType::AT_OBJECT,
  65. LLInventoryType::IT_ATTACHMENT,
  66. std::string("Sample Object"),
  67. std::string("Used for Testing"),
  68. sale_info,
  69. flags,
  70. creation);
  71. return item;
  72. }
  73. LLPointer<LLInventoryCategory> create_random_inventory_cat()
  74. {
  75. LLUUID item_id;
  76. item_id.generate();
  77. LLUUID parent_id;
  78. parent_id.generate();
  79. LLPointer<LLInventoryCategory> cat = new LLInventoryCategory(
  80. item_id,
  81. parent_id,
  82. LLFolderType::FT_NONE,
  83. std::string("Sample category"));
  84. return cat;
  85. }
  86. namespace tut
  87. {
  88. struct inventory_data
  89. {
  90. };
  91. typedef test_group<inventory_data> inventory_test;
  92. typedef inventory_test::object inventory_object;
  93. tut::inventory_test inv("LLInventory");
  94. //***class LLInventoryType***//
  95. template<> template<>
  96. void inventory_object::test<1>()
  97. {
  98. LLInventoryType::EType retType = LLInventoryType::lookup(std::string("sound"));
  99. ensure("1.LLInventoryType::lookup(char*) failed", retType == LLInventoryType::IT_SOUND);
  100. retType = LLInventoryType::lookup(std::string("snapshot"));
  101. ensure("2.LLInventoryType::lookup(char*) failed", retType == LLInventoryType::IT_SNAPSHOT);
  102. }
  103. template<> template<>
  104. void inventory_object::test<2>()
  105. {
  106. static std::string retType = LLInventoryType::lookup(LLInventoryType::IT_CALLINGCARD);
  107. ensure("1.LLInventoryType::lookup(EType) failed", (retType == "callcard"));
  108. retType = LLInventoryType::lookup(LLInventoryType::IT_LANDMARK);
  109. ensure("2.LLInventoryType::lookup(EType) failed", (retType == "landmark"));
  110. }
  111. template<> template<>
  112. void inventory_object::test<3>()
  113. {
  114. static std::string retType = LLInventoryType::lookupHumanReadable(LLInventoryType::IT_CALLINGCARD);
  115. ensure("1.LLInventoryType::lookupHumanReadable(EType) failed", (retType == "calling card"));
  116. retType = LLInventoryType::lookupHumanReadable(LLInventoryType::IT_LANDMARK);
  117. ensure("2.LLInventoryType::lookupHumanReadable(EType) failed", (retType == "landmark"));
  118. }
  119. template<> template<>
  120. void inventory_object::test<4>()
  121. {
  122. static LLInventoryType::EType retType = LLInventoryType::defaultForAssetType(LLAssetType::AT_TEXTURE);
  123. ensure("1.LLInventoryType::defaultForAssetType(LLAssetType EType) failed", retType == LLInventoryType::IT_TEXTURE);
  124. retType = LLInventoryType::defaultForAssetType(LLAssetType::AT_LANDMARK);
  125. ensure("2.LLInventoryType::defaultForAssetType(LLAssetType EType) failed", retType == LLInventoryType::IT_LANDMARK);
  126. }
  127. //*****class LLInventoryItem*****//
  128. template<> template<>
  129. void inventory_object::test<5>()
  130. {
  131. LLPointer<LLInventoryItem> src = create_random_inventory_item();
  132. LLSD sd = ll_create_sd_from_inventory_item(src);
  133. //llinfos << "sd: " << *sd << llendl;
  134. LLPointer<LLInventoryItem> dst = new LLInventoryItem;
  135. bool successful_parse = dst->fromLLSD(sd);
  136. ensure_equals("0.LLInventoryItem::fromLLSD()", successful_parse, true);
  137. ensure_equals("1.item id::getUUID() failed", dst->getUUID(), src->getUUID());
  138. ensure_equals("2.parent::getParentUUID() failed", dst->getParentUUID(), src->getParentUUID());
  139. ensure_equals("3.name::getName() failed", dst->getName(), src->getName());
  140. ensure_equals("4.type::getType() failed", dst->getType(), src->getType());
  141. ensure_equals("5.permissions::getPermissions() failed", dst->getPermissions(), src->getPermissions());
  142. ensure_equals("6.description::getDescription() failed", dst->getDescription(), src->getDescription());
  143. ensure_equals("7.sale type::getSaleType() failed", dst->getSaleInfo().getSaleType(), src->getSaleInfo().getSaleType());
  144. ensure_equals("8.sale price::getSalePrice() failed", dst->getSaleInfo().getSalePrice(), src->getSaleInfo().getSalePrice());
  145. ensure_equals("9.asset id::getAssetUUID() failed", dst->getAssetUUID(), src->getAssetUUID());
  146. ensure_equals("10.inventory type::getInventoryType() failed", dst->getInventoryType(), src->getInventoryType());
  147. ensure_equals("11.flags::getFlags() failed", dst->getFlags(), src->getFlags());
  148. ensure_equals("12.creation::getCreationDate() failed", dst->getCreationDate(), src->getCreationDate());
  149. LLUUID new_item_id, new_parent_id;
  150. new_item_id.generate();
  151. src->setUUID(new_item_id);
  152. new_parent_id.generate();
  153. src->setParent(new_parent_id);
  154. std::string new_name = "LindenLab";
  155. src->rename(new_name);
  156. src->setType(LLAssetType::AT_SOUND);
  157. LLUUID new_asset_id;
  158. new_asset_id.generate();
  159. src->setAssetUUID(new_asset_id);
  160. std::string new_desc = "SecondLife Testing";
  161. src->setDescription(new_desc);
  162. S32 new_price = rand();
  163. LLSaleInfo new_sale_info(LLSaleInfo::FS_COPY, new_price);
  164. src->setSaleInfo(new_sale_info);
  165. U32 new_flags = rand();
  166. S32 new_creation = time(NULL);
  167. LLPermissions new_perm;
  168. LLUUID new_creator_id;
  169. new_creator_id.generate();
  170. LLUUID new_owner_id;
  171. new_owner_id.generate();
  172. LLUUID last_owner_id;
  173. last_owner_id.generate();
  174. LLUUID new_group_id;
  175. new_group_id.generate();
  176. new_perm.init(new_creator_id, new_owner_id, last_owner_id, new_group_id);
  177. new_perm.initMasks(PERM_ALL, PERM_ALL, PERM_COPY, PERM_COPY, PERM_MODIFY | PERM_COPY);
  178. src->setPermissions(new_perm);
  179. src->setInventoryType(LLInventoryType::IT_SOUND);
  180. src->setFlags(new_flags);
  181. src->setCreationDate(new_creation);
  182. sd = ll_create_sd_from_inventory_item(src);
  183. //llinfos << "sd: " << *sd << llendl;
  184. successful_parse = dst->fromLLSD(sd);
  185. ensure_equals("13.item id::getUUID() failed", dst->getUUID(), src->getUUID());
  186. ensure_equals("14.parent::getParentUUID() failed", dst->getParentUUID(), src->getParentUUID());
  187. ensure_equals("15.name::getName() failed", dst->getName(), src->getName());
  188. ensure_equals("16.type::getType() failed", dst->getType(), src->getType());
  189. ensure_equals("17.permissions::getPermissions() failed", dst->getPermissions(), src->getPermissions());
  190. ensure_equals("18.description::getDescription() failed", dst->getDescription(), src->getDescription());
  191. ensure_equals("19.sale type::getSaleType() failed type", dst->getSaleInfo().getSaleType(), src->getSaleInfo().getSaleType());
  192. ensure_equals("20.sale price::getSalePrice() failed price", dst->getSaleInfo().getSalePrice(), src->getSaleInfo().getSalePrice());
  193. ensure_equals("21.asset id::getAssetUUID() failed id", dst->getAssetUUID(), src->getAssetUUID());
  194. ensure_equals("22.inventory type::getInventoryType() failed type", dst->getInventoryType(), src->getInventoryType());
  195. ensure_equals("23.flags::getFlags() failed", dst->getFlags(), src->getFlags());
  196. ensure_equals("24.creation::getCreationDate() failed", dst->getCreationDate(), src->getCreationDate());
  197. }
  198. template<> template<>
  199. void inventory_object::test<6>()
  200. {
  201. LLPointer<LLInventoryItem> src = create_random_inventory_item();
  202. LLUUID new_item_id, new_parent_id;
  203. new_item_id.generate();
  204. src->setUUID(new_item_id);
  205. new_parent_id.generate();
  206. src->setParent(new_parent_id);
  207. std::string new_name = "LindenLab";
  208. src->rename(new_name);
  209. src->setType(LLAssetType::AT_SOUND);
  210. LLUUID new_asset_id;
  211. new_asset_id.generate();
  212. src->setAssetUUID(new_asset_id);
  213. std::string new_desc = "SecondLife Testing";
  214. src->setDescription(new_desc);
  215. S32 new_price = rand();
  216. LLSaleInfo new_sale_info(LLSaleInfo::FS_COPY, new_price);
  217. src->setSaleInfo(new_sale_info);
  218. U32 new_flags = rand();
  219. S32 new_creation = time(NULL);
  220. LLPermissions new_perm;
  221. LLUUID new_creator_id;
  222. new_creator_id.generate();
  223. LLUUID new_owner_id;
  224. new_owner_id.generate();
  225. LLUUID last_owner_id;
  226. last_owner_id.generate();
  227. LLUUID new_group_id;
  228. new_group_id.generate();
  229. new_perm.init(new_creator_id, new_owner_id, last_owner_id, new_group_id);
  230. new_perm.initMasks(PERM_ALL, PERM_ALL, PERM_COPY, PERM_COPY, PERM_MODIFY | PERM_COPY);
  231. src->setPermissions(new_perm);
  232. src->setInventoryType(LLInventoryType::IT_SOUND);
  233. src->setFlags(new_flags);
  234. src->setCreationDate(new_creation);
  235. // test a save/load cycle to LLSD and back again
  236. LLSD sd = ll_create_sd_from_inventory_item(src);
  237. LLPointer<LLInventoryItem> dst = new LLInventoryItem;
  238. bool successful_parse = dst->fromLLSD(sd);
  239. ensure_equals("0.LLInventoryItem::fromLLSD()", successful_parse, true);
  240. LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
  241. src1->copyItem(src);
  242. ensure_equals("1.item id::getUUID() failed", dst->getUUID(), src1->getUUID());
  243. ensure_equals("2.parent::getParentUUID() failed", dst->getParentUUID(), src1->getParentUUID());
  244. ensure_equals("3.name::getName() failed", dst->getName(), src1->getName());
  245. ensure_equals("4.type::getType() failed", dst->getType(), src1->getType());
  246. ensure_equals("5.permissions::getPermissions() failed", dst->getPermissions(), src1->getPermissions());
  247. ensure_equals("6.description::getDescription() failed", dst->getDescription(), src1->getDescription());
  248. ensure_equals("7.sale type::getSaleType() failed type", dst->getSaleInfo().getSaleType(), src1->getSaleInfo().getSaleType());
  249. ensure_equals("8.sale price::getSalePrice() failed price", dst->getSaleInfo().getSalePrice(), src1->getSaleInfo().getSalePrice());
  250. ensure_equals("9.asset id::getAssetUUID() failed id", dst->getAssetUUID(), src1->getAssetUUID());
  251. ensure_equals("10.inventory type::getInventoryType() failed type", dst->getInventoryType(), src1->getInventoryType());
  252. ensure_equals("11.flags::getFlags() failed", dst->getFlags(), src1->getFlags());
  253. ensure_equals("12.creation::getCreationDate() failed", dst->getCreationDate(), src1->getCreationDate());
  254. // quick test to make sure generateUUID() really works
  255. src1->generateUUID();
  256. ensure_not_equals("13.item id::generateUUID() failed", src->getUUID(), src1->getUUID());
  257. }
  258. template<> template<>
  259. void inventory_object::test<7>()
  260. {
  261. LLFILE* fp = LLFile::fopen("linden_file.dat","w+");
  262. if(!fp)
  263. {
  264. llerrs << "file could not be opened\n" << llendl;
  265. return;
  266. }
  267. LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
  268. src1->exportFile(fp, TRUE);
  269. fclose(fp);
  270. LLPointer<LLInventoryItem> src2 = new LLInventoryItem();
  271. fp = LLFile::fopen("linden_file.dat","r+");
  272. if(!fp)
  273. {
  274. llerrs << "file could not be opened\n" << llendl;
  275. return;
  276. }
  277. src2->importFile(fp);
  278. fclose(fp);
  279. ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
  280. ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
  281. ensure_equals("3.permissions::getPermissions() failed", src1->getPermissions(), src2->getPermissions());
  282. ensure_equals("4.sale price::getSalePrice() failed price", src1->getSaleInfo().getSalePrice(), src2->getSaleInfo().getSalePrice());
  283. ensure_equals("5.asset id::getAssetUUID() failed id", src1->getAssetUUID(), src2->getAssetUUID());
  284. ensure_equals("6.type::getType() failed", src1->getType(), src2->getType());
  285. ensure_equals("7.inventory type::getInventoryType() failed type", src1->getInventoryType(), src2->getInventoryType());
  286. ensure_equals("8.name::getName() failed", src1->getName(), src2->getName());
  287. ensure_equals("9.description::getDescription() failed", src1->getDescription(), src2->getDescription());
  288. ensure_equals("10.creation::getCreationDate() failed", src1->getCreationDate(), src2->getCreationDate());
  289. }
  290. template<> template<>
  291. void inventory_object::test<8>()
  292. {
  293. LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
  294. std::ostringstream ostream;
  295. src1->exportLegacyStream(ostream, TRUE);
  296. std::istringstream istream(ostream.str());
  297. LLPointer<LLInventoryItem> src2 = new LLInventoryItem();
  298. src2->importLegacyStream(istream);
  299. ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
  300. ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
  301. ensure_equals("3.permissions::getPermissions() failed", src1->getPermissions(), src2->getPermissions());
  302. ensure_equals("4.sale price::getSalePrice() failed price", src1->getSaleInfo().getSalePrice(), src2->getSaleInfo().getSalePrice());
  303. ensure_equals("5.asset id::getAssetUUID() failed id", src1->getAssetUUID(), src2->getAssetUUID());
  304. ensure_equals("6.type::getType() failed", src1->getType(), src2->getType());
  305. ensure_equals("7.inventory type::getInventoryType() failed type", src1->getInventoryType(), src2->getInventoryType());
  306. ensure_equals("8.name::getName() failed", src1->getName(), src2->getName());
  307. ensure_equals("9.description::getDescription() failed", src1->getDescription(), src2->getDescription());
  308. ensure_equals("10.creation::getCreationDate() failed", src1->getCreationDate(), src2->getCreationDate());
  309. }
  310. template<> template<>
  311. void inventory_object::test<9>()
  312. {
  313. // Deleted LLInventoryItem::exportFileXML() and LLInventoryItem::importXML()
  314. // because I can't find any non-test code references to it. 2009-05-04 JC
  315. }
  316. template<> template<>
  317. void inventory_object::test<10>()
  318. {
  319. LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
  320. U8* bin_bucket = new U8[300];
  321. S32 bin_bucket_size = src1->packBinaryBucket(bin_bucket, NULL);
  322. LLPointer<LLInventoryItem> src2 = new LLInventoryItem();
  323. src2->unpackBinaryBucket(bin_bucket, bin_bucket_size);
  324. ensure_equals("1.sale price::getSalePrice() failed price", src1->getSaleInfo().getSalePrice(), src2->getSaleInfo().getSalePrice());
  325. ensure_equals("2.sale type::getSaleType() failed type", src1->getSaleInfo().getSaleType(), src2->getSaleInfo().getSaleType());
  326. ensure_equals("3.type::getType() failed", src1->getType(), src2->getType());
  327. ensure_equals("4.inventory type::getInventoryType() failed type", src1->getInventoryType(), src2->getInventoryType());
  328. ensure_equals("5.name::getName() failed", src1->getName(), src2->getName());
  329. ensure_equals("6.description::getDescription() failed", src1->getDescription(), src2->getDescription());
  330. ensure_equals("7.flags::getFlags() failed", src1->getFlags(), src2->getFlags());
  331. }
  332. template<> template<>
  333. void inventory_object::test<11>()
  334. {
  335. LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
  336. LLSD retSd = src1->asLLSD();
  337. LLPointer<LLInventoryItem> src2 = new LLInventoryItem();
  338. src2->fromLLSD(retSd);
  339. ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
  340. ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
  341. ensure_equals("3.permissions::getPermissions() failed", src1->getPermissions(), src2->getPermissions());
  342. ensure_equals("4.asset id::getAssetUUID() failed id", src1->getAssetUUID(), src2->getAssetUUID());
  343. ensure_equals("5.type::getType() failed", src1->getType(), src2->getType());
  344. ensure_equals("6.inventory type::getInventoryType() failed type", src1->getInventoryType(), src2->getInventoryType());
  345. ensure_equals("7.flags::getFlags() failed", src1->getFlags(), src2->getFlags());
  346. ensure_equals("8.sale type::getSaleType() failed type", src1->getSaleInfo().getSaleType(), src2->getSaleInfo().getSaleType());
  347. ensure_equals("9.sale price::getSalePrice() failed price", src1->getSaleInfo().getSalePrice(), src2->getSaleInfo().getSalePrice());
  348. ensure_equals("10.name::getName() failed", src1->getName(), src2->getName());
  349. ensure_equals("11.description::getDescription() failed", src1->getDescription(), src2->getDescription());
  350. ensure_equals("12.creation::getCreationDate() failed", src1->getCreationDate(), src2->getCreationDate());
  351. }
  352. //******class LLInventoryCategory*******//
  353. template<> template<>
  354. void inventory_object::test<12>()
  355. {
  356. LLPointer<LLInventoryCategory> src = create_random_inventory_cat();
  357. LLSD sd = ll_create_sd_from_inventory_category(src);
  358. LLPointer<LLInventoryCategory> dst = ll_create_category_from_sd(sd);
  359. ensure_equals("1.item id::getUUID() failed", dst->getUUID(), src->getUUID());
  360. ensure_equals("2.parent::getParentUUID() failed", dst->getParentUUID(), src->getParentUUID());
  361. ensure_equals("3.name::getName() failed", dst->getName(), src->getName());
  362. ensure_equals("4.type::getType() failed", dst->getType(), src->getType());
  363. ensure_equals("5.preferred type::getPreferredType() failed", dst->getPreferredType(), src->getPreferredType());
  364. src->setPreferredType( LLFolderType::FT_TEXTURE);
  365. sd = ll_create_sd_from_inventory_category(src);
  366. dst = ll_create_category_from_sd(sd);
  367. ensure_equals("6.preferred type::getPreferredType() failed", dst->getPreferredType(), src->getPreferredType());
  368. }
  369. template<> template<>
  370. void inventory_object::test<13>()
  371. {
  372. LLFILE* fp = LLFile::fopen("linden_file.dat","w");
  373. if(!fp)
  374. {
  375. llerrs << "file coudnt be opened\n" << llendl;
  376. return;
  377. }
  378. LLPointer<LLInventoryCategory> src1 = create_random_inventory_cat();
  379. src1->exportFile(fp, TRUE);
  380. fclose(fp);
  381. LLPointer<LLInventoryCategory> src2 = new LLInventoryCategory();
  382. fp = LLFile::fopen("linden_file.dat","r");
  383. if(!fp)
  384. {
  385. llerrs << "file coudnt be opened\n" << llendl;
  386. return;
  387. }
  388. src2->importFile(fp);
  389. fclose(fp);
  390. ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
  391. ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
  392. ensure_equals("3.type::getType() failed", src1->getType(), src2->getType());
  393. ensure_equals("4.preferred type::getPreferredType() failed", src1->getPreferredType(), src2->getPreferredType());
  394. ensure_equals("5.name::getName() failed", src1->getName(), src2->getName());
  395. }
  396. template<> template<>
  397. void inventory_object::test<14>()
  398. {
  399. LLPointer<LLInventoryCategory> src1 = create_random_inventory_cat();
  400. std::ostringstream ostream;
  401. src1->exportLegacyStream(ostream, TRUE);
  402. std::istringstream istream(ostream.str());
  403. LLPointer<LLInventoryCategory> src2 = new LLInventoryCategory();
  404. src2->importLegacyStream(istream);
  405. ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
  406. ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
  407. ensure_equals("3.type::getType() failed", src1->getType(), src2->getType());
  408. ensure_equals("4.preferred type::getPreferredType() failed", src1->getPreferredType(), src2->getPreferredType());
  409. ensure_equals("5.name::getName() failed", src1->getName(), src2->getName());
  410. }
  411. }