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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial