toolkit/crashreporter/google-breakpad/src/processor/map_serializers_unittest.cc

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:666c29d46d76
1 // Copyright (c) 2010, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 // map_serializers_unittest.cc: Unit tests for std::map serializer and
31 // std::map wrapper serializers.
32 //
33 // Author: Siyang Xie (lambxsy@google.com)
34
35 #include <climits>
36 #include <map>
37 #include <string>
38 #include <utility>
39 #include <iostream>
40 #include <sstream>
41
42 #include "breakpad_googletest_includes.h"
43 #include "map_serializers-inl.h"
44
45 #include "processor/address_map-inl.h"
46 #include "processor/range_map-inl.h"
47 #include "processor/contained_range_map-inl.h"
48
49 typedef int32_t AddrType;
50 typedef int32_t EntryType;
51
52 const int kSizeOfInt = 4;
53
54 class TestStdMapSerializer : public ::testing::Test {
55 protected:
56 void SetUp() {
57 serialized_size_ = 0;
58 serialized_data_ = NULL;
59 }
60
61 void TearDown() {
62 delete [] serialized_data_;
63 }
64
65 std::map<AddrType, EntryType> std_map_;
66 google_breakpad::StdMapSerializer<AddrType, EntryType> serializer_;
67 uint32_t serialized_size_;
68 char *serialized_data_;
69 };
70
71 TEST_F(TestStdMapSerializer, EmptyMapTestCase) {
72 const int32_t correct_data[] = { 0 };
73 uint32_t correct_size = sizeof(correct_data);
74
75 // std_map_ is empty.
76 serialized_data_ = serializer_.Serialize(std_map_, &serialized_size_);
77
78 EXPECT_EQ(correct_size, serialized_size_);
79 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
80 }
81
82 TEST_F(TestStdMapSerializer, MapWithTwoElementsTestCase) {
83 const int32_t correct_data[] = {
84 // # of nodes
85 2,
86 // Offsets
87 20, 24,
88 // Keys
89 1, 3,
90 // Values
91 2, 6
92 };
93 uint32_t correct_size = sizeof(correct_data);
94
95 std_map_.insert(std::make_pair(1, 2));
96 std_map_.insert(std::make_pair(3, 6));
97
98 serialized_data_ = serializer_.Serialize(std_map_, &serialized_size_);
99
100 EXPECT_EQ(correct_size, serialized_size_);
101 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
102 }
103
104 TEST_F(TestStdMapSerializer, MapWithFiveElementsTestCase) {
105 const int32_t correct_data[] = {
106 // # of nodes
107 5,
108 // Offsets
109 44, 48, 52, 56, 60,
110 // Keys
111 1, 2, 3, 4, 5,
112 // Values
113 11, 12, 13, 14, 15
114 };
115 uint32_t correct_size = sizeof(correct_data);
116
117 for (int i = 1; i < 6; ++i)
118 std_map_.insert(std::make_pair(i, 10 + i));
119
120 serialized_data_ = serializer_.Serialize(std_map_, &serialized_size_);
121
122 EXPECT_EQ(correct_size, serialized_size_);
123 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
124 }
125
126 class TestAddressMapSerializer : public ::testing::Test {
127 protected:
128 void SetUp() {
129 serialized_size_ = 0;
130 serialized_data_ = 0;
131 }
132
133 void TearDown() {
134 delete [] serialized_data_;
135 }
136
137 google_breakpad::AddressMap<AddrType, EntryType> address_map_;
138 google_breakpad::AddressMapSerializer<AddrType, EntryType> serializer_;
139 uint32_t serialized_size_;
140 char *serialized_data_;
141 };
142
143 TEST_F(TestAddressMapSerializer, EmptyMapTestCase) {
144 const int32_t correct_data[] = { 0 };
145 uint32_t correct_size = sizeof(correct_data);
146
147 // std_map_ is empty.
148 serialized_data_ = serializer_.Serialize(address_map_, &serialized_size_);
149
150 EXPECT_EQ(correct_size, serialized_size_);
151 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
152 }
153
154 TEST_F(TestAddressMapSerializer, MapWithTwoElementsTestCase) {
155 const int32_t correct_data[] = {
156 // # of nodes
157 2,
158 // Offsets
159 20, 24,
160 // Keys
161 1, 3,
162 // Values
163 2, 6
164 };
165 uint32_t correct_size = sizeof(correct_data);
166
167 address_map_.Store(1, 2);
168 address_map_.Store(3, 6);
169
170 serialized_data_ = serializer_.Serialize(address_map_, &serialized_size_);
171
172 EXPECT_EQ(correct_size, serialized_size_);
173 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
174 }
175
176 TEST_F(TestAddressMapSerializer, MapWithFourElementsTestCase) {
177 const int32_t correct_data[] = {
178 // # of nodes
179 4,
180 // Offsets
181 36, 40, 44, 48,
182 // Keys
183 -6, -4, 8, 123,
184 // Values
185 2, 3, 5, 8
186 };
187 uint32_t correct_size = sizeof(correct_data);
188
189 address_map_.Store(-6, 2);
190 address_map_.Store(-4, 3);
191 address_map_.Store(8, 5);
192 address_map_.Store(123, 8);
193
194 serialized_data_ = serializer_.Serialize(address_map_, &serialized_size_);
195
196 EXPECT_EQ(correct_size, serialized_size_);
197 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
198 }
199
200
201 class TestRangeMapSerializer : public ::testing::Test {
202 protected:
203 void SetUp() {
204 serialized_size_ = 0;
205 serialized_data_ = 0;
206 }
207
208 void TearDown() {
209 delete [] serialized_data_;
210 }
211
212 google_breakpad::RangeMap<AddrType, EntryType> range_map_;
213 google_breakpad::RangeMapSerializer<AddrType, EntryType> serializer_;
214 uint32_t serialized_size_;
215 char *serialized_data_;
216 };
217
218 TEST_F(TestRangeMapSerializer, EmptyMapTestCase) {
219 const int32_t correct_data[] = { 0 };
220 uint32_t correct_size = sizeof(correct_data);
221
222 // range_map_ is empty.
223 serialized_data_ = serializer_.Serialize(range_map_, &serialized_size_);
224
225 EXPECT_EQ(correct_size, serialized_size_);
226 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
227 }
228
229 TEST_F(TestRangeMapSerializer, MapWithOneRangeTestCase) {
230 const int32_t correct_data[] = {
231 // # of nodes
232 1,
233 // Offsets
234 12,
235 // Keys: high address
236 10,
237 // Values: (low address, entry) pairs
238 1, 6
239 };
240 uint32_t correct_size = sizeof(correct_data);
241
242 range_map_.StoreRange(1, 10, 6);
243
244 serialized_data_ = serializer_.Serialize(range_map_, &serialized_size_);
245
246 EXPECT_EQ(correct_size, serialized_size_);
247 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
248 }
249
250 TEST_F(TestRangeMapSerializer, MapWithThreeRangesTestCase) {
251 const int32_t correct_data[] = {
252 // # of nodes
253 3,
254 // Offsets
255 28, 36, 44,
256 // Keys: high address
257 5, 9, 20,
258 // Values: (low address, entry) pairs
259 2, 1, 6, 2, 10, 3
260 };
261 uint32_t correct_size = sizeof(correct_data);
262
263 ASSERT_TRUE(range_map_.StoreRange(2, 4, 1));
264 ASSERT_TRUE(range_map_.StoreRange(6, 4, 2));
265 ASSERT_TRUE(range_map_.StoreRange(10, 11, 3));
266
267 serialized_data_ = serializer_.Serialize(range_map_, &serialized_size_);
268
269 EXPECT_EQ(correct_size, serialized_size_);
270 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
271 }
272
273
274 class TestContainedRangeMapSerializer : public ::testing::Test {
275 protected:
276 void SetUp() {
277 serialized_size_ = 0;
278 serialized_data_ = 0;
279 }
280
281 void TearDown() {
282 delete [] serialized_data_;
283 }
284
285 google_breakpad::ContainedRangeMap<AddrType, EntryType> crm_map_;
286 google_breakpad::ContainedRangeMapSerializer<AddrType, EntryType> serializer_;
287 uint32_t serialized_size_;
288 char *serialized_data_;
289 };
290
291 TEST_F(TestContainedRangeMapSerializer, EmptyMapTestCase) {
292 const int32_t correct_data[] = {
293 0, // base address of root
294 4, // size of entry
295 0, // entry stored at root
296 0 // empty map stored at root
297 };
298 uint32_t correct_size = sizeof(correct_data);
299
300 // crm_map_ is empty.
301 serialized_data_ = serializer_.Serialize(&crm_map_, &serialized_size_);
302
303 EXPECT_EQ(correct_size, serialized_size_);
304 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
305 }
306
307 TEST_F(TestContainedRangeMapSerializer, MapWithOneRangeTestCase) {
308 const int32_t correct_data[] = {
309 0, // base address of root
310 4, // size of entry
311 0, // entry stored at root
312 // Map stored at root node:
313 1, // # of nodes
314 12, // offset
315 9, // key
316 // value: a child ContainedRangeMap
317 3, // base address of child CRM
318 4, // size of entry
319 -1, // entry stored in child CRM
320 0 // empty sub-map stored in child CRM
321 };
322 uint32_t correct_size = sizeof(correct_data);
323
324 crm_map_.StoreRange(3, 7, -1);
325
326 serialized_data_ = serializer_.Serialize(&crm_map_, &serialized_size_);
327
328 EXPECT_EQ(correct_size, serialized_size_);
329 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
330 }
331
332 TEST_F(TestContainedRangeMapSerializer, MapWithTwoLevelsTestCase) {
333 // Tree structure of ranges:
334 // root level 0
335 // |
336 // map
337 // / \ level 1: child1, child2
338 // 2~8 10~20
339 // | |
340 // map map
341 // / \ |
342 // 3~4 6~7 16-20 level 2: grandchild1, grandchild2, grandchild3
343
344 const int32_t correct_data[] = {
345 // root: base, entry_size, entry
346 0, 4, 0,
347 // root's map: # of nodes, offset1, offset2, key1, key2
348 2, 20, 84, 8, 20,
349 // child1: base, entry_size, entry:
350 2, 4, -1,
351 // child1's map: # of nodes, offset1, offset2, key1, key2
352 2, 20, 36, 4, 7,
353 // grandchild1: base, entry_size, entry, empty_map
354 3, 4, -1, 0,
355 // grandchild2: base, entry_size, entry, empty_map
356 6, 4, -1, 0,
357 // child2: base, entry_size, entry:
358 10, 4, -1,
359 // child2's map: # of nodes, offset1, key1
360 1, 12, 20,
361 // grandchild3: base, entry_size, entry, empty_map
362 16, 4, -1, 0
363 };
364 uint32_t correct_size = sizeof(correct_data);
365
366 // Store child1.
367 ASSERT_TRUE(crm_map_.StoreRange(2, 7, -1));
368 // Store child2.
369 ASSERT_TRUE(crm_map_.StoreRange(10, 11, -1));
370 // Store grandchild1.
371 ASSERT_TRUE(crm_map_.StoreRange(3, 2, -1));
372 // Store grandchild2.
373 ASSERT_TRUE(crm_map_.StoreRange(6, 2, -1));
374 // Store grandchild3.
375 ASSERT_TRUE(crm_map_.StoreRange(16, 5, -1));
376
377 serialized_data_ = serializer_.Serialize(&crm_map_, &serialized_size_);
378
379 EXPECT_EQ(correct_size, serialized_size_);
380 EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
381 }
382
383
384 int main(int argc, char *argv[]) {
385 ::testing::InitGoogleTest(&argc, argv);
386
387 return RUN_ALL_TESTS();
388 }

mercurial