BasicCircuitTests.cs 13 KB

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