forked from sbozzie/test-intel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntelSessionTests.cs
More file actions
268 lines (233 loc) · 9.71 KB
/
Copy pathIntelSessionTests.cs
File metadata and controls
268 lines (233 loc) · 9.71 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.IO;
using System.Net;
using System.Security.Authentication;
using System.Text;
namespace PleaseIgnore.IntelMap.Tests {
/// <summary>
/// Tests of the <see cref="IntelSession"/> class.
/// </summary>
[TestClass]
public class IntelSessionTests {
const string testPassword = "ABC αβγ";
const string testPasswordHash = "a79e978f450304a9a7660803fb3aff7ec631f641";
const string testUsername = "test-user";
const string testSession = "01234567890abcdef";
const string intelChannel = "random-intel";
const string intelString = "this-is-some-hot-intel";
// Combination tested in ExtensionTests.ToUnixTime()
const string intelTimeString = "1095357343";
private readonly static DateTime intelTimestamp = new DateTime(2004, 9, 16, 17, 55, 43, DateTimeKind.Utc);
// For routing into our Mock WebRequest
private readonly static Uri sessionUri = new Uri(TestHelpers.TestScheme + "://blah-blah-blah");
[TestCleanup]
public void Cleanup() {
TestHelpers.Cleanup();
}
/// <summary>
/// Tests the <see cref="IntelSession.HashPassword"/> member.
/// </summary>
[TestMethod]
public void HashPassword() {
// Should be equivalent to the SHA1 has of the password
Assert.AreEqual(testPasswordHash, IntelSession.HashPassword(testPassword));
}
/// <summary>
/// Creates an instance of <see cref="IntelSession"/> and logs
/// it in.
/// </summary>
/// <returns>
/// Initialized instance of <see cref="IntelSession"/>.
/// </returns>
private IntelSession Login() {
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "200 AUTH " + testSession + " 5");
var session = new IntelSession(testUsername, testPasswordHash, sessionUri);
Assert.IsTrue(session.IsConnected);
Assert.AreEqual(5, session.Users);
Assert.AreEqual(
"username=" + testUsername + "&"
+ "password=" + testPasswordHash
+ "&action=AUTH&version=2.2.0",
requestBody.ToString());
return session;
}
/// <summary>
/// Tests the <see cref="IntelSession.IntelSession"/> constructor
/// with a successful login.
/// </summary>
[TestMethod]
public void LoginSuccess() {
Login();
}
/// <summary>
/// Tests the <see cref="IntelSession.IntelSession"/> constructor
/// with a simulated network error.
/// </summary>
[TestMethod]
[ExpectedException(typeof(WebException))]
public void LoginError() {
TestHelpers.CreateRequestError<WebException>(sessionUri);
var session = new IntelSession(testUsername, testPasswordHash, sessionUri);
}
/// <summary>
/// Tests the <see cref="IntelSession.IntelSession"/> constructor
/// with a rejected login.
/// </summary>
[TestMethod]
[ExpectedException(typeof(AuthenticationException))]
public void LoginRejected() {
TestHelpers.CreateRequestMock(sessionUri, "500 ERROR AUTH");
var session = new IntelSession(testUsername, testPasswordHash, sessionUri);
}
/// <summary>
/// Tests that <see cref="IntelSession.Dispose"/> properly logs
/// us out.
/// </summary>
[TestMethod]
public void Dispose() {
var session = Login();
var closedCount = 0;
session.Closed += (sender, e) => ++closedCount;
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "201 AUTH Logged Off");
session.Dispose();
Assert.AreEqual(
"username=" + testUsername
+ "&session=" + testSession
+ "&action=LOGOFF",
requestBody.ToString());
Assert.IsFalse(session.IsConnected);
Assert.AreEqual(1, closedCount);
}
/// <summary>
/// Tests that <see cref="IntelSession.KeepAlive"/> properly pings
/// the server.
/// </summary>
[TestMethod]
public void KeepAlive() {
var session = Login();
session.Closed += (sender, e) => Assert.Fail("IntelSession.Closed raised inappropriately");
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "203 ALIVE OK 15");
session.KeepAlive();
Assert.AreEqual("session=" + testSession + "&action=ALIVE", requestBody.ToString());
Assert.AreEqual(15, session.Users);
Assert.IsTrue(session.IsConnected);
}
/// <summary>
/// Tests that <see cref="IntelSession.KeepAlive"/> throws an error
/// for an invalid response.
/// </summary>
[TestMethod]
[ExpectedException(typeof(WebException))]
public void KeepAliveInvalid() {
var session = Login();
session.Closed += (sender, e) => Assert.Fail("IntelSession.Closed raised inappropriately");
try {
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "SOMETHING IS NOT RIGHT");
session.KeepAlive();
} catch {
Assert.IsTrue(session.IsConnected);
throw;
}
}
/// <summary>
/// Tests that <see cref="IntelSession.KeepAlive"/> disconnects after
/// hitting the error limit.
/// </summary>
[TestMethod]
public void KeepAliveErrorLimit() {
var session = Login();
var closedCount = 0;
session.Closed += (sender, e) => ++closedCount;
try {
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "SOMETHING IS NOT RIGHT");
session.KeepAlive();
} catch (WebException) {
}
Assert.IsTrue(session.IsConnected);
Assert.AreEqual(0, closedCount);
try {
var requestBody = TestHelpers.CreateRequestError<WebException>(sessionUri);
session.KeepAlive();
} catch (WebException) {
}
Assert.IsTrue(session.IsConnected);
Assert.AreEqual(0, closedCount);
try {
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "SOMETHING IS NOT RIGHT");
session.KeepAlive();
} catch (WebException) {
}
Assert.IsFalse(session.IsConnected);
Assert.AreEqual(1, closedCount);
}
/// <summary>
/// Tests that <see cref="IntelSession.Report"/> properly pings
/// the server.
/// </summary>
[TestMethod]
public void Report() {
var session = Login();
session.Closed += (sender, e) => Assert.Fail("IntelSession.Closed raised inappropriately");
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "202 INTEL Accepted");
session.Report(intelChannel, intelTimestamp, intelString);
Assert.IsTrue(session.IsConnected);
Assert.AreEqual(
"session=" + testSession
+ "&inteltime=" + intelTimeString
+ "&action=INTEL"
+ "®ion=" + intelChannel
+ "&intel=" + intelString + "%0D",
requestBody.ToString());
}
/// <summary>
/// Tests that <see cref="IntelSession.Report"/> throws an error
/// for an invalid response.
/// </summary>
[TestMethod]
[ExpectedException(typeof(WebException))]
public void ReportInvalid() {
var session = Login();
session.Closed += (sender, e) => Assert.Fail("IntelSession.Closed raised inappropriately");
try {
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "SOMETHING IS NOT RIGHT");
session.Report(intelChannel, intelTimestamp, intelString);
} catch {
Assert.IsTrue(session.IsConnected);
throw;
}
}
/// <summary>
/// Tests that <see cref="IntelSession.Report"/> disconnects after
/// hitting the error limit.
/// </summary>
[TestMethod]
public void ReportErrorLimit() {
var session = Login();
var closedCount = 0;
session.Closed += (sender, e) => ++closedCount;
try {
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "SOMETHING IS NOT RIGHT");
session.Report(intelChannel, intelTimestamp, intelString);
} catch (WebException) {
}
Assert.IsTrue(session.IsConnected);
Assert.AreEqual(0, closedCount);
try {
var requestBody = TestHelpers.CreateRequestError<WebException>(sessionUri);
session.Report(intelChannel, intelTimestamp, intelString);
} catch (WebException) {
}
Assert.IsTrue(session.IsConnected);
Assert.AreEqual(0, closedCount);
try {
var requestBody = TestHelpers.CreateRequestMock(sessionUri, "SOMETHING IS NOT RIGHT");
session.Report(intelChannel, intelTimestamp, intelString);
} catch (WebException) {
}
Assert.IsFalse(session.IsConnected);
Assert.AreEqual(1, closedCount);
}
}
}