comparison tests/testCPE.py @ 40:1d9b2b06067e

Add a CPE parser (and tests)
author Benoît Allard <benoit.allard@greenbone.net>
date Mon, 29 Dec 2014 14:30:39 +0100
parents
children bb1dd2a55643
comparison
equal deleted inserted replaced
37:ba0eb65d4134 40:1d9b2b06067e
1 import unittest
2
3 from farolluz.parsers.cpe import CPE, CPEAttribute
4
5 class testbindToURI(unittest.TestCase):
6
7 def test_example1(self):
8 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.0\.6001'), update=CPEAttribute('beta'), edition=CPEAttribute(any=True))
9
10 self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:microsoft:internet_explorer:8.0.6001:beta')
11
12 def test_example2(self):
13 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.*'), update=CPEAttribute('sp?'))
14
15 self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:microsoft:internet_explorer:8.%02:sp%01')
16
17 def test_example3(self):
18 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('insight_diagnostics'), version=CPEAttribute(r'7\.4\.0\.1570'), update=CPEAttribute(na=True), sw_edition=CPEAttribute('online'), target_sw=CPEAttribute('win2003'), target_hw=CPEAttribute("x64"))
19
20 self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:hp:insight_diagnostics:7.4.0.1570:-:~~online~win2003~x64~')
21
22 def test_example4(self):
23 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('openview_network_manager'), version=CPEAttribute(r'7\.51'), target_sw=CPEAttribute('linux'))
24
25 self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:hp:openview_network_manager:7.51::~~~linux~~')
26
27 def test_example5(self):
28 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute(r'foo\\bar'), product=CPEAttribute(r'big\$money_manager_2010'), sw_edition=CPEAttribute('special'), target_sw=CPEAttribute('ipod_touch'), target_hw=CPEAttribute("80gb"))
29
30 self.assertEqual(cpe.bind_to_URI(), 'cpe:/a:foo%5cbar:big%24money_manager_2010:::~~special~ipod_touch~80gb~')
31
32 class testunbindURI(unittest.TestCase):
33
34 def test_example1(self):
35 cpe = CPE()
36 cpe.unbind_URI('cpe:/a:microsoft:internet_explorer:8.0.6001:beta')
37 self.assertEqual(cpe.part.value, 'a')
38 self.assertEqual(cpe.vendor.value, 'microsoft')
39 self.assertEqual(cpe.product.value, 'internet_explorer')
40 self.assertEqual(cpe.version.value, r'8\.0\.6001')
41 self.assertEqual(cpe.update.value, 'beta')
42 self.assertTrue(cpe.edition.any)
43 self.assertTrue(cpe.language.any)
44
45 def test_example2(self):
46 cpe = CPE()
47 cpe.unbind_URI('cpe:/a:microsoft:internet_explorer:8.%2a:sp%3f')
48 self.assertEqual(cpe.part.value, 'a')
49 self.assertEqual(cpe.vendor.value, 'microsoft')
50 self.assertEqual(cpe.product.value, 'internet_explorer')
51 self.assertEqual(cpe.version.value, r'8\.\*')
52 self.assertEqual(cpe.update.value, 'sp\?')
53 self.assertTrue(cpe.edition.any)
54 self.assertTrue(cpe.language.any)
55
56 def test_example3(self):
57 cpe = CPE()
58 cpe.unbind_URI('cpe:/a:microsoft:internet_explorer:8.%02:sp%01')
59 self.assertEqual(cpe.part.value, 'a')
60 self.assertEqual(cpe.vendor.value, 'microsoft')
61 self.assertEqual(cpe.product.value, 'internet_explorer')
62 self.assertEqual(cpe.version.value, r'8\.*')
63 self.assertEqual(cpe.update.value, 'sp?')
64 self.assertTrue(cpe.edition.any)
65 self.assertTrue(cpe.language.any)
66
67 def test_example4(self):
68 cpe = CPE()
69 cpe.unbind_URI('cpe:/a:hp:insight_diagnostics:7.4.0.1570::~~online~win2003~x64~')
70 self.assertEqual(cpe.part.value, 'a')
71 self.assertEqual(cpe.vendor.value, 'hp')
72 self.assertEqual(cpe.product.value, 'insight_diagnostics')
73 self.assertEqual(cpe.version.value, '7\.4\.0\.1570')
74 self.assertTrue(cpe.update.any)
75 self.assertTrue(cpe.edition.any)
76 self.assertEqual(cpe.sw_edition.value, 'online')
77 self.assertEqual(cpe.target_sw.value, 'win2003')
78 self.assertEqual(cpe.target_hw.value, 'x64')
79 self.assertTrue(cpe.other.any)
80 self.assertTrue(cpe.language.any)
81
82 def test_example5(self):
83 cpe = CPE()
84 cpe.unbind_URI('cpe:/a:hp:openview_network_manager:7.51:-:~~~linux~~')
85 self.assertEqual(cpe.part.value, 'a')
86 self.assertEqual(cpe.vendor.value, 'hp')
87 self.assertEqual(cpe.product.value, 'openview_network_manager')
88 self.assertEqual(cpe.version.value, '7\.51')
89 self.assertTrue(cpe.update.na)
90 self.assertTrue(cpe.edition.any)
91 self.assertTrue(cpe.sw_edition.any)
92 self.assertEqual(cpe.target_sw.value, 'linux')
93 self.assertTrue(cpe.target_hw.any)
94 self.assertTrue(cpe.other.any)
95 self.assertTrue(cpe.language.any)
96
97
98 def test_example6(self):
99 cpe = CPE()
100 self.assertRaises(KeyError, cpe.unbind_URI, 'cpe:/a:foo%5cbar:big%24money_2010%07:::~~special~ipod_touch~80gb~')
101
102
103 def test_example7(self):
104 cpe = CPE()
105 cpe.unbind_URI('cpe:/a:foo~bar:big%7emoney_2010')
106 self.assertEqual(cpe.part.value, 'a')
107 self.assertEqual(cpe.vendor.value, 'foo\~bar')
108 self.assertEqual(cpe.product.value, 'big\~money_2010')
109 self.assertTrue(cpe.version.any)
110 self.assertTrue(cpe.update.any)
111 self.assertTrue(cpe.edition.any)
112 self.assertTrue(cpe.language.any)
113
114 def test_example8(self):
115 cpe = CPE()
116 self.assertRaises(ValueError, cpe.unbind_URI, 'cpe:/a:foo:bar:12.%02.1234')
117
118 class testbindFS(unittest.TestCase):
119
120 def test_example1(self):
121 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.0\.6001'), update=CPEAttribute('beta'), edition=CPEAttribute(any=True))
122
123 self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*')
124
125 def test_example2(self):
126 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('microsoft'), product=CPEAttribute('internet_explorer'), version=CPEAttribute(r'8\.*'), update=CPEAttribute('sp?'), edition=CPEAttribute(any=True))
127
128 self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:microsoft:internet_explorer:8.*:sp?:*:*:*:*:*:*')
129
130 cpe.version = CPEAttribute(r'8\.\*')
131
132 self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:microsoft:internet_explorer:8.\*:sp?:*:*:*:*:*:*')
133
134 def test_example3(self):
135 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('insight'), version=CPEAttribute(r'7\.4\.0\.1570'), update=CPEAttribute(na=True), sw_edition=CPEAttribute("online"), target_sw=CPEAttribute("win2003"), target_hw=CPEAttribute("x64"))
136
137 self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:hp:insight:7.4.0.1570:-:*:*:online:win2003:x64:*')
138
139 def test_example4(self):
140 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute('hp'), product=CPEAttribute('openview_network_manager'), version=CPEAttribute(r'7\.51'), target_sw=CPEAttribute('linux'))
141
142 self.assertEqual(cpe.bind_to_fs(), 'cpe:2.3:a:hp:openview_network_manager:7.51:*:*:*:*:linux:*:*')
143
144 def test_example5(self):
145 cpe = CPE(part=CPEAttribute('a'), vendor=CPEAttribute(r'foo\\bar'), product=CPEAttribute(r'big\$money_2010'), sw_edition=CPEAttribute('special'), target_sw=CPEAttribute('ipod_touch'), target_hw=CPEAttribute("80gb"))
146
147 self.assertEqual(cpe.bind_to_fs(), r'cpe:2.3:a:foo\\bar:big\$money_2010:*:*:*:*:special:ipod_touch:80gb:*')
148
149 class testunbind_fs(unittest.TestCase):
150
151 def test_example1(self):
152 cpe = CPE()
153 cpe.unbind_fs('cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:*:*:*:*:*')
154 self.assertEqual(cpe.part.value, 'a')
155 self.assertEqual(cpe.vendor.value, 'microsoft')
156 self.assertEqual(cpe.product.value, 'internet_explorer')
157 self.assertEqual(cpe.version.value, '8\.0\.6001')
158 self.assertEqual(cpe.update.value, "beta")
159 self.assertTrue(cpe.edition.any)
160 self.assertTrue(cpe.sw_edition.any)
161 self.assertTrue(cpe.target_sw.any)
162 self.assertTrue(cpe.target_hw.any)
163 self.assertTrue(cpe.other.any)
164 self.assertTrue(cpe.language.any)
165
166 def test_example2(self):
167 cpe = CPE()
168 cpe.unbind_fs('cpe:2.3:a:microsoft:internet_explorer:8.*:sp?:*:*:*:*:*:*')
169 self.assertEqual(cpe.part.value, 'a')
170 self.assertEqual(cpe.vendor.value, 'microsoft')
171 self.assertEqual(cpe.product.value, 'internet_explorer')
172 self.assertEqual(cpe.version.value, '8\.*')
173 self.assertEqual(cpe.update.value, "sp?")
174 self.assertTrue(cpe.edition.any)
175 self.assertTrue(cpe.sw_edition.any)
176 self.assertTrue(cpe.target_sw.any)
177 self.assertTrue(cpe.target_hw.any)
178 self.assertTrue(cpe.other.any)
179 self.assertTrue(cpe.language.any)
180
181 def test_example3(self):
182 cpe = CPE()
183 cpe.unbind_fs('cpe:2.3:a:hp:insight_diagnostics:7.4.0.1570:-:*:*:online:win2003:x64:*')
184 self.assertEqual(cpe.part.value, 'a')
185 self.assertEqual(cpe.vendor.value, 'hp')
186 self.assertEqual(cpe.product.value, 'insight_diagnostics')
187 self.assertEqual(cpe.version.value, '7\.4\.0\.1570')
188 self.assertTrue(cpe.update.na)
189 self.assertTrue(cpe.edition.any)
190 self.assertEqual(cpe.sw_edition.value, "online")
191 self.assertEqual(cpe.target_sw.value, "win2003")
192 self.assertEqual(cpe.target_hw.value, "x64")
193 self.assertTrue(cpe.other.any)
194 self.assertTrue(cpe.language.any)
195
196 self.assertRaises(ValueError, cpe.unbind_fs, 'cpe:2.3:a:hp:insight_diagnostics:7.4.*.1570:*:*:*:*:*:*')
197
198
199 def test_example4(self):
200 cpe = CPE()
201 cpe.unbind_fs(r'cpe:2.3:a:foo\\bar:big\$money:2010:*:*:*:special:ipod_touch:80gb:*')
202 self.assertEqual(cpe.part.value, 'a')
203 self.assertEqual(cpe.vendor.value, r'foo\\bar')
204 self.assertEqual(cpe.product.value, 'big\$money')
205 self.assertEqual(cpe.version.value, '2010')
206 self.assertTrue(cpe.update.any)
207 self.assertTrue(cpe.edition.any)
208 self.assertEqual(cpe.sw_edition.value, "special")
209 self.assertEqual(cpe.target_sw.value, "ipod_touch")
210 self.assertEqual(cpe.target_hw.value, "80gb")
211 self.assertTrue(cpe.other.any)
212 self.assertTrue(cpe.language.any)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)