1
0

AssetTests.cs 8.1 KB

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