-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathXmppConnectionTests.cs
283 lines (232 loc) · 6.08 KB
/
XmppConnectionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Waher.Events;
using Waher.Events.Console;
using Waher.Networking.Sniffers;
using Waher.Networking.XMPP.AuthenticationErrors;
namespace Waher.Networking.XMPP.Test
{
[TestClass]
public class XmppConnectionTests
{
private static ConsoleEventSink sink = null;
protected AutoResetEvent connected = new(false);
protected AutoResetEvent error = new(false);
protected AutoResetEvent offline = new(false);
protected XmppClient client;
protected Exception ex = null;
protected Type permittedExceptionType = null;
public XmppConnectionTests()
{
}
[ClassInitialize]
public static void ClassInitialize(TestContext _)
{
sink = new ConsoleEventSink();
Log.Register(sink);
}
[ClassCleanup]
public static async Task ClassCleanup()
{
if (sink is not null)
{
Log.Unregister(sink);
await sink.DisposeAsync();
sink = null;
}
}
[TestInitialize]
public void Setup()
{
this.connected.Reset();
this.error.Reset();
this.offline.Reset();
this.ex = null;
this.permittedExceptionType = null;
this.client = new XmppClient(this.GetCredentials(), "en", typeof(CommunicationTests).Assembly)
{
DefaultNrRetries = 2,
DefaultRetryTimeout = 1000,
DefaultMaxRetryTimeout = 5000,
DefaultDropOff = true
};
this.client.SetTag("ShowE2E", true);
this.client.Add(new ConsoleOutSniffer(BinaryPresentationMethod.ByteCount, LineEnding.NewLine));
this.client.OnConnectionError += this.Client_OnConnectionError;
this.client.OnError += this.Client_OnError;
this.client.OnStateChanged += this.Client_OnStateChanged;
this.client.Connect();
}
public virtual XmppCredentials GetCredentials()
{
return new XmppCredentials()
{
Host = "waher.se",
Port = 5222,
Account = "xmppclient.test01",
Password = "testpassword"
};
}
private Task Client_OnStateChanged(object Sender, XmppState NewState)
{
switch (NewState)
{
case XmppState.Connected:
this.connected.Set();
break;
case XmppState.Error:
this.error.Set();
break;
case XmppState.Offline:
this.offline.Set();
break;
}
return Task.CompletedTask;
}
Task Client_OnError(object Sender, Exception Exception)
{
this.ex = Exception;
return Task.CompletedTask;
}
Task Client_OnConnectionError(object Sender, Exception Exception)
{
this.ex = Exception;
return Task.CompletedTask;
}
private int Wait(int Timeout)
{
return WaitHandle.WaitAny(new WaitHandle[] { this.connected, this.error, this.offline }, Timeout);
}
private void WaitConnected(int Timeout)
{
switch (this.Wait(Timeout))
{
default:
Assert.Fail("Unable to connect. Timeout occurred.");
break;
case 0:
break;
case 1:
Assert.Fail("Unable to connect. Error occurred.");
break;
case 2:
Assert.Fail("Unable to connect. Client turned offline.");
break;
}
}
private void WaitError(int Timeout)
{
switch (this.Wait(Timeout))
{
default:
Assert.Fail("Expected error. Timeout occurred.");
break;
case 0:
Assert.Fail("Expected error. Connection successful.");
break;
case 1:
break;
case 2:
Assert.Fail("Expected error. Client turned offline.");
break;
}
this.permittedExceptionType = typeof(NotAuthorizedException);
this.ex = null;
}
private void WaitOffline(int Timeout)
{
switch (this.Wait(Timeout))
{
default:
Assert.Fail("Expected offline. Timeout occurred.");
break;
case 0:
Assert.Fail("Expected offline. Connection successful.");
break;
case 1:
Assert.Fail("Expected offline. Error occured.");
break;
case 2:
break;
}
}
[TestCleanup]
public async Task TearDown()
{
if (this.client is not null)
{
await this.client.OfflineAndDisposeAsync(false);
this.client = null;
}
if (this.ex is not null && this.ex.GetType() != this.permittedExceptionType)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(this.ex).Throw();
}
[TestMethod]
public void Connection_Test_01_Connect_AutoCreate()
{
this.client.AllowRegistration();
this.WaitConnected(10000);
}
[TestMethod]
public void Connection_Test_02_Connect()
{
this.WaitConnected(10000);
}
[TestMethod]
public async Task Connection_Test_03_Disconnect()
{
this.WaitConnected(10000);
if (this.client is not null)
{
await this.client.OfflineAndDisposeAsync(false);
this.client = null;
}
this.WaitOffline(10000);
this.ex = null;
}
[TestMethod]
public async Task Connection_Test_04_NotAuthorized()
{
await this.Connection_Test_03_Disconnect();
this.client = new XmppClient("waher.se", 5222, "xmppclient.test01", "abc", "en", typeof(CommunicationTests).Assembly);
this.client.SetTag("ShowE2E", true);
this.client.OnConnectionError += this.Client_OnConnectionError;
this.client.OnError += this.Client_OnError;
this.client.OnStateChanged += this.Client_OnStateChanged;
await this.client.Connect();
this.WaitError(10000);
}
[TestMethod]
public async Task Connection_Test_05_Reconnect()
{
this.WaitConnected(10000);
this.permittedExceptionType = typeof(OperationCanceledException);
await this.client.HardOffline();
this.WaitOffline(10000);
Thread.Sleep(100);
this.offline.Reset();
this.error.Reset();
this.connected.Reset();
await this.client.Reconnect();
this.WaitConnected(10000);
}
[TestMethod]
[Ignore("Feature not supported on server.")]
public void Connection_Test_06_ChangePassword()
{
AutoResetEvent Changed = new(false);
this.WaitConnected(10000);
this.client.OnPasswordChanged += (Sender, e) =>
{
Changed.Set();
return Task.CompletedTask;
};
this.client.ChangePassword("newtestpassword");
Assert.IsTrue(Changed.WaitOne(10000), "Unable to change password.");
this.client.ChangePassword("testpassword");
Assert.IsTrue(Changed.WaitOne(10000), "Unable to change password back.");
}
}
}