BasicCircuitTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. /*
  89. [Test]
  90. public void TestAddClient()
  91. {
  92. TestHelpers.InMethod();
  93. // TestHelpers.EnableLogging();
  94. TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(m_scene);
  95. UUID myAgentUuid = TestHelpers.ParseTail(0x1);
  96. UUID mySessionUuid = TestHelpers.ParseTail(0x2);
  97. uint myCircuitCode = 123456;
  98. IPEndPoint testEp = new IPEndPoint(IPAddress.Loopback, 999);
  99. UseCircuitCodePacket uccp = new UseCircuitCodePacket();
  100. UseCircuitCodePacket.CircuitCodeBlock uccpCcBlock
  101. = new UseCircuitCodePacket.CircuitCodeBlock();
  102. uccpCcBlock.Code = myCircuitCode;
  103. uccpCcBlock.ID = myAgentUuid;
  104. uccpCcBlock.SessionID = mySessionUuid;
  105. uccp.CircuitCode = uccpCcBlock;
  106. byte[] uccpBytes = uccp.ToBytes();
  107. UDPPacketBuffer upb = new UDPPacketBuffer(testEp, uccpBytes.Length);
  108. upb.DataLength = uccpBytes.Length; // God knows why this isn't set by the constructor.
  109. Buffer.BlockCopy(uccpBytes, 0, upb.Data, 0, uccpBytes.Length);
  110. udpServer.PacketReceived(upb);
  111. // Presence shouldn't exist since the circuit manager doesn't know about this circuit for authentication yet
  112. Assert.That(m_scene.GetScenePresence(myAgentUuid), Is.Null);
  113. AgentCircuitData acd = new AgentCircuitData();
  114. acd.AgentID = myAgentUuid;
  115. acd.SessionID = mySessionUuid;
  116. m_scene.AuthenticateHandler.AddNewCircuit(myCircuitCode, acd);
  117. udpServer.PacketReceived(upb);
  118. // Should succeed now
  119. ScenePresence sp = m_scene.GetScenePresence(myAgentUuid);
  120. Assert.That(sp.UUID, Is.EqualTo(myAgentUuid));
  121. Assert.That(udpServer.PacketsSent.Count, Is.EqualTo(1));
  122. Packet packet = udpServer.PacketsSent[0];
  123. Assert.That(packet, Is.InstanceOf(typeof(PacketAckPacket)));
  124. PacketAckPacket ackPacket = packet as PacketAckPacket;
  125. Assert.That(ackPacket.Packets.Length, Is.EqualTo(1));
  126. Assert.That(ackPacket.Packets[0].ID, Is.EqualTo(0));
  127. }
  128. [Test]
  129. public void TestLogoutClientDueToAck()
  130. {
  131. TestHelpers.InMethod();
  132. // TestHelpers.EnableLogging();
  133. IniConfigSource ics = new IniConfigSource();
  134. IConfig config = ics.AddConfig("ClientStack.LindenUDP");
  135. config.Set("AckTimeout", -1);
  136. TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(m_scene, ics);
  137. ScenePresence sp
  138. = ClientStackHelpers.AddChildClient(
  139. m_scene, udpServer, TestHelpers.ParseTail(0x1), TestHelpers.ParseTail(0x2), 123456);
  140. udpServer.ClientOutgoingPacketHandler(sp.ControllingClient, true, false, false);
  141. ScenePresence spAfterAckTimeout = m_scene.GetScenePresence(sp.UUID);
  142. Assert.That(spAfterAckTimeout, Is.Null);
  143. }
  144. */
  145. // /// <summary>
  146. // /// Test removing a client from the stack
  147. // /// </summary>
  148. // [Test]
  149. // public void TestRemoveClient()
  150. // {
  151. // TestHelper.InMethod();
  152. //
  153. // uint myCircuitCode = 123457;
  154. //
  155. // TestLLUDPServer testLLUDPServer;
  156. // TestLLPacketServer testLLPacketServer;
  157. // AgentCircuitManager acm;
  158. // SetupStack(new MockScene(), out testLLUDPServer, out testLLPacketServer, out acm);
  159. // AddClient(myCircuitCode, new IPEndPoint(IPAddress.Loopback, 1000), testLLUDPServer, acm);
  160. //
  161. // testLLUDPServer.RemoveClientCircuit(myCircuitCode);
  162. // Assert.IsFalse(testLLUDPServer.HasCircuit(myCircuitCode));
  163. //
  164. // // Check that removing a non-existent circuit doesn't have any bad effects
  165. // testLLUDPServer.RemoveClientCircuit(101);
  166. // Assert.IsFalse(testLLUDPServer.HasCircuit(101));
  167. // }
  168. //
  169. // /// <summary>
  170. // /// Make sure that the client stack reacts okay to malformed packets
  171. // /// </summary>
  172. // [Test]
  173. // public void TestMalformedPacketSend()
  174. // {
  175. // TestHelper.InMethod();
  176. //
  177. // uint myCircuitCode = 123458;
  178. // EndPoint testEp = new IPEndPoint(IPAddress.Loopback, 1001);
  179. // MockScene scene = new MockScene();
  180. //
  181. // TestLLUDPServer testLLUDPServer;
  182. // TestLLPacketServer testLLPacketServer;
  183. // AgentCircuitManager acm;
  184. // SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
  185. // AddClient(myCircuitCode, testEp, testLLUDPServer, acm);
  186. //
  187. // byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 };
  188. //
  189. // // Send two garbled 'packets' in succession
  190. // testLLUDPServer.LoadReceive(data, testEp);
  191. // testLLUDPServer.LoadReceive(data, testEp);
  192. // testLLUDPServer.ReceiveData(null);
  193. //
  194. // // Check that we are still here
  195. // Assert.IsTrue(testLLUDPServer.HasCircuit(myCircuitCode));
  196. // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(0));
  197. //
  198. // // Check that sending a valid packet to same circuit still succeeds
  199. // Assert.That(scene.ObjectNameCallsReceived, Is.EqualTo(0));
  200. //
  201. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "helloooo"), testEp);
  202. // testLLUDPServer.ReceiveData(null);
  203. //
  204. // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(1));
  205. // Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(1));
  206. // }
  207. //
  208. // /// <summary>
  209. // /// Test that the stack continues to work even if some client has caused a
  210. // /// SocketException on Socket.BeginReceive()
  211. // /// </summary>
  212. // [Test]
  213. // public void TestExceptionOnBeginReceive()
  214. // {
  215. // TestHelper.InMethod();
  216. //
  217. // MockScene scene = new MockScene();
  218. //
  219. // uint circuitCodeA = 130000;
  220. // EndPoint epA = new IPEndPoint(IPAddress.Loopback, 1300);
  221. // UUID agentIdA = UUID.Parse("00000000-0000-0000-0000-000000001300");
  222. // UUID sessionIdA = UUID.Parse("00000000-0000-0000-0000-000000002300");
  223. //
  224. // uint circuitCodeB = 130001;
  225. // EndPoint epB = new IPEndPoint(IPAddress.Loopback, 1301);
  226. // UUID agentIdB = UUID.Parse("00000000-0000-0000-0000-000000001301");
  227. // UUID sessionIdB = UUID.Parse("00000000-0000-0000-0000-000000002301");
  228. //
  229. // TestLLUDPServer testLLUDPServer;
  230. // TestLLPacketServer testLLPacketServer;
  231. // AgentCircuitManager acm;
  232. // SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
  233. // AddClient(circuitCodeA, epA, agentIdA, sessionIdA, testLLUDPServer, acm);
  234. // AddClient(circuitCodeB, epB, agentIdB, sessionIdB, testLLUDPServer, acm);
  235. //
  236. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet1"), epA);
  237. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet2"), epB);
  238. // testLLUDPServer.LoadReceiveWithBeginException(epA);
  239. // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(2, "packet3"), epB);
  240. // testLLUDPServer.ReceiveData(null);
  241. //
  242. // Assert.IsFalse(testLLUDPServer.HasCircuit(circuitCodeA));
  243. //
  244. // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(3));
  245. // Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(3));
  246. // }
  247. }
  248. }