Debian Codegraph @

tests/source_type_test.py

raw
1 # -*- coding: utf-8 -*-
2 """Tests for the source type objects."""
3
4 import unittest
5
6 from artifacts import errors
7 from artifacts import source_type
8
9 from tests import test_lib
10
11
12 class TestSourceType(source_type.SourceType):
13 """Class that implements a test source type."""
14
15 TYPE_INDICATOR = 'test'
16
17 def __init__(self, test=None):
18 """Initializes the source type object.
19
20 Args:
21 test (Optional[str]): test string.
22
23 Raises:
24 FormatError: when test is not set.
25 """
26 if not test:
27 raise errors.FormatError('Missing test value.')
28
29 super(TestSourceType, self).__init__()
30 self.test = test
31
32 def AsDict(self):
33 """Represents a source type as a dictionary.
34
35 Returns:
36 dict[str, str]: source type attributes.
37 """
38 return {'test': self.test}
39
40
41 class SourceTypeTest(test_lib.BaseTestCase):
42 """Class to test the artifact source type."""
43
44
45 class ArtifactGroupSourceTypeTest(test_lib.BaseTestCase):
46 """Class to test the artifact group source type."""
47
48 def testInitialize(self):
49 """Tests the __init__ function."""
50 source_type.ArtifactGroupSourceType(names=['test'])
51
52
53 class FileSourceTypeTest(test_lib.BaseTestCase):
54 """Class to test the files source type."""
55
56 def testInitialize(self):
57 """Tests the __init__ function."""
58 source_type.FileSourceType(paths=['test'])
59 source_type.FileSourceType(paths=['test'], separator='\\')
60
61 with self.assertRaises(errors.FormatError):
62 source_type.FileSourceType()
63
64 with self.assertRaises(errors.FormatError):
65 source_type.FileSourceType(paths='test')
66
67
68 class PathSourceTypeTest(test_lib.BaseTestCase):
69 """Class to test the paths source type."""
70
71 def testInitialize(self):
72 """Tests the __init__ function."""
73 source_type.PathSourceType(paths=['test'])
74 source_type.PathSourceType(paths=['test'], separator='\\')
75
76 with self.assertRaises(errors.FormatError):
77 source_type.PathSourceType()
78
79 with self.assertRaises(errors.FormatError):
80 source_type.PathSourceType(paths='test')
81
82
83 class WindowsRegistryKeySourceTypeTest(test_lib.BaseTestCase):
84 """Class to test the Windows Registry keys source type."""
85
86 def testInitialize(self):
87 """Tests the __init__ function."""
88 source_type.WindowsRegistryKeySourceType(keys=['HKEY_LOCAL_MACHINE\\test'])
89
90 with self.assertRaises(errors.FormatError):
91 source_type.WindowsRegistryKeySourceType(keys='HKEY_LOCAL_MACHINE\\test')
92
93
94 class WindowsRegistryValueSourceTypeTest(test_lib.BaseTestCase):
95 """Class to test the Windows Registry value source type."""
96
97 def testInitialize(self):
98 """Tests the __init__ function."""
99 key_value_pair = {'key': 'HKEY_LOCAL_MACHINE\\test', 'value': 'test'}
100 source_type.WindowsRegistryValueSourceType(key_value_pairs=[key_value_pair])
101
102 key_value_pair = {'bad': 'test', 'value': 'test'}
103 with self.assertRaises(errors.FormatError):
104 source_type.WindowsRegistryValueSourceType(
105 key_value_pairs=[key_value_pair])
106
107 with self.assertRaises(errors.FormatError):
108 source_type.WindowsRegistryValueSourceType(key_value_pairs=key_value_pair)
109
110
111 class WMIQuerySourceTypeTest(test_lib.BaseTestCase):
112 """Class to test the WMI query source type."""
113
114 def testInitialize(self):
115 """Tests the __init__ function."""
116 source_type.WMIQuerySourceType(query='test')
117
118
119 class SourceTypeFactoryTest(test_lib.BaseTestCase):
120 """Class to test the source type factory."""
121
122 def testCreateSourceType(self):
123 """Tests the source type creation."""
124 source_type.SourceTypeFactory.RegisterSourceTypes([TestSourceType])
125
126 with self.assertRaises(KeyError):
127 source_type.SourceTypeFactory.RegisterSourceTypes([TestSourceType])
128
129 source_object = source_type.SourceTypeFactory.CreateSourceType(
130 'test', {'test': 'test123'})
131
132 self.assertIsNotNone(source_object)
133 self.assertEqual(source_object.test, 'test123')
134
135 with self.assertRaises(errors.FormatError):
136 source_object = source_type.SourceTypeFactory.CreateSourceType(
137 'test', {})
138
139 with self.assertRaises(errors.FormatError):
140 source_object = source_type.SourceTypeFactory.CreateSourceType(
141 'bogus', {})
142
143 source_type.SourceTypeFactory.DeregisterSourceType(TestSourceType)
144
145 def testRegisterSourceType(self):
146 """Tests the source type registration functions."""
147 expected_number_of_source_types = len(
148 source_type.SourceTypeFactory.GetSourceTypes())
149
150 source_type.SourceTypeFactory.RegisterSourceType(TestSourceType)
151
152 number_of_source_types = len(source_type.SourceTypeFactory.GetSourceTypes())
153 self.assertEqual(
154 number_of_source_types, expected_number_of_source_types + 1)
155
156 source_type.SourceTypeFactory.DeregisterSourceType(TestSourceType)
157
158 number_of_source_types = len(source_type.SourceTypeFactory.GetSourceTypes())
159 self.assertEqual(number_of_source_types, expected_number_of_source_types)
160
161 def testRegisterSourceTypeRaisesWhenAlreadyRegistered(self):
162 """Tests the source type registration functions when already registered."""
163 source_type.SourceTypeFactory.RegisterSourceType(TestSourceType)
164
165 with self.assertRaises(KeyError):
166 source_type.SourceTypeFactory.RegisterSourceType(TestSourceType)
167
168 source_type.SourceTypeFactory.DeregisterSourceType(TestSourceType)
169
170
171 if __name__ == '__main__':
172 unittest.main()