opensim-development – Blame information for rev 1

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