OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
BasicCircuitTests.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Net;
30 using log4net.Config;
31 using Nini.Config;
32 using NUnit.Framework;
33 using OpenMetaverse;
34 using OpenMetaverse.Packets;
35 using OpenSim.Framework;
36 using OpenSim.Framework.Monitoring;
37 using OpenSim.Region.Framework.Scenes;
38 using OpenSim.Tests.Common;
39 
40 namespace OpenSim.Region.ClientStack.LindenUDP.Tests
41 {
45  [TestFixture]
47  {
48  private Scene m_scene;
49 
50  [TestFixtureSetUp]
51  public void FixtureInit()
52  {
53  // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
54  Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
55  }
56 
57  [TestFixtureTearDown]
58  public void TearDown()
59  {
60  // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
61  // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
62  // tests really shouldn't).
63  Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
64  }
65 
66  [SetUp]
67  public override void SetUp()
68  {
69  base.SetUp();
70  m_scene = new SceneHelpers().SetupScene();
71  StatsManager.SimExtraStats = new SimExtraStatsCollector();
72  }
73 
74 // /// <summary>
75 // /// Build an object name packet for test purposes
76 // /// </summary>
77 // /// <param name="objectLocalId"></param>
78 // /// <param name="objectName"></param>
79 // private ObjectNamePacket BuildTestObjectNamePacket(uint objectLocalId, string objectName)
80 // {
81 // ObjectNamePacket onp = new ObjectNamePacket();
82 // ObjectNamePacket.ObjectDataBlock odb = new ObjectNamePacket.ObjectDataBlock();
83 // odb.LocalID = objectLocalId;
84 // odb.Name = Utils.StringToBytes(objectName);
85 // onp.ObjectData = new ObjectNamePacket.ObjectDataBlock[] { odb };
86 // onp.Header.Zerocoded = false;
87 //
88 // return onp;
89 // }
90 //
94  [Test]
95  public void TestAddClient()
96  {
97  TestHelpers.InMethod();
98 // TestHelpers.EnableLogging();
99 
100  TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(m_scene);
101 
102  UUID myAgentUuid = TestHelpers.ParseTail(0x1);
103  UUID mySessionUuid = TestHelpers.ParseTail(0x2);
104  uint myCircuitCode = 123456;
105  IPEndPoint testEp = new IPEndPoint(IPAddress.Loopback, 999);
106 
107  UseCircuitCodePacket uccp = new UseCircuitCodePacket();
108 
109  UseCircuitCodePacket.CircuitCodeBlock uccpCcBlock
110  = new UseCircuitCodePacket.CircuitCodeBlock();
111  uccpCcBlock.Code = myCircuitCode;
112  uccpCcBlock.ID = myAgentUuid;
113  uccpCcBlock.SessionID = mySessionUuid;
114  uccp.CircuitCode = uccpCcBlock;
115 
116  byte[] uccpBytes = uccp.ToBytes();
117  UDPPacketBuffer upb = new UDPPacketBuffer(testEp, uccpBytes.Length);
118  upb.DataLength = uccpBytes.Length; // God knows why this isn't set by the constructor.
119  Buffer.BlockCopy(uccpBytes, 0, upb.Data, 0, uccpBytes.Length);
120 
121  udpServer.PacketReceived(upb);
122 
123  // Presence shouldn't exist since the circuit manager doesn't know about this circuit for authentication yet
124  Assert.That(m_scene.GetScenePresence(myAgentUuid), Is.Null);
125 
127  acd.AgentID = myAgentUuid;
128  acd.SessionID = mySessionUuid;
129 
130  m_scene.AuthenticateHandler.AddNewCircuit(myCircuitCode, acd);
131 
132  udpServer.PacketReceived(upb);
133 
134  // Should succeed now
135  ScenePresence sp = m_scene.GetScenePresence(myAgentUuid);
136  Assert.That(sp.UUID, Is.EqualTo(myAgentUuid));
137 
138  Assert.That(udpServer.PacketsSent.Count, Is.EqualTo(1));
139 
140  Packet packet = udpServer.PacketsSent[0];
141  Assert.That(packet, Is.InstanceOf(typeof(PacketAckPacket)));
142 
143  PacketAckPacket ackPacket = packet as PacketAckPacket;
144  Assert.That(ackPacket.Packets.Length, Is.EqualTo(1));
145  Assert.That(ackPacket.Packets[0].ID, Is.EqualTo(0));
146  }
147 
148  [Test]
150  {
151  TestHelpers.InMethod();
152 // TestHelpers.EnableLogging();
153 
154  IniConfigSource ics = new IniConfigSource();
155  IConfig config = ics.AddConfig("ClientStack.LindenUDP");
156  config.Set("AckTimeout", -1);
157  TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(m_scene, ics);
158 
159  ScenePresence sp
160  = ClientStackHelpers.AddChildClient(
161  m_scene, udpServer, TestHelpers.ParseTail(0x1), TestHelpers.ParseTail(0x2), 123456);
162 
163  udpServer.ClientOutgoingPacketHandler(sp.ControllingClient, true, false, false);
164 
165  ScenePresence spAfterAckTimeout = m_scene.GetScenePresence(sp.UUID);
166  Assert.That(spAfterAckTimeout, Is.Null);
167  }
168 
169 // /// <summary>
170 // /// Test removing a client from the stack
171 // /// </summary>
172 // [Test]
173 // public void TestRemoveClient()
174 // {
175 // TestHelper.InMethod();
176 //
177 // uint myCircuitCode = 123457;
178 //
179 // TestLLUDPServer testLLUDPServer;
180 // TestLLPacketServer testLLPacketServer;
181 // AgentCircuitManager acm;
182 // SetupStack(new MockScene(), out testLLUDPServer, out testLLPacketServer, out acm);
183 // AddClient(myCircuitCode, new IPEndPoint(IPAddress.Loopback, 1000), testLLUDPServer, acm);
184 //
185 // testLLUDPServer.RemoveClientCircuit(myCircuitCode);
186 // Assert.IsFalse(testLLUDPServer.HasCircuit(myCircuitCode));
187 //
188 // // Check that removing a non-existent circuit doesn't have any bad effects
189 // testLLUDPServer.RemoveClientCircuit(101);
190 // Assert.IsFalse(testLLUDPServer.HasCircuit(101));
191 // }
192 //
193 // /// <summary>
194 // /// Make sure that the client stack reacts okay to malformed packets
195 // /// </summary>
196 // [Test]
197 // public void TestMalformedPacketSend()
198 // {
199 // TestHelper.InMethod();
200 //
201 // uint myCircuitCode = 123458;
202 // EndPoint testEp = new IPEndPoint(IPAddress.Loopback, 1001);
203 // MockScene scene = new MockScene();
204 //
205 // TestLLUDPServer testLLUDPServer;
206 // TestLLPacketServer testLLPacketServer;
207 // AgentCircuitManager acm;
208 // SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
209 // AddClient(myCircuitCode, testEp, testLLUDPServer, acm);
210 //
211 // byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 };
212 //
213 // // Send two garbled 'packets' in succession
214 // testLLUDPServer.LoadReceive(data, testEp);
215 // testLLUDPServer.LoadReceive(data, testEp);
216 // testLLUDPServer.ReceiveData(null);
217 //
218 // // Check that we are still here
219 // Assert.IsTrue(testLLUDPServer.HasCircuit(myCircuitCode));
220 // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(0));
221 //
222 // // Check that sending a valid packet to same circuit still succeeds
223 // Assert.That(scene.ObjectNameCallsReceived, Is.EqualTo(0));
224 //
225 // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "helloooo"), testEp);
226 // testLLUDPServer.ReceiveData(null);
227 //
228 // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(1));
229 // Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(1));
230 // }
231 //
232 // /// <summary>
233 // /// Test that the stack continues to work even if some client has caused a
234 // /// SocketException on Socket.BeginReceive()
235 // /// </summary>
236 // [Test]
237 // public void TestExceptionOnBeginReceive()
238 // {
239 // TestHelper.InMethod();
240 //
241 // MockScene scene = new MockScene();
242 //
243 // uint circuitCodeA = 130000;
244 // EndPoint epA = new IPEndPoint(IPAddress.Loopback, 1300);
245 // UUID agentIdA = UUID.Parse("00000000-0000-0000-0000-000000001300");
246 // UUID sessionIdA = UUID.Parse("00000000-0000-0000-0000-000000002300");
247 //
248 // uint circuitCodeB = 130001;
249 // EndPoint epB = new IPEndPoint(IPAddress.Loopback, 1301);
250 // UUID agentIdB = UUID.Parse("00000000-0000-0000-0000-000000001301");
251 // UUID sessionIdB = UUID.Parse("00000000-0000-0000-0000-000000002301");
252 //
253 // TestLLUDPServer testLLUDPServer;
254 // TestLLPacketServer testLLPacketServer;
255 // AgentCircuitManager acm;
256 // SetupStack(scene, out testLLUDPServer, out testLLPacketServer, out acm);
257 // AddClient(circuitCodeA, epA, agentIdA, sessionIdA, testLLUDPServer, acm);
258 // AddClient(circuitCodeB, epB, agentIdB, sessionIdB, testLLUDPServer, acm);
259 //
260 // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet1"), epA);
261 // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(1, "packet2"), epB);
262 // testLLUDPServer.LoadReceiveWithBeginException(epA);
263 // testLLUDPServer.LoadReceive(BuildTestObjectNamePacket(2, "packet3"), epB);
264 // testLLUDPServer.ReceiveData(null);
265 //
266 // Assert.IsFalse(testLLUDPServer.HasCircuit(circuitCodeA));
267 //
268 // Assert.That(testLLPacketServer.GetTotalPacketsReceived(), Is.EqualTo(3));
269 // Assert.That(testLLPacketServer.GetPacketsReceivedFor(PacketType.ObjectName), Is.EqualTo(3));
270 // }
271  }
272 }
Collects sim statistics which aren't already being collected for the linden viewer's statistics pane ...
void TestAddClient()
Test adding a client to the stack
static UUID ParseTail(int tail)
Parse tail section into full UUID.
Definition: TestHelpers.cs:140
This class enables regression testing of the LLUDPServer by allowing us to intercept outgoing data...
Circuit data for an agent. Connection information shared between regions that accept UDP connections ...
This will contain basic tests for the LindenUDP client stack
Helpers for setting up scenes.
Definition: SceneHelpers.cs:60
Interactive OpenSim region server
Definition: OpenSim.cs:55