BasicCircuitTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.Net;
  28. using log4net.Config;
  29. using Nini.Config;
  30. using NUnit.Framework;
  31. using NUnit.Framework.SyntaxHelpers;
  32. using OpenMetaverse;
  33. using OpenMetaverse.Packets;
  34. using OpenSim.Framework;
  35. using OpenSim.Tests.Common;
  36. namespace OpenSim.Region.ClientStack.LindenUDP.Tests
  37. {
  38. /// <summary>
  39. /// This will contain basic tests for the LindenUDP client stack
  40. /// </summary>
  41. [TestFixture]
  42. public class BasicCircuitTests
  43. {
  44. [SetUp]
  45. public void Init()
  46. {
  47. try
  48. {
  49. XmlConfigurator.Configure();
  50. }
  51. catch
  52. {
  53. // I don't care, just leave log4net off
  54. }
  55. }
  56. /// <summary>
  57. /// Add a client for testing
  58. /// </summary>
  59. /// <param name="scene"></param>
  60. /// <param name="testLLUDPServer"></param>
  61. /// <param name="testPacketServer"></param>
  62. /// <param name="acm">Agent circuit manager used in setting up the stack</param>
  63. protected void SetupStack(
  64. IScene scene, out TestLLUDPServer testLLUDPServer, out TestLLPacketServer testPacketServer,
  65. out AgentCircuitManager acm)
  66. {
  67. IConfigSource configSource = new IniConfigSource();
  68. ClientStackUserSettings userSettings = new ClientStackUserSettings();
  69. testLLUDPServer = new TestLLUDPServer();
  70. acm = new AgentCircuitManager();
  71. uint port = 666;
  72. testLLUDPServer.Initialise(null, ref port, 0, false, configSource, acm);
  73. testPacketServer = new TestLLPacketServer(testLLUDPServer, userSettings);
  74. testLLUDPServer.LocalScene = scene;
  75. }
  76. /// <summary>
  77. /// Set up a client for tests which aren't concerned with this process itself and where only one client is being
  78. /// tested
  79. /// </summary>
  80. /// <param name="circuitCode"></param>
  81. /// <param name="epSender"></param>
  82. /// <param name="testLLUDPServer"></param>
  83. /// <param name="acm"></param>
  84. protected void AddClient(
  85. uint circuitCode, EndPoint epSender, TestLLUDPServer testLLUDPServer, AgentCircuitManager acm)
  86. {
  87. UUID myAgentUuid = UUID.Parse("00000000-0000-0000-0000-000000000001");
  88. UUID mySessionUuid = UUID.Parse("00000000-0000-0000-0000-000000000002");
  89. AddClient(circuitCode, epSender, myAgentUuid, mySessionUuid, testLLUDPServer, acm);
  90. }
  91. /// <summary>
  92. /// Set up a client for tests which aren't concerned with this process itself
  93. /// </summary>
  94. /// <param name="circuitCode"></param>
  95. /// <param name="epSender"></param>
  96. /// <param name="agentId"></param>
  97. /// <param name="sessionId"></param>
  98. /// <param name="testLLUDPServer"></param>
  99. /// <param name="acm"></param>
  100. protected void AddClient(
  101. uint circuitCode, EndPoint epSender, UUID agentId, UUID sessionId,
  102. TestLLUDPServer testLLUDPServer, AgentCircuitManager acm)
  103. {
  104. AgentCircuitData acd = new AgentCircuitData();
  105. acd.AgentID = agentId;
  106. acd.SessionID = sessionId;
  107. UseCircuitCodePacket uccp = new UseCircuitCodePacket();
  108. UseCircuitCodePacket.CircuitCodeBlock uccpCcBlock
  109. = new UseCircuitCodePacket.CircuitCodeBlock();
  110. uccpCcBlock.Code = circuitCode;
  111. uccpCcBlock.ID = agentId;
  112. uccpCcBlock.SessionID = sessionId;
  113. uccp.CircuitCode = uccpCcBlock;
  114. acm.AddNewCircuit(circuitCode, acd);
  115. testLLUDPServer.LoadReceive(uccp, epSender);
  116. testLLUDPServer.ReceiveData(null);
  117. }
  118. /// <summary>
  119. /// Build an object name packet for test purposes
  120. /// </summary>
  121. /// <param name="objectLocalId"></param>
  122. /// <param name="objectName"></param>
  123. protected ObjectNamePacket BuildTestObjectNamePacket(uint objectLocalId, string objectName)
  124. {
  125. ObjectNamePacket onp = new ObjectNamePacket();
  126. ObjectNamePacket.ObjectDataBlock odb = new ObjectNamePacket.ObjectDataBlock();
  127. odb.LocalID = objectLocalId;
  128. odb.Name = Utils.StringToBytes(objectName);
  129. onp.ObjectData = new ObjectNamePacket.ObjectDataBlock[] { odb };
  130. onp.Header.Zerocoded = false;
  131. return onp;
  132. }
  133. /// <summary>
  134. /// Test adding a client to the stack
  135. /// </summary>
  136. [Test, LongRunning]
  137. public void TestAddClient()
  138. {
  139. TestHelper.InMethod();
  140. uint myCircuitCode = 123456;
  141. UUID myAgentUuid = UUID.Parse("00000000-0000-0000-0000-000000000001");
  142. UUID mySessionUuid = UUID.Parse("00000000-0000-0000-0000-000000000002");
  143. TestLLUDPServer testLLUDPServer;
  144. TestLLPacketServer testLLPacketServer;
  145. AgentCircuitManager acm;
  146. SetupStack(new MockScene(), out testLLUDPServer, out testLLPacketServer, out acm);
  147. AgentCircuitData acd = new AgentCircuitData();
  148. acd.AgentID = myAgentUuid;
  149. acd.SessionID = mySessionUuid;
  150. UseCircuitCodePacket uccp = new UseCircuitCodePacket();
  151. UseCircuitCodePacket.CircuitCodeBlock uccpCcBlock
  152. = new UseCircuitCodePacket.CircuitCodeBlock();
  153. uccpCcBlock.Code = myCircuitCode;
  154. uccpCcBlock.ID = myAgentUuid;
  155. uccpCcBlock.SessionID = mySessionUuid;
  156. uccp.CircuitCode = uccpCcBlock;
  157. EndPoint testEp = new IPEndPoint(IPAddress.Loopback, 999);
  158. testLLUDPServer.LoadReceive(uccp, testEp);
  159. testLLUDPServer.ReceiveData(null);
  160. // Circuit shouildn't exist since the circuit manager doesn't know about this circuit for authentication yet
  161. Assert.IsFalse(testLLUDPServer.HasCircuit(myCircuitCode));
  162. acm.AddNewCircuit(myCircuitCode, acd);
  163. testLLUDPServer.LoadReceive(uccp, testEp);
  164. testLLUDPServer.ReceiveData(null);
  165. // Should succeed now
  166. Assert.IsTrue(testLLUDPServer.HasCircuit(myCircuitCode));
  167. Assert.IsFalse(testLLUDPServer.HasCircuit(101));
  168. }
  169. /// <summary>
  170. /// Test removing a client from the stack
  171. /// </summary>
  172. [Test]
  173. public void TestRemoveClient()
  174. {
  175. TestHelper.InMethod();
  176. uint myCircuitCode = 123457;
  177. TestLLUDPServer testLLUDPServer;
  178. TestLLPacketServer testLLPacketServer;
  179. AgentCircuitManager acm;
  180. SetupStack(new MockScene(), out testLLUDPServer, out testLLPacketServer, out acm);
  181. AddClient(myCircuitCode, new IPEndPoint(IPAddress.Loopback, 1000), testLLUDPServer, acm);
  182. testLLUDPServer.RemoveClientCircuit(myCircuitCode);
  183. Assert.IsFalse(testLLUDPServer.HasCircuit(myCircuitCode));
  184. // Check that removing a non-existant circuit doesn't have any bad effects
  185. testLLUDPServer.RemoveClientCircuit(101);
  186. Assert.IsFalse(testLLUDPServer.HasCircuit(101));
  187. }
  188. /// <summary>
  189. /// Make sure that the client stack reacts okay to malformed packets
  190. /// </summary>
  191. [Test]
  192. public void TestMalformedPacketSend()
  193. {
  194. TestHelper.InMethod();
  195. uint myCircuitCode = 123458;
  196. EndPoint testEp = new IPEndPoint(IPAddress.Loopback, 1001);
  197. MockScene scene = new MockScene();
  198. TestLLUDPServer testLLUDPServer;
  199. TestLLPacketServer testLLPacketServer;
  200. AgentCircuitManager acm;
  201. SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
  202. AddClient(myCircuitCode, testEp, testLLUDPServer, acm);
  203. byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 };
  204. // Send two garbled 'packets' in succession
  205. testLLUDPServer.LoadReceive(data, testEp);
  206. testLLUDPServer.LoadReceive(data, testEp);
  207. testLLUDPServer.ReceiveData(null);
  208. // Check that we are still here
  209. Assert.IsTrue(testLLUDPServer.HasCircuit(myCircuitCode));
  210. Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(0));
  211. // Check that sending a valid packet to same circuit still succeeds
  212. Assert.That(scene.ObjectNameCallsReceived, Is.EqualTo(0));
  213. testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "helloooo"), testEp);
  214. testLLUDPServer.ReceiveData(null);
  215. Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(1));
  216. Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(1));
  217. }
  218. /// <summary>
  219. /// Test that the stack continues to work even if some client has caused a
  220. /// SocketException on Socket.BeginReceive()
  221. /// </summary>
  222. [Test]
  223. public void TestExceptionOnBeginReceive()
  224. {
  225. TestHelper.InMethod();
  226. MockScene scene = new MockScene();
  227. uint circuitCodeA = 130000;
  228. EndPoint epA = new IPEndPoint(IPAddress.Loopback, 1300);
  229. UUID agentIdA = UUID.Parse("00000000-0000-0000-0000-000000001300");
  230. UUID sessionIdA = UUID.Parse("00000000-0000-0000-0000-000000002300");
  231. uint circuitCodeB = 130001;
  232. EndPoint epB = new IPEndPoint(IPAddress.Loopback, 1301);
  233. UUID agentIdB = UUID.Parse("00000000-0000-0000-0000-000000001301");
  234. UUID sessionIdB = UUID.Parse("00000000-0000-0000-0000-000000002301");
  235. TestLLUDPServer testLLUDPServer;
  236. TestLLPacketServer testLLPacketServer;
  237. AgentCircuitManager acm;
  238. SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
  239. AddClient(circuitCodeA, epA, agentIdA, sessionIdA, testLLUDPServer, acm);
  240. AddClient(circuitCodeB, epB, agentIdB, sessionIdB, testLLUDPServer, acm);
  241. testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet1"), epA);
  242. testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet2"), epB);
  243. testLLUDPServer.LoadReceiveWithBeginException(epA);
  244. testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(2, "packet3"), epB);
  245. testLLUDPServer.ReceiveData(null);
  246. Assert.IsFalse(testLLUDPServer.HasCircuit(circuitCodeA));
  247. Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(3));
  248. Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(3));
  249. }
  250. }
  251. }