forked from pulpocaminante/Stuxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugTests.cpp
executable file
·379 lines (298 loc) · 9.89 KB
/
DebugTests.cpp
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
#define BOOST_TEST_MODULE DebugTests
#define BOOST_TEST_LOG_COMPACT_ON_SUCCESS
#include <boost/test/unit_test.hpp>
#include "../ExportInterface.hpp"
#include "../WMIConnection.hpp"
#include "Shell32Wrapper.hpp"
#include "wmi.h"
std::wstring encodedFileContentsWrite;
std::wstring encodedFileContentsRead;
BOOST_AUTO_TEST_CASE(Query_Success) {
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
QueryInterface IQuery(ICom, IWmi);
std::wstring sQuery = L"SELECT * FROM Win32_LogicalDisk";
std::vector<std::map<std::wstring, std::any>> vOut;
// Act
HRESULT hres = IQuery.Query(sQuery, vOut);
// Assert
BOOST_CHECK(SUCCEEDED(hres));
BOOST_CHECK(!vOut.empty());
}
BOOST_AUTO_TEST_CASE(Query_Fail) {
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
QueryInterface IQuery(ICom, IWmi);
std::wstring sQuery = L"Invalid WQL query";
std::vector<std::map<std::wstring, std::any>> vOut;
// Act
HRESULT hres = IQuery.Query(sQuery, vOut);
BOOST_CHECK(FAILED(hres));
BOOST_CHECK(vOut.empty());
}
BOOST_AUTO_TEST_CASE(Query_NoResults) {
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
QueryInterface IQuery(ICom, IWmi);
std::wstring sQuery = L"SELECT * FROM Win32_LogicalDisk WHERE DriveType = 0";
std::vector<std::map<std::wstring, std::any>> vOut;
// Act
HRESULT hres = IQuery.Query(sQuery, vOut);
// Assert
BOOST_CHECK(SUCCEEDED(hres));
BOOST_CHECK(vOut.empty());
}
BOOST_AUTO_TEST_CASE(ExportInterfaceLeak_Check)
{
// Arrange
IExport IFind;
// Act
LPVOID address = IFind.LoadAndFindSingleExport("sdh.le2ll3l", "ScheuextWlEexlE");
// Assert
BOOST_REQUIRE_NE(address, nullptr);
}
BOOST_AUTO_TEST_CASE(RailfenceCipherIntegrity_Check)
{
// Arrange
IExport IFind;
const wchar_t* ciphertext = L"RutOcrneOeierTStC2\\y";
wchar_t plaintext[256];
// Act
IFind.railfence_wdecipher(5, ciphertext, plaintext);
// Assert
wchar_t expected_plaintext[] = L"ROOT\\SecurityCenter2";
BOOST_REQUIRE_MESSAGE(wcscmp(plaintext, expected_plaintext) == 0, "plaintext and expected_plaintext are not equal.");
}
BOOST_AUTO_TEST_CASE(InitializeCOM_Check)
{
// Arrange
COMWrapper ICom;
// Assert
BOOST_REQUIRE_EQUAL(ICom.IReady, TRUE);
}
BOOST_AUTO_TEST_CASE(InitializeWMI_Check)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
// Assert
BOOST_REQUIRE_EQUAL(IWmi.IReady, TRUE);
BOOST_REQUIRE_EQUAL(ICom.IReady, TRUE);
}
BOOST_AUTO_TEST_CASE(Base64ReadWriteIntegrity_Check)
{
// Arrange
Railfence ciph;
encodedFileContentsWrite.clear();
encodedFileContentsRead.clear();
// Act
std::string sFilePathIn = "C:\\GOG Games\\DOOM\\DOOM.WAD";
std::string sFilePathOut = "C:\\GOG Games\\DOOM\\DOOM_CPY.WAD";
ciph.FileToBase64UTF16(sFilePathIn, encodedFileContentsWrite);
ciph.Base64UTF16ToFile(encodedFileContentsWrite, sFilePathOut);
std::ifstream fileIn(sFilePathIn, std::ios::binary | std::ios::ate);
std::ifstream fileOut(sFilePathOut, std::ios::binary | std::ios::ate);
std::streamsize sizeIn = fileIn.tellg();
std::streamsize sizeOut = fileOut.tellg();
// Assert
BOOST_REQUIRE_EQUAL(sizeIn, sizeOut);
BOOST_REQUIRE_MESSAGE(encodedFileContentsWrite == encodedFileContentsRead, "File contents are not equal.");
}
BOOST_AUTO_TEST_SUITE(WMIFS_Suite)
BOOST_AUTO_TEST_CASE(CreateDrive_Success)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
std::wstring sDriveName = L"Test";
std::wstring sFileContent = L"Some file content";
HRESULT hres = IWMIFS.CreateDrive(sDriveName, &sFileContent);
BOOST_CHECK(SUCCEEDED(hres));
}
BOOST_AUTO_TEST_CASE(CreateDrive_Fail)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
std::wstring sDriveName = L"Inva11111111111111111111111111111111lidDri#$*Jdfs-fsd-veName";
std::wstring sFileContent = L"Some file content";
HRESULT hres = IWMIFS.CreateDrive(sDriveName, &sFileContent);
BOOST_CHECK(FAILED(hres));
}
BOOST_AUTO_TEST_CASE(CreateDriveInstance_Success) {
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
HRESULT hresCreate = NULL;
HRESULT hresCreateInstance = NULL;
HRESULT hresDelete = NULL;
// Act
std::wstring sDriveName = L"Test3";
hresCreate = IWMIFS.CreateDrive(sDriveName, nullptr);
hresCreateInstance = IWMIFS.CreateDriveInstance(sDriveName);
hresDelete = IWMIFS.DeleteDrive(sDriveName);
// Assert
BOOST_CHECK_MESSAGE(SUCCEEDED(hresCreate), "Drive creation failed.");
BOOST_CHECK_MESSAGE(SUCCEEDED(hresCreateInstance), "Instance creation failed.");
BOOST_CHECK_MESSAGE(SUCCEEDED(hresDelete), "Drive deletion failed.");
}
BOOST_AUTO_TEST_CASE(CreateDriveInstance_Fail_GetObject) {
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
// Act
std::wstring sDriveName = L"Inva@lid#--)/.DriveN$ame";
HRESULT hres = IWMIFS.CreateDriveInstance(sDriveName);
// Assert
BOOST_CHECK(FAILED(hres));
}
BOOST_AUTO_TEST_CASE(CreateDriveInstance_Fail_GetObject2) {
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
// Act
std::wstring sDriveName = L"LegalYetInvalidName";
HRESULT hres = IWMIFS.CreateDriveInstance(sDriveName);
// Assert
BOOST_CHECK(FAILED(hres));
}
BOOST_AUTO_TEST_CASE(WMIFSWriteFile_Fail_FilePath)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
Railfence ciph;
HRESULT hres;
std::wstring fileContents;
// Act
std::string sFilePath = "fds333fs3dfsfs3df";
hres = ciph.FileToBase64UTF16(sFilePath, fileContents);
// Assert
BOOST_CHECK(FAILED(hres));
// Act
hres = IWMIFS.WriteFile(L"Test", L"FileName", fileContents);
// Assert
BOOST_CHECK(FAILED(hres));
}
BOOST_AUTO_TEST_CASE(WMIFSWriteFile_Fail_FileDrive)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
Railfence ciph;
HRESULT hres;
std::wstring fileContents;
// Act
std::string sFilePath = "C:\\GOG Games\\DOOM\\DOOM.WAD";
ciph.FileToBase64UTF16(sFilePath, fileContents);
hres = IWMIFS.WriteFile(L"$@#$@#RSFDSGHDJDDSfdssdfsdf", L"FileName", fileContents);
// Assert
BOOST_CHECK(FAILED(hres));
}
BOOST_AUTO_TEST_CASE(WMIFSReadWrite_Success)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
Railfence ciph;
HRESULT hresWrite;
HRESULT hresRead;
HRESULT hresCreate;
HRESULT hresDelete;
std::wstring fileContentsWrite;
std::wstring fileContentsRead;
std::string sFilePath = "C:\\GOG Games\\DOOM\\unins000.exe";
ciph.FileToBase64UTF16(sFilePath, fileContentsWrite);
// Act
hresCreate = IWMIFS.CreateDrive(L"Test2", NULL);
hresWrite = IWMIFS.WriteFile(L"Test2", L"Test", fileContentsWrite);
hresRead = IWMIFS.ReadFile(L"Test2", L"Test", fileContentsRead);
hresDelete = IWMIFS.DeleteDrive(L"Test2");
// Assert
BOOST_CHECK_MESSAGE(SUCCEEDED(hresWrite), "Write failed");
BOOST_CHECK_MESSAGE(SUCCEEDED(hresRead), "Read failed");
BOOST_CHECK_MESSAGE(SUCCEEDED(hresCreate), "Create failed");
BOOST_CHECK_MESSAGE(SUCCEEDED(hresDelete), "Delete failed");
BOOST_CHECK_MESSAGE(fileContentsWrite == fileContentsRead, "File contents are not equal");
}
BOOST_AUTO_TEST_CASE(WMIFSReadFile_Fail_FileContents)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
Railfence ciph;
HRESULT hres;
std::wstring contents = L"";
// Act
hres = IWMIFS.ReadFile(L"Tes423423t", L"Te$@$@$@st", contents);
// Assert
BOOST_CHECK(FAILED(hres));
}
BOOST_AUTO_TEST_CASE(WMIFSReadFile_Fail_FileDrive)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
Railfence ciph;
HRESULT hres;
std::wstring fileContents;
// Act
hres = IWMIFS.ReadFile(L"Tes123t$@#$@@#$", L"Test", encodedFileContentsRead);
// Assert
BOOST_CHECK(FAILED(hres));
}
BOOST_AUTO_TEST_CASE(WMIFSDeleteDrive_Success)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
HRESULT hres;
std::wstring fileContents = L"";
// Act
hres = IWMIFS.CreateDrive(L"Test", &fileContents);
// Assert
BOOST_CHECK(SUCCEEDED(hres));
// Act
hres = IWMIFS.DeleteDrive(L"Test");
// Assert
BOOST_CHECK(SUCCEEDED(hres));
}
BOOST_AUTO_TEST_CASE(WMIFSDeleteDrive_Fail)
{
// Arrange
COMWrapper ICom;
WMIConnection IWmi(ICom);
ClassFactory IFactory(ICom, IWmi);
WMIFSInterface IWMIFS(ICom, IWmi, IFactory);
HRESULT hres;
// Act
hres = IWMIFS.DeleteDrive(L"Te31223123123st");
// Assert
BOOST_CHECK(FAILED(hres));
}
BOOST_AUTO_TEST_SUITE_END(WMIFS_Suite)