AssetTests.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 System.Collections.Generic;
  29. using log4net.Config;
  30. using NUnit.Framework;
  31. using NUnit.Framework.Constraints;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Tests.Common;
  35. using System.Data.Common;
  36. using log4net;
  37. // DBMS-specific:
  38. using MySql.Data.MySqlClient;
  39. using OpenSim.Data.MySQL;
  40. using Mono.Data.Sqlite;
  41. using OpenSim.Data.SQLite;
  42. namespace OpenSim.Data.Tests
  43. {
  44. [TestFixture(Description = "Asset store tests (SQLite)")]
  45. public class SQLiteAssetTests : AssetTests<SqliteConnection, SQLiteAssetData>
  46. {
  47. }
  48. [TestFixture(Description = "Asset store tests (MySQL)")]
  49. public class MySqlAssetTests : AssetTests<MySqlConnection, MySQLAssetData>
  50. {
  51. }
  52. public class AssetTests<TConn, TAssetData> : BasicDataServiceTest<TConn, TAssetData>
  53. where TConn : DbConnection, new()
  54. where TAssetData : AssetDataBase, new()
  55. {
  56. TAssetData m_db;
  57. public UUID uuid1 = UUID.Random();
  58. public UUID uuid2 = UUID.Random();
  59. public UUID uuid3 = UUID.Random();
  60. public string critter1 = UUID.Random().ToString();
  61. public string critter2 = UUID.Random().ToString();
  62. public string critter3 = UUID.Random().ToString();
  63. public byte[] data1 = new byte[100];
  64. PropertyScrambler<AssetBase> scrambler = new PropertyScrambler<AssetBase>()
  65. .DontScramble(x => x.ID)
  66. .DontScramble(x => x.Type)
  67. .DontScramble(x => x.FullID)
  68. .DontScramble(x => x.Metadata.ID)
  69. .DontScramble(x => x.Metadata.CreatorID)
  70. .DontScramble(x => x.Metadata.ContentType)
  71. .DontScramble(x => x.Metadata.FullID)
  72. .DontScramble(x => x.Data);
  73. protected override void InitService(object service)
  74. {
  75. ClearDB();
  76. m_db = (TAssetData)service;
  77. m_db.Initialise(m_connStr);
  78. }
  79. private void ClearDB()
  80. {
  81. DropTables("assets");
  82. ResetMigrations("AssetStore");
  83. }
  84. [Test]
  85. public void T001_LoadEmpty()
  86. {
  87. TestHelpers.InMethod();
  88. bool[] exist = m_db.AssetsExist(new[] { uuid1, uuid2, uuid3 });
  89. Assert.IsFalse(exist[0]);
  90. Assert.IsFalse(exist[1]);
  91. Assert.IsFalse(exist[2]);
  92. }
  93. [Test]
  94. public void T010_StoreReadVerifyAssets()
  95. {
  96. TestHelpers.InMethod();
  97. AssetBase a1 = new AssetBase(uuid1, "asset one", (sbyte)AssetType.Texture, critter1.ToString());
  98. AssetBase a2 = new AssetBase(uuid2, "asset two", (sbyte)AssetType.Texture, critter2.ToString());
  99. AssetBase a3 = new AssetBase(uuid3, "asset three", (sbyte)AssetType.Texture, critter3.ToString());
  100. a1.Data = data1;
  101. a2.Data = data1;
  102. a3.Data = data1;
  103. scrambler.Scramble(a1);
  104. scrambler.Scramble(a2);
  105. scrambler.Scramble(a3);
  106. m_db.StoreAsset(a1);
  107. m_db.StoreAsset(a2);
  108. m_db.StoreAsset(a3);
  109. AssetBase a1a = m_db.GetAsset(uuid1);
  110. Assert.That(a1a, Constraints.PropertyCompareConstraint(a1));
  111. AssetBase a2a = m_db.GetAsset(uuid2);
  112. Assert.That(a2a, Constraints.PropertyCompareConstraint(a2));
  113. AssetBase a3a = m_db.GetAsset(uuid3);
  114. Assert.That(a3a, Constraints.PropertyCompareConstraint(a3));
  115. scrambler.Scramble(a1a);
  116. scrambler.Scramble(a2a);
  117. scrambler.Scramble(a3a);
  118. m_db.StoreAsset(a1a);
  119. m_db.StoreAsset(a2a);
  120. m_db.StoreAsset(a3a);
  121. AssetBase a1b = m_db.GetAsset(uuid1);
  122. Assert.That(a1b, Constraints.PropertyCompareConstraint(a1a));
  123. AssetBase a2b = m_db.GetAsset(uuid2);
  124. Assert.That(a2b, Constraints.PropertyCompareConstraint(a2a));
  125. AssetBase a3b = m_db.GetAsset(uuid3);
  126. Assert.That(a3b, Constraints.PropertyCompareConstraint(a3a));
  127. bool[] exist = m_db.AssetsExist(new[] { uuid1, uuid2, uuid3 });
  128. Assert.IsTrue(exist[0]);
  129. Assert.IsTrue(exist[1]);
  130. Assert.IsTrue(exist[2]);
  131. List<AssetMetadata> metadatas = m_db.FetchAssetMetadataSet(0, 1000);
  132. Assert.That(metadatas.Count >= 3, "FetchAssetMetadataSet() should have returned at least 3 assets!");
  133. // It is possible that the Asset table is filled with data, in which case we don't try to find "our"
  134. // assets there:
  135. if (metadatas.Count < 1000)
  136. {
  137. AssetMetadata metadata = metadatas.Find(x => x.FullID == uuid1);
  138. Assert.That(metadata.Name, Is.EqualTo(a1b.Name));
  139. Assert.That(metadata.Description, Is.EqualTo(a1b.Description));
  140. Assert.That(metadata.Type, Is.EqualTo(a1b.Type));
  141. Assert.That(metadata.Temporary, Is.EqualTo(a1b.Temporary));
  142. Assert.That(metadata.FullID, Is.EqualTo(a1b.FullID));
  143. }
  144. }
  145. [Test]
  146. public void T020_CheckForWeirdCreatorID()
  147. {
  148. TestHelpers.InMethod();
  149. // It is expected that eventually the CreatorID might be an arbitrary string (an URI)
  150. // rather than a valid UUID (?). This test is to make sure that the database layer does not
  151. // attempt to convert CreatorID to GUID, but just passes it both ways as a string.
  152. AssetBase a1 = new AssetBase(uuid1, "asset one", (sbyte)AssetType.Texture, critter1);
  153. AssetBase a2 = new AssetBase(uuid2, "asset two", (sbyte)AssetType.Texture, "This is not a GUID!");
  154. AssetBase a3 = new AssetBase(uuid3, "asset three", (sbyte)AssetType.Texture, "");
  155. a1.Data = data1;
  156. a2.Data = data1;
  157. a3.Data = data1;
  158. m_db.StoreAsset(a1);
  159. m_db.StoreAsset(a2);
  160. m_db.StoreAsset(a3);
  161. AssetBase a1a = m_db.GetAsset(uuid1);
  162. Assert.That(a1a, Constraints.PropertyCompareConstraint(a1));
  163. AssetBase a2a = m_db.GetAsset(uuid2);
  164. Assert.That(a2a, Constraints.PropertyCompareConstraint(a2));
  165. AssetBase a3a = m_db.GetAsset(uuid3);
  166. Assert.That(a3a, Constraints.PropertyCompareConstraint(a3));
  167. }
  168. }
  169. }