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