BasicCircuitTests.cs 14 KB

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