aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/Tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/Tests/BasicAssetTest.cs34
-rw-r--r--OpenSim/Data/Tests/BasicEstateTest.cs12
-rw-r--r--OpenSim/Data/Tests/BasicGridTest.cs20
-rw-r--r--OpenSim/Data/Tests/BasicInventoryTest.cs44
-rw-r--r--OpenSim/Data/Tests/BasicRegionTest.cs24
-rw-r--r--OpenSim/Data/Tests/BasicUserTest.cs12
-rw-r--r--OpenSim/Data/Tests/PropertyScrambler.cs159
-rw-r--r--OpenSim/Data/Tests/ScrambleForTesting.cs129
8 files changed, 210 insertions, 224 deletions
diff --git a/OpenSim/Data/Tests/BasicAssetTest.cs b/OpenSim/Data/Tests/BasicAssetTest.cs
index 23041ad..09131c1 100644
--- a/OpenSim/Data/Tests/BasicAssetTest.cs
+++ b/OpenSim/Data/Tests/BasicAssetTest.cs
@@ -69,18 +69,20 @@ namespace OpenSim.Data.Tests
69 AssetBase a1 = new AssetBase(uuid1, "asset one"); 69 AssetBase a1 = new AssetBase(uuid1, "asset one");
70 AssetBase a2 = new AssetBase(uuid2, "asset two"); 70 AssetBase a2 = new AssetBase(uuid2, "asset two");
71 AssetBase a3 = new AssetBase(uuid3, "asset three"); 71 AssetBase a3 = new AssetBase(uuid3, "asset three");
72
73 ScrambleForTesting.Scramble(a1);
74 ScrambleForTesting.Scramble(a2);
75 ScrambleForTesting.Scramble(a3);
76
77 a1.Data = asset1; 72 a1.Data = asset1;
78 a2.Data = asset1; 73 a2.Data = asset1;
79 a3.Data = asset1; 74 a3.Data = asset1;
80 75
81 a1.FullID = uuid1; 76 PropertyScrambler<AssetBase> scrambler = new PropertyScrambler<AssetBase>()
82 a2.FullID = uuid2; 77 .DontScramble(x => x.Data)
83 a3.FullID = uuid3; 78 .DontScramble(x => x.ID)
79 .DontScramble(x => x.FullID)
80 .DontScramble(x => x.Metadata.ID)
81 .DontScramble(x => x.Metadata.FullID);
82
83 scrambler.Scramble(a1);
84 scrambler.Scramble(a2);
85 scrambler.Scramble(a3);
84 86
85 db.CreateAsset(a1); 87 db.CreateAsset(a1);
86 db.CreateAsset(a2); 88 db.CreateAsset(a2);
@@ -95,17 +97,9 @@ namespace OpenSim.Data.Tests
95 AssetBase a3a = db.FetchAsset(uuid3); 97 AssetBase a3a = db.FetchAsset(uuid3);
96 Assert.That(a3a, Constraints.PropertyCompareConstraint(a3)); 98 Assert.That(a3a, Constraints.PropertyCompareConstraint(a3));
97 99
98 ScrambleForTesting.Scramble(a1a); 100 scrambler.Scramble(a1a);
99 ScrambleForTesting.Scramble(a2a); 101 scrambler.Scramble(a2a);
100 ScrambleForTesting.Scramble(a3a); 102 scrambler.Scramble(a3a);
101
102 a1a.Data = asset1;
103 a2a.Data = asset1;
104 a3a.Data = asset1;
105
106 a1a.FullID = uuid1;
107 a2a.FullID = uuid2;
108 a3a.FullID = uuid3;
109 103
110 db.UpdateAsset(a1a); 104 db.UpdateAsset(a1a);
111 db.UpdateAsset(a2a); 105 db.UpdateAsset(a2a);
diff --git a/OpenSim/Data/Tests/BasicEstateTest.cs b/OpenSim/Data/Tests/BasicEstateTest.cs
index b702e2a..284d066 100644
--- a/OpenSim/Data/Tests/BasicEstateTest.cs
+++ b/OpenSim/Data/Tests/BasicEstateTest.cs
@@ -40,7 +40,6 @@ namespace OpenSim.Data.Tests
40{ 40{
41 public class BasicEstateTest 41 public class BasicEstateTest
42 { 42 {
43 //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44 public IEstateDataStore db; 43 public IEstateDataStore db;
45 public IRegionDataStore regionDb; 44 public IRegionDataStore regionDb;
46 45
@@ -57,14 +56,7 @@ namespace OpenSim.Data.Tests
57 56
58 public void SuperInit() 57 public void SuperInit()
59 { 58 {
60 try 59 OpenSim.Tests.Common.TestLogging.LogToConsole();
61 {
62 XmlConfigurator.Configure();
63 }
64 catch (Exception)
65 {
66 // I don't care, just leave log4net off
67 }
68 } 60 }
69 61
70 #region 0Tests 62 #region 0Tests
@@ -168,7 +160,7 @@ namespace OpenSim.Data.Tests
168 160
169 // Letting estate store generate rows to database for us 161 // Letting estate store generate rows to database for us
170 EstateSettings originalSettings = db.LoadEstateSettings(REGION_ID); 162 EstateSettings originalSettings = db.LoadEstateSettings(REGION_ID);
171 ScrambleForTesting.Scramble(originalSettings); 163 new PropertyScrambler<EstateSettings>().Scramble(originalSettings);
172 164
173 // Saving settings. 165 // Saving settings.
174 db.StoreEstateSettings(originalSettings); 166 db.StoreEstateSettings(originalSettings);
diff --git a/OpenSim/Data/Tests/BasicGridTest.cs b/OpenSim/Data/Tests/BasicGridTest.cs
index b0ee4a0..a25b736 100644
--- a/OpenSim/Data/Tests/BasicGridTest.cs
+++ b/OpenSim/Data/Tests/BasicGridTest.cs
@@ -28,22 +28,18 @@
28using System; 28using System;
29using System.Collections.Generic; 29using System.Collections.Generic;
30using System.Text; 30using System.Text;
31using log4net.Config;
32using NUnit.Framework; 31using NUnit.Framework;
33using NUnit.Framework.SyntaxHelpers; 32using NUnit.Framework.SyntaxHelpers;
34using OpenMetaverse; 33using OpenMetaverse;
35using log4net;
36using System.Reflection;
37 34
38namespace OpenSim.Data.Tests 35namespace OpenSim.Data.Tests
39{ 36{
40 public class BasicGridTest 37 public class BasicGridTest
41 { 38 {
42 //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 39 public IGridDataPlugin db;
43 public GridDataBase db;
44 public UUID region1, region2, region3; 40 public UUID region1, region2, region3;
45 public UUID zero = UUID.Zero; 41 public UUID zero = UUID.Zero;
46 public static Random random; 42 public static Random random = new Random();
47 43
48 [TearDown] 44 [TearDown]
49 public void removeAllRegions() 45 public void removeAllRegions()
@@ -61,24 +57,16 @@ namespace OpenSim.Data.Tests
61 57
62 public void SuperInit() 58 public void SuperInit()
63 { 59 {
64 try 60 OpenSim.Tests.Common.TestLogging.LogToConsole();
65 {
66 XmlConfigurator.Configure();
67 }
68 catch (Exception)
69 {
70 // I don't care, just leave log4net off
71 }
72 region1 = UUID.Random(); 61 region1 = UUID.Random();
73 region2 = UUID.Random(); 62 region2 = UUID.Random();
74 region3 = UUID.Random(); 63 region3 = UUID.Random();
75 random = new Random();
76 } 64 }
77 65
78 protected RegionProfileData createRegion(UUID regionUUID, string regionName) 66 protected RegionProfileData createRegion(UUID regionUUID, string regionName)
79 { 67 {
80 RegionProfileData reg = new RegionProfileData(); 68 RegionProfileData reg = new RegionProfileData();
81 ScrambleForTesting.Scramble(reg); 69 new PropertyScrambler<RegionProfileData>().Scramble(reg);
82 reg.Uuid = regionUUID; 70 reg.Uuid = regionUUID;
83 reg.RegionName = regionName; 71 reg.RegionName = regionName;
84 72
diff --git a/OpenSim/Data/Tests/BasicInventoryTest.cs b/OpenSim/Data/Tests/BasicInventoryTest.cs
index 21552c8..900186b 100644
--- a/OpenSim/Data/Tests/BasicInventoryTest.cs
+++ b/OpenSim/Data/Tests/BasicInventoryTest.cs
@@ -66,14 +66,7 @@ namespace OpenSim.Data.Tests
66 66
67 public void SuperInit() 67 public void SuperInit()
68 { 68 {
69 try 69 OpenSim.Tests.Common.TestLogging.LogToConsole();
70 {
71 XmlConfigurator.Configure();
72 }
73 catch (Exception)
74 {
75 // I don't care, just leave log4net off
76 }
77 70
78 folder1 = UUID.Random(); 71 folder1 = UUID.Random();
79 folder2 = UUID.Random(); 72 folder2 = UUID.Random();
@@ -258,37 +251,38 @@ namespace OpenSim.Data.Tests
258 [Test] 251 [Test]
259 public void T104_RandomUpdateItem() 252 public void T104_RandomUpdateItem()
260 { 253 {
254 PropertyScrambler<InventoryFolderBase> folderScrambler =
255 new PropertyScrambler<InventoryFolderBase>()
256 .DontScramble(x => x.Owner)
257 .DontScramble(x => x.ParentID)
258 .DontScramble(x => x.ID);
261 UUID owner = UUID.Random(); 259 UUID owner = UUID.Random();
262 UUID folder = UUID.Random(); 260 UUID folder = UUID.Random();
263 UUID rootId = UUID.Random(); 261 UUID rootId = UUID.Random();
264 UUID rootAsset = UUID.Random(); 262 UUID rootAsset = UUID.Random();
265 InventoryFolderBase f1 = NewFolder(folder, zero, owner, name1); 263 InventoryFolderBase f1 = NewFolder(folder, zero, owner, name1);
266 ScrambleForTesting.Scramble(f1); 264 folderScrambler.Scramble(f1);
267 f1.Owner = owner;
268 f1.ParentID = zero;
269 f1.ID = folder;
270 265
271 // succeed with true
272 db.addInventoryFolder(f1); 266 db.addInventoryFolder(f1);
273 InventoryFolderBase f1a = db.getUserRootFolder(owner); 267 InventoryFolderBase f1a = db.getUserRootFolder(owner);
274 Assert.That(f1a, Constraints.PropertyCompareConstraint(f1)); 268 Assert.That(f1a, Constraints.PropertyCompareConstraint(f1));
275 269
276 ScrambleForTesting.Scramble(f1a); 270 folderScrambler.Scramble(f1a);
277 f1a.Owner = owner; 271
278 f1a.ParentID = zero;
279 f1a.ID = folder;
280 db.updateInventoryFolder(f1a); 272 db.updateInventoryFolder(f1a);
281 273
282 InventoryFolderBase f1b = db.getUserRootFolder(owner); 274 InventoryFolderBase f1b = db.getUserRootFolder(owner);
283 Assert.That(f1b, Constraints.PropertyCompareConstraint(f1a)); 275 Assert.That(f1b, Constraints.PropertyCompareConstraint(f1a));
284 276
285 //Now we have a valid folder to insert into, we can insert the item. 277 //Now we have a valid folder to insert into, we can insert the item.
278 PropertyScrambler<InventoryItemBase> inventoryScrambler =
279 new PropertyScrambler<InventoryItemBase>()
280 .DontScramble(x => x.ID)
281 .DontScramble(x => x.AssetID)
282 .DontScramble(x => x.Owner)
283 .DontScramble(x => x.Folder);
286 InventoryItemBase root = NewItem(rootId, folder, owner, iname1, rootAsset); 284 InventoryItemBase root = NewItem(rootId, folder, owner, iname1, rootAsset);
287 ScrambleForTesting.Scramble(root); 285 inventoryScrambler.Scramble(root);
288 root.ID = rootId;
289 root.AssetID = rootAsset;
290 root.Owner = owner;
291 root.Folder = folder;
292 db.addInventoryItem(root); 286 db.addInventoryItem(root);
293 287
294 InventoryItemBase expected = db.getInventoryItem(rootId); 288 InventoryItemBase expected = db.getInventoryItem(rootId);
@@ -298,11 +292,7 @@ namespace OpenSim.Data.Tests
298 .IgnoreProperty(x => x.Description) 292 .IgnoreProperty(x => x.Description)
299 .IgnoreProperty(x => x.CreatorId)); 293 .IgnoreProperty(x => x.CreatorId));
300 294
301 ScrambleForTesting.Scramble(expected); 295 inventoryScrambler.Scramble(expected);
302 expected.ID = rootId;
303 expected.AssetID = rootAsset;
304 expected.Owner = owner;
305 expected.Folder = folder;
306 db.updateInventoryItem(expected); 296 db.updateInventoryItem(expected);
307 297
308 InventoryItemBase actual = db.getInventoryItem(rootId); 298 InventoryItemBase actual = db.getInventoryItem(rootId);
diff --git a/OpenSim/Data/Tests/BasicRegionTest.cs b/OpenSim/Data/Tests/BasicRegionTest.cs
index 8373922..8474921 100644
--- a/OpenSim/Data/Tests/BasicRegionTest.cs
+++ b/OpenSim/Data/Tests/BasicRegionTest.cs
@@ -71,14 +71,7 @@ namespace OpenSim.Data.Tests
71 71
72 public void SuperInit() 72 public void SuperInit()
73 { 73 {
74 try 74 OpenSim.Tests.Common.TestLogging.LogToConsole();
75 {
76 XmlConfigurator.Configure();
77 }
78 catch (Exception)
79 {
80 // I don't care, just leave log4net off
81 }
82 75
83 region1 = UUID.Random(); 76 region1 = UUID.Random();
84 region3 = UUID.Random(); 77 region3 = UUID.Random();
@@ -536,6 +529,9 @@ namespace OpenSim.Data.Tests
536 [Test] 529 [Test]
537 public void T016_RandomSogWithSceneParts() 530 public void T016_RandomSogWithSceneParts()
538 { 531 {
532 PropertyScrambler<SceneObjectPart> scrambler =
533 new PropertyScrambler<SceneObjectPart>()
534 .DontScramble(x => x.UUID);
539 UUID tmpSog = UUID.Random(); 535 UUID tmpSog = UUID.Random();
540 UUID tmp1 = UUID.Random(); 536 UUID tmp1 = UUID.Random();
541 UUID tmp2 = UUID.Random(); 537 UUID tmp2 = UUID.Random();
@@ -547,14 +543,18 @@ namespace OpenSim.Data.Tests
547 p1.Shape = PrimitiveBaseShape.Default; 543 p1.Shape = PrimitiveBaseShape.Default;
548 p2.Shape = PrimitiveBaseShape.Default; 544 p2.Shape = PrimitiveBaseShape.Default;
549 p3.Shape = PrimitiveBaseShape.Default; 545 p3.Shape = PrimitiveBaseShape.Default;
550 ScrambleForTesting.Scramble(p1);
551 ScrambleForTesting.Scramble(p2);
552 ScrambleForTesting.Scramble(p3);
553 p1.UUID = tmp1; 546 p1.UUID = tmp1;
554 p2.UUID = tmp2; 547 p2.UUID = tmp2;
555 p3.UUID = tmp3; 548 p3.UUID = tmp3;
549 scrambler.Scramble(p1);
550 scrambler.Scramble(p2);
551 scrambler.Scramble(p3);
552
556 SceneObjectGroup sog = NewSOG("Sop 0", tmpSog, newregion); 553 SceneObjectGroup sog = NewSOG("Sop 0", tmpSog, newregion);
557 ScrambleForTesting.Scramble(sog); 554 PropertyScrambler<SceneObjectGroup> sogScrambler =
555 new PropertyScrambler<SceneObjectGroup>()
556 .DontScramble(x => x.UUID);
557 sogScrambler.Scramble(sog);
558 sog.UUID = tmpSog; 558 sog.UUID = tmpSog;
559 sog.AddPart(p1); 559 sog.AddPart(p1);
560 sog.AddPart(p2); 560 sog.AddPart(p2);
diff --git a/OpenSim/Data/Tests/BasicUserTest.cs b/OpenSim/Data/Tests/BasicUserTest.cs
index a3c125d..f9feb9b 100644
--- a/OpenSim/Data/Tests/BasicUserTest.cs
+++ b/OpenSim/Data/Tests/BasicUserTest.cs
@@ -71,14 +71,7 @@ namespace OpenSim.Data.Tests
71 71
72 public void SuperInit() 72 public void SuperInit()
73 { 73 {
74 try 74 OpenSim.Tests.Common.TestLogging.LogToConsole();
75 {
76 XmlConfigurator.Configure();
77 }
78 catch (Exception)
79 {
80 // I don't care, just leave log4net off
81 }
82 random = new Random(); 75 random = new Random();
83 user1 = UUID.Random(); 76 user1 = UUID.Random();
84 user2 = UUID.Random(); 77 user2 = UUID.Random();
@@ -395,8 +388,7 @@ namespace OpenSim.Data.Tests
395 { 388 {
396 UUID id = user5; 389 UUID id = user5;
397 UserProfileData u = db.GetUserByUUID(id); 390 UserProfileData u = db.GetUserByUUID(id);
398 ScrambleForTesting.Scramble(u); 391 new PropertyScrambler<UserProfileData>().DontScramble(x=>x.ID).Scramble(u);
399 u.ID = id;
400 392
401 db.UpdateUserProfile(u); 393 db.UpdateUserProfile(u);
402 UserProfileData u1a = db.GetUserByUUID(id); 394 UserProfileData u1a = db.GetUserByUUID(id);
diff --git a/OpenSim/Data/Tests/PropertyScrambler.cs b/OpenSim/Data/Tests/PropertyScrambler.cs
new file mode 100644
index 0000000..c56c10f
--- /dev/null
+++ b/OpenSim/Data/Tests/PropertyScrambler.cs
@@ -0,0 +1,159 @@
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq.Expressions;
5using System.Reflection;
6using System.Text;
7using NUnit.Framework;
8using NUnit.Framework.SyntaxHelpers;
9using OpenMetaverse;
10using OpenSim.Framework;
11
12namespace OpenSim.Data.Tests
13{
14
15 //This is generic so that the lambda expressions will work right in IDEs.
16 public class PropertyScrambler<T>
17 {
18 readonly System.Collections.Generic.List<string> membersToNotScramble = new List<string>();
19
20 private void AddExpressionToNotScrableList(Expression expression)
21 {
22 UnaryExpression unaryExpression = expression as UnaryExpression;
23 if(unaryExpression != null)
24 {
25 AddExpressionToNotScrableList(unaryExpression.Operand);
26 return;
27 }
28
29 MemberExpression memberExpression = expression as MemberExpression;
30 if (memberExpression != null)
31 {
32 if (!(memberExpression.Member is PropertyInfo))
33 {
34 throw new NotImplementedException("I don't know how deal with a MemberExpression that is a " + expression.Type);
35 }
36 membersToNotScramble.Add(memberExpression.Member.Name);
37 return;
38 }
39
40 throw new NotImplementedException("I don't know how to parse a " + expression.Type);
41 }
42
43 public PropertyScrambler<T> DontScramble(Expression<Func<T, object>> expression)
44 {
45 AddExpressionToNotScrableList(expression.Body);
46 return this;
47 }
48
49 public void Scramble(T obj)
50 {
51 internalScramble(obj);
52 }
53
54 private void internalScramble(object obj)
55 {
56 PropertyInfo[] properties = obj.GetType().GetProperties();
57 foreach (var property in properties)
58 {
59 //Skip indexers of classes. We will assume that everything that has an indexer
60 // is also IEnumberable. May not always be true, but should be true normally.
61 if(property.GetIndexParameters().Length > 0)
62 continue;
63
64 RandomizeProperty(obj, property, null);
65 }
66 //Now if it implments IEnumberable, it's probably some kind of list, so we should randomize
67 // everything inside of it.
68 IEnumerable enumerable = obj as IEnumerable;
69 if(enumerable != null)
70 {
71 foreach (object value in enumerable)
72 {
73 internalScramble(value);
74 }
75 }
76 }
77
78 private readonly Random random = new Random();
79 private void RandomizeProperty(object obj, PropertyInfo property, object[] index)
80 {//I'd like a better way to compare, but I had lots of problems with InventoryFolderBase because the ID is inherited.
81 if(membersToNotScramble.Contains(property.Name))
82 return;
83 Type t = property.PropertyType;
84 if (!property.CanWrite)
85 return;
86 object value = property.GetValue(obj, index);
87 if (value == null)
88 return;
89
90 if (t == typeof(string))
91 property.SetValue(obj, RandomName(), index);
92 else if (t == typeof(UUID))
93 property.SetValue(obj, UUID.Random(), index);
94 else if (t == typeof(sbyte))
95 property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index);
96 else if (t == typeof(short))
97 property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index);
98 else if (t == typeof(int))
99 property.SetValue(obj, random.Next(), index);
100 else if (t == typeof(long))
101 property.SetValue(obj, random.Next() * int.MaxValue, index);
102 else if (t == typeof(byte))
103 property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index);
104 else if (t == typeof(ushort))
105 property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index);
106 else if (t == typeof(uint))
107 property.SetValue(obj, Convert.ToUInt32(random.Next()), index);
108 else if (t == typeof(ulong))
109 property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index);
110 else if (t == typeof(bool))
111 property.SetValue(obj, true, index);
112 else if (t == typeof(byte[]))
113 {
114 byte[] bytes = new byte[30];
115 random.NextBytes(bytes);
116 property.SetValue(obj, bytes, index);
117 }
118 else
119 internalScramble(value);
120 }
121
122 private string RandomName()
123 {
124 StringBuilder name = new StringBuilder();
125 int size = random.Next(5, 12);
126 for (int i = 0; i < size; i++)
127 {
128 char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
129 name.Append(ch);
130 }
131 return name.ToString();
132 }
133 }
134
135 [TestFixture]
136 public class PropertyScramblerTests
137 {
138 [Test]
139 public void TestScramble()
140 {
141 AssetBase actual = new AssetBase(UUID.Random(), "asset one");
142 new PropertyScrambler<AssetBase>().Scramble(actual);
143 }
144
145 [Test]
146 public void DontScramble()
147 {
148 UUID uuid = UUID.Random();
149 AssetBase asset = new AssetBase();
150 asset.FullID = uuid;
151 new PropertyScrambler<AssetBase>()
152 .DontScramble(x => x.Metadata)
153 .DontScramble(x => x.FullID)
154 .DontScramble(x => x.ID)
155 .Scramble(asset);
156 Assert.That(asset.FullID, Is.EqualTo(uuid));
157 }
158 }
159} \ No newline at end of file
diff --git a/OpenSim/Data/Tests/ScrambleForTesting.cs b/OpenSim/Data/Tests/ScrambleForTesting.cs
deleted file mode 100644
index 3a22347..0000000
--- a/OpenSim/Data/Tests/ScrambleForTesting.cs
+++ /dev/null
@@ -1,129 +0,0 @@
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
28using System;
29using System.Collections;
30using System.Reflection;
31using System.Text;
32using NUnit.Framework;
33using OpenMetaverse;
34using OpenSim.Framework;
35
36namespace OpenSim.Data.Tests
37{
38 public static class ScrambleForTesting
39 {
40 private static readonly Random random = new Random();
41 public static void Scramble(object obj)
42 {
43 PropertyInfo[] properties = obj.GetType().GetProperties();
44 foreach (var property in properties)
45 {
46 //Skip indexers of classes. We will assume that everything that has an indexer
47 // is also IEnumberable. May not always be true, but should be true normally.
48 if (property.GetIndexParameters().Length > 0)
49 continue;
50
51 RandomizeProperty(obj, property, null);
52 }
53 //Now if it implments IEnumberable, it's probably some kind of list, so we should randomize
54 // everything inside of it.
55 IEnumerable enumerable = obj as IEnumerable;
56 if (enumerable != null)
57 {
58 foreach (object value in enumerable)
59 {
60 Scramble(value);
61 }
62 }
63 }
64
65 private static void RandomizeProperty(object obj, PropertyInfo property, object[] index)
66 {
67 Type t = property.PropertyType;
68 if (!property.CanWrite)
69 return;
70 object value = property.GetValue(obj, index);
71 if (value == null)
72 return;
73
74 if (t == typeof (string))
75 property.SetValue(obj, RandomName(), index);
76 else if (t == typeof (UUID))
77 property.SetValue(obj, UUID.Random(), index);
78 else if (t == typeof (sbyte))
79 property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index);
80 else if (t == typeof (short))
81 property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index);
82 else if (t == typeof (int))
83 property.SetValue(obj, random.Next(), index);
84 else if (t == typeof (long))
85 property.SetValue(obj, random.Next() * int.MaxValue, index);
86 else if (t == typeof (byte))
87 property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index);
88 else if (t == typeof (ushort))
89 property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index);
90 else if (t == typeof (uint))
91 property.SetValue(obj, Convert.ToUInt32(random.Next()), index);
92 else if (t == typeof (ulong))
93 property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index);
94 else if (t == typeof (bool))
95 property.SetValue(obj, true, index);
96 else if (t == typeof (byte[]))
97 {
98 byte[] bytes = new byte[30];
99 random.NextBytes(bytes);
100 property.SetValue(obj, bytes, index);
101 }
102 else
103 Scramble(value);
104 }
105
106 private static string RandomName()
107 {
108 StringBuilder name = new StringBuilder();
109 int size = random.Next(5, 12);
110 for (int i = 0; i < size; i++)
111 {
112 char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
113 name.Append(ch);
114 }
115 return name.ToString();
116 }
117 }
118
119 [TestFixture]
120 public class ScrableForTestingTest
121 {
122 [Test]
123 public void TestScramble()
124 {
125 AssetBase actual = new AssetBase(UUID.Random(), "asset one");
126 ScrambleForTesting.Scramble(actual);
127 }
128 }
129} \ No newline at end of file