aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/Tests/ScrambleForTesting.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/Tests/ScrambleForTesting.cs102
1 files changed, 102 insertions, 0 deletions
diff --git a/OpenSim/Data/Tests/ScrambleForTesting.cs b/OpenSim/Data/Tests/ScrambleForTesting.cs
new file mode 100644
index 0000000..c6e467f
--- /dev/null
+++ b/OpenSim/Data/Tests/ScrambleForTesting.cs
@@ -0,0 +1,102 @@
1using System;
2using System.Collections;
3using System.Reflection;
4using System.Text;
5using NUnit.Framework;
6using OpenMetaverse;
7using OpenSim.Framework;
8
9namespace OpenSim.Data.Tests
10{
11 public static class ScrambleForTesting
12 {
13 private static readonly Random random = new Random();
14 public static void Scramble(object obj)
15 {
16 PropertyInfo[] properties = obj.GetType().GetProperties();
17 foreach (var property in properties)
18 {
19 //Skip indexers of classes. We will assume that everything that has an indexer
20 // is also IEnumberable. May not always be true, but should be true normally.
21 if(property.GetIndexParameters().Length > 0)
22 continue;
23
24 RandomizeProperty(obj, property, null);
25 }
26 //Now if it implments IEnumberable, it's probably some kind of list, so we should randomize
27 // everything inside of it.
28 IEnumerable enumerable = obj as IEnumerable;
29 if(enumerable != null)
30 {
31 foreach (object value in enumerable)
32 {
33 Scramble(value);
34 }
35 }
36 }
37
38 private static void RandomizeProperty(object obj, PropertyInfo property, object[] index)
39 {
40 Type t = property.PropertyType;
41 if (!property.CanWrite)
42 return;
43 object value = property.GetValue(obj, index);
44 if (value == null)
45 return;
46
47 if (t == typeof (string))
48 property.SetValue(obj, RandomName(), index);
49 else if (t == typeof (UUID))
50 property.SetValue(obj, UUID.Random(), index);
51 else if (t == typeof (sbyte))
52 property.SetValue(obj, (sbyte)random.Next(sbyte.MinValue, sbyte.MaxValue), index);
53 else if (t == typeof (short))
54 property.SetValue(obj, (short)random.Next(short.MinValue, short.MaxValue), index);
55 else if (t == typeof (int))
56 property.SetValue(obj, random.Next(), index);
57 else if (t == typeof (long))
58 property.SetValue(obj, random.Next() * int.MaxValue, index);
59 else if (t == typeof (byte))
60 property.SetValue(obj, (byte)random.Next(byte.MinValue, byte.MaxValue), index);
61 else if (t == typeof (ushort))
62 property.SetValue(obj, (ushort)random.Next(ushort.MinValue, ushort.MaxValue), index);
63 else if (t == typeof (uint))
64 property.SetValue(obj, Convert.ToUInt32(random.Next()), index);
65 else if (t == typeof (ulong))
66 property.SetValue(obj, Convert.ToUInt64(random.Next()) * Convert.ToUInt64(UInt32.MaxValue), index);
67 else if (t == typeof (bool))
68 property.SetValue(obj, true, index);
69 else if (t == typeof (byte[]))
70 {
71 byte[] bytes = new byte[30];
72 random.NextBytes(bytes);
73 property.SetValue(obj, bytes, index);
74 }
75 else
76 Scramble(value);
77 }
78
79 private static string RandomName()
80 {
81 StringBuilder name = new StringBuilder();
82 int size = random.Next(5, 12);
83 for (int i = 0; i < size; i++)
84 {
85 char ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
86 name.Append(ch);
87 }
88 return name.ToString();
89 }
90 }
91
92 [TestFixture]
93 public class ScrableForTestingTest
94 {
95 [Test]
96 public void TestScramble()
97 {
98 AssetBase actual = new AssetBase(UUID.Random(), "asset one");
99 ScrambleForTesting.Scramble(actual);
100 }
101 }
102} \ No newline at end of file