PageRenderTime 82ms CodeModel.GetById 10ms app.highlight 66ms 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
  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
 29#include "linden_common.h"
 30#include "llsd.h"
 31
 32#include "../llinventory.h"
 33
 34#include "../test/lltut.h"
 35
 36
 37#if LL_WINDOWS
 38// disable unreachable code warnings
 39#pragma warning(disable: 4702)
 40#endif
 41
 42LLPointer<LLInventoryItem> create_random_inventory_item()
 43{
 44	LLUUID item_id;
 45	item_id.generate();
 46	LLUUID parent_id;
 47	parent_id.generate();
 48	LLPermissions perm;
 49	LLUUID creator_id;
 50	creator_id.generate();
 51	LLUUID owner_id;
 52	owner_id.generate();
 53	LLUUID last_owner_id;
 54	last_owner_id.generate();
 55	LLUUID group_id;
 56	group_id.generate();
 57	perm.init(creator_id, owner_id, last_owner_id, group_id);
 58	perm.initMasks(PERM_ALL, PERM_ALL, PERM_COPY, PERM_COPY, PERM_MODIFY | PERM_COPY);
 59	LLUUID asset_id;
 60	asset_id.generate();
 61	S32 price = rand();
 62	LLSaleInfo sale_info(LLSaleInfo::FS_COPY, price);
 63	U32 flags = rand();
 64	S32 creation = time(NULL);
 65
 66	LLPointer<LLInventoryItem> item = new LLInventoryItem(
 67		item_id,
 68		parent_id,
 69		perm,
 70		asset_id,
 71		LLAssetType::AT_OBJECT,
 72		LLInventoryType::IT_ATTACHMENT,
 73		std::string("Sample Object"),
 74		std::string("Used for Testing"),
 75		sale_info,
 76		flags,
 77		creation);
 78	return item;
 79}
 80
 81LLPointer<LLInventoryCategory> create_random_inventory_cat()
 82{
 83	LLUUID item_id;
 84	item_id.generate();
 85	LLUUID parent_id;
 86	parent_id.generate();
 87
 88	LLPointer<LLInventoryCategory> cat = new LLInventoryCategory(
 89		item_id,
 90		parent_id,
 91		LLFolderType::FT_NONE,
 92		std::string("Sample category"));
 93	return cat;
 94}
 95
 96namespace tut
 97{
 98	struct inventory_data
 99	{
100	};
101	typedef test_group<inventory_data> inventory_test;
102	typedef inventory_test::object inventory_object;
103	tut::inventory_test inv("LLInventory");
104
105//***class LLInventoryType***//
106
107
108	template<> template<>
109	void inventory_object::test<1>()
110	{
111		LLInventoryType::EType retType =  LLInventoryType::lookup(std::string("sound"));
112		ensure("1.LLInventoryType::lookup(char*) failed", retType == LLInventoryType::IT_SOUND);
113
114		retType = LLInventoryType::lookup(std::string("snapshot"));
115		ensure("2.LLInventoryType::lookup(char*) failed", retType == LLInventoryType::IT_SNAPSHOT);
116	}
117
118	template<> template<>
119	void inventory_object::test<2>()
120	{
121		static std::string retType =  LLInventoryType::lookup(LLInventoryType::IT_CALLINGCARD);
122		ensure("1.LLInventoryType::lookup(EType) failed", (retType == "callcard")); 
123
124		retType = LLInventoryType::lookup(LLInventoryType::IT_LANDMARK);
125		ensure("2.LLInventoryType::lookup(EType) failed", (retType == "landmark"));
126		
127	}
128
129	template<> template<>
130	void inventory_object::test<3>()
131	{
132		static std::string retType =  LLInventoryType::lookupHumanReadable(LLInventoryType::IT_CALLINGCARD);
133		ensure("1.LLInventoryType::lookupHumanReadable(EType) failed", (retType == "calling card")); 
134
135		retType =  LLInventoryType::lookupHumanReadable(LLInventoryType::IT_LANDMARK);
136		ensure("2.LLInventoryType::lookupHumanReadable(EType) failed", (retType == "landmark")); 
137	}
138
139	template<> template<>
140	void inventory_object::test<4>()
141	{
142		static LLInventoryType::EType retType =  LLInventoryType::defaultForAssetType(LLAssetType::AT_TEXTURE);
143		ensure("1.LLInventoryType::defaultForAssetType(LLAssetType EType) failed", retType == LLInventoryType::IT_TEXTURE);
144
145		retType =  LLInventoryType::defaultForAssetType(LLAssetType::AT_LANDMARK);
146		ensure("2.LLInventoryType::defaultForAssetType(LLAssetType EType) failed", retType == LLInventoryType::IT_LANDMARK);
147	}
148
149//*****class LLInventoryItem*****//
150
151	template<> template<>
152	void inventory_object::test<5>()
153	{
154		LLPointer<LLInventoryItem> src = create_random_inventory_item();
155		LLSD sd = ll_create_sd_from_inventory_item(src);
156		//llinfos << "sd: " << *sd << llendl;
157		LLPointer<LLInventoryItem> dst = new LLInventoryItem;
158		bool successful_parse = dst->fromLLSD(sd);
159		ensure_equals("0.LLInventoryItem::fromLLSD()", successful_parse, true);
160		ensure_equals("1.item id::getUUID() failed", dst->getUUID(), src->getUUID());
161		ensure_equals("2.parent::getParentUUID() failed", dst->getParentUUID(), src->getParentUUID());
162		ensure_equals("3.name::getName() failed", dst->getName(), src->getName());
163		ensure_equals("4.type::getType() failed", dst->getType(), src->getType());
164		
165		ensure_equals("5.permissions::getPermissions() failed", dst->getPermissions(), src->getPermissions());
166		ensure_equals("6.description::getDescription() failed", dst->getDescription(), src->getDescription());
167		ensure_equals("7.sale type::getSaleType() failed", dst->getSaleInfo().getSaleType(), src->getSaleInfo().getSaleType());
168		ensure_equals("8.sale price::getSalePrice() failed", dst->getSaleInfo().getSalePrice(), src->getSaleInfo().getSalePrice());
169		ensure_equals("9.asset id::getAssetUUID() failed", dst->getAssetUUID(), src->getAssetUUID());
170		ensure_equals("10.inventory type::getInventoryType() failed", dst->getInventoryType(), src->getInventoryType());
171		ensure_equals("11.flags::getFlags() failed", dst->getFlags(), src->getFlags());
172		ensure_equals("12.creation::getCreationDate() failed", dst->getCreationDate(), src->getCreationDate());
173
174		LLUUID new_item_id, new_parent_id;
175		new_item_id.generate();
176		src->setUUID(new_item_id);
177		
178		new_parent_id.generate();
179		src->setParent(new_parent_id);
180		
181		std::string new_name = "LindenLab";
182		src->rename(new_name);
183		
184		src->setType(LLAssetType::AT_SOUND);
185
186		LLUUID new_asset_id;
187		new_asset_id.generate();
188		
189		src->setAssetUUID(new_asset_id);
190		std::string new_desc = "SecondLife Testing";
191		src->setDescription(new_desc);
192		
193		S32 new_price = rand();
194		LLSaleInfo new_sale_info(LLSaleInfo::FS_COPY, new_price);
195		src->setSaleInfo(new_sale_info);
196
197		U32 new_flags = rand();
198		S32 new_creation = time(NULL);
199
200		LLPermissions new_perm;
201
202		LLUUID new_creator_id;
203		new_creator_id.generate();
204	
205		LLUUID new_owner_id;
206		new_owner_id.generate();
207
208		LLUUID last_owner_id;
209		last_owner_id.generate();
210
211		LLUUID new_group_id;
212		new_group_id.generate();
213		new_perm.init(new_creator_id, new_owner_id, last_owner_id, new_group_id);
214		new_perm.initMasks(PERM_ALL, PERM_ALL, PERM_COPY, PERM_COPY, PERM_MODIFY | PERM_COPY);
215		src->setPermissions(new_perm);
216
217		src->setInventoryType(LLInventoryType::IT_SOUND);
218		src->setFlags(new_flags);
219		src->setCreationDate(new_creation);
220
221		sd = ll_create_sd_from_inventory_item(src);
222		//llinfos << "sd: " << *sd << llendl;
223		successful_parse = dst->fromLLSD(sd);
224		ensure_equals("13.item id::getUUID() failed", dst->getUUID(), src->getUUID());
225		ensure_equals("14.parent::getParentUUID() failed", dst->getParentUUID(), src->getParentUUID());
226		ensure_equals("15.name::getName() failed", dst->getName(), src->getName());
227		ensure_equals("16.type::getType() failed", dst->getType(), src->getType());
228		
229		ensure_equals("17.permissions::getPermissions() failed", dst->getPermissions(), src->getPermissions());
230		ensure_equals("18.description::getDescription() failed", dst->getDescription(), src->getDescription());
231		ensure_equals("19.sale type::getSaleType() failed type", dst->getSaleInfo().getSaleType(), src->getSaleInfo().getSaleType());
232		ensure_equals("20.sale price::getSalePrice() failed price", dst->getSaleInfo().getSalePrice(), src->getSaleInfo().getSalePrice());
233		ensure_equals("21.asset id::getAssetUUID() failed id", dst->getAssetUUID(), src->getAssetUUID());
234		ensure_equals("22.inventory type::getInventoryType() failed type", dst->getInventoryType(), src->getInventoryType());
235		ensure_equals("23.flags::getFlags() failed", dst->getFlags(), src->getFlags());
236		ensure_equals("24.creation::getCreationDate() failed", dst->getCreationDate(), src->getCreationDate());
237		
238	}
239
240	template<> template<>
241	void inventory_object::test<6>()
242	{
243		LLPointer<LLInventoryItem> src = create_random_inventory_item();
244		
245		LLUUID new_item_id, new_parent_id;
246		new_item_id.generate();
247		src->setUUID(new_item_id);
248		
249		new_parent_id.generate();
250		src->setParent(new_parent_id);
251		
252		std::string new_name = "LindenLab";
253		src->rename(new_name);
254		
255		src->setType(LLAssetType::AT_SOUND);
256
257		LLUUID new_asset_id;
258		new_asset_id.generate();
259		
260		src->setAssetUUID(new_asset_id);
261		std::string new_desc = "SecondLife Testing";
262		src->setDescription(new_desc);
263		
264		S32 new_price = rand();
265		LLSaleInfo new_sale_info(LLSaleInfo::FS_COPY, new_price);
266		src->setSaleInfo(new_sale_info);
267
268		U32 new_flags = rand();
269		S32 new_creation = time(NULL);
270
271		LLPermissions new_perm;
272
273		LLUUID new_creator_id;
274		new_creator_id.generate();
275	
276		LLUUID new_owner_id;
277		new_owner_id.generate();
278
279		LLUUID last_owner_id;
280		last_owner_id.generate();
281
282		LLUUID new_group_id;
283		new_group_id.generate();
284		new_perm.init(new_creator_id, new_owner_id, last_owner_id, new_group_id);
285		new_perm.initMasks(PERM_ALL, PERM_ALL, PERM_COPY, PERM_COPY, PERM_MODIFY | PERM_COPY);
286		src->setPermissions(new_perm);
287
288		src->setInventoryType(LLInventoryType::IT_SOUND);
289		src->setFlags(new_flags);
290		src->setCreationDate(new_creation);
291
292		// test a save/load cycle to LLSD and back again
293		LLSD sd = ll_create_sd_from_inventory_item(src);
294		LLPointer<LLInventoryItem> dst = new LLInventoryItem;
295		bool successful_parse = dst->fromLLSD(sd);
296		ensure_equals("0.LLInventoryItem::fromLLSD()", successful_parse, true);
297
298		LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
299		src1->copyItem(src);
300		
301		ensure_equals("1.item id::getUUID() failed", dst->getUUID(), src1->getUUID());
302		ensure_equals("2.parent::getParentUUID() failed", dst->getParentUUID(), src1->getParentUUID());
303		ensure_equals("3.name::getName() failed", dst->getName(), src1->getName());
304		ensure_equals("4.type::getType() failed", dst->getType(), src1->getType());
305		
306		ensure_equals("5.permissions::getPermissions() failed", dst->getPermissions(), src1->getPermissions());
307		ensure_equals("6.description::getDescription() failed", dst->getDescription(), src1->getDescription());
308		ensure_equals("7.sale type::getSaleType() failed type", dst->getSaleInfo().getSaleType(), src1->getSaleInfo().getSaleType());
309		ensure_equals("8.sale price::getSalePrice() failed price", dst->getSaleInfo().getSalePrice(), src1->getSaleInfo().getSalePrice());
310		ensure_equals("9.asset id::getAssetUUID() failed id", dst->getAssetUUID(), src1->getAssetUUID());
311		ensure_equals("10.inventory type::getInventoryType() failed type", dst->getInventoryType(), src1->getInventoryType());
312		ensure_equals("11.flags::getFlags() failed", dst->getFlags(), src1->getFlags());
313		ensure_equals("12.creation::getCreationDate() failed", dst->getCreationDate(), src1->getCreationDate());
314
315		// quick test to make sure generateUUID() really works
316		src1->generateUUID();	
317		ensure_not_equals("13.item id::generateUUID() failed", src->getUUID(), src1->getUUID());
318	}
319
320	template<> template<>
321	void inventory_object::test<7>()
322	{
323		LLFILE* fp = LLFile::fopen("linden_file.dat","w+");
324		if(!fp)
325		{
326			llerrs << "file could not be opened\n" << llendl;
327			return;
328		}
329			
330		LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
331		src1->exportFile(fp, TRUE);
332		fclose(fp);
333
334		LLPointer<LLInventoryItem> src2 = new LLInventoryItem();	
335		fp = LLFile::fopen("linden_file.dat","r+");
336		if(!fp)
337		{
338			llerrs << "file could not be opened\n" << llendl;
339			return;
340		}
341		
342		src2->importFile(fp);
343		fclose(fp);
344		
345		ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
346		ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
347		ensure_equals("3.permissions::getPermissions() failed", src1->getPermissions(), src2->getPermissions());
348		ensure_equals("4.sale price::getSalePrice() failed price", src1->getSaleInfo().getSalePrice(), src2->getSaleInfo().getSalePrice());
349		ensure_equals("5.asset id::getAssetUUID() failed id", src1->getAssetUUID(), src2->getAssetUUID());
350		ensure_equals("6.type::getType() failed", src1->getType(), src2->getType());
351		ensure_equals("7.inventory type::getInventoryType() failed type", src1->getInventoryType(), src2->getInventoryType());
352		ensure_equals("8.name::getName() failed", src1->getName(), src2->getName());
353		ensure_equals("9.description::getDescription() failed", src1->getDescription(), src2->getDescription());				
354		ensure_equals("10.creation::getCreationDate() failed", src1->getCreationDate(), src2->getCreationDate());
355			
356	}
357
358	template<> template<>
359	void inventory_object::test<8>()
360	{
361			
362		LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
363
364		std::ostringstream ostream;
365		src1->exportLegacyStream(ostream, TRUE);
366		
367		std::istringstream istream(ostream.str());
368		LLPointer<LLInventoryItem> src2 = new LLInventoryItem();
369		src2->importLegacyStream(istream);
370					
371		ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
372		ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
373		ensure_equals("3.permissions::getPermissions() failed", src1->getPermissions(), src2->getPermissions());
374		ensure_equals("4.sale price::getSalePrice() failed price", src1->getSaleInfo().getSalePrice(), src2->getSaleInfo().getSalePrice());
375		ensure_equals("5.asset id::getAssetUUID() failed id", src1->getAssetUUID(), src2->getAssetUUID());
376		ensure_equals("6.type::getType() failed", src1->getType(), src2->getType());
377		ensure_equals("7.inventory type::getInventoryType() failed type", src1->getInventoryType(), src2->getInventoryType());
378		ensure_equals("8.name::getName() failed", src1->getName(), src2->getName());
379		ensure_equals("9.description::getDescription() failed", src1->getDescription(), src2->getDescription());				
380		ensure_equals("10.creation::getCreationDate() failed", src1->getCreationDate(), src2->getCreationDate());
381		
382		
383	}
384
385	template<> template<>
386	void inventory_object::test<9>()
387	{
388		// Deleted LLInventoryItem::exportFileXML() and LLInventoryItem::importXML()
389		// because I can't find any non-test code references to it. 2009-05-04 JC
390	}
391		
392	template<> template<>
393	void inventory_object::test<10>()
394	{
395		LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
396		U8* bin_bucket = new U8[300];
397		S32 bin_bucket_size = src1->packBinaryBucket(bin_bucket, NULL);
398
399		LLPointer<LLInventoryItem> src2 = new LLInventoryItem();
400		src2->unpackBinaryBucket(bin_bucket, bin_bucket_size);
401
402		ensure_equals("1.sale price::getSalePrice() failed price", src1->getSaleInfo().getSalePrice(), src2->getSaleInfo().getSalePrice());
403		ensure_equals("2.sale type::getSaleType() failed type", src1->getSaleInfo().getSaleType(), src2->getSaleInfo().getSaleType());
404		ensure_equals("3.type::getType() failed", src1->getType(), src2->getType());
405		ensure_equals("4.inventory type::getInventoryType() failed type", src1->getInventoryType(), src2->getInventoryType());
406		ensure_equals("5.name::getName() failed", src1->getName(), src2->getName());
407		ensure_equals("6.description::getDescription() failed", src1->getDescription(), src2->getDescription());				
408		ensure_equals("7.flags::getFlags() failed", src1->getFlags(), src2->getFlags());
409	
410	}
411	
412	template<> template<>
413	void inventory_object::test<11>()
414	{
415		LLPointer<LLInventoryItem> src1 = create_random_inventory_item();
416		LLSD retSd = src1->asLLSD();
417		LLPointer<LLInventoryItem> src2 = new LLInventoryItem();
418		src2->fromLLSD(retSd);
419
420		ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
421		ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
422		ensure_equals("3.permissions::getPermissions() failed", src1->getPermissions(), src2->getPermissions());
423		ensure_equals("4.asset id::getAssetUUID() failed id", src1->getAssetUUID(), src2->getAssetUUID());
424		ensure_equals("5.type::getType() failed", src1->getType(), src2->getType());
425		ensure_equals("6.inventory type::getInventoryType() failed type", src1->getInventoryType(), src2->getInventoryType());
426		ensure_equals("7.flags::getFlags() failed", src1->getFlags(), src2->getFlags());
427		ensure_equals("8.sale type::getSaleType() failed type", src1->getSaleInfo().getSaleType(), src2->getSaleInfo().getSaleType());
428		ensure_equals("9.sale price::getSalePrice() failed price", src1->getSaleInfo().getSalePrice(), src2->getSaleInfo().getSalePrice());
429		ensure_equals("10.name::getName() failed", src1->getName(), src2->getName());
430		ensure_equals("11.description::getDescription() failed", src1->getDescription(), src2->getDescription());				
431		ensure_equals("12.creation::getCreationDate() failed", src1->getCreationDate(), src2->getCreationDate());
432	}	
433
434//******class LLInventoryCategory*******//
435
436	template<> template<>
437	void inventory_object::test<12>()
438	{
439		LLPointer<LLInventoryCategory> src = create_random_inventory_cat();
440		LLSD sd = ll_create_sd_from_inventory_category(src);
441		LLPointer<LLInventoryCategory> dst = ll_create_category_from_sd(sd);
442		
443		ensure_equals("1.item id::getUUID() failed", dst->getUUID(), src->getUUID());
444		ensure_equals("2.parent::getParentUUID() failed", dst->getParentUUID(), src->getParentUUID());
445		ensure_equals("3.name::getName() failed", dst->getName(), src->getName());
446		ensure_equals("4.type::getType() failed", dst->getType(), src->getType());
447		ensure_equals("5.preferred type::getPreferredType() failed", dst->getPreferredType(), src->getPreferredType());
448
449		src->setPreferredType( LLFolderType::FT_TEXTURE);
450		sd = ll_create_sd_from_inventory_category(src);
451		dst = ll_create_category_from_sd(sd);
452		ensure_equals("6.preferred type::getPreferredType() failed", dst->getPreferredType(), src->getPreferredType());
453
454		
455	}
456
457	template<> template<>
458	void inventory_object::test<13>()
459	{
460		LLFILE* fp = LLFile::fopen("linden_file.dat","w");
461		if(!fp)
462		{
463			llerrs << "file coudnt be opened\n" << llendl;
464			return;
465		}
466			
467		LLPointer<LLInventoryCategory> src1 = create_random_inventory_cat();
468		src1->exportFile(fp, TRUE);
469		fclose(fp);
470
471		LLPointer<LLInventoryCategory> src2 = new LLInventoryCategory();	
472		fp = LLFile::fopen("linden_file.dat","r");
473		if(!fp)
474		{
475			llerrs << "file coudnt be opened\n" << llendl;
476			return;
477		}
478		
479		src2->importFile(fp);
480		fclose(fp);
481
482		ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
483		ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
484 		ensure_equals("3.type::getType() failed", src1->getType(), src2->getType());
485		ensure_equals("4.preferred type::getPreferredType() failed", src1->getPreferredType(), src2->getPreferredType());
486		ensure_equals("5.name::getName() failed", src1->getName(), src2->getName());
487	}
488
489	template<> template<>
490	void inventory_object::test<14>()
491	{
492		LLPointer<LLInventoryCategory> src1 = create_random_inventory_cat();
493
494		std::ostringstream ostream;
495		src1->exportLegacyStream(ostream, TRUE);
496
497		std::istringstream istream(ostream.str());
498		LLPointer<LLInventoryCategory> src2 = new LLInventoryCategory();
499		src2->importLegacyStream(istream);
500					
501		ensure_equals("1.item id::getUUID() failed", src1->getUUID(), src2->getUUID());
502		ensure_equals("2.parent::getParentUUID() failed", src1->getParentUUID(), src2->getParentUUID());
503		ensure_equals("3.type::getType() failed", src1->getType(), src2->getType());
504		ensure_equals("4.preferred type::getPreferredType() failed", src1->getPreferredType(), src2->getPreferredType());
505		ensure_equals("5.name::getName() failed", src1->getName(), src2->getName());
506			
507	}
508}