1
0

BasicCircuitTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.Region.Framework.Scenes;
  36. using OpenSim.Tests.Common;
  37. using OpenSim.Tests.Common.Mock;
  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. private TestLLUDPServer m_udpServer;
  48. [TestFixtureSetUp]
  49. public void FixtureInit()
  50. {
  51. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  52. Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
  53. }
  54. [TestFixtureTearDown]
  55. public void TearDown()
  56. {
  57. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  58. // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
  59. // tests really shouldn't).
  60. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  61. }
  62. [SetUp]
  63. public override void SetUp()
  64. {
  65. base.SetUp();
  66. m_scene = new SceneHelpers().SetupScene();
  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. return onp;
  82. }
  83. private void AddUdpServer()
  84. {
  85. AddUdpServer(new IniConfigSource());
  86. }
  87. private void AddUdpServer(IniConfigSource configSource)
  88. {
  89. uint port = 0;
  90. AgentCircuitManager acm = m_scene.AuthenticateHandler;
  91. m_udpServer = new TestLLUDPServer(IPAddress.Any, ref port, 0, false, configSource, acm);
  92. m_udpServer.AddScene(m_scene);
  93. }
  94. /// <summary>
  95. /// Used by tests that aren't testing this stage.
  96. /// </summary>
  97. private ScenePresence AddClient()
  98. {
  99. UUID myAgentUuid = TestHelpers.ParseTail(0x1);
  100. UUID mySessionUuid = TestHelpers.ParseTail(0x2);
  101. uint myCircuitCode = 123456;
  102. IPEndPoint testEp = new IPEndPoint(IPAddress.Loopback, 999);
  103. UseCircuitCodePacket uccp = new UseCircuitCodePacket();
  104. UseCircuitCodePacket.CircuitCodeBlock uccpCcBlock
  105. = new UseCircuitCodePacket.CircuitCodeBlock();
  106. uccpCcBlock.Code = myCircuitCode;
  107. uccpCcBlock.ID = myAgentUuid;
  108. uccpCcBlock.SessionID = mySessionUuid;
  109. uccp.CircuitCode = uccpCcBlock;
  110. byte[] uccpBytes = uccp.ToBytes();
  111. UDPPacketBuffer upb = new UDPPacketBuffer(testEp, uccpBytes.Length);
  112. upb.DataLength = uccpBytes.Length; // God knows why this isn't set by the constructor.
  113. Buffer.BlockCopy(uccpBytes, 0, upb.Data, 0, uccpBytes.Length);
  114. AgentCircuitData acd = new AgentCircuitData();
  115. acd.AgentID = myAgentUuid;
  116. acd.SessionID = mySessionUuid;
  117. m_scene.AuthenticateHandler.AddNewCircuit(myCircuitCode, acd);
  118. m_udpServer.PacketReceived(upb);
  119. return m_scene.GetScenePresence(myAgentUuid);
  120. }
  121. /// <summary>
  122. /// Test adding a client to the stack
  123. /// </summary>
  124. [Test]
  125. public void TestAddClient()
  126. {
  127. TestHelpers.InMethod();
  128. // TestHelpers.EnableLogging();
  129. AddUdpServer();
  130. UUID myAgentUuid = TestHelpers.ParseTail(0x1);
  131. UUID mySessionUuid = TestHelpers.ParseTail(0x2);
  132. uint myCircuitCode = 123456;
  133. IPEndPoint testEp = new IPEndPoint(IPAddress.Loopback, 999);
  134. UseCircuitCodePacket uccp = new UseCircuitCodePacket();
  135. UseCircuitCodePacket.CircuitCodeBlock uccpCcBlock
  136. = new UseCircuitCodePacket.CircuitCodeBlock();
  137. uccpCcBlock.Code = myCircuitCode;
  138. uccpCcBlock.ID = myAgentUuid;
  139. uccpCcBlock.SessionID = mySessionUuid;
  140. uccp.CircuitCode = uccpCcBlock;
  141. byte[] uccpBytes = uccp.ToBytes();
  142. UDPPacketBuffer upb = new UDPPacketBuffer(testEp, uccpBytes.Length);
  143. upb.DataLength = uccpBytes.Length; // God knows why this isn't set by the constructor.
  144. Buffer.BlockCopy(uccpBytes, 0, upb.Data, 0, uccpBytes.Length);
  145. m_udpServer.PacketReceived(upb);
  146. // Presence shouldn't exist since the circuit manager doesn't know about this circuit for authentication yet
  147. Assert.That(m_scene.GetScenePresence(myAgentUuid), Is.Null);
  148. AgentCircuitData acd = new AgentCircuitData();
  149. acd.AgentID = myAgentUuid;
  150. acd.SessionID = mySessionUuid;
  151. m_scene.AuthenticateHandler.AddNewCircuit(myCircuitCode, acd);
  152. m_udpServer.PacketReceived(upb);
  153. // Should succeed now
  154. ScenePresence sp = m_scene.GetScenePresence(myAgentUuid);
  155. Assert.That(sp.UUID, Is.EqualTo(myAgentUuid));
  156. Assert.That(m_udpServer.PacketsSent.Count, Is.EqualTo(1));
  157. Packet packet = m_udpServer.PacketsSent[0];
  158. Assert.That(packet, Is.InstanceOf(typeof(PacketAckPacket)));
  159. PacketAckPacket ackPacket = packet as PacketAckPacket;
  160. Assert.That(ackPacket.Packets.Length, Is.EqualTo(1));
  161. Assert.That(ackPacket.Packets[0].ID, Is.EqualTo(0));
  162. }
  163. [Test]
  164. public void TestLogoutClientDueToAck()
  165. {
  166. TestHelpers.InMethod();
  167. // TestHelpers.EnableLogging();
  168. IniConfigSource ics = new IniConfigSource();
  169. IConfig config = ics.AddConfig("ClientStack.LindenUDP");
  170. config.Set("AckTimeout", -1);
  171. AddUdpServer(ics);
  172. ScenePresence sp = AddClient();
  173. m_udpServer.ClientOutgoingPacketHandler(sp.ControllingClient, true, false, false);
  174. ScenePresence spAfterAckTimeout = m_scene.GetScenePresence(sp.UUID);
  175. Assert.That(spAfterAckTimeout, Is.Null);
  176. // TestHelpers.DisableLogging();
  177. }
  178. // /// <summary>
  179. // /// Test removing a client from the stack
  180. // /// </summary>
  181. // [Test]
  182. // public void TestRemoveClient()
  183. // {
  184. // TestHelper.InMethod();
  185. //
  186. // uint myCircuitCode = 123457;
  187. //
  188. // TestLLUDPServer testLLUDPServer;
  189. // TestLLPacketServer testLLPacketServer;
  190. // AgentCircuitManager acm;
  191. // SetupStack(new MockScene(), out testLLUDPServer, out testLLPacketServer, out acm);
  192. // AddClient(myCircuitCode, new IPEndPoint(IPAddress.Loopback, 1000), testLLUDPServer, acm);
  193. //
  194. // testLLUDPServer.RemoveClientCircuit(myCircuitCode);
  195. // Assert.IsFalse(testLLUDPServer.HasCircuit(myCircuitCode));
  196. //
  197. // // Check that removing a non-existant circuit doesn't have any bad effects
  198. // testLLUDPServer.RemoveClientCircuit(101);
  199. // Assert.IsFalse(testLLUDPServer.HasCircuit(101));
  200. // }
  201. //
  202. // /// <summary>
  203. // /// Make sure that the client stack reacts okay to malformed packets
  204. // /// </summary>
  205. // [Test]
  206. // public void TestMalformedPacketSend()
  207. // {
  208. // TestHelper.InMethod();
  209. //
  210. // uint myCircuitCode = 123458;
  211. // EndPoint testEp = new IPEndPoint(IPAddress.Loopback, 1001);
  212. // MockScene scene = new MockScene();
  213. //
  214. // TestLLUDPServer testLLUDPServer;
  215. // TestLLPacketServer testLLPacketServer;
  216. // AgentCircuitManager acm;
  217. // SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
  218. // AddClient(myCircuitCode, testEp, testLLUDPServer, acm);
  219. //
  220. // byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 };
  221. //
  222. // // Send two garbled 'packets' in succession
  223. // testLLUDPServer.LoadReceive(data, testEp);
  224. // testLLUDPServer.LoadReceive(data, testEp);
  225. // testLLUDPServer.ReceiveData(null);
  226. //
  227. // // Check that we are still here
  228. // Assert.IsTrue(testLLUDPServer.HasCircuit(myCircuitCode));
  229. // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(0));
  230. //
  231. // // Check that sending a valid packet to same circuit still succeeds
  232. // Assert.That(scene.ObjectNameCallsReceived, Is.EqualTo(0));
  233. //
  234. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "helloooo"), testEp);
  235. // testLLUDPServer.ReceiveData(null);
  236. //
  237. // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(1));
  238. // Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(1));
  239. // }
  240. //
  241. // /// <summary>
  242. // /// Test that the stack continues to work even if some client has caused a
  243. // /// SocketException on Socket.BeginReceive()
  244. // /// </summary>
  245. // [Test]
  246. // public void TestExceptionOnBeginReceive()
  247. // {
  248. // TestHelper.InMethod();
  249. //
  250. // MockScene scene = new MockScene();
  251. //
  252. // uint circuitCodeA = 130000;
  253. // EndPoint epA = new IPEndPoint(IPAddress.Loopback, 1300);
  254. // UUID agentIdA = UUID.Parse("00000000-0000-0000-0000-000000001300");
  255. // UUID sessionIdA = UUID.Parse("00000000-0000-0000-0000-000000002300");
  256. //
  257. // uint circuitCodeB = 130001;
  258. // EndPoint epB = new IPEndPoint(IPAddress.Loopback, 1301);
  259. // UUID agentIdB = UUID.Parse("00000000-0000-0000-0000-000000001301");
  260. // UUID sessionIdB = UUID.Parse("00000000-0000-0000-0000-000000002301");
  261. //
  262. // TestLLUDPServer testLLUDPServer;
  263. // TestLLPacketServer testLLPacketServer;
  264. // AgentCircuitManager acm;
  265. // SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
  266. // AddClient(circuitCodeA, epA, agentIdA, sessionIdA, testLLUDPServer, acm);
  267. // AddClient(circuitCodeB, epB, agentIdB, sessionIdB, testLLUDPServer, acm);
  268. //
  269. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet1"), epA);
  270. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet2"), epB);
  271. // testLLUDPServer.LoadReceiveWithBeginException(epA);
  272. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(2, "packet3"), epB);
  273. // testLLUDPServer.ReceiveData(null);
  274. //
  275. // Assert.IsFalse(testLLUDPServer.HasCircuit(circuitCodeA));
  276. //
  277. // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(3));
  278. // Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(3));
  279. // }
  280. }
  281. }