Skip to content
Snippets Groups Projects
Commit e8009064 authored by Rajmund Hruška's avatar Rajmund Hruška
Browse files

Add unit test for node names. (Redmine issue: #6497)

parent 2032b53d
No related branches found
No related tags found
No related merge requests found
......@@ -37,7 +37,7 @@ class TestMentatIdeaJSON(unittest.TestCase):
#
verbose = False
idea_raw = {
idea_raw_1 = {
'Format': 'IDEA0',
'ID': '4390fc3f-c753-4a3e-bc83-1b44f24baf75',
'CreateTime': '2012-11-03T10:00:02Z',
......@@ -114,6 +114,66 @@ class TestMentatIdeaJSON(unittest.TestCase):
}
}
idea_raw_2 = {
'Format': 'IDEA0',
'ID': '4390fc3f-c753-4a3e-bc83-1b44f24baf76',
'DetectTime': '2012-11-03T10:00:07Z',
'Category': ['Fraud.Phishing', 'Test'],
'Node': [
{
'Name': 'org.example.kippo_honey',
'Realm': 'cesnet.cz',
'Type': ['Protocol', 'Honeypot'],
'SW': ['Kippo'],
'AggrWin': '00:05:00'
},
{
'Name': 'org.example.kippo_honey2',
'Realm': 'cesnet.cz',
'Type': ['Protocol', 'Honeypot'],
'SW': ['Kippo'],
'AggrWin': '00:05:00'
},
]
}
idea_raw_3 = {
'Format': 'IDEA0',
'ID': '4390fc3f-c753-4a3e-bc83-1b44f24baf76',
'DetectTime': '2012-11-03T10:00:07Z',
'Category': ['Fraud.Phishing', 'Test'],
'Node': [
{
'Name': 'org.example.kippo_honey',
'Realm': 'cesnet.cz',
'Type': ['Protocol', 'Honeypot'],
'SW': ['Kippo'],
'AggrWin': '00:05:00'
},
{
'Type': ['External']
}
]
}
idea_raw_4 = {
'Format': 'IDEA0',
'ID': '4390fc3f-c753-4a3e-bc83-1b44f24baf77',
'DetectTime': '2012-11-03T10:00:07Z',
'Category': ['Fraud.Phishing', 'Test'],
'Node': [
{
'Realm': 'cesnet.cz',
'Type': ['Protocol', 'Honeypot'],
'SW': ['Kippo'],
'AggrWin': '00:05:00'
},
{
'Type': ['External']
}
]
}
def test_01_conv_internal(self):
"""
Perform basic parsing and conversion tests from ``menat.idea.internal.Idea`` class
......@@ -125,11 +185,11 @@ class TestMentatIdeaJSON(unittest.TestCase):
#
# Test conversions of raw JSON IDEA into 'mentat.idea.internal.Idea'.
#
idea_internal = mentat.idea.internal.Idea(self.idea_raw)
idea_internal = mentat.idea.internal.Idea(self.idea_raw_1)
if self.verbose:
print("'mentat.idea.internal.Idea' out of raw JSON IDEA:")
print(json.dumps(idea_internal, indent=4, sort_keys=True, default=idea_internal.json_default))
orig = json.dumps(self.idea_raw, indent=4, sort_keys=True)
orig = json.dumps(self.idea_raw_1, indent=4, sort_keys=True)
new = json.dumps(idea_internal, indent=4, sort_keys=True, default=idea_internal.json_default)
self.assertEqual(orig, new, "\n".join([l for l in difflib.context_diff(orig.split("\n"), new.split("\n"))]))
......@@ -176,6 +236,29 @@ class TestMentatIdeaJSON(unittest.TestCase):
self.assertEqual(idea_sqldb.cesnet_eventseverity, 'low')
self.assertEqual(idea_sqldb.cesnet_inspectionerrors, ['Demonstration error - first', 'Demonstration error - second'])
def test_02_missing_node_names(self):
"""
Perform test of presence of node names in the event.
"""
# Only one node
idea_internal = mentat.idea.internal.Idea(self.idea_raw_1)
idea_sqldb = mentat.idea.sqldb.Idea(idea_internal)
self.assertEqual(idea_sqldb.node_name, ['org.example.kippo_honey'])
# Two nodes with two names
idea_internal = mentat.idea.internal.Idea(self.idea_raw_2)
idea_sqldb = mentat.idea.sqldb.Idea(idea_internal)
self.assertEqual(idea_sqldb.node_name, ['org.example.kippo_honey', 'org.example.kippo_honey2'])
# Two nodes but only one of them has a name
idea_internal = mentat.idea.internal.Idea(self.idea_raw_3)
idea_sqldb = mentat.idea.sqldb.Idea(idea_internal)
self.assertEqual(idea_sqldb.node_name, ['org.example.kippo_honey'])
# Two nodes but there is no name
idea_internal = mentat.idea.internal.Idea(self.idea_raw_4)
with self.assertRaisesRegexp(KeyError, "Missing Node name"):
idea_sqldb = mentat.idea.sqldb.Idea(idea_internal)
#-------------------------------------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment