forked from sbozzie/test-intel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntelReporterTests.cs
More file actions
515 lines (434 loc) · 22.2 KB
/
Copy pathIntelReporterTests.cs
File metadata and controls
515 lines (434 loc) · 22.2 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Moq.Protected;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Authentication;
using System.Text;
using System.Threading;
namespace PleaseIgnore.IntelMap.Tests {
/// <summary>
/// Unit tests for the <see cref="IntelReporter"/> component.
/// </summary>
[TestClass]
public class IntelReporterTests {
private readonly static string[] channelList = new string[] { "Channel1", "ChannelA" };
private static readonly Uri channelListUri = new Uri(TestHelpers.TestScheme + "://channels");
private static readonly Uri serviceUri = new Uri(TestHelpers.TestScheme + "://service");
/// <summary>
/// Verifies that <see cref="IntelReporter"/> constructs without
/// error.
/// </summary>
[TestMethod]
public void Construct() {
var reporter = new IntelReporter();
Assert.AreEqual(false, reporter.IsRunning);
Assert.AreEqual(IntelStatus.Stopped, reporter.Status);
}
/// <summary>
/// Verifies that <see cref="IntelReporter"/> disposes without
/// error.
/// </summary>
[TestMethod]
public void Dispose() {
var reporter = new IntelReporter();
reporter.Dispose();
Assert.IsFalse(reporter.IsRunning);
Assert.AreEqual(IntelStatus.Disposed, reporter.Status);
}
/// <summary>
/// Verifies that <see cref="IntelReporter"/> starts and stops correctly.
/// </summary>
[TestMethod]
public void StartStop() {
using (var testDir = new TempDirectory()) {
using (var reporter = new IntelReporter()) {
reporter.Path = testDir.FullName;
reporter.Username = "username";
reporter.PasswordHash = "password";
reporter.ChannelListUri = channelListUri.OriginalString;
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
reporter.ServiceUri = serviceUri.OriginalString;
var requestBody = TestHelpers.CreateRequestMock(serviceUri, "200 AUTH 0123456789ABCDEF 5");
reporter.Start();
Thread.Sleep(100);
Assert.IsTrue(reporter.IsRunning);
Assert.IsTrue(requestBody.Length > 0);
Assert.AreEqual(IntelStatus.Active, reporter.Status);
Assert.AreEqual(2, reporter.Channels.Count);
Assert.IsNotNull(reporter.Channels.Single(x => x.Name == channelList[0]));
Assert.IsNotNull(reporter.Channels.Single(x => x.Name == channelList[1]));
Assert.IsTrue(reporter.Channels.All(x => x.IsRunning));
TestHelpers.Cleanup();
requestBody = TestHelpers.CreateRequestMock(serviceUri, "201 AUTH Logged Off");
reporter.Stop();
Thread.Sleep(100);
Assert.IsFalse(reporter.IsRunning);
Assert.IsTrue(requestBody.Length > 0);
Assert.AreEqual(IntelStatus.Stopped, reporter.Status);
Assert.IsTrue(reporter.Channels.All(x => !x.IsRunning));
}
}
}
/// <summary>
/// Verifies that <see cref="IntelReporter"/> correctly signals an
/// error if it cannot contact the server when starting.
/// </summary>
[TestMethod]
public void StartWebError() {
using (var testDir = new TempDirectory()) {
using (var reporter = new IntelReporter()) {
reporter.Path = testDir.FullName;
reporter.Username = "username";
reporter.PasswordHash = "password";
reporter.ChannelListUri = channelListUri.OriginalString;
TestHelpers.CreateRequestError<WebException>(channelListUri);
reporter.ServiceUri = serviceUri.OriginalString;
var requestBody = TestHelpers.CreateRequestError<WebException>(serviceUri);
reporter.Start();
Thread.Sleep(100);
Assert.IsTrue(reporter.IsRunning);
Assert.IsTrue(requestBody.Length > 0);
Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelReporter"/> correctly signals an
/// error if it cannot contact login when starting.
/// </summary>
[TestMethod]
public void StartAuthError() {
using (var testDir = new TempDirectory()) {
using (var reporter = new IntelReporter()) {
reporter.Path = testDir.FullName;
reporter.Username = "username";
reporter.PasswordHash = "password";
reporter.ChannelListUri = channelListUri.OriginalString;
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
reporter.ServiceUri = serviceUri.OriginalString;
var requestBody = TestHelpers.CreateRequestMock(serviceUri, "500 ERROR AUTH");
reporter.Start();
Thread.Sleep(100);
Assert.IsTrue(reporter.IsRunning);
Assert.IsTrue(requestBody.Length > 0);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelReporter"/> correctly signals an
/// error if it starts without any credentials set.
/// </summary>
[TestMethod]
public void StartNoAuth() {
using (var testDir = new TempDirectory()) {
using (var reporter = new IntelReporter()) {
reporter.Path = testDir.FullName;
reporter.ChannelListUri = channelListUri.OriginalString;
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
reporter.Start();
Thread.Sleep(100);
Assert.IsTrue(reporter.IsRunning);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelReporter"/> correctly raises the
/// <see cref="IntelReporter.IntelReported"/> event.
/// </summary>
[TestMethod]
public void IntelReported() {
using (var testDir = new TempDirectory()) {
using (var reporter = new IntelReporter()) {
reporter.Path = testDir.FullName;
IntelEventArgs received = null;
reporter.IntelReported += (sender, e) => received = e;
reporter.ChannelListUri = channelListUri.OriginalString;
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
reporter.Start();
var testEvent = new IntelEventArgs(channelList[0], DateTime.UtcNow, "Test Message");
Thread.Sleep(100);
Assert.AreEqual(0, reporter.IntelDropped);
Assert.AreEqual(0, reporter.IntelSent);
reporter.Channels.First().OnIntelReported(testEvent);
Thread.Sleep(100);
Assert.IsTrue(reporter.IsRunning);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
Assert.AreEqual(received, testEvent);
Assert.AreEqual(1, reporter.IntelDropped);
Assert.AreEqual(0, reporter.IntelSent);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelStatus.NetworkError"/> properly clears.
/// </summary>
[TestMethod]
public void WebErrorClear() {
var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
CallBase = true
};
TestHelpers.CreateRequestMock(serviceUri, "200 AUTH 0123456789ABCDEF 5");
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
var testEvent = new IntelEventArgs(channelList[0], DateTime.UtcNow, "Test Message");
var sessionMock = new Mock<IntelSession>(MockBehavior.Loose, "username", "password", serviceUri);
sessionMock.Setup(x => x.Report(testEvent.Channel, testEvent.Timestamp, testEvent.Message))
.Returns(true);
IntelSession session = null;
reporterMock.Protected()
.Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
.Returns(new Func<IntelSession>(delegate() {
if (session == null) throw new WebException();
return session;
}));
using (var testDir = new TempDirectory()) {
using (var reporter = reporterMock.Object) {
reporter.Path = testDir.FullName;
reporter.Username = "username";
reporter.PasswordHash = "password";
reporter.ServiceUri = serviceUri.OriginalString;
reporter.ChannelListUri = channelListUri.OriginalString;
reporter.Start();
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);
reporter.OnIntelReported(testEvent);
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);
Assert.AreEqual(1, reporter.IntelDropped);
Assert.AreEqual(0, reporter.IntelSent);
session = sessionMock.Object;
reporter.OnIntelReported(testEvent);
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.Active, reporter.Status);
Assert.AreEqual(1, reporter.IntelDropped);
Assert.AreEqual(1, reporter.IntelSent);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelStatus.AuthenticationError"/>
/// properly clears.
/// </summary>
[TestMethod]
public void AuthErrorClear() {
var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
CallBase = true
};
TestHelpers.CreateRequestMock(serviceUri, "200 AUTH 0123456789ABCDEF 5");
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
var testEvent = new IntelEventArgs(channelList[0], DateTime.UtcNow, "Test Message");
var sessionMock = new Mock<IntelSession>(MockBehavior.Loose, "username", "password", serviceUri);
sessionMock.Setup(x => x.Report(testEvent.Channel, testEvent.Timestamp, testEvent.Message))
.Returns(true);
IntelSession session = null;
reporterMock.Protected()
.Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
.Returns(new Func<IntelSession>(delegate() {
if (session == null) throw new AuthenticationException();
return session;
}));
using (var testDir = new TempDirectory()) {
using (var reporter = reporterMock.Object) {
reporter.Path = testDir.FullName;
reporter.Username = "username";
reporter.PasswordHash = "password";
reporter.AuthenticationRetryTimeout = new TimeSpan(0, 0, 0, 0, 10);
reporter.ServiceUri = serviceUri.OriginalString;
reporter.ChannelListUri = channelListUri.OriginalString;
reporter.Start();
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
reporter.OnIntelReported(testEvent);
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
Assert.AreEqual(1, reporter.IntelDropped);
Assert.AreEqual(0, reporter.IntelSent);
session = sessionMock.Object;
reporter.OnIntelReported(testEvent);
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.Active, reporter.Status);
Assert.AreEqual(1, reporter.IntelDropped);
Assert.AreEqual(1, reporter.IntelSent);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelStatus.AuthenticationError"/>
/// transitions into <see cref="IntelStatus.NetworkError"/>.
/// </summary>
[TestMethod]
public void AuthErrorToNetworkError() {
var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
CallBase = true
};
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
var testEvent = new IntelEventArgs(channelList[0], DateTime.UtcNow, "Test Message");
var sessionMock = new Mock<IntelSession>(MockBehavior.Loose, "username", "password", serviceUri);
sessionMock.Setup(x => x.Report(testEvent.Channel, testEvent.Timestamp, testEvent.Message))
.Returns(true);
Exception exception = new AuthenticationException();
reporterMock.Protected()
.Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
.Returns(() => { throw exception; });
using (var testDir = new TempDirectory()) {
using (var reporter = reporterMock.Object) {
reporter.Path = testDir.FullName;
reporter.Username = "username";
reporter.PasswordHash = "password";
reporter.AuthenticationRetryTimeout = new TimeSpan(0, 0, 0, 0, 10);
reporter.ServiceUri = serviceUri.OriginalString;
reporter.ChannelListUri = channelListUri.OriginalString;
reporter.Start();
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
reporter.OnIntelReported(testEvent);
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
Assert.AreEqual(1, reporter.IntelDropped);
Assert.AreEqual(0, reporter.IntelSent);
exception = new WebException();
reporter.OnIntelReported(testEvent);
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);
Assert.AreEqual(2, reporter.IntelDropped);
Assert.AreEqual(0, reporter.IntelSent);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelStatus.NetworkError"/>
/// transitions into <see cref="IntelStatus.AuthenticationException"/>.
/// </summary>
[TestMethod]
public void NetworkErrorToAuthError() {
var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
CallBase = true
};
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
var testEvent = new IntelEventArgs(channelList[0], DateTime.UtcNow, "Test Message");
var sessionMock = new Mock<IntelSession>(MockBehavior.Loose, "username", "password", serviceUri);
sessionMock.Setup(x => x.Report(testEvent.Channel, testEvent.Timestamp, testEvent.Message))
.Returns(true);
Exception exception = new WebException();
reporterMock.Protected()
.Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
.Returns(() => { throw exception; });
using (var testDir = new TempDirectory()) {
using (var reporter = reporterMock.Object) {
reporter.Path = testDir.FullName;
reporter.Username = "username";
reporter.PasswordHash = "password";
reporter.AuthenticationRetryTimeout = new TimeSpan(0, 0, 0, 0, 10);
reporter.ServiceUri = serviceUri.OriginalString;
reporter.ChannelListUri = channelListUri.OriginalString;
reporter.Start();
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);
reporter.OnIntelReported(testEvent);
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.NetworkError, reporter.Status);
Assert.AreEqual(1, reporter.IntelDropped);
Assert.AreEqual(0, reporter.IntelSent);
exception = new AuthenticationException();
reporter.OnIntelReported(testEvent);
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
Assert.AreEqual(2, reporter.IntelDropped);
Assert.AreEqual(0, reporter.IntelSent);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelReporter.Authenticate"/> attempts
/// to authenticate.
/// </summary>
[TestMethod]
public void Authenticate() {
var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
CallBase = true
};
reporterMock.Protected()
.Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
.Throws<AuthenticationException>();
TestHelpers.CreateRequestMock(serviceUri, "200 AUTH 0123456789ABCDEF 5");
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
using (var testDir = new TempDirectory()) {
using (var reporter = reporterMock.Object) {
reporter.Path = testDir.FullName;
reporter.Username = "username2";
reporter.PasswordHash = "password";
reporter.ServiceUri = serviceUri.OriginalString;
reporter.ChannelListUri = channelListUri.OriginalString;
reporter.Start();
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
reporter.Authenticate("username", "password");
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.Active, reporter.Status);
Assert.AreEqual("username", reporter.Username);
Assert.AreEqual(IntelSession.HashPassword("password"), reporter.PasswordHash);
// So that Dispose() works
TestHelpers.CreateRequestError<WebException>(serviceUri);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelReporter.Authenticate"/> throws
/// <see cref="AuthenticationError"/> if rejected.
/// </summary>
[TestMethod, ExpectedException(typeof(AuthenticationException))]
public void AuthenticateAuthError() {
var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
CallBase = true
};
reporterMock.Protected()
.Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
.Throws<AuthenticationException>();
TestHelpers.CreateRequestMock(serviceUri, "500 ERROR AUTH");
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
using (var testDir = new TempDirectory()) {
using (var reporter = reporterMock.Object) {
reporter.Path = testDir.FullName;
reporter.ServiceUri = serviceUri.OriginalString;
reporter.ChannelListUri = channelListUri.OriginalString;
reporter.Start();
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
reporter.Authenticate("username", "password");
Thread.Sleep(100);
}
}
}
/// <summary>
/// Verifies that <see cref="IntelReporter.Authenticate"/> throws
/// <see cref="AuthenticationError"/> cannot contact the server.
/// </summary>
[TestMethod, ExpectedException(typeof(WebException))]
public void AuthenticateNetworkError() {
var reporterMock = new Mock<IntelReporter>(MockBehavior.Loose) {
CallBase = true
};
reporterMock.Protected()
.Setup<IntelSession>("GetSession", ItExpr.IsAny<bool>())
.Throws<AuthenticationException>();
TestHelpers.CreateRequestError<WebException>(serviceUri);
TestHelpers.CreateRequestMock(channelListUri, String.Join("\r\n", channelList));
using (var testDir = new TempDirectory()) {
using (var reporter = reporterMock.Object) {
reporter.Path = testDir.FullName;
reporter.ServiceUri = serviceUri.OriginalString;
reporter.ChannelListUri = channelListUri.OriginalString;
reporter.Start();
Thread.Sleep(100);
Assert.AreEqual(IntelStatus.AuthenticationError, reporter.Status);
reporter.Authenticate("username", "password");
Thread.Sleep(100);
}
}
}
}
}