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 os, unittest
|
|
18 |
|
|
19 |
from testautomation.utils import hex_to_bindata
|
|
20 |
|
|
21 |
from hcrplugin.hcr_reader import HcrReader
|
|
22 |
from hcrplugin import hcr_exceptions
|
|
23 |
from hcrplugin.hcrrepository import HcrRecord
|
|
24 |
|
|
25 |
|
|
26 |
class TestHcrReader(unittest.TestCase):
|
|
27 |
def setUp(self):
|
|
28 |
self.reader = HcrReader()
|
|
29 |
|
|
30 |
def test_read_repo_with_invalid_record_section_size(self):
|
|
31 |
# Record section size: 4 * 20 = 80
|
|
32 |
# LSD offset: 32 + 80 = 112
|
|
33 |
# LSD size: 0
|
|
34 |
data = [
|
|
35 |
# Header
|
|
36 |
# Record count should be 4, but is 6 here
|
|
37 |
"48435266 0200 0300 06000000 70000000",
|
|
38 |
"00000000 000000000000000000000000",
|
|
39 |
# Record section
|
|
40 |
"01000000 01000000 08000000 0000 0000 01000000", # bool
|
|
41 |
"02000000 01000000 04000000 0000 0000 85FFFFFF", # int8
|
|
42 |
"03000000 01000000 40000000 0000 0000 CC000000", # uint8
|
|
43 |
"01000000 02000000 02000000 0000 0000 91CBFFFF", # int16
|
|
44 |
]
|
|
45 |
data = ''.join(map(lambda x: hex_to_bindata(x), data))
|
|
46 |
|
|
47 |
try:
|
|
48 |
self.reader.parse_repository_from_bindata(data)
|
|
49 |
self.fail("Expected exception not raised")
|
|
50 |
except hcr_exceptions.InvalidHcrDataSizeError:
|
|
51 |
pass
|
|
52 |
|
|
53 |
def test_read_repo_with_invalid_lsd_section_size(self):
|
|
54 |
# Record section size: 4 * 20 = 80
|
|
55 |
# LSD offset: 32 + 80 = 112
|
|
56 |
# LSD size: 0
|
|
57 |
data = [
|
|
58 |
# Header
|
|
59 |
# LSD section size should be 0, but is 40 here
|
|
60 |
"48435266 0200 0300 04000000 70000000",
|
|
61 |
"28000000 000000000000000000000000",
|
|
62 |
# Record section
|
|
63 |
"01000000 01000000 08000000 0000 0000 01000000", # bool
|
|
64 |
"02000000 01000000 04000000 0000 0000 85FFFFFF", # int8
|
|
65 |
"03000000 01000000 40000000 0000 0000 CC000000", # uint8
|
|
66 |
"01000000 02000000 02000000 0000 0000 91CBFFFF", # int16
|
|
67 |
]
|
|
68 |
data = ''.join(map(lambda x: hex_to_bindata(x), data))
|
|
69 |
|
|
70 |
try:
|
|
71 |
self.reader.parse_repository_from_bindata(data)
|
|
72 |
self.fail("Expected exception not raised")
|
|
73 |
except hcr_exceptions.InvalidHcrDataSizeError:
|
|
74 |
pass
|
|
75 |
|
|
76 |
def test_read_repo_with_invalid_lsd_section_offset(self):
|
|
77 |
# Record section size: 2 * 20 = 40
|
|
78 |
# LSD offset: 32 + 40 = 72
|
|
79 |
# LSD size: 8 + 8 = 16
|
|
80 |
data = [
|
|
81 |
# Header, LSD offset here is 60
|
|
82 |
"48435266 0200 0300 02000000 3C000000",
|
|
83 |
"10000000 000000000000000000000000",
|
|
84 |
# Record section
|
|
85 |
"01000000 01000000 00000001 0000 0800 00000000", # int64, lsd pos = (0, 8)
|
|
86 |
"02000000 01000000 00000002 0000 0800 08000000", # uint64, lsd pos = (8, 8)
|
|
87 |
# LSD section
|
|
88 |
"FC73 978B B823 D47F", # 8 bytes
|
|
89 |
"14FD 32B4 F410 2295", # 8 bytes
|
|
90 |
]
|
|
91 |
data = ''.join(map(lambda x: hex_to_bindata(x), data))
|
|
92 |
|
|
93 |
try:
|
|
94 |
self.reader.parse_repository_from_bindata(data)
|
|
95 |
self.fail("Expected exception not raised")
|
|
96 |
except hcr_exceptions.InvalidLsdSectionOffsetError:
|
|
97 |
pass
|
|
98 |
|
|
99 |
def test_read_repo_with_invalid_lsd_pos_in_record(self):
|
|
100 |
# Record section size: 2 * 20 = 40
|
|
101 |
# LSD offset: 32 + 40 = 72
|
|
102 |
# LSD size: 8 + 8 = 16
|
|
103 |
data = [
|
|
104 |
# Header
|
|
105 |
"48435266 0200 0300 02000000 48000000",
|
|
106 |
"10000000 000000000000000000000000",
|
|
107 |
# Record section
|
|
108 |
"01000000 01000000 00000001 0000 0800 00000000", # int64, lsd pos = (0, 8)
|
|
109 |
"02000000 01000000 00000002 0000 0800 0C000000", # uint64, lsd pos = (12, 8), should be (8, 8)
|
|
110 |
# LSD section
|
|
111 |
"FC73 978B B823 D47F", # 8 bytes
|
|
112 |
"14FD 32B4 F410 2295", # 8 bytes
|
|
113 |
]
|
|
114 |
data = ''.join(map(lambda x: hex_to_bindata(x), data))
|
|
115 |
|
|
116 |
try:
|
|
117 |
self.reader.parse_repository_from_bindata(data)
|
|
118 |
self.fail("Expected exception not raised")
|
|
119 |
except hcr_exceptions.InvalidRecordLsdPositionError:
|
|
120 |
pass
|
|
121 |
|
|
122 |
def test_read_repo_with_invalid_record_value_type(self):
|
|
123 |
# Record section size: 2 * 20 = 40
|
|
124 |
# LSD offset: 32 + 40 = 72
|
|
125 |
# LSD size: 8 + 8 = 16
|
|
126 |
data = [
|
|
127 |
# Header
|
|
128 |
"48435266 0200 0300 02000000 48000000",
|
|
129 |
"10000000 000000000000000000000000",
|
|
130 |
# Record section
|
|
131 |
"01000000 01000000 00000001 0000 0800 00000000", # int64, lsd pos = (0, 8)
|
|
132 |
"02000000 01000000 DEADBEEF 0000 0800 0C000000", # invalid type
|
|
133 |
# LSD section
|
|
134 |
"FC73 978B B823 D47F", # 8 bytes
|
|
135 |
"14FD 32B4 F410 2295", # 8 bytes
|
|
136 |
]
|
|
137 |
data = ''.join(map(lambda x: hex_to_bindata(x), data))
|
|
138 |
|
|
139 |
try:
|
|
140 |
self.reader.parse_repository_from_bindata(data)
|
|
141 |
self.fail("Expected exception not raised")
|
|
142 |
except hcr_exceptions.InvalidRecordValueTypeError:
|
|
143 |
pass
|
|
144 |
|
|
145 |
def _run_test_read_record_with_invalid_lsd_size(self, value_type, lsd_data):
|
|
146 |
try:
|
|
147 |
self.reader.parse_record_value_from_lsd_bindata(value_type, lsd_data)
|
|
148 |
self.fail("Expected exception not raised")
|
|
149 |
except hcr_exceptions.InvalidRecordLsdPositionError:
|
|
150 |
pass
|
|
151 |
|
|
152 |
def test_read_record_with_invalid_lsd_size_int64(self):
|
|
153 |
data = hex_to_bindata("0000 0000 0000 00")
|
|
154 |
self._run_test_read_record_with_invalid_lsd_size(HcrRecord.VALTYPE_INT64, data)
|
|
155 |
|
|
156 |
def test_read_record_with_invalid_lsd_size_uint64(self):
|
|
157 |
data = hex_to_bindata("0000 0000 0000 00")
|
|
158 |
self._run_test_read_record_with_invalid_lsd_size(HcrRecord.VALTYPE_UINT64, data)
|
|
159 |
|
|
160 |
def test_read_record_with_invalid_lsd_size_arrayint32(self):
|
|
161 |
data = hex_to_bindata("0000 0000 0000 00")
|
|
162 |
self._run_test_read_record_with_invalid_lsd_size(HcrRecord.VALTYPE_ARRAY_INT32, data)
|
|
163 |
|
|
164 |
def test_read_record_with_invalid_lsd_size_arrayuint32(self):
|
|
165 |
data = hex_to_bindata("0000 0000 0000 00")
|
|
166 |
self._run_test_read_record_with_invalid_lsd_size(HcrRecord.VALTYPE_ARRAY_UINT32, data)
|
|
167 |
|
|
168 |
|
|
169 |
def test_read_record_with_invalid_data_size(self):
|
|
170 |
try:
|
|
171 |
self.reader.parse_record_from_bindata('1234')
|
|
172 |
self.fail("Parsing invalid record data succeeded!")
|
|
173 |
except hcr_exceptions.HcrReaderError:
|
|
174 |
pass
|
|
175 |
|
|
176 |
def test_read_signed_integer_in_record(self):
|
|
177 |
#Test that padding bytes don't matter when reading the type
|
|
178 |
def check(record, data):
|
|
179 |
self.assertEquals(self.reader.parse_record_from_bindata(data)[0], record)
|
|
180 |
|
|
181 |
r = HcrRecord(HcrRecord.VALTYPE_INT8, -123, 12, 43, 5)
|
|
182 |
d = hex_to_bindata("0C000000 2B000000 04000000 0500 0000 85FFFFFF")
|
|
183 |
check(r, d)
|
|
184 |
d = hex_to_bindata("0C000000 2B000000 04000000 0500 0000 85000000")
|
|
185 |
check(r, d)
|
|
186 |
|
|
187 |
r = HcrRecord(HcrRecord.VALTYPE_INT16, -12345, 12, 43, 5)
|
|
188 |
d = hex_to_bindata("0C000000 2B000000 02000000 0500 0000 C7CFFFFF")
|
|
189 |
check(r, d)
|
|
190 |
d = hex_to_bindata("0C000000 2B000000 02000000 0500 0000 C7CF0000")
|
|
191 |
check(r, d)
|
|
192 |
|
|
193 |
|