Skip to content

Commit d77c5a3

Browse files
committed
fix: [tests] Added tests for standalone Network Traffic Observable objects conversion from STIX 2.1
- Reusing test code between 2.1 and 2.1 amongst multiple methods for standalone AND embedded or referenced observable objects
1 parent 714fc16 commit d77c5a3

File tree

4 files changed

+99
-59
lines changed

4 files changed

+99
-59
lines changed

tests/_test_stix_import.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,34 @@ def _check_file_with_pe_fields(self, misp_object, file_object, object_id = None)
10181018
uuid5(self._UUIDv4, f'{object_id} - size-in-bytes - {size.value}')
10191019
)
10201020

1021+
def _check_payload_object_fields(self, misp_object, artifact, object_id=None):
1022+
if object_id is None:
1023+
object_id = artifact.id
1024+
self.assertEqual(misp_object.name, 'artifact')
1025+
self.assertEqual(len(misp_object.attributes), 3)
1026+
md5, sha256, url = misp_object.attributes
1027+
hashes = artifact.hashes
1028+
self.assertEqual(md5.type, 'md5')
1029+
self.assertEqual(md5.object_relation, 'md5')
1030+
self.assertEqual(md5.value, hashes['MD5'])
1031+
self.assertEqual(
1032+
md5.uuid, uuid5(self._UUIDv4, f'{object_id} - md5 - {md5.value}')
1033+
)
1034+
self.assertEqual(sha256.type, 'sha256')
1035+
self.assertEqual(sha256.object_relation, 'sha256')
1036+
self.assertEqual(sha256.value, hashes['SHA-256'])
1037+
self.assertEqual(
1038+
sha256.uuid,
1039+
uuid5(
1040+
self._UUIDv4, f'{object_id} - sha256 - {sha256.value}'
1041+
)
1042+
)
1043+
self._assert_multiple_equal(url.type, url.object_relation, 'url')
1044+
self.assertEqual(url.value, artifact.url)
1045+
self.assertEqual(
1046+
url.uuid, uuid5(self._UUIDv4, f'{object_id} - url - {url.value}')
1047+
)
1048+
10211049
def _check_pe_fields(self, misp_object, pe_extension, object_id):
10221050
self.assertEqual(len(misp_object.attributes), 3)
10231051
compilation_timestamp, number_of_sections, pe_type = misp_object.attributes

