-
Notifications
You must be signed in to change notification settings - Fork 3
/
utests.py
585 lines (457 loc) · 17.8 KB
/
utests.py
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import atdlib
from atdlib import *
import logging
import unittest
# ------------------------------------
global host, user, pswd, file, srcip, md5s, jobid, taskid, jobs, sjobs, tasks, stasks, ssl
# ----- Please verify the following test data -----
# ---- before running to get meaningful results ---
ssl = True # Use ssl connection. Can be True or False
host = '169.254.254.100' # IP-address or hostname of your ATD box
user = 'atduser' # Your ATD user with REST-API access
pswd = 'atdpassword' # The respective password
file = 'sample.exe' # Sample file for upload, located in the working dir
srcip = '10.10.10.10' # Any valid IP address for enriching sample context
md5s = '10e4a1d2132ccb5c6759f038cdb6f3c9' # MD5 hash sum of the sample file
jobid = 39 # The id of an existing job on the ATD box
taskid = 62 # The id of an existing task on the ATD box
jobs = (39, 40, 41) # The tuple of existing jobs' ids on the ATD box
sjobs = ('39', '40', '41') # Same as above (string values)
tasks = (62, 63, 64) # The tuple of existing tasks' ids on the ATD box
stasks = ('62', '63', '64') # Same as above (string values)
# --------------------------------------------------
# ---- Constructor Test Case ---------
class TestATDConstructor(unittest.TestCase):
def setUp(self):
self.atd = None
def tearDown(self):
del self.atd
def test_atdsession_sslstr(self):
with self.assertRaises(TypeError):
self.atd = atdsession(ssl='abc')
def test_atdsession_uagnum(self):
with self.assertRaises(TypeError):
self.atd = atdsession(uag=123)
# ------------------------------------
# ---- Open Method Test Case ---------
class TestATDOpenMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
def tearDown(self):
self.atd.reset()
del self.atd
def test_open(self):
self.assertEqual(self.atd.open(host, user, pswd), True)
def test_open_dup(self):
self.atd.open(host, user, pswd)
with self.assertRaises(ATDStateError):
self.atd.open(host, user, pswd)
def test_open_numhname(self):
with self.assertRaises(TypeError):
self.atd.open(123, user, pswd)
def test_open_numuname(self):
with self.assertRaises(TypeError):
self.atd.open(host, 123, pswd)
def test_open_emptyuname(self):
with self.assertRaises(ATDAuthError):
self.atd.open(host, '', pswd)
def test_open_nonexistuname(self):
with self.assertRaises(ATDClientError):
self.atd.open(host, 'definitelynotauser', pswd)
def test_open_numpasswd(self):
with self.assertRaises(TypeError):
self.atd.open(host, user, 123)
def test_open_emptypasswd(self):
with self.assertRaises(ATDAuthError):
self.atd.open(host, user, '')
def test_open_wrongpasswd(self):
with self.assertRaises(ATDAuthError):
self.atd.open(host, user, 'McAfee123')
# ------------------------------------
# ---- Close Method Test Case ---------
class TestATDCloseMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
def tearDown(self):
self.atd.reset()
del self.atd
def test_close(self):
self.assertEqual(self.atd.close(), True)
def test_close_dup(self):
self.atd.close()
with self.assertRaises(ATDStateError):
self.atd.close()
# ------------------------------------
# ---- Fileup Method Test Case ---------
class TestATDFileupMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
def tearDown(self):
self.atd.close()
del self.atd
def test_fileup_ip_nore(self):
self.assertGreater(self.atd.fileup(file, srcip, False), 0)
def test_fileup_noip_nore(self):
self.assertGreater(self.atd.fileup(file, '', False), 0)
def test_fileup_ip_re(self):
self.assertGreater(self.atd.fileup(file, srcip, True), 0)
def test_fileup_noip_re(self):
self.assertGreater(self.atd.fileup(file, '', True), 0)
def test_fileup_num_fname(self):
with self.assertRaises(TypeError):
self.atd.fileup(123, srcip, True)
def test_fileup_empty_fname(self):
with self.assertRaises(IOError):
self.atd.fileup('', srcip, True)
def test_fileup_num_srcip(self):
with self.assertRaises(TypeError):
self.atd.fileup(file, 123, True)
def test_fileup_str_reanalyze(self):
with self.assertRaises(TypeError):
self.atd.fileup(file, srcip, '')
# ------------------------------------
# ---- _MD5Log Method Test Case ---------
class TestATD_MD5LogMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
self.ret = None
def tearDown(self):
self.atd.close()
del self.atd
del self.ret
def test__md5log(self):
self.ret = self.atd._md5log(md5s)
self.assertIsInstance(self.ret, dict)
self.assertIn('success', self.ret)
self.assertEqual(self.ret['success'], True)
#print self.ret
def test__md5log_nonexist(self):
self.ret = self.atd._md5log('00000000000000000000000000000000')
self.assertIsInstance(self.ret, dict)
self.assertIn('success', self.ret)
self.assertEqual(self.ret['success'], True)
#print self.ret
def test__md5log_invalid_md5(self):
with self.assertRaises(ValueError):
self.ret = self.atd._md5log('abcdefghijklmnopqrstuvwxyz')
def test__md5log_num_md5(self):
with self.assertRaises(TypeError):
self.ret = self.atd._md5log(123)
# ------------------------------------
# ---- MD5Status Method Test Case ---------
class TestATDMD5StatusMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
self.ret = None
def tearDown(self):
self.atd.close()
del self.atd
del self.ret
def test_md5status(self):
self.ret = self.atd.md5status(md5s)
self.assertIsInstance(self.ret, dict)
self.assertIn('status', self.ret)
self.assertIn('severity', self.ret)
#print self.ret
def test_md5status_nonexist(self):
self.ret = self.atd.md5status('00000000000000000000000000000000')
self.assertIsInstance(self.ret, dict)
self.assertEqual(self.ret['status'], 0)
self.assertEqual(self.ret['severity'], -6)
#print self.ret
def test_md5status_invalid_md5(self):
with self.assertRaises(ValueError):
self.ret = self.atd.md5status('abcdefghijklmnopqrstuvwxyz')
def test_md5status_num_md5(self):
with self.assertRaises(TypeError):
self.ret = self.atd.md5status(123)
# ------------------------------------
# ---- JobStatus Method Test Case ---------
class TestATDJobStatusMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
self.ret = None
def tearDown(self):
self.atd.close()
del self.atd
del self.ret
def test_jobstatus_int(self):
self.ret = self.atd.jobstatus(jobid)
self.assertIsInstance(self.ret, dict)
self.assertIn('status', self.ret)
self.assertIn('severity', self.ret)
#print self.ret
def test_jobstatus_intstr(self):
self.ret = self.atd.jobstatus(str(jobid))
self.assertIsInstance(self.ret, dict)
self.assertIn('status', self.ret)
self.assertIn('severity', self.ret)
#print self.ret
def test_jobstatus_nonexist(self):
# current API implementation returns ATDError
with self.assertRaises((ATDClientError, ATDError)):
self.ret = self.atd.jobstatus(65535)
def test_jobstatus_str(self):
with self.assertRaises(ValueError):
self.ret = self.atd.jobstatus("justastring")
def test_jobstatus_emptystr(self):
with self.assertRaises(ValueError):
self.ret = self.atd.jobstatus("")
# ------------------------------------
# ---- TaskStatus Method Test Case ---------
class TestATDTaskStatusMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
self.ret = None
def tearDown(self):
self.atd.close()
del self.atd
del self.ret
def test_taskstatus_int(self):
self.ret = self.atd.taskstatus(taskid)
self.assertIsInstance(self.ret, int)
self.assertIn(self.ret, [1,2,3,4,-1])
#print self.ret
def test_taskstatus_intstr(self):
self.ret = self.atd.taskstatus(str(taskid))
self.assertIsInstance(self.ret, int)
self.assertIn(self.ret, [1,2,3,4,-1])
#print self.ret
def test_taskstatus_nonexist(self):
# current API implementation returns ATDClientError
with self.assertRaises((ATDClientError, ATDError)):
self.ret = self.atd.taskstatus(65535)
def test_taskstatus_str(self):
with self.assertRaises(ValueError):
self.ret = self.atd.taskstatus("justastring")
def test_taskstatus_emptystr(self):
with self.assertRaises(ValueError):
self.ret = self.atd.taskstatus("")
# ------------------------------------
# ---- JobTasks Method Test Case ---------
class TestATDJobTasksMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
self.ret = None
def tearDown(self):
self.atd.close()
del self.atd
del self.ret
def test_jobtasks_int(self):
self.ret = self.atd.jobtasks(jobid)
self.assertIsInstance(self.ret, list)
self.assertGreater(len(self.ret), 0)
#print self.ret
def test_jobtasks_intstr(self):
self.ret = self.atd.jobtasks(str(jobid))
self.assertIsInstance(self.ret, list)
self.assertGreater(len(self.ret), 0)
#print self.ret
def test_jobtasks_nonexist(self):
# current API implementation returns empty list
self.ret = self.atd.jobtasks(65535)
self.assertIsInstance(self.ret, list)
self.assertEqual(len(self.ret), 0)
#print self.ret
def test_jobtasks_str(self):
with self.assertRaises(ValueError):
self.ret = self.atd.jobtasks("justastring")
def test_jobtasks_emptystr(self):
with self.assertRaises(ValueError):
self.ret = self.atd.jobtasks("")
# ------------------------------------
# ---- BulkStatus Method Test Case ---------
class TestATDBulkStatusMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
def tearDown(self):
self.atd.close()
del self.atd
def test_bulkstatus_tasks(self):
self.ret = self.atd.bulkstatus(tasks=tasks)
self.assertIsInstance(self.ret, list)
self.assertGreater(len(self.ret), 0)
#print self.ret
def test_bulkstatus_stasks(self):
self.ret = self.atd.bulkstatus(tasks=stasks)
self.assertIsInstance(self.ret, list)
self.assertGreater(len(self.ret), 0)
#print self.ret
def test_bulkstatus_jobs(self):
self.ret = self.atd.bulkstatus(jobs=jobs)
self.assertIsInstance(self.ret, list)
self.assertGreater(len(self.ret), 0)
#print self.ret
def test_bulkstatus_sjobs(self):
self.ret = self.atd.bulkstatus(jobs=sjobs)
self.assertIsInstance(self.ret, list)
self.assertGreater(len(self.ret), 0)
#print self.ret
def test_bulkstatus_tasksjobs(self):
self.ret = self.atd.bulkstatus(tasks=tasks, jobs=jobs)
self.assertIsInstance(self.ret, list)
self.assertGreater(len(self.ret), 0)
#print self.ret
def test_bulkstatus_nonexisttasks(self):
#current API returns the list with {taskid=<id>, status=-1, severity=-1} items
self.ret = self.atd.bulkstatus(tasks=[65535,65534])
self.assertIsInstance(self.ret, list)
self.assertEqual(len(self.ret), 2)
#print self.ret
def test_bulkstatus_nonexistjobs(self):
#current API returns the list with {jobid=<id>, status=-1, severity=-1} items
self.ret = self.atd.bulkstatus(jobs=[65535,65534])
self.assertIsInstance(self.ret, list)
self.assertEqual(len(self.ret), 2)
#print self.ret
def test_bulkstatus_notasksnojobs(self):
with self.assertRaises(ValueError):
self.ret = self.atd.bulkstatus()
def test_bulkstatus_num_tasks(self):
with self.assertRaises(TypeError):
self.ret = self.atd.bulkstatus(tasks=123)
def test_bulkstatus_num_jobs(self):
with self.assertRaises(TypeError):
self.ret = self.atd.bulkstatus(jobs=123)
def test_bulkstatus_str_tasks(self):
with self.assertRaises(ValueError):
self.ret = self.atd.bulkstatus(tasks="thisisjustastring")
def test_bulkstatus_str_jobs(self):
with self.assertRaises(ValueError):
self.ret = self.atd.bulkstatus(jobs="thisisjustastring")
# ------------------------------------
# ---- TaskReport Method Test Case ---------
class TestATDTaskReportMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
self.ret = None
def tearDown(self):
self.atd.close()
del self.atd
del self.ret
def test_taskreport_int(self):
self.ret = self.atd.taskreport(taskid)
self.assertGreater(len(self.ret), 0)
def test_taskreport_intstr(self):
self.ret = self.atd.taskreport(str(taskid))
self.assertGreater(len(self.ret), 0)
def test_taskreport_list(self):
with self.assertRaises(TypeError):
self.ret = self.atd.taskreport(list(taskid))
def test_taskreport_nonexist(self):
# current API implementation returns ATDClientError
with self.assertRaises(ATDClientError):
self.ret = self.atd.taskreport(65535)
def test_taskreport_invalidreptype(self):
with self.assertRaises(ValueError):
self.ret = self.atd.taskreport(taskid=taskid, type='nosuchtype')
# ------------------------------------
# ---- JobReport Method Test Case ---------
class TestATDJobReportMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
self.ret = None
def tearDown(self):
self.atd.close()
del self.atd
del self.ret
def test_jobreport_int(self):
self.ret = self.atd.jobreport(jobid)
self.assertGreater(len(self.ret), 0)
def test_jobreport_intstr(self):
self.ret = self.atd.jobreport(str(jobid))
self.assertGreater(len(self.ret), 0)
def test_jobreport_list(self):
with self.assertRaises(TypeError):
self.ret = self.atd.jobreport(list(jobid))
def test_jobreport_nonexist(self):
# current API implementation returns ATDClientError
with self.assertRaises(ATDClientError):
self.ret = self.atd.jobreport(65535)
def test_jobreport_invalidreptype(self):
with self.assertRaises(ValueError):
self.ret = self.atd.jobreport(jobid=jobid, type='nosuchtype')
# ------------------------------------
# ---- MD5Report Method Test Case ---------
class TestATDMD5ReportMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
self.ret = None
def tearDown(self):
self.atd.close()
del self.atd
del self.ret
def test_md5report(self):
self.ret = self.atd.md5report(md5s)
self.assertGreater(len(self.ret), 0)
def test_md5report_nonexist(self):
# current API implementation returns ATDServerError 'submission not found'
with self.assertRaises(ATDServerError):
self.ret = self.atd.md5report('00000000000000000000000000000000')
def test_md5report_invalid(self):
with self.assertRaises(ValueError):
self.ret = self.atd.md5report('abcdefghijklmnopqrstuvwxyz')
def test_md5report_numeric(self):
with self.assertRaises(TypeError):
self.ret = self.atd.md5report(123)
# ------------------------------------
# ---- ListLookup Method Test Case ---------
class TestATDListLookupMethod(unittest.TestCase):
def setUp(self):
self.atd = atdsession(ssl=ssl)
self.atd.open(host, user, pswd)
self.ret = None
def tearDown(self):
self.atd.close()
del self.atd
del self.ret
def test_listlookup(self):
self.ret = self.atd.listlookup(md5s)
self.assertIn(self.ret, ('0', 'w', 'b'))
def test_listlookup_nonexist(self):
self.ret = self.atd.listlookup('00000000000000000000000000000000')
self.assertEquals(self.ret, '0')
def test_listlookup_invalid(self):
with self.assertRaises(ValueError):
self.ret = self.atd.listlookup('abcdefghijklmnopqrstuvwxyz')
def test_listlookup_numeric(self):
with self.assertRaises(TypeError):
self.ret = self.atd.listlookup(123)
# ------------------------------------
# ----------- Main Unit Test -------------
if __name__ == '__main__':
logFormat = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
logHandler = logging.FileHandler('atdlib14.log')
logHandler.setFormatter(logFormat)
mylog = logging.getLogger('atdlib')
mylog.addHandler(logHandler)
mylog.setLevel(logging.INFO)
t = {}
t['suiteCreate'] = unittest.TestLoader().loadTestsFromTestCase(TestATDConstructor)
t['suiteOpen'] = unittest.TestLoader().loadTestsFromTestCase(TestATDOpenMethod)
t['suiteClose'] = unittest.TestLoader().loadTestsFromTestCase(TestATDCloseMethod)
t['suiteFileup'] = unittest.TestLoader().loadTestsFromTestCase(TestATDFileupMethod)
t['suiteMD5Status'] = unittest.TestLoader().loadTestsFromTestCase(TestATDMD5StatusMethod)
t['suite_MD5Log'] = unittest.TestLoader().loadTestsFromTestCase(TestATD_MD5LogMethod)
t['suiteJobStatus'] = unittest.TestLoader().loadTestsFromTestCase(TestATDJobStatusMethod)
t['suiteTaskStatus'] = unittest.TestLoader().loadTestsFromTestCase(TestATDTaskStatusMethod)
t['suiteJobTasks'] = unittest.TestLoader().loadTestsFromTestCase(TestATDJobTasksMethod)
t['suiteBulkStatus'] = unittest.TestLoader().loadTestsFromTestCase(TestATDBulkStatusMethod)
t['suiteTaskReport'] = unittest.TestLoader().loadTestsFromTestCase(TestATDTaskReportMethod)
t['suiteJobReport'] = unittest.TestLoader().loadTestsFromTestCase(TestATDJobReportMethod)
t['suiteMD5Report'] = unittest.TestLoader().loadTestsFromTestCase(TestATDMD5ReportMethod)
t['suiteListLookup'] = unittest.TestLoader().loadTestsFromTestCase(TestATDListLookupMethod)
alltests = unittest.TestSuite(t.values())
#alltests = unittest.TestSuite(t['suiteListLookup'])
unittest.TextTestRunner(verbosity=5).run(alltests)