Skip to content

Commit e80c38a

Browse files
committed
test_database.py: use a different port for each unit test
1 parent 300c663 commit e80c38a

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

tests/test_database.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,20 @@
4343
"",
4444
)
4545

46+
random_port = 6379
47+
48+
49+
def get_random_port():
50+
global random_port
51+
random_port += 1
52+
return random_port
53+
4654

4755
def test_getProfileIdFromIP():
4856
"""unit test for add_profile and getProfileIdFromIP"""
49-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
57+
db = ModuleFactory().create_db_manager_obj(
58+
get_random_port(), flush_db=True
59+
)
5060

5161
# add a profile
5262
db.add_profile("profile_192.168.1.1", "00:00")
@@ -57,7 +67,9 @@ def test_getProfileIdFromIP():
5767
def test_timewindows():
5868
"""unit tests for addNewTW , getLastTWforProfile and
5969
getFirstTWforProfile"""
60-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
70+
db = ModuleFactory().create_db_manager_obj(
71+
get_random_port(), flush_db=True
72+
)
6173
profileid = "profile_192.168.1.1"
6274
# add a profile
6375
db.add_profile(profileid, "00:00")
@@ -70,7 +82,9 @@ def test_timewindows():
7082

7183

7284
def test_add_ips():
73-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
85+
db = ModuleFactory().create_db_manager_obj(
86+
get_random_port(), flush_db=True
87+
)
7488
# add a profile
7589
db.add_profile(profileid, "00:00")
7690
# add a tw to that profile
@@ -82,7 +96,9 @@ def test_add_ips():
8296

8397

8498
def test_add_port():
85-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
99+
db = ModuleFactory().create_db_manager_obj(
100+
get_random_port(), flush_db=True
101+
)
86102
new_flow = flow
87103
new_flow.state = "Not Established"
88104
db.add_port(profileid, twid, flow, "Server", "Dst")
@@ -93,7 +109,9 @@ def test_add_port():
93109

94110

95111
def test_set_evidence():
96-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
112+
db = ModuleFactory().create_db_manager_obj(
113+
get_random_port(), flush_db=True
114+
)
97115
attacker: Attacker = Attacker(
98116
direction=Direction.SRC, attacker_type=IoCType.IP, value=test_ip
99117
)
@@ -125,7 +143,9 @@ def test_set_evidence():
125143

126144
def test_setInfoForDomains():
127145
"""tests setInfoForDomains, setNewDomain and getDomainData"""
128-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
146+
db = ModuleFactory().create_db_manager_obj(
147+
get_random_port(), flush_db=True
148+
)
129149
domain = "www.google.com"
130150
domain_data = {"threatintelligence": "sample data"}
131151
db.set_info_for_domains(domain, domain_data)
@@ -136,7 +156,9 @@ def test_setInfoForDomains():
136156

137157

138158
def test_subscribe():
139-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
159+
db = ModuleFactory().create_db_manager_obj(
160+
get_random_port(), flush_db=True
161+
)
140162
# invalid channel
141163
assert db.subscribe("invalid_channel") is False
142164
# valid channel, shoud return a pubsub object
@@ -145,7 +167,9 @@ def test_subscribe():
145167

146168
def test_profile_moddule_labels():
147169
"""tests set and get_profile_module_label"""
148-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
170+
db = ModuleFactory().create_db_manager_obj(
171+
get_random_port(), flush_db=True
172+
)
149173
module_label = "malicious"
150174
module_name = "test"
151175
db.set_profile_module_label(profileid, module_name, module_label)
@@ -155,7 +179,7 @@ def test_profile_moddule_labels():
155179

156180

157181
def test_add_mac_addr_to_profile():
158-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
182+
db = ModuleFactory().create_db_manager_obj(1234, flush_db=True)
159183
ipv4 = "192.168.1.5"
160184
profileid_ipv4 = f"profile_{ipv4}"
161185
mac_addr = "00:00:5e:00:53:af"
@@ -179,15 +203,17 @@ def test_add_mac_addr_to_profile():
179203
assert ipv6 in db.r.hget("MAC", mac_addr)
180204
# make sure the ipv4 is associated with this
181205
# ipv6 profile
182-
assert ipv4 in str(db.r.hmget(profileid_ipv6, "IPv4"))
206+
assert ipv4 in db.get_ipv4_from_profile(profileid_ipv6)
183207

184208
# make sure the ipv6 is associated with the
185209
# profile that has the same ipv4 as the mac
186210
assert ipv6 in str(db.r.hmget(profileid_ipv4, "IPv6"))
187211

188212

189213
def test_get_the_other_ip_version():
190-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
214+
db = ModuleFactory().create_db_manager_obj(
215+
get_random_port(), flush_db=True
216+
)
191217
# profileid is ipv4
192218
ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
193219
db.set_ipv6_of_profile(profileid, ipv6)
@@ -215,7 +241,9 @@ def test_get_the_other_ip_version():
215241
],
216242
)
217243
def test_add_tuple(tupleid: str, symbol, expected_direction, role, flow):
218-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
244+
db = ModuleFactory().create_db_manager_obj(
245+
get_random_port(), flush_db=True
246+
)
219247
db.add_tuple(profileid, twid, tupleid, symbol, role, flow)
220248
assert symbol[0] in db.r.hget(
221249
f"profile_{flow.saddr}_{twid}", expected_direction
@@ -233,7 +261,9 @@ def test_add_tuple(tupleid: str, symbol, expected_direction, role, flow):
233261
def test_update_max_threat_level(
234262
max_threat_level, cur_threat_level, expected_max
235263
):
236-
db = ModuleFactory().create_db_manager_obj(6379, flush_db=True)
264+
db = ModuleFactory().create_db_manager_obj(
265+
get_random_port(), flush_db=True
266+
)
237267
db.set_max_threat_level(profileid, max_threat_level)
238268
assert (
239269
db.update_max_threat_level(profileid, cur_threat_level) == expected_max

0 commit comments

Comments
 (0)