AssetTests.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. a1.UploadAttempts = 0;
  110. a2.UploadAttempts = 0;
  111. a3.UploadAttempts = 0;
  112. AssetBase a1a = m_db.GetAsset(uuid1);
  113. a1a.UploadAttempts = 0;
  114. Assert.That(a1a, Constraints.PropertyCompareConstraint(a1));
  115. AssetBase a2a = m_db.GetAsset(uuid2);
  116. a2a.UploadAttempts = 0;
  117. Assert.That(a2a, Constraints.PropertyCompareConstraint(a2));
  118. AssetBase a3a = m_db.GetAsset(uuid3);
  119. a3a.UploadAttempts = 0;
  120. Assert.That(a3a, Constraints.PropertyCompareConstraint(a3));
  121. scrambler.Scramble(a1a);
  122. scrambler.Scramble(a2a);
  123. scrambler.Scramble(a3a);
  124. m_db.StoreAsset(a1a);
  125. m_db.StoreAsset(a2a);
  126. m_db.StoreAsset(a3a);
  127. a1a.UploadAttempts = 0;
  128. a2a.UploadAttempts = 0;
  129. a3a.UploadAttempts = 0;
  130. AssetBase a1b = m_db.GetAsset(uuid1);
  131. a1b.UploadAttempts = 0;
  132. Assert.That(a1b, Constraints.PropertyCompareConstraint(a1a));
  133. AssetBase a2b = m_db.GetAsset(uuid2);
  134. a2b.UploadAttempts = 0;
  135. Assert.That(a2b, Constraints.PropertyCompareConstraint(a2a));
  136. AssetBase a3b = m_db.GetAsset(uuid3);
  137. a3b.UploadAttempts = 0;
  138. Assert.That(a3b, Constraints.PropertyCompareConstraint(a3a));
  139. bool[] exist = m_db.AssetsExist(new[] { uuid1, uuid2, uuid3 });
  140. Assert.IsTrue(exist[0]);
  141. Assert.IsTrue(exist[1]);
  142. Assert.IsTrue(exist[2]);
  143. List<AssetMetadata> metadatas = m_db.FetchAssetMetadataSet(0, 1000);
  144. Assert.That(metadatas.Count >= 3, "FetchAssetMetadataSet() should have returned at least 3 assets!");
  145. // It is possible that the Asset table is filled with data, in which case we don't try to find "our"
  146. // assets there:
  147. if (metadatas.Count < 1000)
  148. {
  149. AssetMetadata metadata = metadatas.Find(x => x.FullID == uuid1);
  150. Assert.That(metadata.Name, Is.EqualTo(a1b.Name));
  151. Assert.That(metadata.Description, Is.EqualTo(a1b.Description));
  152. Assert.That(metadata.Type, Is.EqualTo(a1b.Type));
  153. Assert.That(metadata.Temporary, Is.EqualTo(a1b.Temporary));
  154. Assert.That(metadata.FullID, Is.EqualTo(a1b.FullID));
  155. }
  156. }
  157. [Test]
  158. public void T020_CheckForWeirdCreatorID()
  159. {
  160. TestHelpers.InMethod();
  161. // It is expected that eventually the CreatorID might be an arbitrary string (an URI)
  162. // rather than a valid UUID (?). This test is to make sure that the database layer does not
  163. // attempt to convert CreatorID to GUID, but just passes it both ways as a string.
  164. AssetBase a1 = new AssetBase(uuid1, "asset one", (sbyte)AssetType.Texture, critter1);
  165. AssetBase a2 = new AssetBase(uuid2, "asset two", (sbyte)AssetType.Texture, "This is not a GUID!");
  166. AssetBase a3 = new AssetBase(uuid3, "asset three", (sbyte)AssetType.Texture, "");
  167. a1.Data = data1;
  168. a2.Data = data1;
  169. a3.Data = data1;
  170. m_db.StoreAsset(a1);
  171. a1.UploadAttempts = 0;
  172. m_db.StoreAsset(a2);
  173. a2.UploadAttempts = 0;
  174. m_db.StoreAsset(a3);
  175. a3.UploadAttempts = 0;
  176. AssetBase a1a = m_db.GetAsset(uuid1);
  177. a1a.UploadAttempts = 0;
  178. Assert.That(a1a, Constraints.PropertyCompareConstraint(a1));
  179. AssetBase a2a = m_db.GetAsset(uuid2);
  180. a2a.UploadAttempts = 0;
  181. Assert.That(a2a, Constraints.PropertyCompareConstraint(a2));
  182. AssetBase a3a = m_db.GetAsset(uuid3);
  183. a3a.UploadAttempts = 0;
  184. Assert.That(a3a, Constraints.PropertyCompareConstraint(a3));
  185. }
  186. }
  187. }