Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(552)

Side by Side Diff: util/numeric/checked_address_range_test.cc

Issue 1052813002: win: make CrashpadInfo retrievable (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: . Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "util/mac/checked_mach_address_range.h" 15 #include "util/numeric/checked_address_range.h"
16
17 #include <mach/mach.h>
18 16
19 #include <limits> 17 #include <limits>
20 18
21 #include "base/basictypes.h" 19 #include "base/basictypes.h"
20 #include "base/format_macros.h"
22 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
23 #include "build/build_config.h" 22 #include "build/build_config.h"
24 #include "gtest/gtest.h" 23 #include "gtest/gtest.h"
25 24
26 namespace crashpad { 25 namespace crashpad {
27 namespace test { 26 namespace test {
28 namespace { 27 namespace {
29 28
29 using CheckedAddressRange = CheckedAddressRangeGeneric<uintptr_t, size_t>;
30
30 enum Validity { 31 enum Validity {
31 kInvalid = false, 32 kInvalid = false,
32 kValid, 33 kValid,
33 kValid64Invalid32, 34 kValid64Invalid32,
34 }; 35 };
35 36
36 bool ExpectationForValidity32(Validity validity) { 37 bool ExpectationForValidity32(Validity validity) {
37 return validity == kValid; 38 return validity == kValid;
38 } 39 }
39 40
40 bool ExpectationForValidity64(Validity validity) { 41 bool ExpectationForValidity64(Validity validity) {
41 return validity == kValid || validity == kValid64Invalid32; 42 return validity == kValid || validity == kValid64Invalid32;
42 } 43 }
43 44
44 TEST(CheckedMachAddressRange, IsValid) { 45 TEST(CheckedAddressRange, IsValid) {
45 const struct TestData { 46 const struct TestData {
46 mach_vm_address_t base; 47 uintptr_t base;
Mark Mentovai 2015/05/01 19:05:31 I think these should both be uint64_t or this test
scottmg 2015/05/01 19:46:32 Sorry, that was dumb. Done.
47 mach_vm_size_t size; 48 size_t size;
48 Validity validity; 49 Validity validity;
49 } kTestData[] = { 50 } kTestData[] = {
50 {0, 0, kValid}, 51 {0, 0, kValid},
51 {0, 1, kValid}, 52 {0, 1, kValid},
52 {0, 2, kValid}, 53 {0, 2, kValid},
53 {0, 0x7fffffff, kValid}, 54 {0, 0x7fffffff, kValid},
54 {0, 0x80000000, kValid}, 55 {0, 0x80000000, kValid},
55 {0, 0xfffffffe, kValid}, 56 {0, 0xfffffffe, kValid},
56 {0, 0xffffffff, kValid}, 57 {0, 0xffffffff, kValid},
57 {0, 0xffffffffffffffff, kValid64Invalid32}, 58 {0, 0xffffffffffffffff, kValid64Invalid32},
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 {0x8000000000000000, 0xffffffffffffffff, kInvalid}, 111 {0x8000000000000000, 0xffffffffffffffff, kInvalid},
111 {0xfffffffffffffffe, 0, kValid64Invalid32}, 112 {0xfffffffffffffffe, 0, kValid64Invalid32},
112 {0xfffffffffffffffe, 1, kValid64Invalid32}, 113 {0xfffffffffffffffe, 1, kValid64Invalid32},
113 {0xfffffffffffffffe, 2, kInvalid}, 114 {0xfffffffffffffffe, 2, kInvalid},
114 {0xffffffffffffffff, 0, kValid64Invalid32}, 115 {0xffffffffffffffff, 0, kValid64Invalid32},
115 {0xffffffffffffffff, 1, kInvalid}, 116 {0xffffffffffffffff, 1, kInvalid},
116 }; 117 };
117 118
118 for (size_t index = 0; index < arraysize(kTestData); ++index) { 119 for (size_t index = 0; index < arraysize(kTestData); ++index) {
119 const TestData& testcase = kTestData[index]; 120 const TestData& testcase = kTestData[index];
120 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx", 121 SCOPED_TRACE(base::StringPrintf("index %" PRIuS ", base 0x%lx, size 0x%lx",
121 index, 122 index,
122 testcase.base, 123 testcase.base,
123 testcase.size)); 124 testcase.size));
124 125
125 CheckedMachAddressRange range_32(false, testcase.base, testcase.size); 126 CheckedAddressRange range_32(false, testcase.base, testcase.size);
126 EXPECT_EQ(ExpectationForValidity32(testcase.validity), range_32.IsValid()); 127 EXPECT_EQ(ExpectationForValidity32(testcase.validity), range_32.IsValid());
127 128
128 CheckedMachAddressRange range_64(true, testcase.base, testcase.size); 129 CheckedAddressRange range_64(true, testcase.base, testcase.size);
129 EXPECT_EQ(ExpectationForValidity64(testcase.validity), range_64.IsValid()); 130 EXPECT_EQ(ExpectationForValidity64(testcase.validity), range_64.IsValid());
130 } 131 }
131 } 132 }
132 133
133 TEST(CheckedMachAddressRange, ContainsValue) { 134 TEST(CheckedAddressRange, ContainsValue) {
134 const struct TestData { 135 const struct TestData {
135 mach_vm_address_t value; 136 uintptr_t value;
136 bool expectation; 137 bool expectation;
137 } kTestData[] = { 138 } kTestData[] = {
138 {0, false}, 139 {0, false},
139 {1, false}, 140 {1, false},
140 {0x1fff, false}, 141 {0x1fff, false},
141 {0x2000, true}, 142 {0x2000, true},
142 {0x2001, true}, 143 {0x2001, true},
143 {0x2ffe, true}, 144 {0x2ffe, true},
144 {0x2fff, true}, 145 {0x2fff, true},
145 {0x3000, false}, 146 {0x3000, false},
146 {0x3001, false}, 147 {0x3001, false},
147 {0x7fffffff, false}, 148 {0x7fffffff, false},
148 {0x80000000, false}, 149 {0x80000000, false},
149 {0x80000001, false}, 150 {0x80000001, false},
150 {0x80001fff, false}, 151 {0x80001fff, false},
151 {0x80002000, false}, 152 {0x80002000, false},
152 {0x80002001, false}, 153 {0x80002001, false},
153 {0x80002ffe, false}, 154 {0x80002ffe, false},
154 {0x80002fff, false}, 155 {0x80002fff, false},
155 {0x80003000, false}, 156 {0x80003000, false},
156 {0x80003001, false}, 157 {0x80003001, false},
157 {0xffffcfff, false}, 158 {0xffffcfff, false},
158 {0xffffdfff, false}, 159 {0xffffdfff, false},
159 {0xffffefff, false}, 160 {0xffffefff, false},
160 {0xffffffff, false}, 161 {0xffffffff, false},
161 {0x100000000, false}, 162 {0x100000000, false},
162 {0xffffffffffffffff, false}, 163 {0xffffffffffffffff, false},
163 }; 164 };
164 165
165 CheckedMachAddressRange parent_range_32(false, 0x2000, 0x1000); 166 CheckedAddressRange parent_range_32(false, 0x2000, 0x1000);
166 ASSERT_TRUE(parent_range_32.IsValid()); 167 ASSERT_TRUE(parent_range_32.IsValid());
167 168
168 for (size_t index = 0; index < arraysize(kTestData); ++index) { 169 for (size_t index = 0; index < arraysize(kTestData); ++index) {
169 const TestData& testcase = kTestData[index]; 170 const TestData& testcase = kTestData[index];
170 SCOPED_TRACE( 171 SCOPED_TRACE(base::StringPrintf(
171 base::StringPrintf("index %zu, value 0x%llx", index, testcase.value)); 172 "index %" PRIuS ", value 0x%lx", index, testcase.value));
172 173
173 EXPECT_EQ(testcase.expectation, 174 EXPECT_EQ(testcase.expectation,
174 parent_range_32.ContainsValue(testcase.value)); 175 parent_range_32.ContainsValue(testcase.value));
175 } 176 }
176 177
177 CheckedMachAddressRange parent_range_64(true, 0x100000000, 0x1000); 178 CheckedAddressRange parent_range_64(true, 0x100000000, 0x1000);
178 ASSERT_TRUE(parent_range_64.IsValid()); 179 ASSERT_TRUE(parent_range_64.IsValid());
179 EXPECT_FALSE(parent_range_64.ContainsValue(0xffffffff)); 180 EXPECT_FALSE(parent_range_64.ContainsValue(0xffffffff));
180 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000000)); 181 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000000));
181 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000001)); 182 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000001));
182 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000fff)); 183 EXPECT_TRUE(parent_range_64.ContainsValue(0x100000fff));
183 EXPECT_FALSE(parent_range_64.ContainsValue(0x100001000)); 184 EXPECT_FALSE(parent_range_64.ContainsValue(0x100001000));
184 } 185 }
185 186
186 TEST(CheckedMachAddressRange, ContainsRange) { 187 TEST(CheckedAddressRange, ContainsRange) {
187 const struct TestData { 188 const struct TestData {
188 mach_vm_address_t base; 189 uintptr_t base;
189 mach_vm_size_t size; 190 size_t size;
190 bool expectation; 191 bool expectation;
191 } kTestData[] = { 192 } kTestData[] = {
192 {0, 0, false}, 193 {0, 0, false},
193 {0, 1, false}, 194 {0, 1, false},
194 {0x2000, 0x1000, true}, 195 {0x2000, 0x1000, true},
195 {0, 0x2000, false}, 196 {0, 0x2000, false},
196 {0x3000, 0x1000, false}, 197 {0x3000, 0x1000, false},
197 {0x1800, 0x1000, false}, 198 {0x1800, 0x1000, false},
198 {0x2800, 0x1000, false}, 199 {0x2800, 0x1000, false},
199 {0x2000, 0x800, true}, 200 {0x2000, 0x800, true},
(...skipping 12 matching lines...) Expand all
212 {0x3000, 0, true}, 213 {0x3000, 0, true},
213 {0x3001, 0, false}, 214 {0x3001, 0, false},
214 {0x1fff, 1, false}, 215 {0x1fff, 1, false},
215 {0x2000, 1, true}, 216 {0x2000, 1, true},
216 {0x2001, 1, true}, 217 {0x2001, 1, true},
217 {0x2fff, 1, true}, 218 {0x2fff, 1, true},
218 {0x3000, 1, false}, 219 {0x3000, 1, false},
219 {0x3001, 1, false}, 220 {0x3001, 1, false},
220 }; 221 };
221 222
222 CheckedMachAddressRange parent_range_32(false, 0x2000, 0x1000); 223 CheckedAddressRange parent_range_32(false, 0x2000, 0x1000);
223 ASSERT_TRUE(parent_range_32.IsValid()); 224 ASSERT_TRUE(parent_range_32.IsValid());
224 225
225 for (size_t index = 0; index < arraysize(kTestData); ++index) { 226 for (size_t index = 0; index < arraysize(kTestData); ++index) {
226 const TestData& testcase = kTestData[index]; 227 const TestData& testcase = kTestData[index];
227 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%llx, size 0x%llx", 228 SCOPED_TRACE(base::StringPrintf("index %" PRIuS ", base 0x%lx, size 0x%lx",
228 index, 229 index,
229 testcase.base, 230 testcase.base,
230 testcase.size)); 231 testcase.size));
231 232
232 CheckedMachAddressRange child_range_32(false, testcase.base, testcase.size); 233 CheckedAddressRange child_range_32(false, testcase.base, testcase.size);
233 ASSERT_TRUE(child_range_32.IsValid()); 234 ASSERT_TRUE(child_range_32.IsValid());
234 EXPECT_EQ(testcase.expectation, 235 EXPECT_EQ(testcase.expectation,
235 parent_range_32.ContainsRange(child_range_32)); 236 parent_range_32.ContainsRange(child_range_32));
236 } 237 }
237 238
238 CheckedMachAddressRange parent_range_64(true, 0x100000000, 0x1000); 239 CheckedAddressRange parent_range_64(true, 0x100000000, 0x1000);
239 ASSERT_TRUE(parent_range_64.IsValid()); 240 ASSERT_TRUE(parent_range_64.IsValid());
240 241
241 CheckedMachAddressRange child_range_64(true, 0xffffffff, 2); 242 CheckedAddressRange child_range_64(true, 0xffffffff, 2);
242 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); 243 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64));
243 244
244 child_range_64.SetRange(true, 0x100000000, 2); 245 child_range_64.SetRange(true, 0x100000000, 2);
245 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); 246 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64));
246 247
247 child_range_64.SetRange(true, 0x100000ffe, 2); 248 child_range_64.SetRange(true, 0x100000ffe, 2);
248 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64)); 249 EXPECT_TRUE(parent_range_64.ContainsRange(child_range_64));
249 250
250 child_range_64.SetRange(true, 0x100000fff, 2); 251 child_range_64.SetRange(true, 0x100000fff, 2);
251 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64)); 252 EXPECT_FALSE(parent_range_64.ContainsRange(child_range_64));
252 } 253 }
253 254
254 } // namespace 255 } // namespace
255 } // namespace test 256 } // namespace test
256 } // namespace crashpad 257 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698