1
0

AssetTests.cs 8.3 KB

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