-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest.py
116 lines (87 loc) · 3.68 KB
/
test.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
__author__ = 'JP Senior'
import unittest
from netaddr import IPAddress
from threataggregator import *
class RepDBTest(unittest.TestCase):
""" Tests the RepDB class object and constructors
"""
# Invoked before each test method
def setUp(self):
self.db = RepDB()
self.db.add('127.0.0.1', 'http://localhost2', 'CIDR test')
self.db.add('127.0.0.2', 'http://localhost3', 'Localhost test2')
self.db.add('127.0.0.3', 'http://localhost3-A', 'Localhost test3-A')
self.db.add('127.0.0.3', 'http://localhost3-B', 'Localhost test3-B')
self.db.add('127.0.0.4', 'http://localhost4', 'Localhost test4')
def test_len(self):
# We expect exactly 5 entries in the database
self.assertEqual(len(self.db), 5)
def test_search(self):
# There should be two results for 127.0.0.3
results = self.db.search('127.0.0.3')
self.assertEqual(len(results), 2)
def test_search_top(self):
# There should be only one result for 127.0.0.3 if Top=True
results = self.db.search("127.0.0.3", top=True)
self.assertEqual(len(results), 1)
def test_search_empty(self):
# An invalid IP address should return an empty list
results = self.db.search("4.4.4.4")
self.assertEqual(len(results), 0, "An invalid IP should return a zero-length list")
self.assertListEqual(results, [], "Searching for an invalid IP should return an empty list")
def test_cidr(self):
db = RepDB()
db.add('127.0.0.0/24', 'http://localhost2', 'CIDR test')
# 256 IP addresses in a subnet
#There should be 256 IPs in a subnet
self.assertEqual(len(db), 256)
# First IP should be .0
#
self.assertEqual(db.entries[0]['ip'], IPAddress('127.0.0.0'), "First IP in range is not valid")
# Last IP should be .255
self.assertEqual(db.entries[-1]['ip'], IPAddress('127.0.0.255'), "Last IP in range is not valid")
def test_geo_locate(self):
pass
class FakeUrlLib(object):
pass
def urlopen(xurl):
return xurl
class BuildCompareTest(unittest.TestCase):
""" Tests for the BuildCompare class object
"""
def setUp(self):
old = ['127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4', '127.0.0.5']
new = ['127.0.0.1', '127.0.0.3', '127.0.0.4', '127.0.0.5', '127.0.0.6', '127.0.0.7']
self.compare = BuildCompare(old, new)
def test_add(self):
# Only .6 and .7 are added.
self.assertListEqual(self.compare.add, ['127.0.0.6', '127.0.0.7'])
def test_delete(self):
# only .2 is added
self.assertListEqual(self.compare.delete, ['127.0.0.2'])
def test_equal(self):
# .1, .3, .4, .5 are equal.
self.assertListEqual(self.compare.equal, ['127.0.0.1', '127.0.0.3', '127.0.0.4', '127.0.0.5'])
class EmergingThreatTest(unittest.TestCase):
def setUp(self):
self.data = '''
#Type1
107.150.36.226
109.123.109.132
123.157.215.216
173.230.133.99
#Type2
1.116.0.0/24
5.34.242.0/24
5.72.0.0/24
14.4.0.0/24
'''.splitlines()
self.db = RepDB()
def test_emerging_threat(self):
self.db = emergingthreat('http://localhost.com', self.data)
self.assertDictEqual(self.db.entries[0], {'priority': 1, 'source': 'http://localhost.com', 'reputation': 1,
'description': 'Type1', 'city': u'Kansas City', 'latitude': 39.1472,
'ip': IPAddress('107.150.36.2'), 'country': u'United States',
'longitude': -94.5735}, "RepDB results did not come back as expected")
if __name__ == "__main__":
unittest.main()