1# Copyright (c) 2017, The MITRE Corporation. All rights reserved.
2# See LICENSE.txt for complete terms.
3
4import unittest
5
6from stix.test import EntityTestCase, assert_warnings
7
8from stix.utils import silence_warnings
9from stix.common.related import (
10    RelatedCampaign, RelatedCampaignRef, RelatedIdentity, RelatedCOA,
11    RelatedPackage, RelatedPackageRef, RelatedExploitTarget, RelatedIncident,
12    RelatedIndicator, RelatedObservable, RelatedThreatActor, RelatedTTP,
13    RelatedPackageRefs, RelatedPackages, RelatedReports, RelatedReport
14)
15
16
17class RelatedReportTests(EntityTestCase, unittest.TestCase):
18    klass = RelatedReport
19    _full_dict = {
20        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
21        'information_source': {
22            'description': "Source of the relationship",
23        },
24        'relationship': "Associated",
25        'report': {
26            'id': 'example:bar-1',
27            'version': '1.0',
28            'header': {
29                'title': 'Test'
30            }
31        }
32    }
33
34
35class RelatedReportsTests(EntityTestCase, unittest.TestCase):
36    klass = RelatedReports
37
38    _full_dict = {
39        'scope': 'inclusive',
40        'related_reports': [
41            {
42                'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
43                'information_source': {
44                    'description': "Source of the relationship",
45                },
46                'relationship': "Associated",
47                'report': {
48                    'id': 'example:bar-1',
49                    'version': '1.2',
50                    'header': {
51                        'title': 'Test'
52                    }
53                }
54            },
55            {
56                'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
57                'information_source': {
58                    'description': "Source of the relationship",
59                },
60                'relationship': "Associated",
61                'report': {
62                    'id': 'example:bar-2',
63                    'version': '1.2',
64                    'header': {
65                        'title': 'Test'
66                    }
67                }
68            }
69        ]
70    }
71
72
73class RelatedPackageRefsTests(EntityTestCase, unittest.TestCase):
74    klass = RelatedPackageRefs
75    _full_dict = {
76        'packages': [
77            {
78                'idref': "example:foo-1",
79                'timestamp': "2014-01-31T06:14:46",
80                'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
81                'information_source': {
82                    'description': "Source of the relationship",
83                },
84                'relationship': "Associated"
85            },
86            {
87                'idref': "example:foo--2",
88                'timestamp': "2014-01-31T06:14:46",
89                'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
90                'information_source': {
91                    'description': "Source of the relationship",
92                },
93                'relationship': "Associated"
94            }
95        ]
96    }
97
98    @silence_warnings
99    def test_add_stix_package(self):
100        from stix.core import STIXPackage
101
102        l = RelatedPackageRefs()
103        l.append(STIXPackage())
104
105        self.assertEqual(1, len(l))
106
107
108    @silence_warnings
109    def test_add_bad_type(self):
110        from stix.indicator import Indicator
111
112        l = RelatedPackageRefs()
113
114        self.assertRaises(
115            TypeError,
116            l.append,
117            Indicator()
118        )
119
120    @assert_warnings
121    def test_deprecated_warning(self):
122        from stix.core import STIXPackage
123
124        l = RelatedPackageRefs()
125        l.append(STIXPackage())
126
127
128class RelatedPackageRefTests(EntityTestCase, unittest.TestCase):
129    klass = RelatedPackageRef
130    _full_dict = {
131        'idref': "example:Campaign-133",
132        'timestamp': "2014-01-31T06:14:46",
133        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
134        'information_source': {
135            'description': "Source of the relationship",
136        },
137        'relationship': "Associated",
138    }
139
140
141class RelatedCampaignTests(EntityTestCase, unittest.TestCase):
142    klass = RelatedCampaign
143    _full_dict = {
144        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
145        'information_source': {
146            'description': "Source of the relationship",
147        },
148        'relationship': "Associated",
149        'campaign': {
150            'id': 'example:bar-1',
151            'title': 'Test'
152        }
153    }
154
155
156class RelatedIndicatorTests(EntityTestCase, unittest.TestCase):
157    klass = RelatedIndicator
158    _full_dict = {
159        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
160        'information_source': {
161            'description': "Source of the relationship",
162        },
163        'relationship': "Associated",
164        'indicator': {
165            'id': 'example:bar-1',
166            'title': 'Test'
167        }
168    }
169
170
171class RelatedIncidentTests(EntityTestCase, unittest.TestCase):
172    klass = RelatedIncident
173    _full_dict = {
174        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
175        'information_source': {
176            'description': "Source of the relationship",
177        },
178        'relationship': "Associated",
179        'incident': {
180            'id': 'example:bar-1',
181            'title': 'Test'
182        }
183    }
184
185
186class RelatedExploitTargetTests(EntityTestCase, unittest.TestCase):
187    klass = RelatedExploitTarget
188    _full_dict = {
189        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
190        'information_source': {
191            'description': "Source of the relationship",
192        },
193        'relationship': "Associated",
194        'exploit_target': {
195            'id': 'example:bar-1',
196            'title': 'Test'
197        }
198    }
199
200
201class RelatedThreatActorTests(EntityTestCase, unittest.TestCase):
202    klass = RelatedThreatActor
203    _full_dict = {
204        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
205        'information_source': {
206            'description': "Source of the relationship",
207        },
208        'relationship': "Associated",
209        'threat_actor': {
210            'id': 'example:bar-1',
211            'title': 'Test'
212        }
213    }
214
215class RelatedCOATests(EntityTestCase, unittest.TestCase):
216    klass = RelatedCOA
217    _full_dict = {
218        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
219        'information_source': {
220            'description': "Source of the relationship",
221        },
222        'relationship': "Associated",
223        'course_of_action': {
224            'id': 'example:bar-1',
225            'title': 'Test'
226        }
227    }
228
229class RelatedTTPTests(EntityTestCase, unittest.TestCase):
230    klass = RelatedTTP
231    _full_dict = {
232        #'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
233        'information_source': {
234            'description': "Source of the relationship",
235        },
236        #'relationship': "Associated",
237#         'ttp': {
238#             'id': 'example:bar-1',
239#             'title': 'Test'
240#         }
241    }
242
243class RelatedIdentityTests(EntityTestCase, unittest.TestCase):
244    klass = RelatedIdentity
245    _full_dict = {
246        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
247        'information_source': {
248            'description': "Source of the relationship",
249        },
250        'relationship': "Associated",
251        'identity': {
252            'id': 'example:bar-1',
253            'name': 'Test'
254        }
255    }
256
257class RelatedObservableTests(EntityTestCase, unittest.TestCase):
258    klass = RelatedObservable
259    _full_dict = {
260        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
261        'information_source': {
262            'description': "Source of the relationship",
263        },
264        'relationship': "Associated",
265        'observable': {
266            'id': 'example:bar-1',
267            'title': 'Test'
268        }
269    }
270
271class RelatedPackageTests(EntityTestCase, unittest.TestCase):
272    klass = RelatedPackage
273    _full_dict = {
274        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
275        'information_source': {
276            'description': "Source of the relationship",
277        },
278        'relationship': "Associated",
279        'package': {
280            'id': 'example:bar-1',
281            'version': '1.2',
282            'stix_header': {
283                'title': 'Test'
284            }
285        }
286    }
287
288class RelatedPackagesTests(EntityTestCase, unittest.TestCase):
289    klass = RelatedPackages
290    _full_dict = {
291        'scope': 'inclusive',
292        'related_packages': [
293            {
294                'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
295                'information_source': {
296                    'description': "Source of the relationship",
297                },
298                'relationship': "Associated",
299                'package': {
300                    'id': 'example:bar-1',
301                    'version': '1.2',
302                    'stix_header': {
303                        'title': 'Test'
304                    }
305                }
306            },
307            {
308                'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
309                'information_source': {
310                    'description': "Source of the relationship",
311                },
312                'relationship': "Associated",
313                'package': {
314                    'id': 'example:bar-2',
315                    'version': '1.2',
316                    'stix_header': {
317                        'title': 'Test'
318                    }
319                }
320            }
321        ]
322    }
323
324class RelatedCampaignRefTests(EntityTestCase, unittest.TestCase):
325    klass = RelatedCampaignRef
326    _full_dict = {
327        'confidence': {'value': {'value': "Medium", 'xsi:type':'stixVocabs:HighMediumLowVocab-1.0'}},
328        'information_source': {
329            'description': "Source of the relationship",
330        },
331        'relationship': "Associated",
332        'campaign': {
333            'idref': "example:foo-1",
334            'timestamp': "2014-01-31T06:14:46",
335            'names': ["foo", "bar"]
336        }
337    }
338
339
340if __name__ == "__main__":
341    unittest.main()
342
343