0
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
|
|
17 |
import unittest
|
|
18 |
import __init__
|
|
19 |
from cone.public import plugin
|
|
20 |
from CRMLPlugin.crml_model import *
|
|
21 |
from CRMLPlugin.crml_comparator import CrmlComparator
|
|
22 |
from cone.public.plugin import FlatComparisonResultEntry as Entry
|
|
23 |
|
|
24 |
class TestComparator(unittest.TestCase):
|
|
25 |
def setUp(self):
|
|
26 |
keys = [
|
|
27 |
CrmlSimpleKey(ref='Foo.Bar', int='0x1'),
|
|
28 |
CrmlBitmaskKey(int='0x2', bits=[CrmlBit(ref='Foo.Bit1', index=1),
|
|
29 |
CrmlBit(ref='Foo.Bit2', index=2),
|
|
30 |
CrmlBit(ref='Foo.Bit4', index=4, invert=True)]),
|
|
31 |
CrmlKeyRange(first_int='0x10000000', last_int='0x1FFFFFFF', ref="Foo.Seq",
|
|
32 |
subkeys=[CrmlKeyRangeSubKey(ref='Sub1', name='Sub-key 1', type='int', int='0x1'),
|
|
33 |
CrmlKeyRangeSubKey(ref='Sub2', name='Sub-key 2', type='real', int='0x2')]),
|
|
34 |
]
|
|
35 |
|
|
36 |
self.repo = CrmlRepository(
|
|
37 |
uid_value = '0x10203040',
|
|
38 |
uid_name = 'KCrUidTest',
|
|
39 |
owner = '0x11223344',
|
|
40 |
backup = True,
|
|
41 |
rfs = True,
|
|
42 |
access = CrmlAccess(),
|
|
43 |
keys = keys)
|
|
44 |
|
|
45 |
self.comparator = CrmlComparator('test.crml', self.repo)
|
|
46 |
|
|
47 |
|
|
48 |
def assert_comparison_result_after_change_equals(self,
|
|
49 |
modification_code, added=[], removed=[], modified=[],
|
|
50 |
check_keys_in_entry_data=True,
|
|
51 |
target_resource_ref='test.crml'):
|
|
52 |
# Make a copy of the test repo and modify it using the given code
|
|
53 |
repo = self.repo.copy()
|
|
54 |
exec(modification_code)
|
|
55 |
|
|
56 |
expected_result = plugin.FlatComparisonResult(only_in_source = removed,
|
|
57 |
only_in_target = added,
|
|
58 |
modified = modified)
|
|
59 |
|
|
60 |
comparator = CrmlComparator('test.crml', self.repo)
|
|
61 |
actual_result = comparator.flat_compare(target_resource_ref, repo)
|
|
62 |
self.assertEquals(expected_result, actual_result)
|
|
63 |
|
|
64 |
# Assert that all comparison result entries have references to the
|
|
65 |
# repository objects
|
|
66 |
for entry in actual_result.only_in_source:
|
|
67 |
self.assertTrue(isinstance(entry.data['repo'], CrmlRepository))
|
|
68 |
for entry in actual_result.only_in_target:
|
|
69 |
self.assertTrue(isinstance(entry.data['repo'], CrmlRepository))
|
|
70 |
for entry in actual_result.modified:
|
|
71 |
self.assertTrue(isinstance(entry.data['source_repo'], CrmlRepository))
|
|
72 |
self.assertTrue(isinstance(entry.data['target_repo'], CrmlRepository))
|
|
73 |
|
|
74 |
# Check also references to CRML key objects if specified
|
|
75 |
if check_keys_in_entry_data:
|
|
76 |
for entry in actual_result.only_in_source:
|
|
77 |
self.assertTrue(isinstance(entry.data['key'], CrmlKeyBase))
|
|
78 |
for entry in actual_result.only_in_target:
|
|
79 |
self.assertTrue(isinstance(entry.data['key'], CrmlKeyBase))
|
|
80 |
for entry in actual_result.modified:
|
|
81 |
self.assertTrue(isinstance(entry.data['source_key'], CrmlKeyBase))
|
|
82 |
self.assertTrue(isinstance(entry.data['target_key'], CrmlKeyBase))
|
|
83 |
|
|
84 |
def test_simple_key_changed(self):
|
|
85 |
def check(attrname, value_id, old_value, new_value):
|
|
86 |
self.assert_comparison_result_after_change_equals(
|
|
87 |
'repo.keys[0].%s = %r' % (attrname, new_value),
|
|
88 |
modified=[Entry(sub_id = '0x00000001',
|
|
89 |
value_id = value_id,
|
|
90 |
source_value = old_value,
|
|
91 |
target_value = new_value)])
|
|
92 |
|
|
93 |
check('ref', 'ref', 'Foo.Bar', 'Foo.Baz')
|
|
94 |
check('name', 'name', None, 'Foobar')
|
|
95 |
check('backup', 'backup', False, True)
|
|
96 |
check('read_only', 'read_only', False, True)
|
|
97 |
check('access.cap_rd', 'cap_rd', None, 'FooCapability')
|
|
98 |
check('access.cap_wr', 'cap_wr', None, 'FooCapability')
|
|
99 |
check('access.sid_rd', 'sid_rd', None, '0x12345678')
|
|
100 |
check('access.sid_wr', 'sid_wr', None, '0x12345678')
|
|
101 |
|
|
102 |
|
|
103 |
self.assert_comparison_result_after_change_equals(
|
|
104 |
'repo.keys[0].int = "0x5"',
|
|
105 |
added = [Entry(sub_id='0x00000005')],
|
|
106 |
removed = [Entry(sub_id='0x00000001')])
|
|
107 |
|
|
108 |
def test_bitmask_key_changed(self):
|
|
109 |
def check(attrname, value_id, old_value, new_value):
|
|
110 |
self.assert_comparison_result_after_change_equals(
|
|
111 |
'repo.keys[1].%s = %r' % (attrname, new_value),
|
|
112 |
modified=[Entry(sub_id = '0x00000002',
|
|
113 |
value_id = value_id,
|
|
114 |
source_value = old_value,
|
|
115 |
target_value = new_value)])
|
|
116 |
|
|
117 |
check('name', 'name', None, 'Foobar')
|
|
118 |
check('backup', 'backup', False, True)
|
|
119 |
check('read_only', 'read_only', False, True)
|
|
120 |
check('access.cap_rd', 'cap_rd', None, 'FooCapability')
|
|
121 |
check('access.cap_wr', 'cap_wr', None, 'FooCapability')
|
|
122 |
check('access.sid_rd', 'sid_rd', None, '0x12345678')
|
|
123 |
check('access.sid_wr', 'sid_wr', None, '0x12345678')
|
|
124 |
|
|
125 |
self.assert_comparison_result_after_change_equals(
|
|
126 |
'repo.keys[1].int = "0x5"',
|
|
127 |
added = [Entry(sub_id='0x00000005')],
|
|
128 |
removed = [Entry(sub_id='0x00000002')])
|
|
129 |
|
|
130 |
def test_bitmask_key_bit_changed(self):
|
|
131 |
def check(attrname, value_id, old_value, new_value):
|
|
132 |
self.assert_comparison_result_after_change_equals(
|
|
133 |
'repo.keys[1].bits[0].%s = %r' % (attrname, new_value),
|
|
134 |
modified = [Entry(sub_id = '0x00000002 (bit 1)',
|
|
135 |
value_id = value_id,
|
|
136 |
source_value = old_value,
|
|
137 |
target_value = new_value)])
|
|
138 |
|
|
139 |
check('ref', 'ref', 'Foo.Bit1', 'Foo.Bar.Bit')
|
|
140 |
check('invert', 'invert', False, True)
|
|
141 |
|
|
142 |
self.assert_comparison_result_after_change_equals(
|
|
143 |
'repo.keys[1].bits[0].index = 6',
|
|
144 |
added = [Entry(sub_id='0x00000002 (bit 6)')],
|
|
145 |
removed = [Entry(sub_id='0x00000002 (bit 1)')])
|
|
146 |
|
|
147 |
|
|
148 |
def test_key_range_changed(self):
|
|
149 |
def check(attrname, value_id, old_value, new_value):
|
|
150 |
self.assert_comparison_result_after_change_equals(
|
|
151 |
'repo.keys[2].%s = %r' % (attrname, new_value),
|
|
152 |
modified=[Entry(sub_id = '0x10000000-0x1FFFFFFF',
|
|
153 |
value_id = value_id,
|
|
154 |
source_value = old_value,
|
|
155 |
target_value = new_value)])
|
|
156 |
|
|
157 |
check('ref', 'ref', 'Foo.Seq', 'Foo.Bar')
|
|
158 |
check('name', 'name', None, 'Foobar')
|
|
159 |
check('backup', 'backup', False, True)
|
|
160 |
check('read_only', 'read_only', False, True)
|
|
161 |
check('access.cap_rd', 'cap_rd', None, 'FooCapability')
|
|
162 |
check('access.cap_wr', 'cap_wr', None, 'FooCapability')
|
|
163 |
check('access.sid_rd', 'sid_rd', None, '0x12345678')
|
|
164 |
check('access.sid_wr', 'sid_wr', None, '0x12345678')
|
|
165 |
|
|
166 |
def check(attrname, new_value, old_id, new_id):
|
|
167 |
self.assert_comparison_result_after_change_equals(
|
|
168 |
'repo.keys[2].%s = %r' % (attrname, new_value),
|
|
169 |
added = [Entry(sub_id=new_id)],
|
|
170 |
removed = [Entry(sub_id=old_id)])
|
|
171 |
|
|
172 |
check('first_int', '0x00000000', '0x10000000-0x1FFFFFFF', '0x00000000-0x1FFFFFFF')
|
|
173 |
check('last_int', '0x20000000', '0x10000000-0x1FFFFFFF', '0x10000000-0x20000000')
|
|
174 |
|
|
175 |
def test_key_range_subkey_changed(self):
|
|
176 |
def check(attrname, old_value, new_value):
|
|
177 |
self.assert_comparison_result_after_change_equals(
|
|
178 |
'repo.keys[2].subkeys[0].%s = %r' % (attrname, new_value),
|
|
179 |
modified = [Entry(sub_id = '0x10000000-0x1FFFFFFF (sub-key 0x00000001)',
|
|
180 |
value_id = attrname,
|
|
181 |
source_value = old_value,
|
|
182 |
target_value = new_value)])
|
|
183 |
|
|
184 |
check('ref', 'Sub1', 'FooSub')
|
|
185 |
check('name', 'Sub-key 1', 'Foo')
|
|
186 |
check('type', 'int', 'real')
|
|
187 |
|
|
188 |
self.assert_comparison_result_after_change_equals(
|
|
189 |
'repo.keys[2].subkeys[0].int = "0xA"',
|
|
190 |
added = [Entry(sub_id='0x10000000-0x1FFFFFFF (sub-key 0x0000000A)')],
|
|
191 |
removed = [Entry(sub_id='0x10000000-0x1FFFFFFF (sub-key 0x00000001)')])
|
|
192 |
|
|
193 |
def test_key_type_changed(self):
|
|
194 |
# Change a bitmask key into a simple key
|
|
195 |
self.assert_comparison_result_after_change_equals(
|
|
196 |
"repo.keys[1] = CrmlSimpleKey(ref='Foo.Bar', name='Foo', int='0x2')",
|
|
197 |
modified = [Entry(sub_id = '0x00000002',
|
|
198 |
value_id = 'key_type',
|
|
199 |
source_value = 'bitmask_key',
|
|
200 |
target_value = 'simple_key'),
|
|
201 |
Entry(sub_id = '0x00000002',
|
|
202 |
value_id = 'name',
|
|
203 |
source_value = None,
|
|
204 |
target_value = 'Foo')])
|
|
205 |
|
|
206 |
# Change a simple key into a bitmask key
|
|
207 |
self.assert_comparison_result_after_change_equals(
|
|
208 |
"repo.keys[0] = CrmlBitmaskKey(int='0x1', name='Foo', bits=[CrmlBit(ref='Foo.Bit1', index=1)])",
|
|
209 |
modified = [Entry(sub_id = '0x00000001',
|
|
210 |
value_id = 'key_type',
|
|
211 |
source_value = 'simple_key',
|
|
212 |
target_value = 'bitmask_key'),
|
|
213 |
Entry(sub_id = '0x00000001',
|
|
214 |
value_id = 'name',
|
|
215 |
source_value = None,
|
|
216 |
target_value = 'Foo')])
|
|
217 |
|
|
218 |
def test_repository_attrs_changed(self):
|
|
219 |
def check(attrname, value_id, old_value, new_value):
|
|
220 |
self.assert_comparison_result_after_change_equals(
|
|
221 |
'repo.%s = %r' % (attrname, new_value),
|
|
222 |
modified=[Entry(sub_id = None,
|
|
223 |
value_id = value_id,
|
|
224 |
source_value = old_value,
|
|
225 |
target_value = new_value)],
|
|
226 |
check_keys_in_entry_data=False)
|
|
227 |
|
|
228 |
check('uid_name', 'uid_name', 'KCrUidTest', 'Foobar')
|
|
229 |
check('owner', 'owner', '0x11223344', '0xAABBCCDD')
|
|
230 |
check('backup', 'backup', True, False)
|
|
231 |
check('rfs', 'rfs', True, False)
|
|
232 |
check('access.cap_rd', 'cap_rd', None, 'FooCapability')
|
|
233 |
check('access.cap_wr', 'cap_wr', None, 'FooCapability')
|
|
234 |
check('access.sid_rd', 'sid_rd', None, '0x12345678')
|
|
235 |
check('access.sid_wr', 'sid_wr', None, '0x12345678')
|
|
236 |
|
|
237 |
def test_repository_file_changed(self):
|
|
238 |
self.assert_comparison_result_after_change_equals(
|
|
239 |
'', # Make no modifications in the repository
|
|
240 |
modified=[Entry(sub_id = None,
|
|
241 |
value_id = 'file',
|
|
242 |
source_value = 'test.crml',
|
|
243 |
target_value = 'xyz.crml')],
|
|
244 |
target_resource_ref = 'xyz.crml',
|
|
245 |
check_keys_in_entry_data = False)
|
|
246 |
|
|
247 |
if __name__ == '__main__':
|
|
248 |
unittest.main() |