TextureSenderTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 NUnit.Framework.SyntaxHelpers;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Tests.Common;
  33. using OpenSim.Tests.Common.Mock;
  34. namespace OpenSim.Region.CoreModules.Agent.TextureSender
  35. {
  36. [TestFixture]
  37. public class UserTextureSenderTests
  38. {
  39. public UUID uuid1;
  40. public UUID uuid2;
  41. public UUID uuid3;
  42. public UUID uuid4;
  43. public int npackets, testsize;
  44. public TestClient client;
  45. public TextureSender ts;
  46. public static Random random = new Random();
  47. [TestFixtureSetUp]
  48. public void Init()
  49. {
  50. AgentCircuitData agent = new AgentCircuitData();
  51. agent.AgentID = UUID.Random();
  52. agent.firstname = "testfirstname";
  53. agent.lastname = "testlastname";
  54. agent.SessionID = UUID.Zero;
  55. agent.SecureSessionID = UUID.Zero;
  56. agent.circuitcode = 123;
  57. agent.BaseFolder = UUID.Zero;
  58. agent.InventoryFolder = UUID.Zero;
  59. agent.startpos = Vector3.Zero;
  60. agent.CapsPath = "http://wibble.com";
  61. client = new TestClient(agent, null);
  62. ts = new TextureSender(client, 0, 0);
  63. testsize = random.Next(5000,15000);
  64. npackets = CalculateNumPackets(testsize);
  65. uuid1 = UUID.Random();
  66. uuid2 = UUID.Random();
  67. uuid3 = UUID.Random();
  68. uuid4 = UUID.Random();
  69. }
  70. /// <summary>
  71. /// Test sending package
  72. /// </summary>
  73. [Test]
  74. public void T010_SendPkg()
  75. {
  76. TestHelper.InMethod();
  77. // Normal sending
  78. AssetBase abase = new AssetBase(uuid1, "asset one");
  79. byte[] abdata = new byte[testsize];
  80. random.NextBytes(abdata);
  81. abase.Data = abdata;
  82. bool isdone = false;
  83. ts.TextureReceived(abase);
  84. for (int i = 0; i < npackets; i++) {
  85. isdone = ts.SendTexturePacket();
  86. }
  87. Assert.That(isdone,Is.False);
  88. isdone = ts.SendTexturePacket();
  89. Assert.That(isdone,Is.True);
  90. }
  91. [Test]
  92. public void T011_UpdateReq()
  93. {
  94. TestHelper.InMethod();
  95. // Test packet number start
  96. AssetBase abase = new AssetBase(uuid2, "asset two");
  97. byte[] abdata = new byte[testsize];
  98. random.NextBytes(abdata);
  99. abase.Data = abdata;
  100. bool isdone = false;
  101. ts.TextureReceived(abase);
  102. ts.UpdateRequest(0,3);
  103. for (int i = 0; i < npackets-3; i++) {
  104. isdone = ts.SendTexturePacket();
  105. }
  106. Assert.That(isdone,Is.False);
  107. isdone = ts.SendTexturePacket();
  108. Assert.That(isdone,Is.True);
  109. // Test discard level
  110. abase = new AssetBase(uuid3, "asset three");
  111. abdata = new byte[testsize];
  112. random.NextBytes(abdata);
  113. abase.Data = abdata;
  114. isdone = false;
  115. ts.TextureReceived(abase);
  116. ts.UpdateRequest(-1,0);
  117. Assert.That(ts.SendTexturePacket(),Is.True);
  118. abase = new AssetBase(uuid4, "asset four");
  119. abdata = new byte[testsize];
  120. random.NextBytes(abdata);
  121. abase.Data = abdata;
  122. isdone = false;
  123. ts.TextureReceived(abase);
  124. ts.UpdateRequest(0,5);
  125. for (int i = 0; i < npackets-5; i++) {
  126. isdone = ts.SendTexturePacket();
  127. }
  128. Assert.That(isdone,Is.False);
  129. isdone = ts.SendTexturePacket();
  130. Assert.That(isdone,Is.True);
  131. }
  132. [Test]
  133. public void T999_FinishStatus()
  134. {
  135. TestHelper.InMethod();
  136. // Of the 4 assets "sent", only 2 sent the first part.
  137. Assert.That(client.sentdatapkt.Count,Is.EqualTo(2));
  138. // Sum of all packets sent:
  139. int totalpkts = (npackets) + (npackets - 2) + (npackets - 4);
  140. Assert.That(client.sentpktpkt.Count,Is.EqualTo(totalpkts));
  141. }
  142. /// <summary>
  143. /// Calculate the number of packets that will be required to send the texture loaded into this sender
  144. /// This is actually the number of 1000 byte packets not including an initial 600 byte packet...
  145. /// Borrowed from TextureSender.cs
  146. /// </summary>
  147. /// <param name="length"></param>
  148. /// <returns></returns>
  149. private int CalculateNumPackets(int length)
  150. {
  151. int numPackets = 0;
  152. if (length > 600)
  153. {
  154. //over 600 bytes so split up file
  155. int restData = (length - 600);
  156. int restPackets = ((restData + 999) / 1000);
  157. numPackets = restPackets;
  158. }
  159. return numPackets;
  160. }
  161. }
  162. }