TextureSenderTests.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 OpenSim 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;
  29. using NUnit.Framework;
  30. using NUnit.Framework.SyntaxHelpers;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Tests.Common.Mock;
  34. namespace OpenSim.Region.Environment.Modules.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. // Normal sending
  77. AssetBase abase = new AssetBase(uuid1, "asset one");
  78. byte[] abdata = new byte[testsize];
  79. random.NextBytes(abdata);
  80. abase.Data = abdata;
  81. bool isdone = false;
  82. ts.TextureReceived(abase);
  83. for (int i = 0; i < npackets; i++) {
  84. isdone = ts.SendTexturePacket();
  85. }
  86. Assert.That(isdone,Is.False);
  87. isdone = ts.SendTexturePacket();
  88. Assert.That(isdone,Is.True);
  89. }
  90. [Test]
  91. public void T011_UpdateReq()
  92. {
  93. // Test packet number start
  94. AssetBase abase = new AssetBase(uuid2, "asset two");
  95. byte[] abdata = new byte[testsize];
  96. random.NextBytes(abdata);
  97. abase.Data = abdata;
  98. bool isdone = false;
  99. ts.TextureReceived(abase);
  100. ts.UpdateRequest(0,3);
  101. for (int i = 0; i < npackets-3; i++) {
  102. isdone = ts.SendTexturePacket();
  103. }
  104. Assert.That(isdone,Is.False);
  105. isdone = ts.SendTexturePacket();
  106. Assert.That(isdone,Is.True);
  107. // Test discard level
  108. abase = new AssetBase(uuid3, "asset three");
  109. abdata = new byte[testsize];
  110. random.NextBytes(abdata);
  111. abase.Data = abdata;
  112. isdone = false;
  113. ts.TextureReceived(abase);
  114. ts.UpdateRequest(-1,0);
  115. Assert.That(ts.SendTexturePacket(),Is.True);
  116. abase = new AssetBase(uuid4, "asset four");
  117. abdata = new byte[testsize];
  118. random.NextBytes(abdata);
  119. abase.Data = abdata;
  120. isdone = false;
  121. ts.TextureReceived(abase);
  122. ts.UpdateRequest(0,5);
  123. for (int i = 0; i < npackets-5; i++) {
  124. isdone = ts.SendTexturePacket();
  125. }
  126. Assert.That(isdone,Is.False);
  127. isdone = ts.SendTexturePacket();
  128. Assert.That(isdone,Is.True);
  129. }
  130. [Test]
  131. public void T999_FinishStatus()
  132. {
  133. // Of the 4 assets "sent", only 2 sent the first part.
  134. Assert.That(client.sentdatapkt.Count,Is.EqualTo(2));
  135. // Sum of all packets sent:
  136. int totalpkts = (npackets) + (npackets - 2) + (npackets - 4);
  137. Assert.That(client.sentpktpkt.Count,Is.EqualTo(totalpkts));
  138. }
  139. /// <summary>
  140. /// Calculate the number of packets that will be required to send the texture loaded into this sender
  141. /// This is actually the number of 1000 byte packets not including an initial 600 byte packet...
  142. /// Borrowed from TextureSender.cs
  143. /// </summary>
  144. /// <param name="length"></param>
  145. /// <returns></returns>
  146. private int CalculateNumPackets(int length)
  147. {
  148. int numPackets = 0;
  149. if (length > 600)
  150. {
  151. //over 600 bytes so split up file
  152. int restData = (length - 600);
  153. int restPackets = ((restData + 999) / 1000);
  154. numPackets = restPackets;
  155. }
  156. return numPackets;
  157. }
  158. }
  159. }