BasicCircuitTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.Net;
  29. using log4net.Config;
  30. using Nini.Config;
  31. using NUnit.Framework;
  32. using OpenMetaverse;
  33. using OpenMetaverse.Packets;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Monitoring;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Tests.Common;
  38. namespace OpenSim.Region.ClientStack.LindenUDP.Tests
  39. {
  40. /// <summary>
  41. /// This will contain basic tests for the LindenUDP client stack
  42. /// </summary>
  43. [TestFixture]
  44. public class BasicCircuitTests : OpenSimTestCase
  45. {
  46. private Scene m_scene;
  47. [TestFixtureSetUp]
  48. public void FixtureInit()
  49. {
  50. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  51. Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
  52. }
  53. [TestFixtureTearDown]
  54. public void TearDown()
  55. {
  56. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  57. // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
  58. // tests really shouldn't).
  59. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  60. }
  61. [SetUp]
  62. public override void SetUp()
  63. {
  64. base.SetUp();
  65. m_scene = new SceneHelpers().SetupScene();
  66. StatsManager.SimExtraStats = new SimExtraStatsCollector();
  67. }
  68. // /// <summary>
  69. // /// Build an object name packet for test purposes
  70. // /// </summary>
  71. // /// <param name="objectLocalId"></param>
  72. // /// <param name="objectName"></param>
  73. // private ObjectNamePacket BuildTestObjectNamePacket(uint objectLocalId, string objectName)
  74. // {
  75. // ObjectNamePacket onp = new ObjectNamePacket();
  76. // ObjectNamePacket.ObjectDataBlock odb = new ObjectNamePacket.ObjectDataBlock();
  77. // odb.LocalID = objectLocalId;
  78. // odb.Name = Utils.StringToBytes(objectName);
  79. // onp.ObjectData = new ObjectNamePacket.ObjectDataBlock[] { odb };
  80. // onp.Header.Zerocoded = false;
  81. //
  82. // return onp;
  83. // }
  84. //
  85. /// <summary>
  86. /// Test adding a client to the stack
  87. /// </summary>
  88. [Test]
  89. public void TestAddClient()
  90. {
  91. TestHelpers.InMethod();
  92. // TestHelpers.EnableLogging();
  93. TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(m_scene);
  94. UUID myAgentUuid = TestHelpers.ParseTail(0x1);
  95. UUID mySessionUuid = TestHelpers.ParseTail(0x2);
  96. uint myCircuitCode = 123456;
  97. IPEndPoint testEp = new IPEndPoint(IPAddress.Loopback, 999);
  98. UseCircuitCodePacket uccp = new UseCircuitCodePacket();
  99. UseCircuitCodePacket.CircuitCodeBlock uccpCcBlock
  100. = new UseCircuitCodePacket.CircuitCodeBlock();
  101. uccpCcBlock.Code = myCircuitCode;
  102. uccpCcBlock.ID = myAgentUuid;
  103. uccpCcBlock.SessionID = mySessionUuid;
  104. uccp.CircuitCode = uccpCcBlock;
  105. byte[] uccpBytes = uccp.ToBytes();
  106. UDPPacketBuffer upb = new UDPPacketBuffer(testEp, uccpBytes.Length);
  107. upb.DataLength = uccpBytes.Length; // God knows why this isn't set by the constructor.
  108. Buffer.BlockCopy(uccpBytes, 0, upb.Data, 0, uccpBytes.Length);
  109. udpServer.PacketReceived(upb);
  110. // Presence shouldn't exist since the circuit manager doesn't know about this circuit for authentication yet
  111. Assert.That(m_scene.GetScenePresence(myAgentUuid), Is.Null);
  112. AgentCircuitData acd = new AgentCircuitData();
  113. acd.AgentID = myAgentUuid;
  114. acd.SessionID = mySessionUuid;
  115. m_scene.AuthenticateHandler.AddNewCircuit(myCircuitCode, acd);
  116. udpServer.PacketReceived(upb);
  117. // Should succeed now
  118. ScenePresence sp = m_scene.GetScenePresence(myAgentUuid);
  119. Assert.That(sp.UUID, Is.EqualTo(myAgentUuid));
  120. Assert.That(udpServer.PacketsSent.Count, Is.EqualTo(1));
  121. Packet packet = udpServer.PacketsSent[0];
  122. Assert.That(packet, Is.InstanceOf(typeof(PacketAckPacket)));
  123. PacketAckPacket ackPacket = packet as PacketAckPacket;
  124. Assert.That(ackPacket.Packets.Length, Is.EqualTo(1));
  125. Assert.That(ackPacket.Packets[0].ID, Is.EqualTo(0));
  126. }
  127. [Test]
  128. public void TestLogoutClientDueToAck()
  129. {
  130. TestHelpers.InMethod();
  131. // TestHelpers.EnableLogging();
  132. IniConfigSource ics = new IniConfigSource();
  133. IConfig config = ics.AddConfig("ClientStack.LindenUDP");
  134. config.Set("AckTimeout", -1);
  135. TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(m_scene, ics);
  136. ScenePresence sp
  137. = ClientStackHelpers.AddChildClient(
  138. m_scene, udpServer, TestHelpers.ParseTail(0x1), TestHelpers.ParseTail(0x2), 123456);
  139. udpServer.ClientOutgoingPacketHandler(sp.ControllingClient, true, false, false);
  140. ScenePresence spAfterAckTimeout = m_scene.GetScenePresence(sp.UUID);
  141. Assert.That(spAfterAckTimeout, Is.Null);
  142. }
  143. // /// <summary>
  144. // /// Test removing a client from the stack
  145. // /// </summary>
  146. // [Test]
  147. // public void TestRemoveClient()
  148. // {
  149. // TestHelper.InMethod();
  150. //
  151. // uint myCircuitCode = 123457;
  152. //
  153. // TestLLUDPServer testLLUDPServer;
  154. // TestLLPacketServer testLLPacketServer;
  155. // AgentCircuitManager acm;
  156. // SetupStack(new MockScene(), out testLLUDPServer, out testLLPacketServer, out acm);
  157. // AddClient(myCircuitCode, new IPEndPoint(IPAddress.Loopback, 1000), testLLUDPServer, acm);
  158. //
  159. // testLLUDPServer.RemoveClientCircuit(myCircuitCode);
  160. // Assert.IsFalse(testLLUDPServer.HasCircuit(myCircuitCode));
  161. //
  162. // // Check that removing a non-existent circuit doesn't have any bad effects
  163. // testLLUDPServer.RemoveClientCircuit(101);
  164. // Assert.IsFalse(testLLUDPServer.HasCircuit(101));
  165. // }
  166. //
  167. // /// <summary>
  168. // /// Make sure that the client stack reacts okay to malformed packets
  169. // /// </summary>
  170. // [Test]
  171. // public void TestMalformedPacketSend()
  172. // {
  173. // TestHelper.InMethod();
  174. //
  175. // uint myCircuitCode = 123458;
  176. // EndPoint testEp = new IPEndPoint(IPAddress.Loopback, 1001);
  177. // MockScene scene = new MockScene();
  178. //
  179. // TestLLUDPServer testLLUDPServer;
  180. // TestLLPacketServer testLLPacketServer;
  181. // AgentCircuitManager acm;
  182. // SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
  183. // AddClient(myCircuitCode, testEp, testLLUDPServer, acm);
  184. //
  185. // byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 };
  186. //
  187. // // Send two garbled 'packets' in succession
  188. // testLLUDPServer.LoadReceive(data, testEp);
  189. // testLLUDPServer.LoadReceive(data, testEp);
  190. // testLLUDPServer.ReceiveData(null);
  191. //
  192. // // Check that we are still here
  193. // Assert.IsTrue(testLLUDPServer.HasCircuit(myCircuitCode));
  194. // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(0));
  195. //
  196. // // Check that sending a valid packet to same circuit still succeeds
  197. // Assert.That(scene.ObjectNameCallsReceived, Is.EqualTo(0));
  198. //
  199. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "helloooo"), testEp);
  200. // testLLUDPServer.ReceiveData(null);
  201. //
  202. // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(1));
  203. // Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(1));
  204. // }
  205. //
  206. // /// <summary>
  207. // /// Test that the stack continues to work even if some client has caused a
  208. // /// SocketException on Socket.BeginReceive()
  209. // /// </summary>
  210. // [Test]
  211. // public void TestExceptionOnBeginReceive()
  212. // {
  213. // TestHelper.InMethod();
  214. //
  215. // MockScene scene = new MockScene();
  216. //
  217. // uint circuitCodeA = 130000;
  218. // EndPoint epA = new IPEndPoint(IPAddress.Loopback, 1300);
  219. // UUID agentIdA = UUID.Parse("00000000-0000-0000-0000-000000001300");
  220. // UUID sessionIdA = UUID.Parse("00000000-0000-0000-0000-000000002300");
  221. //
  222. // uint circuitCodeB = 130001;
  223. // EndPoint epB = new IPEndPoint(IPAddress.Loopback, 1301);
  224. // UUID agentIdB = UUID.Parse("00000000-0000-0000-0000-000000001301");
  225. // UUID sessionIdB = UUID.Parse("00000000-0000-0000-0000-000000002301");
  226. //
  227. // TestLLUDPServer testLLUDPServer;
  228. // TestLLPacketServer testLLPacketServer;
  229. // AgentCircuitManager acm;
  230. // SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
  231. // AddClient(circuitCodeA, epA, agentIdA, sessionIdA, testLLUDPServer, acm);
  232. // AddClient(circuitCodeB, epB, agentIdB, sessionIdB, testLLUDPServer, acm);
  233. //
  234. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet1"), epA);
  235. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet2"), epB);
  236. // testLLUDPServer.LoadReceiveWithBeginException(epA);
  237. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(2, "packet3"), epB);
  238. // testLLUDPServer.ReceiveData(null);
  239. //
  240. // Assert.IsFalse(testLLUDPServer.HasCircuit(circuitCodeA));
  241. //
  242. // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(3));
  243. // Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(3));
  244. // }
  245. }
  246. }