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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/map_serializers_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,388 @@
     1.4 +// Copyright (c) 2010, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +// map_serializers_unittest.cc: Unit tests for std::map serializer and
    1.34 +// std::map wrapper serializers.
    1.35 +//
    1.36 +// Author: Siyang Xie (lambxsy@google.com)
    1.37 +
    1.38 +#include <climits>
    1.39 +#include <map>
    1.40 +#include <string>
    1.41 +#include <utility>
    1.42 +#include <iostream>
    1.43 +#include <sstream>
    1.44 +
    1.45 +#include "breakpad_googletest_includes.h"
    1.46 +#include "map_serializers-inl.h"
    1.47 +
    1.48 +#include "processor/address_map-inl.h"
    1.49 +#include "processor/range_map-inl.h"
    1.50 +#include "processor/contained_range_map-inl.h"
    1.51 +
    1.52 +typedef int32_t AddrType;
    1.53 +typedef int32_t EntryType;
    1.54 +
    1.55 +const int kSizeOfInt = 4;
    1.56 +
    1.57 +class TestStdMapSerializer : public ::testing::Test {
    1.58 + protected:
    1.59 +  void SetUp() {
    1.60 +    serialized_size_ = 0;
    1.61 +    serialized_data_ = NULL;
    1.62 +  }
    1.63 +
    1.64 +  void TearDown() {
    1.65 +    delete [] serialized_data_;
    1.66 +  }
    1.67 +
    1.68 +  std::map<AddrType, EntryType> std_map_;
    1.69 +  google_breakpad::StdMapSerializer<AddrType, EntryType> serializer_;
    1.70 +  uint32_t serialized_size_;
    1.71 +  char *serialized_data_;
    1.72 +};
    1.73 +
    1.74 +TEST_F(TestStdMapSerializer, EmptyMapTestCase) {
    1.75 +  const int32_t correct_data[] = { 0 };
    1.76 +  uint32_t correct_size = sizeof(correct_data);
    1.77 +
    1.78 +  // std_map_ is empty.
    1.79 +  serialized_data_ = serializer_.Serialize(std_map_, &serialized_size_);
    1.80 +
    1.81 +  EXPECT_EQ(correct_size, serialized_size_);
    1.82 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
    1.83 +}
    1.84 +
    1.85 +TEST_F(TestStdMapSerializer, MapWithTwoElementsTestCase) {
    1.86 +  const int32_t correct_data[] = {
    1.87 +      // # of nodes
    1.88 +      2,
    1.89 +      // Offsets
    1.90 +      20, 24,
    1.91 +      // Keys
    1.92 +      1, 3,
    1.93 +      // Values
    1.94 +      2, 6
    1.95 +  };
    1.96 +  uint32_t correct_size = sizeof(correct_data);
    1.97 +
    1.98 +  std_map_.insert(std::make_pair(1, 2));
    1.99 +  std_map_.insert(std::make_pair(3, 6));
   1.100 +
   1.101 +  serialized_data_ = serializer_.Serialize(std_map_, &serialized_size_);
   1.102 +
   1.103 +  EXPECT_EQ(correct_size, serialized_size_);
   1.104 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.105 +}
   1.106 +
   1.107 +TEST_F(TestStdMapSerializer, MapWithFiveElementsTestCase) {
   1.108 +  const int32_t correct_data[] = {
   1.109 +      // # of nodes
   1.110 +      5,
   1.111 +      // Offsets
   1.112 +      44, 48, 52, 56, 60,
   1.113 +      // Keys
   1.114 +      1, 2, 3, 4, 5,
   1.115 +      // Values
   1.116 +      11, 12, 13, 14, 15
   1.117 +  };
   1.118 +  uint32_t correct_size = sizeof(correct_data);
   1.119 +
   1.120 +  for (int i = 1; i < 6; ++i)
   1.121 +    std_map_.insert(std::make_pair(i, 10 + i));
   1.122 +
   1.123 +  serialized_data_ = serializer_.Serialize(std_map_, &serialized_size_);
   1.124 +
   1.125 +  EXPECT_EQ(correct_size, serialized_size_);
   1.126 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.127 +}
   1.128 +
   1.129 +class TestAddressMapSerializer : public ::testing::Test {
   1.130 + protected:
   1.131 +  void SetUp() {
   1.132 +    serialized_size_ = 0;
   1.133 +    serialized_data_ = 0;
   1.134 +  }
   1.135 +
   1.136 +  void TearDown() {
   1.137 +    delete [] serialized_data_;
   1.138 +  }
   1.139 +
   1.140 +  google_breakpad::AddressMap<AddrType, EntryType> address_map_;
   1.141 +  google_breakpad::AddressMapSerializer<AddrType, EntryType> serializer_;
   1.142 +  uint32_t serialized_size_;
   1.143 +  char *serialized_data_;
   1.144 +};
   1.145 +
   1.146 +TEST_F(TestAddressMapSerializer, EmptyMapTestCase) {
   1.147 +  const int32_t correct_data[] = { 0 };
   1.148 +  uint32_t correct_size = sizeof(correct_data);
   1.149 +
   1.150 +  // std_map_ is empty.
   1.151 +  serialized_data_ = serializer_.Serialize(address_map_, &serialized_size_);
   1.152 +
   1.153 +  EXPECT_EQ(correct_size, serialized_size_);
   1.154 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.155 +}
   1.156 +
   1.157 +TEST_F(TestAddressMapSerializer, MapWithTwoElementsTestCase) {
   1.158 +  const int32_t correct_data[] = {
   1.159 +      // # of nodes
   1.160 +      2,
   1.161 +      // Offsets
   1.162 +      20, 24,
   1.163 +      // Keys
   1.164 +      1, 3,
   1.165 +      // Values
   1.166 +      2, 6
   1.167 +  };
   1.168 +  uint32_t correct_size = sizeof(correct_data);
   1.169 +
   1.170 +  address_map_.Store(1, 2);
   1.171 +  address_map_.Store(3, 6);
   1.172 +
   1.173 +  serialized_data_ = serializer_.Serialize(address_map_, &serialized_size_);
   1.174 +
   1.175 +  EXPECT_EQ(correct_size, serialized_size_);
   1.176 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.177 +}
   1.178 +
   1.179 +TEST_F(TestAddressMapSerializer, MapWithFourElementsTestCase) {
   1.180 +  const int32_t correct_data[] = {
   1.181 +      // # of nodes
   1.182 +      4,
   1.183 +      // Offsets
   1.184 +      36, 40, 44, 48,
   1.185 +      // Keys
   1.186 +      -6, -4, 8, 123,
   1.187 +      // Values
   1.188 +      2, 3, 5, 8
   1.189 +  };
   1.190 +  uint32_t correct_size = sizeof(correct_data);
   1.191 +
   1.192 +  address_map_.Store(-6, 2);
   1.193 +  address_map_.Store(-4, 3);
   1.194 +  address_map_.Store(8, 5);
   1.195 +  address_map_.Store(123, 8);
   1.196 +
   1.197 +  serialized_data_ = serializer_.Serialize(address_map_, &serialized_size_);
   1.198 +
   1.199 +  EXPECT_EQ(correct_size, serialized_size_);
   1.200 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.201 +}
   1.202 +
   1.203 +
   1.204 +class TestRangeMapSerializer : public ::testing::Test {
   1.205 + protected:
   1.206 +  void SetUp() {
   1.207 +    serialized_size_ = 0;
   1.208 +    serialized_data_ = 0;
   1.209 +  }
   1.210 +
   1.211 +  void TearDown() {
   1.212 +    delete [] serialized_data_;
   1.213 +  }
   1.214 +
   1.215 +  google_breakpad::RangeMap<AddrType, EntryType> range_map_;
   1.216 +  google_breakpad::RangeMapSerializer<AddrType, EntryType> serializer_;
   1.217 +  uint32_t serialized_size_;
   1.218 +  char *serialized_data_;
   1.219 +};
   1.220 +
   1.221 +TEST_F(TestRangeMapSerializer, EmptyMapTestCase) {
   1.222 +  const int32_t correct_data[] = { 0 };
   1.223 +  uint32_t correct_size = sizeof(correct_data);
   1.224 +
   1.225 +  // range_map_ is empty.
   1.226 +  serialized_data_ = serializer_.Serialize(range_map_, &serialized_size_);
   1.227 +
   1.228 +  EXPECT_EQ(correct_size, serialized_size_);
   1.229 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.230 +}
   1.231 +
   1.232 +TEST_F(TestRangeMapSerializer, MapWithOneRangeTestCase) {
   1.233 +  const int32_t correct_data[] = {
   1.234 +      // # of nodes
   1.235 +      1,
   1.236 +      // Offsets
   1.237 +      12,
   1.238 +      // Keys: high address
   1.239 +      10,
   1.240 +      // Values: (low address, entry) pairs
   1.241 +      1, 6
   1.242 +  };
   1.243 +  uint32_t correct_size = sizeof(correct_data);
   1.244 +
   1.245 +  range_map_.StoreRange(1, 10, 6);
   1.246 +
   1.247 +  serialized_data_ = serializer_.Serialize(range_map_, &serialized_size_);
   1.248 +
   1.249 +  EXPECT_EQ(correct_size, serialized_size_);
   1.250 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.251 +}
   1.252 +
   1.253 +TEST_F(TestRangeMapSerializer, MapWithThreeRangesTestCase) {
   1.254 +  const int32_t correct_data[] = {
   1.255 +      // # of nodes
   1.256 +      3,
   1.257 +      // Offsets
   1.258 +      28,    36,    44,
   1.259 +      // Keys: high address
   1.260 +      5,     9,     20,
   1.261 +      // Values: (low address, entry) pairs
   1.262 +      2, 1,  6, 2,  10, 3
   1.263 +  };
   1.264 +  uint32_t correct_size = sizeof(correct_data);
   1.265 +
   1.266 +  ASSERT_TRUE(range_map_.StoreRange(2, 4, 1));
   1.267 +  ASSERT_TRUE(range_map_.StoreRange(6, 4, 2));
   1.268 +  ASSERT_TRUE(range_map_.StoreRange(10, 11, 3));
   1.269 +
   1.270 +  serialized_data_ = serializer_.Serialize(range_map_, &serialized_size_);
   1.271 +
   1.272 +  EXPECT_EQ(correct_size, serialized_size_);
   1.273 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.274 +}
   1.275 +
   1.276 +
   1.277 +class TestContainedRangeMapSerializer : public ::testing::Test {
   1.278 + protected:
   1.279 +  void SetUp() {
   1.280 +    serialized_size_ = 0;
   1.281 +    serialized_data_ = 0;
   1.282 +  }
   1.283 +
   1.284 +  void TearDown() {
   1.285 +    delete [] serialized_data_;
   1.286 +  }
   1.287 +
   1.288 +  google_breakpad::ContainedRangeMap<AddrType, EntryType> crm_map_;
   1.289 +  google_breakpad::ContainedRangeMapSerializer<AddrType, EntryType> serializer_;
   1.290 +  uint32_t serialized_size_;
   1.291 +  char *serialized_data_;
   1.292 +};
   1.293 +
   1.294 +TEST_F(TestContainedRangeMapSerializer, EmptyMapTestCase) {
   1.295 +  const int32_t correct_data[] = {
   1.296 +      0,  // base address of root
   1.297 +      4,  // size of entry
   1.298 +      0,  // entry stored at root
   1.299 +      0   // empty map stored at root
   1.300 +  };
   1.301 +  uint32_t correct_size = sizeof(correct_data);
   1.302 +
   1.303 +  // crm_map_ is empty.
   1.304 +  serialized_data_ = serializer_.Serialize(&crm_map_, &serialized_size_);
   1.305 +
   1.306 +  EXPECT_EQ(correct_size, serialized_size_);
   1.307 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.308 +}
   1.309 +
   1.310 +TEST_F(TestContainedRangeMapSerializer, MapWithOneRangeTestCase) {
   1.311 +  const int32_t correct_data[] = {
   1.312 +      0,  // base address of root
   1.313 +      4,  // size of entry
   1.314 +      0,  // entry stored at root
   1.315 +      // Map stored at root node:
   1.316 +      1,  // # of nodes
   1.317 +      12, // offset
   1.318 +      9,  // key
   1.319 +      // value: a child ContainedRangeMap
   1.320 +      3,  // base address of child CRM
   1.321 +      4,  // size of entry
   1.322 +      -1, // entry stored in child CRM
   1.323 +      0   // empty sub-map stored in child CRM
   1.324 +  };
   1.325 +  uint32_t correct_size = sizeof(correct_data);
   1.326 +
   1.327 +  crm_map_.StoreRange(3, 7, -1);
   1.328 +
   1.329 +  serialized_data_ = serializer_.Serialize(&crm_map_, &serialized_size_);
   1.330 +
   1.331 +  EXPECT_EQ(correct_size, serialized_size_);
   1.332 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.333 +}
   1.334 +
   1.335 +TEST_F(TestContainedRangeMapSerializer, MapWithTwoLevelsTestCase) {
   1.336 +  // Tree structure of ranges:
   1.337 +  //           root              level 0
   1.338 +  //            |
   1.339 +  //           map
   1.340 +  //         /     \             level 1: child1, child2
   1.341 +  //      2~8      10~20
   1.342 +  //       |         |
   1.343 +  //      map       map
   1.344 +  //     /   \       |
   1.345 +  //    3~4 6~7    16-20         level 2: grandchild1, grandchild2, grandchild3
   1.346 +
   1.347 +  const int32_t correct_data[] = {
   1.348 +      // root: base, entry_size, entry
   1.349 +      0, 4, 0,
   1.350 +      // root's map: # of nodes, offset1, offset2, key1, key2
   1.351 +      2, 20, 84, 8, 20,
   1.352 +      // child1: base, entry_size, entry:
   1.353 +      2, 4, -1,
   1.354 +      // child1's map: # of nodes, offset1, offset2, key1, key2
   1.355 +      2, 20, 36, 4, 7,
   1.356 +        // grandchild1: base, entry_size, entry, empty_map
   1.357 +        3, 4, -1, 0,
   1.358 +        // grandchild2: base, entry_size, entry, empty_map
   1.359 +        6, 4, -1, 0,
   1.360 +      // child2: base, entry_size, entry:
   1.361 +      10, 4, -1,
   1.362 +      // child2's map: # of nodes, offset1, key1
   1.363 +      1, 12, 20,
   1.364 +        // grandchild3: base, entry_size, entry, empty_map
   1.365 +        16, 4, -1, 0
   1.366 +  };
   1.367 +  uint32_t correct_size = sizeof(correct_data);
   1.368 +
   1.369 +  // Store child1.
   1.370 +  ASSERT_TRUE(crm_map_.StoreRange(2, 7, -1));
   1.371 +  // Store child2.
   1.372 +  ASSERT_TRUE(crm_map_.StoreRange(10, 11, -1));
   1.373 +  // Store grandchild1.
   1.374 +  ASSERT_TRUE(crm_map_.StoreRange(3, 2, -1));
   1.375 +  // Store grandchild2.
   1.376 +  ASSERT_TRUE(crm_map_.StoreRange(6, 2, -1));
   1.377 +  // Store grandchild3.
   1.378 +  ASSERT_TRUE(crm_map_.StoreRange(16, 5, -1));
   1.379 +
   1.380 +  serialized_data_ = serializer_.Serialize(&crm_map_, &serialized_size_);
   1.381 +
   1.382 +  EXPECT_EQ(correct_size, serialized_size_);
   1.383 +  EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0);
   1.384 +}
   1.385 +
   1.386 +
   1.387 +int main(int argc, char *argv[]) {
   1.388 +  ::testing::InitGoogleTest(&argc, argv);
   1.389 +
   1.390 +  return RUN_ALL_TESTS();
   1.391 +}

mercurial