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