BasicCircuitTests.cs 13 KB

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