OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
InventoryArchiveSaveTests.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Reflection;
32 using System.Threading;
33 using NUnit.Framework;
34 using OpenMetaverse;
35 using OpenSim.Data;
36 using OpenSim.Framework;
37 using OpenSim.Framework.Serialization;
38 using OpenSim.Framework.Serialization.External;
39 using OpenSim.Region.CoreModules.Avatar.Inventory.Archiver;
40 using OpenSim.Region.CoreModules.World.Serialiser;
41 using OpenSim.Region.Framework.Scenes;
42 using OpenSim.Region.Framework.Scenes.Serialization;
43 using OpenSim.Services.Interfaces;
44 using OpenSim.Tests.Common;
45 
46 namespace OpenSim.Region.CoreModules.Avatar.Inventory.Archiver.Tests
47 {
48  [TestFixture]
50  {
51  protected TestScene m_scene;
53 
54  [SetUp]
55  public override void SetUp()
56  {
57  base.SetUp();
58 
59  SerialiserModule serialiserModule = new SerialiserModule();
60  m_archiverModule = new InventoryArchiverModule();
61 
62  m_scene = new SceneHelpers().SetupScene();
63  SceneHelpers.SetupSceneModules(m_scene, serialiserModule, m_archiverModule);
64  }
65 
72  [Test]
73  public void TestOrder()
74  {
75  TestHelpers.InMethod();
76 // log4net.Config.XmlConfigurator.Configure();
77 
78  MemoryStream archiveReadStream = new MemoryStream(m_iarStreamBytes);
79  TarArchiveReader tar = new TarArchiveReader(archiveReadStream);
80  string filePath;
81  TarArchiveReader.TarEntryType tarEntryType;
82 
83  byte[] data = tar.ReadEntry(out filePath, out tarEntryType);
84  Assert.That(filePath, Is.EqualTo(ArchiveConstants.CONTROL_FILE_PATH));
85 
87  = new InventoryArchiveReadRequest(UUID.Random(), null, null, null, null, null, null, (Stream)null, false);
88  iarr.LoadControlFile(filePath, data);
89 
90  Assert.That(iarr.ControlFileLoaded, Is.True);
91  }
92 
93  [Test]
95  {
96  TestHelpers.InMethod();
97 // TestHelpers.EnableLogging();
98 
99  string userFirstName = "Jock";
100  string userLastName = "Stirrup";
101  string userPassword = "troll";
102  UUID userId = TestHelpers.ParseTail(0x20);
103 
104  UserAccountHelpers.CreateUserWithInventory(m_scene, userFirstName, userLastName, userId, userPassword);
105 
106  MemoryStream archiveWriteStream = new MemoryStream();
107  m_archiverModule.OnInventoryArchiveSaved += SaveCompleted;
108 
109  mre.Reset();
110  m_archiverModule.ArchiveInventory(
111  UUID.Random(), userFirstName, userLastName, "/", userPassword, archiveWriteStream);
112  mre.WaitOne(60000, false);
113 
114  // Test created iar
115  byte[] archive = archiveWriteStream.ToArray();
116  MemoryStream archiveReadStream = new MemoryStream(archive);
117  TarArchiveReader tar = new TarArchiveReader(archiveReadStream);
118 
119 // InventoryArchiveUtils.
120  bool gotObjectsFolder = false;
121 
122  string objectsFolderName
123  = string.Format(
124  "{0}{1}",
125  ArchiveConstants.INVENTORY_PATH,
126  InventoryArchiveWriteRequest.CreateArchiveFolderName(
127  UserInventoryHelpers.GetInventoryFolder(m_scene.InventoryService, userId, "Objects")));
128 
129  string filePath;
130  TarArchiveReader.TarEntryType tarEntryType;
131 
132  while (tar.ReadEntry(out filePath, out tarEntryType) != null)
133  {
134 // Console.WriteLine("Got {0}", filePath);
135 
136  // Lazily, we only bother to look for the system objects folder created when we call CreateUserWithInventory()
137  // XXX: But really we need to stop all that stuff being created in tests or check for such folders
138  // more thoroughly
139  if (filePath == objectsFolderName)
140  gotObjectsFolder = true;
141  }
142 
143  Assert.That(gotObjectsFolder, Is.True);
144  }
145 
146  [Test]
148  {
149  TestHelpers.InMethod();
150 // TestHelpers.EnableLogging();
151 
152  string userFirstName = "Jock";
153  string userLastName = "Stirrup";
154  string userPassword = "troll";
155  UUID userId = TestHelpers.ParseTail(0x20);
156 
157  UserAccountHelpers.CreateUserWithInventory(m_scene, userFirstName, userLastName, userId, userPassword);
158 
159  // Create base folder
161  = UserInventoryHelpers.CreateInventoryFolder(m_scene.InventoryService, userId, "f1", true);
162 
163  // Create item1
164  SceneObjectGroup so1 = SceneHelpers.CreateSceneObject(1, userId, "My Little Dog Object", 0x5);
165  InventoryItemBase i1 = UserInventoryHelpers.AddInventoryItem(m_scene, so1, 0x50, 0x60, "f1");
166 
167  // Create embedded folder
168  InventoryFolderBase f1_1
169  = UserInventoryHelpers.CreateInventoryFolder(m_scene.InventoryService, userId, "f1/f1.1", true);
170 
171  // Create embedded item
172  SceneObjectGroup so1_1 = SceneHelpers.CreateSceneObject(1, userId, "My Little Cat Object", 0x6);
173  InventoryItemBase i2 = UserInventoryHelpers.AddInventoryItem(m_scene, so1_1, 0x500, 0x600, "f1/f1.1");
174 
175  MemoryStream archiveWriteStream = new MemoryStream();
176  m_archiverModule.OnInventoryArchiveSaved += SaveCompleted;
177 
178  mre.Reset();
179  m_archiverModule.ArchiveInventory(
180  UUID.Random(), userFirstName, userLastName, "f1", userPassword, archiveWriteStream);
181  mre.WaitOne(60000, false);
182 
183  // Test created iar
184  byte[] archive = archiveWriteStream.ToArray();
185  MemoryStream archiveReadStream = new MemoryStream(archive);
186  TarArchiveReader tar = new TarArchiveReader(archiveReadStream);
187 
188 // InventoryArchiveUtils.
189  bool gotf1 = false, gotf1_1 = false, gotso1 = false, gotso2 = false;
190 
191  string f1FileName
192  = string.Format("{0}{1}", ArchiveConstants.INVENTORY_PATH, InventoryArchiveWriteRequest.CreateArchiveFolderName(f1));
193  string f1_1FileName
194  = string.Format("{0}{1}", f1FileName, InventoryArchiveWriteRequest.CreateArchiveFolderName(f1_1));
195  string so1FileName
196  = string.Format("{0}{1}", f1FileName, InventoryArchiveWriteRequest.CreateArchiveItemName(i1));
197  string so2FileName
198  = string.Format("{0}{1}", f1_1FileName, InventoryArchiveWriteRequest.CreateArchiveItemName(i2));
199 
200  string filePath;
201  TarArchiveReader.TarEntryType tarEntryType;
202 
203  while (tar.ReadEntry(out filePath, out tarEntryType) != null)
204  {
205 // Console.WriteLine("Got {0}", filePath);
206 
207  if (filePath == f1FileName)
208  gotf1 = true;
209  else if (filePath == f1_1FileName)
210  gotf1_1 = true;
211  else if (filePath == so1FileName)
212  gotso1 = true;
213  else if (filePath == so2FileName)
214  gotso2 = true;
215  }
216 
217 // Assert.That(gotControlFile, Is.True, "No control file in archive");
218  Assert.That(gotf1, Is.True);
219  Assert.That(gotf1_1, Is.True);
220  Assert.That(gotso1, Is.True);
221  Assert.That(gotso2, Is.True);
222 
223  // TODO: Test presence of more files and contents of files.
224  }
225 
230  [Test]
231  public void TestSaveItemToIar()
232  {
233  TestHelpers.InMethod();
234 // log4net.Config.XmlConfigurator.Configure();
235 
236  // Create user
237  string userFirstName = "Jock";
238  string userLastName = "Stirrup";
239  string userPassword = "troll";
240  UUID userId = UUID.Parse("00000000-0000-0000-0000-000000000020");
241  UserAccountHelpers.CreateUserWithInventory(m_scene, userFirstName, userLastName, userId, userPassword);
242 
243  // Create asset
244  UUID ownerId = UUID.Parse("00000000-0000-0000-0000-000000000040");
245  SceneObjectGroup object1 = SceneHelpers.CreateSceneObject(1, ownerId, "My Little Dog Object", 0x50);
246 
247  UUID asset1Id = UUID.Parse("00000000-0000-0000-0000-000000000060");
248  AssetBase asset1 = AssetHelpers.CreateAsset(asset1Id, object1);
249  m_scene.AssetService.Store(asset1);
250 
251  // Create item
252  UUID item1Id = UUID.Parse("00000000-0000-0000-0000-000000000080");
253  string item1Name = "My Little Dog";
254  InventoryItemBase item1 = new InventoryItemBase();
255  item1.Name = item1Name;
256  item1.AssetID = asset1.FullID;
257  item1.ID = item1Id;
258  InventoryFolderBase objsFolder
259  = InventoryArchiveUtils.FindFoldersByPath(m_scene.InventoryService, userId, "Objects")[0];
260  item1.Folder = objsFolder.ID;
261  m_scene.AddInventoryItem(item1);
262 
263  MemoryStream archiveWriteStream = new MemoryStream();
264  m_archiverModule.OnInventoryArchiveSaved += SaveCompleted;
265 
266  mre.Reset();
267  m_archiverModule.ArchiveInventory(
268  UUID.Random(), userFirstName, userLastName, "Objects/" + item1Name, userPassword, archiveWriteStream);
269  mre.WaitOne(60000, false);
270 
271  byte[] archive = archiveWriteStream.ToArray();
272  MemoryStream archiveReadStream = new MemoryStream(archive);
273  TarArchiveReader tar = new TarArchiveReader(archiveReadStream);
274 
275  //bool gotControlFile = false;
276  bool gotObject1File = false;
277  //bool gotObject2File = false;
278  string expectedObject1FileName = InventoryArchiveWriteRequest.CreateArchiveItemName(item1);
279  string expectedObject1FilePath = string.Format(
280  "{0}{1}",
281  ArchiveConstants.INVENTORY_PATH,
282  expectedObject1FileName);
283 
284  string filePath;
285  TarArchiveReader.TarEntryType tarEntryType;
286 
287 // Console.WriteLine("Reading archive");
288 
289  while (tar.ReadEntry(out filePath, out tarEntryType) != null)
290  {
291  Console.WriteLine("Got {0}", filePath);
292 
293 // if (ArchiveConstants.CONTROL_FILE_PATH == filePath)
294 // {
295 // gotControlFile = true;
296 // }
297 
298  if (filePath.StartsWith(ArchiveConstants.INVENTORY_PATH) && filePath.EndsWith(".xml"))
299  {
300 // string fileName = filePath.Remove(0, "Objects/".Length);
301 //
302 // if (fileName.StartsWith(part1.Name))
303 // {
304  Assert.That(expectedObject1FilePath, Is.EqualTo(filePath));
305  gotObject1File = true;
306 // }
307 // else if (fileName.StartsWith(part2.Name))
308 // {
309 // Assert.That(fileName, Is.EqualTo(expectedObject2FileName));
310 // gotObject2File = true;
311 // }
312  }
313  }
314 
315 // Assert.That(gotControlFile, Is.True, "No control file in archive");
316  Assert.That(gotObject1File, Is.True, "No item1 file in archive");
317 // Assert.That(gotObject2File, Is.True, "No object2 file in archive");
318 
319  // TODO: Test presence of more files and contents of files.
320  }
321 
325  [Test]
327  {
328  TestHelpers.InMethod();
329 // log4net.Config.XmlConfigurator.Configure();
330 
331  // Create user
332  string userFirstName = "Jock";
333  string userLastName = "Stirrup";
334  string userPassword = "troll";
335  UUID userId = UUID.Parse("00000000-0000-0000-0000-000000000020");
336  UserAccountHelpers.CreateUserWithInventory(m_scene, userFirstName, userLastName, userId, userPassword);
337 
338  // Create asset
339  UUID ownerId = UUID.Parse("00000000-0000-0000-0000-000000000040");
340  SceneObjectGroup object1 = SceneHelpers.CreateSceneObject(1, ownerId, "My Little Dog Object", 0x50);
341 
342  UUID asset1Id = UUID.Parse("00000000-0000-0000-0000-000000000060");
343  AssetBase asset1 = AssetHelpers.CreateAsset(asset1Id, object1);
344  m_scene.AssetService.Store(asset1);
345 
346  // Create item
347  UUID item1Id = UUID.Parse("00000000-0000-0000-0000-000000000080");
348  string item1Name = "My Little Dog";
349  InventoryItemBase item1 = new InventoryItemBase();
350  item1.Name = item1Name;
351  item1.AssetID = asset1.FullID;
352  item1.ID = item1Id;
353  InventoryFolderBase objsFolder
354  = InventoryArchiveUtils.FindFoldersByPath(m_scene.InventoryService, userId, "Objects")[0];
355  item1.Folder = objsFolder.ID;
356  m_scene.AddInventoryItem(item1);
357 
358  MemoryStream archiveWriteStream = new MemoryStream();
359 
360  Dictionary<string, Object> options = new Dictionary<string, Object>();
361  options.Add("noassets", true);
362 
363  // When we're not saving assets, archiving is being done synchronously.
364  m_archiverModule.ArchiveInventory(
365  UUID.Random(), userFirstName, userLastName, "Objects/" + item1Name, userPassword, archiveWriteStream, options);
366 
367  byte[] archive = archiveWriteStream.ToArray();
368  MemoryStream archiveReadStream = new MemoryStream(archive);
369  TarArchiveReader tar = new TarArchiveReader(archiveReadStream);
370 
371  //bool gotControlFile = false;
372  bool gotObject1File = false;
373  //bool gotObject2File = false;
374  string expectedObject1FileName = InventoryArchiveWriteRequest.CreateArchiveItemName(item1);
375  string expectedObject1FilePath = string.Format(
376  "{0}{1}",
377  ArchiveConstants.INVENTORY_PATH,
378  expectedObject1FileName);
379 
380  string filePath;
381  TarArchiveReader.TarEntryType tarEntryType;
382 
383 // Console.WriteLine("Reading archive");
384 
385  while (tar.ReadEntry(out filePath, out tarEntryType) != null)
386  {
387  Console.WriteLine("Got {0}", filePath);
388 
389 // if (ArchiveConstants.CONTROL_FILE_PATH == filePath)
390 // {
391 // gotControlFile = true;
392 // }
393 
394  if (filePath.StartsWith(ArchiveConstants.INVENTORY_PATH) && filePath.EndsWith(".xml"))
395  {
396 // string fileName = filePath.Remove(0, "Objects/".Length);
397 //
398 // if (fileName.StartsWith(part1.Name))
399 // {
400  Assert.That(expectedObject1FilePath, Is.EqualTo(filePath));
401  gotObject1File = true;
402 // }
403 // else if (fileName.StartsWith(part2.Name))
404 // {
405 // Assert.That(fileName, Is.EqualTo(expectedObject2FileName));
406 // gotObject2File = true;
407 // }
408  }
409  else if (filePath.StartsWith(ArchiveConstants.ASSETS_PATH))
410  {
411  Assert.Fail("Found asset path in TestSaveItemToIarNoAssets()");
412  }
413  }
414 
415 // Assert.That(gotControlFile, Is.True, "No control file in archive");
416  Assert.That(gotObject1File, Is.True, "No item1 file in archive");
417 // Assert.That(gotObject2File, Is.True, "No object2 file in archive");
418 
419  // TODO: Test presence of more files and contents of files.
420  }
421  }
422 }
Temporary code to do the bare minimum required to read a tar archive for our purposes ...
Constants for the archiving module
OpenSim.Framework.Serialization.TarArchiveReader TarArchiveReader
void TestSaveItemToIarNoAssets()
Test saving a single inventory item to an IAR without its asset
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
This module loads and saves OpenSimulator inventory archives
Inventory Item - contains all the properties associated with an individual inventory piece...
Helpers for setting up scenes.
Definition: SceneHelpers.cs:60
byte[] ReadEntry(out string filePath, out TarEntryType entryType)
Read the next entry in the tar file.
void TestSaveItemToIar()
Test saving a single inventory item to an IAR (subject to change since there is no fixed format yet)...
void TestOrder()
Test that the IAR has the required files in the right order.