BasicCircuitTests.cs 13 KB

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