0
|
1 |
# *-* coding: utf-8 *-*
|
|
2 |
#
|
|
3 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
# All rights reserved.
|
|
5 |
# This component and the accompanying materials are made available
|
|
6 |
# under the terms of "Eclipse Public License v1.0"
|
|
7 |
# which accompanies this distribution, and is available
|
|
8 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
9 |
#
|
|
10 |
# Initial Contributors:
|
|
11 |
# Nokia Corporation - initial contribution.
|
|
12 |
#
|
|
13 |
# Contributors:
|
|
14 |
#
|
|
15 |
# Description:
|
|
16 |
#
|
|
17 |
|
|
18 |
"""
|
|
19 |
When editing the hex data in the test cases, this may come in handy:
|
|
20 |
|
|
21 |
1. Start the Python command line interpreter
|
|
22 |
2. Paste the following lines there:
|
|
23 |
|
|
24 |
from struct import pack, unpack
|
|
25 |
bin2hex = lambda d: ''.join("%02X" % ord(c) for c in d)
|
|
26 |
hexpack = lambda fmt, *args: bin2hex(pack(fmt, *args))
|
|
27 |
hex2bin = lambda h: ''.join([chr(int(h[i*2:i*2+2], 16)) for i in xrange(len(h)/2)])
|
|
28 |
hexunpack = lambda fmt, data: unpack(fmt, hex2bin(data))
|
|
29 |
|
|
30 |
Now you can get the hex representation for any format supported by
|
|
31 |
struct.pack() easily. For example, formatting a little-endian unsigned short:
|
|
32 |
|
|
33 |
>>> hexpack('<H', 1234)
|
|
34 |
'D204'
|
|
35 |
|
|
36 |
...and conversely:
|
|
37 |
|
|
38 |
>>> hexunpack('<H', 'D204')
|
|
39 |
(1234,)
|
|
40 |
"""
|
|
41 |
|
|
42 |
import unittest
|
|
43 |
import os, shutil, random
|
|
44 |
import sys
|
|
45 |
|
|
46 |
ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
|
|
47 |
|
|
48 |
from testautomation.utils import hex_to_bindata
|
|
49 |
|
|
50 |
from hcrplugin.hcrrepository import HcrRepository, HcrRecord
|
|
51 |
from hcrplugin.hcr_writer import HcrWriter
|
|
52 |
from hcrplugin.hcr_reader import HcrReader
|
|
53 |
from hcrplugin import hcr_exceptions
|
|
54 |
|
|
55 |
class TestReadWriteHcrRepository(unittest.TestCase):
|
|
56 |
|
|
57 |
def setUp(self):
|
|
58 |
self.writer = HcrWriter()
|
|
59 |
self.reader = HcrReader()
|
|
60 |
|
|
61 |
def write_repo_assertion_failure_files(self, actual_data, expected_data, failure_report_dir, filename):
|
|
62 |
dir = os.path.join(ROOT_PATH, 'temp/repo_assertion_failure')
|
|
63 |
dir = os.path.normpath(os.path.join(dir, failure_report_dir))
|
|
64 |
if not os.path.exists(dir):
|
|
65 |
os.makedirs(dir)
|
|
66 |
|
|
67 |
f = open(os.path.join(dir, "actual_" + filename), "wb")
|
|
68 |
try: f.write(actual_data)
|
|
69 |
finally: f.close()
|
|
70 |
|
|
71 |
f = open(os.path.join(dir, "expected_" + filename), "wb")
|
|
72 |
try: f.write(expected_data)
|
|
73 |
finally: f.close()
|
|
74 |
|
|
75 |
return dir
|
|
76 |
|
|
77 |
def _run_test_read_write_repository(self, repo, repo_data, failure_report_dir):
|
|
78 |
"""
|
|
79 |
Test reading and writing the repository using the given data.
|
|
80 |
|
|
81 |
In case of a failure, the actual and expected data are written
|
|
82 |
to the given directory, so that they can be compared using e.g.
|
|
83 |
Beyond Compare.
|
|
84 |
|
|
85 |
@param repo: The repository object to test.
|
|
86 |
@param repo_data: The expected binary data corresponding to the repository object.
|
|
87 |
@param failure_report_dir: The directory where files are written in case of
|
|
88 |
a failure. Note that this should be just e.g. 'empty_repo', the parent dirs
|
|
89 |
are appended automatically.
|
|
90 |
"""
|
|
91 |
# Test writing
|
|
92 |
expected_data = repo_data
|
|
93 |
actual_data = self.writer.get_repository_bindata(repo)
|
|
94 |
if actual_data != expected_data:
|
|
95 |
dir = self.write_repo_assertion_failure_files(
|
|
96 |
actual_data, expected_data,
|
|
97 |
failure_report_dir, 'repo.dat')
|
|
98 |
self.fail("Actual and expected repository data are not equal!\n"+\
|
|
99 |
"See the files in '%s'" % dir)
|
|
100 |
|
|
101 |
|
|
102 |
# Test reading
|
|
103 |
expected_repo = repo
|
|
104 |
actual_repo = self.reader.parse_repository_from_bindata(repo_data)
|
|
105 |
if actual_repo != expected_repo:
|
|
106 |
dir = self.write_repo_assertion_failure_files(
|
|
107 |
repr(actual_repo), repr(expected_repo),
|
|
108 |
failure_report_dir, 'repo.txt')
|
|
109 |
self.fail("Actual and expected repository objects are not equal!\n"+\
|
|
110 |
"See the files in '%s'" % dir)
|
|
111 |
|
|
112 |
# -------------------------------------------------------------------------
|
|
113 |
#
|
|
114 |
# -------------------------------------------------------------------------
|
|
115 |
|
|
116 |
def test_read_write_empty_repository_1(self):
|
|
117 |
repo = HcrRepository([], version=2, flags=3)
|
|
118 |
repo_data = hex_to_bindata(
|
|
119 |
"48435266 0200 0300 00000000 20000000"+\
|
|
120 |
"00000000 000000000000000000000000")
|
|
121 |
|
|
122 |
self._run_test_read_write_repository(repo, repo_data, 'empty_repo_1')
|
|
123 |
|
|
124 |
def test_read_write_empty_repository_2(self):
|
|
125 |
repo = HcrRepository([], version=0xBEEF, flags=0xCAFE)
|
|
126 |
repo_data = hex_to_bindata(
|
|
127 |
"48435266 EFBE FECA 00000000 20000000"+\
|
|
128 |
"00000000 000000000000000000000000")
|
|
129 |
|
|
130 |
self._run_test_read_write_repository(repo, repo_data, 'empty_repo_2')
|
|
131 |
|
|
132 |
# -------------------------------------------------------------------------
|
|
133 |
#
|
|
134 |
# -------------------------------------------------------------------------
|
|
135 |
|
|
136 |
def test_read_write_repository_without_lsd(self):
|
|
137 |
records = [
|
|
138 |
HcrRecord(HcrRecord.VALTYPE_BOOL, True, 1, 1, 0),
|
|
139 |
HcrRecord(HcrRecord.VALTYPE_INT8, -123, 1, 2, 0),
|
|
140 |
HcrRecord(HcrRecord.VALTYPE_UINT8, 204, 1, 3, 0),
|
|
141 |
HcrRecord(HcrRecord.VALTYPE_INT16, -13423, 2, 1, 0),
|
|
142 |
HcrRecord(HcrRecord.VALTYPE_UINT16, 54321, 2, 2, 0),
|
|
143 |
HcrRecord(HcrRecord.VALTYPE_INT32, -1000000000, 2, 3, 0),
|
|
144 |
HcrRecord(HcrRecord.VALTYPE_UINT32, 4000000000, 3, 1, 0),
|
|
145 |
HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 0xAABBCCDD, 3, 2, 0),
|
|
146 |
]
|
|
147 |
# Shuffle the records to make sure that they are sorted properly when writing
|
|
148 |
random.shuffle(records)
|
|
149 |
repo = HcrRepository(records, version=2, flags=3)
|
|
150 |
|
|
151 |
h = hex_to_bindata
|
|
152 |
data = [
|
|
153 |
# Header
|
|
154 |
h("48435266 0200 0300 08000000 C0000000"),
|
|
155 |
h("00000000 000000000000000000000000"),
|
|
156 |
# Record section
|
|
157 |
h("01000000 01000000 08000000 0000 0000 01000000"), # bool
|
|
158 |
h("01000000 02000000 04000000 0000 0000 85FFFFFF"), # int8
|
|
159 |
h("01000000 03000000 40000000 0000 0000 CC000000"), # uint8
|
|
160 |
h("02000000 01000000 02000000 0000 0000 91CBFFFF"), # int16
|
|
161 |
h("02000000 02000000 20000000 0000 0000 31D40000"), # uint16
|
|
162 |
h("02000000 03000000 01000000 0000 0000 003665C4"), # int32
|
|
163 |
h("03000000 01000000 10000000 0000 0000 00286BEE"), # uint32
|
|
164 |
h("03000000 02000000 00010000 0000 0000 DDCCBBAA"), # linaddr
|
|
165 |
]
|
|
166 |
repo_data = ''.join(data)
|
|
167 |
|
|
168 |
self._run_test_read_write_repository(repo, repo_data, 'repo_without_lsd')
|
|
169 |
|
|
170 |
# -------------------------------------------------------------------------
|
|
171 |
#
|
|
172 |
# -------------------------------------------------------------------------
|
|
173 |
|
|
174 |
def test_read_write_repository_with_lsd(self):
|
|
175 |
records = [
|
|
176 |
HcrRecord(HcrRecord.VALTYPE_INT64, 9211026413402420220, 1, 1, 0),
|
|
177 |
HcrRecord(HcrRecord.VALTYPE_UINT64, 10746170304040729876, 1, 2, 0),
|
|
178 |
HcrRecord(HcrRecord.VALTYPE_TEXT8, u'Cost 100€', 1, 3, 0),
|
|
179 |
HcrRecord(HcrRecord.VALTYPE_BIN_DATA, 'test test', 2, 1, 0),
|
|
180 |
HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, [-2**31, 0, 2**31-1], 2, 2, 0),
|
|
181 |
HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [0, 100000, 2**32-1], 2, 3, 0),
|
|
182 |
]
|
|
183 |
# Shuffle the records to make sure that they are sorted properly when writing
|
|
184 |
random.shuffle(records)
|
|
185 |
repo = HcrRepository(records, version=2, flags=3)
|
|
186 |
|
|
187 |
|
|
188 |
# Record section size: 6 * 20 = 120
|
|
189 |
# LSD offset: 32 + 120 = 152
|
|
190 |
# LSD size: 8 + 8 + 12 + 12 + 12 + 12 = 64
|
|
191 |
h = hex_to_bindata
|
|
192 |
data = [
|
|
193 |
# Header
|
|
194 |
h("48435266 0200 0300 06000000 98000000"),
|
|
195 |
h("40000000 000000000000000000000000"),
|
|
196 |
# Record section
|
|
197 |
h("01000000 01000000 00000001 0000 0800 00000000"), # int64, lsd pos = (0, 8)
|
|
198 |
h("01000000 02000000 00000002 0000 0800 08000000"), # uint64, lsd pos = (8, 8)
|
|
199 |
h("01000000 03000000 00000200 0000 0B00 10000000"), # text8, lsd pos = (8 + 8, 11)
|
|
200 |
h("02000000 01000000 00000100 0000 0900 1C000000"), # bindata, lsd pos = (8 + 8 + 12, 9)
|
|
201 |
h("02000000 02000000 00000400 0000 0C00 28000000"), # arrayint32, lsd pos = (8 + 8 + 12 + 12, 12)
|
|
202 |
h("02000000 03000000 00000800 0000 0C00 34000000"), # arrayuint32, lsd pos = (8 + 8 + 12 + 12 + 12, 12)
|
|
203 |
# LSD section
|
|
204 |
h("FC73 978B B823 D47F"), # 8 bytes
|
|
205 |
h("14FD 32B4 F410 2295"), # 8 bytes
|
|
206 |
"Cost 100" + h("E2 82 AC 00"), # 12 bytes
|
|
207 |
"test test" + h("00 00 00"), # 12 bytes
|
|
208 |
h("00000080 00000000 FFFFFF7F"), # 12 bytes
|
|
209 |
h("00000000 A0860100 FFFFFFFF"), # 12 bytes
|
|
210 |
]
|
|
211 |
|
|
212 |
repo_data = ''.join(data)
|
|
213 |
|
|
214 |
self._run_test_read_write_repository(repo, repo_data, 'repo_with_lsd')
|
|
215 |
|
|
216 |
# -------------------------------------------------------------------------
|
|
217 |
#
|
|
218 |
# -------------------------------------------------------------------------
|
|
219 |
|
|
220 |
def test_read_write_repository_with_all_record_types(self):
|
|
221 |
records = [
|
|
222 |
HcrRecord(HcrRecord.VALTYPE_BOOL, True, 1, 1, 0),
|
|
223 |
HcrRecord(HcrRecord.VALTYPE_INT8, -123, 1, 2, 0),
|
|
224 |
HcrRecord(HcrRecord.VALTYPE_UINT8, 204, 1, 3, 0),
|
|
225 |
HcrRecord(HcrRecord.VALTYPE_INT16, -13423, 2, 1, 0),
|
|
226 |
HcrRecord(HcrRecord.VALTYPE_UINT16, 54321, 2, 2, 0),
|
|
227 |
HcrRecord(HcrRecord.VALTYPE_INT32, -1000000000, 2, 3, 0),
|
|
228 |
HcrRecord(HcrRecord.VALTYPE_UINT32, 4000000000, 3, 1, 0),
|
|
229 |
HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 0xAABBCCDD, 3, 2, 0),
|
|
230 |
|
|
231 |
HcrRecord(HcrRecord.VALTYPE_INT64, 9211026413402420220, 4, 1, 0),
|
|
232 |
HcrRecord(HcrRecord.VALTYPE_UINT64, 10746170304040729876, 4, 2, 0),
|
|
233 |
HcrRecord(HcrRecord.VALTYPE_TEXT8, u'Cost 100€', 4, 3, 0),
|
|
234 |
HcrRecord(HcrRecord.VALTYPE_TEXT8, '', 5, 1, 0),
|
|
235 |
HcrRecord(HcrRecord.VALTYPE_BIN_DATA, 'test test', 5, 2, 0),
|
|
236 |
HcrRecord(HcrRecord.VALTYPE_BIN_DATA, '', 5, 3, 0),
|
|
237 |
HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, [-2**31, 0, 2**31-1], 6, 1, 0),
|
|
238 |
HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, [], 6, 2, 0),
|
|
239 |
HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [0, 100000, 2**32-1], 6, 3, 0),
|
|
240 |
HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [], 7, 1, 0),
|
|
241 |
|
|
242 |
HcrRecord(HcrRecord.VALTYPE_BOOL, False, 8, 1, 0),
|
|
243 |
HcrRecord(HcrRecord.VALTYPE_TEXT8, u'Hello world!!!', 8, 2, 0),
|
|
244 |
HcrRecord(HcrRecord.VALTYPE_TEXT8, u'unpadded', 8, 3, 0),
|
|
245 |
]
|
|
246 |
# Shuffle the records to make sure that they are sorted properly when writing
|
|
247 |
random.shuffle(records)
|
|
248 |
repo = HcrRepository(records, version=2, flags=3)
|
|
249 |
|
|
250 |
# Record section size: 21 * 20 = 420
|
|
251 |
# LSD offset: 32 + 420 = 452
|
|
252 |
# LSD size: 8 + 8 + 12 + 12 + 12 + 12 + 16 + 8 = 88
|
|
253 |
h = hex_to_bindata
|
|
254 |
data = [
|
|
255 |
# Header
|
|
256 |
h("48435266 0200 0300 15000000 C4010000"),
|
|
257 |
h("58000000 000000000000000000000000"),
|
|
258 |
|
|
259 |
# Record section
|
|
260 |
h("01000000 01000000 08000000 0000 0000 01000000"), # bool
|
|
261 |
h("01000000 02000000 04000000 0000 0000 85FFFFFF"), # int8
|
|
262 |
h("01000000 03000000 40000000 0000 0000 CC000000"), # uint8
|
|
263 |
h("02000000 01000000 02000000 0000 0000 91CBFFFF"), # int16
|
|
264 |
h("02000000 02000000 20000000 0000 0000 31D40000"), # uint16
|
|
265 |
h("02000000 03000000 01000000 0000 0000 003665C4"), # int32
|
|
266 |
h("03000000 01000000 10000000 0000 0000 00286BEE"), # uint32
|
|
267 |
h("03000000 02000000 00010000 0000 0000 DDCCBBAA"), # linaddr
|
|
268 |
|
|
269 |
h("04000000 01000000 00000001 0000 0800 00000000"), # int64, lsd pos = (0, 8)
|
|
270 |
h("04000000 02000000 00000002 0000 0800 08000000"), # uint64, lsd pos = (8, 8)
|
|
271 |
h("04000000 03000000 00000200 0000 0B00 10000000"), # text8, lsd pos = (8 + 8, 11)
|
|
272 |
h("05000000 01000000 00000200 0000 0000 1C000000"), # text8, lsd pos = (8 + 8 + 12, 0)
|
|
273 |
h("05000000 02000000 00000100 0000 0900 1C000000"), # bindata, lsd pos = (8 + 8 + 12, 9)
|
|
274 |
h("05000000 03000000 00000100 0000 0000 28000000"), # bindata, lsd pos = (8 + 8 + 12 + 12, 0)
|
|
275 |
h("06000000 01000000 00000400 0000 0C00 28000000"), # arrayint32, lsd pos = (8 + 8 + 12 + 12, 12)
|
|
276 |
h("06000000 02000000 00000400 0000 0000 34000000"), # arrayint32, lsd pos = (8 + 8 + 12 + 12 + 12, 0)
|
|
277 |
h("06000000 03000000 00000800 0000 0C00 34000000"), # arrayuint32, lsd pos = (8 + 8 + 12 + 12 + 12, 12)
|
|
278 |
h("07000000 01000000 00000800 0000 0000 40000000"), # arrayuint32, lsd pos = (8 + 8 + 12 + 12 + 12 + 12, 0)
|
|
279 |
|
|
280 |
h("08000000 01000000 08000000 0000 0000 00000000"), # bool
|
|
281 |
h("08000000 02000000 00000200 0000 0E00 40000000"), # text8, lsd pos = (8 + 8 + 12 + 12 + 12 + 12, 14)
|
|
282 |
h("08000000 03000000 00000200 0000 0800 50000000"), # text8, lsd pos = (8 + 8 + 12 + 12 + 12 + 12 + 16, 8)
|
|
283 |
|
|
284 |
# LSD section
|
|
285 |
h("FC73 978B B823 D47F"), # 8 bytes
|
|
286 |
h("14FD 32B4 F410 2295"), # 8 bytes
|
|
287 |
"Cost 100" + h("E2 82 AC 00"), # 12 bytes
|
|
288 |
"",
|
|
289 |
"test test" + h("00 00 00"), # 12 bytes
|
|
290 |
"",
|
|
291 |
h("00000080 00000000 FFFFFF7F"), # 12 bytes
|
|
292 |
"",
|
|
293 |
h("00000000 A0860100 FFFFFFFF"), # 12 bytes
|
|
294 |
"",
|
|
295 |
"Hello world!!!" + h("00 00"), # 16 bytes
|
|
296 |
"unpadded", # 8 bytes
|
|
297 |
]
|
|
298 |
repo_data = ''.join(data)
|
|
299 |
|
|
300 |
self._run_test_read_write_repository(repo, repo_data, 'repo_with_all_record_types')
|