OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "minidump/minidump_thread_id_map.h" |
| 16 |
| 17 #include <vector> |
| 18 |
| 19 #include "base/basictypes.h" |
| 20 #include "gtest/gtest.h" |
| 21 #include "snapshot/test/test_thread_snapshot.h" |
| 22 |
| 23 namespace crashpad { |
| 24 namespace test { |
| 25 namespace { |
| 26 |
| 27 class MinidumpThreadIDMapTest : public testing::Test { |
| 28 public: |
| 29 MinidumpThreadIDMapTest() |
| 30 : Test(), |
| 31 thread_snapshots_(), |
| 32 test_thread_snapshots_() { |
| 33 } |
| 34 |
| 35 ~MinidumpThreadIDMapTest() override {} |
| 36 |
| 37 // testing::Test: |
| 38 void SetUp() override { |
| 39 for (size_t index = 0; index < arraysize(test_thread_snapshots_); ++index) { |
| 40 thread_snapshots_.push_back(&test_thread_snapshots_[index]); |
| 41 } |
| 42 } |
| 43 |
| 44 protected: |
| 45 static bool MapHasKeyValue( |
| 46 const MinidumpThreadIDMap* map, uint64_t key, uint32_t expected_value) { |
| 47 auto iterator = map->find(key); |
| 48 if (iterator == map->end()) { |
| 49 EXPECT_NE(map->end(), iterator); |
| 50 return false; |
| 51 } |
| 52 if (iterator->second != expected_value) { |
| 53 EXPECT_EQ(expected_value, iterator->second); |
| 54 return false; |
| 55 } |
| 56 return true; |
| 57 } |
| 58 |
| 59 void SetThreadID(size_t index, uint64_t thread_id) { |
| 60 ASSERT_LT(index, arraysize(test_thread_snapshots_)); |
| 61 test_thread_snapshots_[index].SetThreadID(thread_id); |
| 62 } |
| 63 |
| 64 const std::vector<const ThreadSnapshot*>& thread_snapshots() const { |
| 65 return thread_snapshots_; |
| 66 } |
| 67 |
| 68 private: |
| 69 std::vector<const ThreadSnapshot*> thread_snapshots_; |
| 70 TestThreadSnapshot test_thread_snapshots_[5]; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(MinidumpThreadIDMapTest); |
| 73 }; |
| 74 |
| 75 TEST_F(MinidumpThreadIDMapTest, NoThreads) { |
| 76 // Don’t use thread_snapshots(), because it’s got some threads in it, and the |
| 77 // point of this test is to make sure that BuildMinidumpThreadIDMap() works |
| 78 // with no threads. |
| 79 std::vector<const ThreadSnapshot*> thread_snapshots; |
| 80 MinidumpThreadIDMap thread_id_map; |
| 81 BuildMinidumpThreadIDMap(thread_snapshots, &thread_id_map); |
| 82 |
| 83 EXPECT_TRUE(thread_id_map.empty()); |
| 84 } |
| 85 |
| 86 TEST_F(MinidumpThreadIDMapTest, SimpleMapping) { |
| 87 SetThreadID(0, 1); |
| 88 SetThreadID(1, 3); |
| 89 SetThreadID(2, 5); |
| 90 SetThreadID(3, 7); |
| 91 SetThreadID(4, 9); |
| 92 |
| 93 MinidumpThreadIDMap thread_id_map; |
| 94 BuildMinidumpThreadIDMap(thread_snapshots(), &thread_id_map); |
| 95 |
| 96 EXPECT_EQ(5u, thread_id_map.size()); |
| 97 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 1, 1); |
| 98 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 3, 3); |
| 99 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 5, 5); |
| 100 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 7, 7); |
| 101 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 9, 9); |
| 102 } |
| 103 |
| 104 TEST_F(MinidumpThreadIDMapTest, Truncation) { |
| 105 SetThreadID(0, 0x0000000000000000); |
| 106 SetThreadID(1, 0x9999999900000001); |
| 107 SetThreadID(2, 0x9999999980000001); |
| 108 SetThreadID(3, 0x99999999fffffffe); |
| 109 SetThreadID(4, 0x99999999ffffffff); |
| 110 |
| 111 MinidumpThreadIDMap thread_id_map; |
| 112 BuildMinidumpThreadIDMap(thread_snapshots(), &thread_id_map); |
| 113 |
| 114 EXPECT_EQ(5u, thread_id_map.size()); |
| 115 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000000000000, 0x00000000); |
| 116 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x9999999900000001, 0x00000001); |
| 117 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x9999999980000001, 0x80000001); |
| 118 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x99999999fffffffe, 0xfffffffe); |
| 119 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x99999999ffffffff, 0xffffffff); |
| 120 } |
| 121 |
| 122 TEST_F(MinidumpThreadIDMapTest, DuplicateThreadID) { |
| 123 SetThreadID(0, 2); |
| 124 SetThreadID(1, 4); |
| 125 SetThreadID(2, 4); |
| 126 SetThreadID(3, 6); |
| 127 SetThreadID(4, 8); |
| 128 |
| 129 MinidumpThreadIDMap thread_id_map; |
| 130 BuildMinidumpThreadIDMap(thread_snapshots(), &thread_id_map); |
| 131 |
| 132 EXPECT_EQ(4u, thread_id_map.size()); |
| 133 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 2, 2); |
| 134 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 4, 4); |
| 135 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 6, 6); |
| 136 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 8, 8); |
| 137 } |
| 138 |
| 139 TEST_F(MinidumpThreadIDMapTest, Collision) { |
| 140 SetThreadID(0, 0x0000000000000010); |
| 141 SetThreadID(1, 0x0000000000000020); |
| 142 SetThreadID(2, 0x0000000000000030); |
| 143 SetThreadID(3, 0x0000000000000040); |
| 144 SetThreadID(4, 0x0000000100000010); |
| 145 |
| 146 MinidumpThreadIDMap thread_id_map; |
| 147 BuildMinidumpThreadIDMap(thread_snapshots(), &thread_id_map); |
| 148 |
| 149 EXPECT_EQ(5u, thread_id_map.size()); |
| 150 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000000000010, 0); |
| 151 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000000000020, 1); |
| 152 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000000000030, 2); |
| 153 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000000000040, 3); |
| 154 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000100000010, 4); |
| 155 } |
| 156 |
| 157 TEST_F(MinidumpThreadIDMapTest, DuplicateAndCollision) { |
| 158 SetThreadID(0, 0x0000000100000010); |
| 159 SetThreadID(1, 0x0000000000000010); |
| 160 SetThreadID(2, 0x0000000000000020); |
| 161 SetThreadID(3, 0x0000000000000030); |
| 162 SetThreadID(4, 0x0000000000000020); |
| 163 |
| 164 MinidumpThreadIDMap thread_id_map; |
| 165 BuildMinidumpThreadIDMap(thread_snapshots(), &thread_id_map); |
| 166 |
| 167 EXPECT_EQ(4u, thread_id_map.size()); |
| 168 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000100000010, 0); |
| 169 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000000000010, 1); |
| 170 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000000000020, 2); |
| 171 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 0x0000000000000030, 3); |
| 172 } |
| 173 |
| 174 TEST_F(MinidumpThreadIDMapTest, AllDuplicates) { |
| 175 SetThreadID(0, 6); |
| 176 SetThreadID(1, 6); |
| 177 SetThreadID(2, 6); |
| 178 SetThreadID(3, 6); |
| 179 SetThreadID(4, 6); |
| 180 |
| 181 MinidumpThreadIDMap thread_id_map; |
| 182 BuildMinidumpThreadIDMap(thread_snapshots(), &thread_id_map); |
| 183 |
| 184 EXPECT_EQ(1u, thread_id_map.size()); |
| 185 EXPECT_PRED3(MapHasKeyValue, &thread_id_map, 6, 6); |
| 186 } |
| 187 |
| 188 } // namespace |
| 189 } // namespace test |
| 190 } // namespace crashpad |
OLD | NEW |