tests/test_external_stix20_import.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -876,34 +876,9 @@ def test_stix20_bundle_with_network_traffic_objects(self):
876876
self.assertEqual(encapsulates2.referenced_uuid, nt4.uuid)
877877
self.assertEqual(payload_ref.referenced_uuid, artifact.uuid)
878878
self.assertEqual(payload_ref.relationship_type, 'source-sent')
879-
self.assertEqual(artifact.name, 'artifact')
880879
self._check_misp_object_fields(artifact, observed_data2, '5')
881-
self.assertEqual(len(artifact.attributes), 3)
882-
md5, sha256, url = artifact.attributes
883-
observable = observed_data2.objects['5']
884-
hashes = observable.hashes
885-
self.assertEqual(md5.type, 'md5')
886-
self.assertEqual(md5.object_relation, 'md5')
887-
self.assertEqual(md5.value, hashes['MD5'])
888-
self.assertEqual(
889-
md5.uuid,
890-
uuid5(self._UUIDv4, f'{observed_data2.id} - 5 - md5 - {md5.value}')
891-
)
892-
self.assertEqual(sha256.type, 'sha256')
893-
self.assertEqual(sha256.object_relation, 'sha256')
894-
self.assertEqual(sha256.value, hashes['SHA-256'])
895-
self.assertEqual(
896-
sha256.uuid,
897-
uuid5(
898-
self._UUIDv4,
899-
f'{observed_data2.id} - 5 - sha256 - {sha256.value}'
900-
)
901-
)
902-
self._assert_multiple_equal(url.type, url.object_relation, 'url')
903-
self.assertEqual(url.value, observable.url)
904-
self.assertEqual(
905-
url.uuid,
906-
uuid5(self._UUIDv4, f'{observed_data2.id} - 5 - url - {url.value}')
880+
self._check_payload_object_fields(
881+
artifact, observed_data2.objects['5'], f'{observed_data2.id} - 5'
907882
)
908883
self._check_network_traffic_object_with_packet_counts(
909884
nt4, observed_data2, '4', '1', '2', 10

tests/test_external_stix21_bundles.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,11 @@ def get_bundle_with_mutex_observable(cls):
23032303
def get_bundle_with_network_traffic_objects(cls):
23042304
return cls.__assemble_bundle(*_NETWORK_TRAFFIC_OBJECTS)
23052305

2306+
@classmethod
2307+
def get_bundle_with_network_traffic_observables(cls):
2308+
_, _, _, ip, _, _, _, *observables = deepcopy(_NETWORK_TRAFFIC_OBJECTS)
2309+
return cls.__assemble_bundle(ip, *observables)
2310+
23062311
@classmethod
23072312
def get_bundle_with_process_objects(cls):
23082313
return cls.__assemble_bundle(*_PROCESS_OBJECTS)

tests/test_external_stix21_import.py

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,26 @@ def _check_network_traffic_object_with_packet_counts(
594594
self, misp_object, obbserved_data, network_traffic,
595595
src_ip, dst_ip, attributes_count):
596596
self.assertEqual(misp_object.name, 'network-traffic')
597-
self._check_misp_object_fields(misp_object, obbserved_data, network_traffic.id)
597+
self._check_misp_object_fields(
598+
misp_object, obbserved_data, network_traffic.id
599+
)
600+
self._check_network_traffic_packet_counts(
601+
misp_object, network_traffic, src_ip, dst_ip, attributes_count
602+
)
603+
604+
def _check_network_traffic_object_with_packet_sizes(
605+
self, misp_object, observed_data, network_traffic,
606+
src_ip, dst_ip, attributes_count):
607+
self.assertEqual(misp_object.name, 'network-traffic')
608+
self._check_misp_object_fields(
609+
misp_object, observed_data, network_traffic.id
610+
)
611+
self._check_network_traffic_packet_sizes(
612+
misp_object, network_traffic, src_ip, dst_ip, attributes_count
613+
)
614+
615+
def _check_network_traffic_packet_counts(self, misp_object, network_traffic,
616+
src_ip, dst_ip, attributes_count):
598617
attributes = misp_object.attributes
599618
self.assertEqual(len(attributes), attributes_count)
600619
src_packets, dst_packets = self._check_network_traffic_fields(
@@ -621,13 +640,8 @@ def _check_network_traffic_object_with_packet_counts(
621640
)
622641
)
623642

624-
def _check_network_traffic_object_with_packet_sizes(
625-
self, misp_object, observed_data, network_traffic,
626-
src_ip, dst_ip, attributes_count):
627-
self.assertEqual(misp_object.name, 'network-traffic')
628-
self._check_misp_object_fields(
629-
misp_object, observed_data, network_traffic.id
630-
)
643+
def _check_network_traffic_packet_sizes(self, misp_object, network_traffic,
644+
src_ip, dst_ip, attributes_count):
631645
attributes = misp_object.attributes
632646
self.assertEqual(len(attributes), attributes_count)
633647
src_bytes, dst_bytes = self._check_network_traffic_fields(
@@ -1232,18 +1246,21 @@ def test_stix21_bundle_with_network_traffic_objects(self):
12321246
misp_objects = self._check_misp_event_features_from_grouping(event, grouping)
12331247
self.assertEqual(len(misp_objects), 5)
12341248
nt_object1, nt_object2, nt_object3, artifact_object, nt_object4 = misp_objects
1249+
12351250
self._check_network_traffic_object_with_packet_sizes(
12361251
nt_object1, od1, nt1, ip1, ip2, 8
12371252
)
12381253
self.assertEqual(len(nt_object1.references), 1)
12391254
encapsulates1 = nt_object1.references[0]
12401255
self.assertEqual(encapsulates1.referenced_uuid, nt_object2.uuid)
1256+
12411257
self._check_network_traffic_object_with_packet_counts(
12421258
nt_object2, od1, nt2, ip1, ip3, 9
12431259
)
12441260
self.assertEqual(len(nt_object2.references), 1)
12451261
encapsulated1 = nt_object2.references[0]
12461262
self.assertEqual(encapsulated1.referenced_uuid, nt_object1.uuid)
1263+
12471264
self._check_network_traffic_object_with_packet_sizes(
12481265
nt_object3, od2, nt3, ip2, ip4, 9
12491266
)
@@ -1252,31 +1269,10 @@ def test_stix21_bundle_with_network_traffic_objects(self):
12521269
self.assertEqual(encapsulates2.referenced_uuid, nt_object4.uuid)
12531270
self.assertEqual(payload_ref.referenced_uuid, artifact_object.uuid)
12541271
self.assertEqual(payload_ref.relationship_type, 'source-sent')
1255-
self.assertEqual(artifact_object.name, 'artifact')
1272+
12561273
self._check_misp_object_fields(artifact_object, od2, artifact.id)
1257-
self.assertEqual(len(artifact_object.attributes), 3)
1258-
md5, sha256, url = artifact_object.attributes
1259-
hashes = artifact.hashes
1260-
self.assertEqual(md5.type, 'md5')
1261-
self.assertEqual(md5.object_relation, 'md5')
1262-
self.assertEqual(md5.value, hashes['MD5'])
1263-
self.assertEqual(
1264-
md5.uuid, uuid5(self._UUIDv4, f'{artifact.id} - md5 - {md5.value}')
1265-
)
1266-
self.assertEqual(sha256.type, 'sha256')
1267-
self.assertEqual(sha256.object_relation, 'sha256')
1268-
self.assertEqual(sha256.value, hashes['SHA-256'])
1269-
self.assertEqual(
1270-
sha256.uuid,
1271-
uuid5(
1272-
self._UUIDv4, f'{artifact.id} - sha256 - {sha256.value}'
1273-
)
1274-
)
1275-
self._assert_multiple_equal(url.type, url.object_relation, 'url')
1276-
self.assertEqual(url.value, artifact.url)
1277-
self.assertEqual(
1278-
url.uuid, uuid5(self._UUIDv4, f'{artifact.id} - url - {url.value}')
1279-
)
1274+
self._check_payload_object_fields(artifact_object, artifact)
1275+
12801276
self._check_network_traffic_object_with_packet_counts(
12811277
nt_object4, od2, nt4, ip4, ip5, 10
12821278
)
@@ -1285,6 +1281,7 @@ def test_stix21_bundle_with_network_traffic_objects(self):
12851281
self.assertEqual(encapsulated2.referenced_uuid, nt_object3.uuid)
12861282
self.assertEqual(payload_ref.referenced_uuid, artifact_object.uuid)
12871283
self.assertEqual(payload_ref.relationship_type, 'destination-sent')
1284+
12881285
self._assert_multiple_equal(
12891286
encapsulates1.relationship_type,
12901287
encapsulates2.relationship_type,
@@ -1296,6 +1293,41 @@ def test_stix21_bundle_with_network_traffic_objects(self):
12961293
'encapsulated-by'
12971294
)
12981295

1296+
def test_stix21_bundle_with_network_traffic_observables(self):
1297+
bundle = TestExternalSTIX21Bundles.get_bundle_with_network_traffic_observables()
1298+
self.parser.load_stix_bundle(bundle)
1299+
self.parser.parse_stix_bundle()
1300+
event = self.parser.misp_event
1301+
_, grouping, ip1, ip2, ip3, nt1, nt2, artifact = bundle.objects
1302+
misp_objects = self._check_misp_event_features_from_grouping(event, grouping)
1303+
self.assertEqual(len(misp_objects), 3)
1304+
nt_object1, artifact_object, nt_object2 = misp_objects
1305+
self._assert_multiple_equal(nt_object1.name, nt_object2.name, 'network-traffic')
1306+
1307+
self.assertEqual(nt_object1.uuid, nt1.id.split('--')[1])
1308+
self._check_network_traffic_packet_sizes(
1309+
nt_object1, nt1, ip1, ip2, 9
1310+
)
1311+
self.assertEqual(len(nt_object1.references), 2)
1312+
payload_ref, encapsulates2 = nt_object1.references
1313+
self.assertEqual(encapsulates2.referenced_uuid, nt_object2.uuid)
1314+
self.assertEqual(encapsulates2.relationship_type, 'encapsulates')
1315+
self.assertEqual(payload_ref.referenced_uuid, artifact_object.uuid)
1316+
self.assertEqual(payload_ref.relationship_type, 'source-sent')
1317+
1318+
self.assertEqual(artifact_object.uuid, artifact.id.split('--')[1])
1319+
self._check_payload_object_fields(artifact_object, artifact)
1320+
1321+
self._check_network_traffic_packet_counts(
1322+
nt_object2, nt2, ip2, ip3, 10
1323+
)
1324+
self.assertEqual(len(nt_object2.references), 2)
1325+
payload_ref, encapsulated2 = nt_object2.references
1326+
self.assertEqual(encapsulated2.referenced_uuid, nt_object1.uuid)
1327+
self.assertEqual(encapsulated2.relationship_type, 'encapsulated-by')
1328+
self.assertEqual(payload_ref.referenced_uuid, artifact_object.uuid)
1329+
self.assertEqual(payload_ref.relationship_type, 'destination-sent')
1330+
12991331
def test_stix21_bundle_with_process_objects(self):
13001332
bundle = TestExternalSTIX21Bundles.get_bundle_with_process_objects()
13011333
self.parser.load_stix_bundle(bundle)

0 commit comments

Comments
 (0)