OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
UtilTest.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 NUnit.Framework;
30 using OpenMetaverse;
31 using OpenSim.Tests.Common;
32 
33 namespace OpenSim.Framework.Tests
34 {
35  [TestFixture]
36  public class UtilTests : OpenSimTestCase
37  {
38  [Test]
39  public void VectorOperationTests()
40  {
41  Vector3 v1, v2;
42  double expectedDistance;
43  double expectedMagnitude;
44  double lowPrecisionTolerance = 0.001;
45 
46  //Lets test a simple case of <0,0,0> and <5,5,5>
47  {
48  v1 = new Vector3(0, 0, 0);
49  v2 = new Vector3(5, 5, 5);
50  expectedDistance = 8.66;
51  Assert.That(Util.GetDistanceTo(v1, v2),
52  new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance),
53  "Calculated distance between two vectors was not within tolerances.");
54 
55  expectedMagnitude = 0;
56  Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero.");
57 
58  expectedMagnitude = 8.66;
59  Assert.That(Util.GetMagnitude(v2),
60  new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance),
61  "Magnitude of vector was incorrect.");
62 /*
63  TestDelegate d = delegate() { Util.GetNormalizedVector(v1); };
64  bool causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d);
65  Assert.That(causesArgumentException, Is.True,
66  "Getting magnitude of null vector did not cause argument exception.");
67 */
68  Vector3 expectedNormalizedVector = new Vector3(.577f, .577f, .577f);
69  double expectedNormalizedMagnitude = 1;
70  Vector3 normalizedVector = Util.GetNormalizedVector(v2);
71  Assert.That(normalizedVector,
72  new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance),
73  "Normalized vector generated from vector was not what was expected.");
74  Assert.That(Util.GetMagnitude(normalizedVector),
75  new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance),
76  "Normalized vector generated from vector does not have magnitude of 1.");
77  }
78 
79  //Lets test a simple case of <0,0,0> and <0,0,0>
80  {
81  v1 = new Vector3(0, 0, 0);
82  v2 = new Vector3(0, 0, 0);
83  expectedDistance = 0;
84  Assert.That(Util.GetDistanceTo(v1, v2),
85  new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance),
86  "Calculated distance between two vectors was not within tolerances.");
87 
88  expectedMagnitude = 0;
89  Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero.");
90 
91  expectedMagnitude = 0;
92  Assert.That(Util.GetMagnitude(v2),
93  new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance),
94  "Magnitude of vector was incorrect.");
95 /*
96  TestDelegate d = delegate() { Util.GetNormalizedVector(v1); };
97  bool causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d);
98  Assert.That(causesArgumentException, Is.True,
99  "Getting magnitude of null vector did not cause argument exception.");
100 
101  d = delegate() { Util.GetNormalizedVector(v2); };
102  causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d);
103  Assert.That(causesArgumentException, Is.True,
104  "Getting magnitude of null vector did not cause argument exception.");
105 */
106  }
107 
108  //Lets test a simple case of <0,0,0> and <-5,-5,-5>
109  {
110  v1 = new Vector3(0, 0, 0);
111  v2 = new Vector3(-5, -5, -5);
112  expectedDistance = 8.66;
113  Assert.That(Util.GetDistanceTo(v1, v2),
114  new DoubleToleranceConstraint(expectedDistance, lowPrecisionTolerance),
115  "Calculated distance between two vectors was not within tolerances.");
116 
117  expectedMagnitude = 0;
118  Assert.That(Util.GetMagnitude(v1), Is.EqualTo(0), "Magnitude of null vector was not zero.");
119 
120  expectedMagnitude = 8.66;
121  Assert.That(Util.GetMagnitude(v2),
122  new DoubleToleranceConstraint(expectedMagnitude, lowPrecisionTolerance),
123  "Magnitude of vector was incorrect.");
124 /*
125  TestDelegate d = delegate() { Util.GetNormalizedVector(v1); };
126  bool causesArgumentException = TestHelpers.AssertThisDelegateCausesArgumentException(d);
127  Assert.That(causesArgumentException, Is.True,
128  "Getting magnitude of null vector did not cause argument exception.");
129 */
130  Vector3 expectedNormalizedVector = new Vector3(-.577f, -.577f, -.577f);
131  double expectedNormalizedMagnitude = 1;
132  Vector3 normalizedVector = Util.GetNormalizedVector(v2);
133  Assert.That(normalizedVector,
134  new VectorToleranceConstraint(expectedNormalizedVector, lowPrecisionTolerance),
135  "Normalized vector generated from vector was not what was expected.");
136  Assert.That(Util.GetMagnitude(normalizedVector),
137  new DoubleToleranceConstraint(expectedNormalizedMagnitude, lowPrecisionTolerance),
138  "Normalized vector generated from vector does not have magnitude of 1.");
139  }
140  }
141 
142  [Test]
143  public void UUIDTests()
144  {
145  Assert.IsTrue(Util.isUUID("01234567-89ab-Cdef-0123-456789AbCdEf"),
146  "A correct UUID wasn't recognized.");
147  Assert.IsFalse(Util.isUUID("FOOBAR67-89ab-Cdef-0123-456789AbCdEf"),
148  "UUIDs with non-hex characters are recognized as correct UUIDs.");
149  Assert.IsFalse(Util.isUUID("01234567"),
150  "Too short UUIDs are recognized as correct UUIDs.");
151  Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123-456789AbCdEf0"),
152  "Too long UUIDs are recognized as correct UUIDs.");
153  Assert.IsFalse(Util.isUUID("01234567-89ab-Cdef-0123+456789AbCdEf"),
154  "UUIDs with wrong format are recognized as correct UUIDs.");
155  }
156 
157  [Test]
158  public void GetHashGuidTests()
159  {
160  string string1 = "This is one string";
161  string string2 = "This is another";
162 
163  // Two consecutive runs should equal the same
164  Assert.AreEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret1"));
165  Assert.AreEqual(Util.GetHashGuid(string2, "secret1"), Util.GetHashGuid(string2, "secret1"));
166 
167  // Varying data should not eqal the same
168  Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string2, "secret1"));
169 
170  // Varying secrets should not eqal the same
171  Assert.AreNotEqual(Util.GetHashGuid(string1, "secret1"), Util.GetHashGuid(string1, "secret2"));
172  }
173 
174  [Test]
176  {
177  int[] assettypes = new int[]{-1,0,1,2,3,5,6,7,8,10,11,12,13,17,18,19,20,21,22
178  ,24,25};
179  string[] contenttypes = new string[]
180  {
181  "application/octet-stream",
182  "image/x-j2c",
183  "audio/ogg",
184  "application/vnd.ll.callingcard",
185  "application/vnd.ll.landmark",
186  "application/vnd.ll.clothing",
187  "application/vnd.ll.primitive",
188  "application/vnd.ll.notecard",
189  "application/vnd.ll.folder",
190  "application/vnd.ll.lsltext",
191  "application/vnd.ll.lslbyte",
192  "image/tga",
193  "application/vnd.ll.bodypart",
194  "audio/x-wav",
195  "image/tga",
196  "image/jpeg",
197  "application/vnd.ll.animation",
198  "application/vnd.ll.gesture",
199  "application/x-metaverse-simstate",
200  "application/vnd.ll.link",
201  "application/vnd.ll.linkfolder",
202  };
203  for (int i=0;i<assettypes.Length;i++)
204  {
205  Assert.That(SLUtil.SLAssetTypeToContentType(assettypes[i]) == contenttypes[i], "Expecting {0} but got {1}", contenttypes[i], SLUtil.SLAssetTypeToContentType(assettypes[i]));
206  }
207 
208  for (int i = 0; i < contenttypes.Length; i++)
209  {
210  int expected;
211  if (contenttypes[i] == "image/tga")
212  expected = 12; // if we know only the content-type "image/tga", then we assume the asset type is TextureTGA; not ImageTGA
213  else
214  expected = assettypes[i];
215  Assert.AreEqual(expected, SLUtil.ContentTypeToSLAssetType(contenttypes[i]),
216  String.Format("Incorrect AssetType mapped from Content-Type {0}", contenttypes[i]));
217  }
218 
219  int[] inventorytypes = new int[] {-1,0,1,2,3,6,7,8,10,15,17,18,20};
220  string[] invcontenttypes = new string[]
221  {
222  "application/octet-stream",
223  "image/x-j2c",
224  "audio/ogg",
225  "application/vnd.ll.callingcard",
226  "application/vnd.ll.landmark",
227  "application/vnd.ll.primitive",
228  "application/vnd.ll.notecard",
229  "application/vnd.ll.rootfolder",
230  "application/vnd.ll.lsltext",
231  "image/x-j2c",
232  "application/vnd.ll.primitive",
233  "application/vnd.ll.clothing",
234  "application/vnd.ll.gesture"
235  };
236 
237  for (int i=0;i<inventorytypes.Length;i++)
238  {
239  Assert.AreEqual(invcontenttypes[i], SLUtil.SLInvTypeToContentType(inventorytypes[i]),
240  String.Format("Incorrect Content-Type mapped from InventoryType {0}", inventorytypes[i]));
241  }
242 
243  invcontenttypes = new string[]
244  {
245  "image/x-j2c","image/jp2","image/tga",
246  "image/jpeg","application/ogg","audio/ogg",
247  "audio/x-wav","application/vnd.ll.callingcard",
248  "application/x-metaverse-callingcard",
249  "application/vnd.ll.landmark",
250  "application/x-metaverse-landmark",
251  "application/vnd.ll.clothing",
252  "application/x-metaverse-clothing","application/vnd.ll.bodypart",
253  "application/x-metaverse-bodypart","application/vnd.ll.primitive",
254  "application/x-metaverse-primitive","application/vnd.ll.notecard",
255  "application/x-metaverse-notecard","application/vnd.ll.folder",
256  "application/vnd.ll.rootfolder","application/vnd.ll.lsltext",
257  "application/x-metaverse-lsl","application/vnd.ll.lslbyte",
258  "application/x-metaverse-lso","application/vnd.ll.trashfolder",
259  "application/vnd.ll.snapshotfolder",
260  "application/vnd.ll.lostandfoundfolder","application/vnd.ll.animation",
261  "application/x-metaverse-animation","application/vnd.ll.gesture",
262  "application/x-metaverse-gesture","application/x-metaverse-simstate",
263  "application/octet-stream"
264  };
265  sbyte[] invtypes = new sbyte[]
266  {
267  0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 18, 18, 18, 18, 6, 6, 7, 7, -1, 8, 10, 10, 10, 10
268  , 14, 15, 16, 19, 19, 20, 20, 15, -1
269  };
270 
271  for (int i = 0; i < invtypes.Length; i++)
272  {
273  Assert.AreEqual(invtypes[i], SLUtil.ContentTypeToSLInvType(invcontenttypes[i]),
274  String.Format("Incorrect InventoryType mapped from Content-Type {0}", invcontenttypes[i]));
275  }
276  }
277 
278  [Test]
279  public void FakeParcelIDTests()
280  {
281  byte[] hexBytes8 = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
282  byte[] hexBytes16 = {
283  0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87,
284  0x77, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f };
285  UInt64 var64Bit = (UInt64)0xfedcba9876543210;
286 
287  //Region handle is for location 255000,256000.
288  ulong regionHandle1 = 1095216660736000;
289  uint x1 = 100;
290  uint y1 = 200;
291  uint z1 = 22;
292  ulong regionHandle2;
293  uint x2, y2, z2;
294  UUID fakeParcelID1, uuid;
295 
296  ulong bigInt64 = Util.BytesToUInt64Big(hexBytes8);
297  Assert.AreEqual(var64Bit, bigInt64,
298  "BytesToUint64Bit conversion of 8 bytes to UInt64 failed.");
299 
300  //Test building and decoding using some typical input values
301  fakeParcelID1 = Util.BuildFakeParcelID(regionHandle1, x1, y1);
302  Util.ParseFakeParcelID(fakeParcelID1, out regionHandle2, out x2, out y2);
303  Assert.AreEqual(regionHandle1, regionHandle2,
304  "region handle decoded from FakeParcelID wth X/Y failed.");
305  Assert.AreEqual(x1, x2,
306  "X coordinate decoded from FakeParcelID wth X/Y failed.");
307  Assert.AreEqual(y1, y2,
308  "Y coordinate decoded from FakeParcelID wth X/Y failed.");
309 
310  fakeParcelID1 = Util.BuildFakeParcelID(regionHandle1, x1, y1, z1);
311  Util.ParseFakeParcelID(fakeParcelID1, out regionHandle2, out x2, out y2, out z2);
312  Assert.AreEqual(regionHandle1, regionHandle2,
313  "region handle decoded from FakeParcelID with X/Y/Z failed.");
314  Assert.AreEqual(x1, x2,
315  "X coordinate decoded from FakeParcelID with X/Y/Z failed.");
316  Assert.AreEqual(y1, y2,
317  "Y coordinate decoded from FakeParcelID with X/Y/Z failed.");
318  Assert.AreEqual(z1, z2,
319  "Z coordinate decoded from FakeParcelID with X/Y/Z failed.");
320 
321  //Do some more extreme tests to check the encoding and decoding
322  x1 = 0x55aa;
323  y1 = 0x9966;
324  z1 = 0x5a96;
325 
326  fakeParcelID1 = Util.BuildFakeParcelID(var64Bit, x1, y1);
327  Util.ParseFakeParcelID(fakeParcelID1, out regionHandle2, out x2, out y2);
328  Assert.AreEqual(var64Bit, regionHandle2,
329  "region handle decoded from FakeParcelID with X/Y/Z failed.");
330  Assert.AreEqual(x1, x2,
331  "X coordinate decoded from FakeParcelID with X/Y/Z failed.");
332  Assert.AreEqual(y1, y2,
333  "Y coordinate decoded from FakeParcelID with X/Y/Z failed.");
334 
335  fakeParcelID1 = Util.BuildFakeParcelID(var64Bit, x1, y1, z1);
336  Util.ParseFakeParcelID(fakeParcelID1, out regionHandle2, out x2, out y2, out z2);
337  Assert.AreEqual(var64Bit, regionHandle2,
338  "region handle decoded from FakeParcelID with X/Y/Z failed.");
339  Assert.AreEqual(x1, x2,
340  "X coordinate decoded from FakeParcelID with X/Y/Z failed.");
341  Assert.AreEqual(y1, y2,
342  "Y coordinate decoded from FakeParcelID with X/Y/Z failed.");
343  Assert.AreEqual(z1, z2,
344  "Z coordinate decoded from FakeParcelID with X/Y/Z failed.");
345 
346 
347  x1 = 64;
348  y1 = 192;
349  fakeParcelID1 = Util.BuildFakeParcelID(regionHandle1, x1, y1);
350  Util.FakeParcelIDToGlobalPosition(fakeParcelID1, out x2, out y2);
351  Assert.AreEqual(255000+x1, x2,
352  "Global X coordinate decoded from regionHandle failed.");
353  Assert.AreEqual(256000+y1, y2,
354  "Global Y coordinate decoded from regionHandle failed.");
355 
356  uuid = new UUID("00dd0700-00d1-0700-3800-000032000000");
357  Util.FakeParcelIDToGlobalPosition(uuid, out x2, out y2);
358  }
359  }
360 }