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