BasicCircuitTests.cs 13 KB

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