UtilTest.cs 19 KB

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