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 "util/numeric/checked_range.h" |
| 16 |
| 17 #include <stdint.h> |
| 18 |
| 19 #include <limits> |
| 20 |
| 21 #include "base/basictypes.h" |
| 22 #include "base/strings/stringprintf.h" |
| 23 #include "gtest/gtest.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 using namespace crashpad; |
| 28 |
| 29 TEST(CheckedRange, IsValid) { |
| 30 const struct UnsignedTestData { |
| 31 uint32_t base; |
| 32 uint32_t size; |
| 33 bool valid; |
| 34 } kUnsignedTestData[] = { |
| 35 {0, 0, true}, |
| 36 {0, 1, true}, |
| 37 {0, 2, true}, |
| 38 {0, 0x7fffffff, true}, |
| 39 {0, 0x80000000, true}, |
| 40 {0, 0xfffffffe, true}, |
| 41 {0, 0xffffffff, true}, |
| 42 {1, 0, true}, |
| 43 {1, 1, true}, |
| 44 {1, 2, true}, |
| 45 {1, 0x7fffffff, true}, |
| 46 {1, 0x80000000, true}, |
| 47 {1, 0xfffffffe, true}, |
| 48 {1, 0xffffffff, false}, |
| 49 {0x7fffffff, 0, true}, |
| 50 {0x7fffffff, 1, true}, |
| 51 {0x7fffffff, 2, true}, |
| 52 {0x7fffffff, 0x7fffffff, true}, |
| 53 {0x7fffffff, 0x80000000, true}, |
| 54 {0x7fffffff, 0xfffffffe, false}, |
| 55 {0x7fffffff, 0xffffffff, false}, |
| 56 {0x80000000, 0, true}, |
| 57 {0x80000000, 1, true}, |
| 58 {0x80000000, 2, true}, |
| 59 {0x80000000, 0x7fffffff, true}, |
| 60 {0x80000000, 0x80000000, false}, |
| 61 {0x80000000, 0xfffffffe, false}, |
| 62 {0x80000000, 0xffffffff, false}, |
| 63 {0xfffffffe, 0, true}, |
| 64 {0xfffffffe, 1, true}, |
| 65 {0xfffffffe, 2, false}, |
| 66 {0xfffffffe, 0x7fffffff, false}, |
| 67 {0xfffffffe, 0x80000000, false}, |
| 68 {0xfffffffe, 0xfffffffe, false}, |
| 69 {0xfffffffe, 0xffffffff, false}, |
| 70 {0xffffffff, 0, true}, |
| 71 {0xffffffff, 1, false}, |
| 72 {0xffffffff, 2, false}, |
| 73 {0xffffffff, 0x7fffffff, false}, |
| 74 {0xffffffff, 0x80000000, false}, |
| 75 {0xffffffff, 0xfffffffe, false}, |
| 76 {0xffffffff, 0xffffffff, false}, |
| 77 }; |
| 78 |
| 79 for (size_t index = 0; index < arraysize(kUnsignedTestData); ++index) { |
| 80 const UnsignedTestData& testcase = kUnsignedTestData[index]; |
| 81 SCOPED_TRACE(base::StringPrintf("unsigned index %zu, base 0x%x, size 0x%x", |
| 82 index, |
| 83 testcase.base, |
| 84 testcase.size)); |
| 85 |
| 86 CheckedRange<uint32_t> range(testcase.base, testcase.size); |
| 87 EXPECT_EQ(testcase.valid, range.IsValid()); |
| 88 } |
| 89 |
| 90 const int32_t kMinInt32 = std::numeric_limits<int32_t>::min(); |
| 91 const struct SignedTestData { |
| 92 int32_t base; |
| 93 uint32_t size; |
| 94 bool valid; |
| 95 } kSignedTestData[] = { |
| 96 {0, 0, true}, |
| 97 {0, 1, true}, |
| 98 {0, 2, true}, |
| 99 {0, 0x7fffffff, true}, |
| 100 {0, 0x80000000, false}, |
| 101 {0, 0xfffffffe, false}, |
| 102 {0, 0xffffffff, false}, |
| 103 {1, 0, true}, |
| 104 {1, 1, true}, |
| 105 {1, 2, true}, |
| 106 {1, 0x7fffffff, false}, |
| 107 {1, 0x80000000, false}, |
| 108 {1, 0xfffffffe, false}, |
| 109 {1, 0xffffffff, false}, |
| 110 {0x7fffffff, 0, true}, |
| 111 {0x7fffffff, 1, false}, |
| 112 {0x7fffffff, 2, false}, |
| 113 {0x7fffffff, 0x7fffffff, false}, |
| 114 {0x7fffffff, 0x80000000, false}, |
| 115 {0x7fffffff, 0xfffffffe, false}, |
| 116 {0x7fffffff, 0xffffffff, false}, |
| 117 {kMinInt32, 0, true}, |
| 118 {kMinInt32, 1, true}, |
| 119 {kMinInt32, 2, true}, |
| 120 {kMinInt32, 0x7fffffff, true}, |
| 121 {kMinInt32, 0x80000000, false}, |
| 122 {kMinInt32, 0xfffffffe, false}, |
| 123 {kMinInt32, 0xffffffff, false}, |
| 124 {-2, 0, true}, |
| 125 {-2, 1, true}, |
| 126 {-2, 2, true}, |
| 127 {-2, 0x7fffffff, true}, |
| 128 {-2, 0x80000000, false}, |
| 129 {-2, 0xfffffffe, false}, |
| 130 {-2, 0xffffffff, false}, |
| 131 {-1, 0, true}, |
| 132 {-1, 1, true}, |
| 133 {-1, 2, true}, |
| 134 {-1, 0x7fffffff, true}, |
| 135 {-1, 0x80000000, false}, |
| 136 {-1, 0xfffffffe, false}, |
| 137 {-1, 0xffffffff, false}, |
| 138 }; |
| 139 |
| 140 for (size_t index = 0; index < arraysize(kSignedTestData); ++index) { |
| 141 const SignedTestData& testcase = kSignedTestData[index]; |
| 142 SCOPED_TRACE(base::StringPrintf("signed index %zu, base 0x%x, size 0x%x", |
| 143 index, |
| 144 testcase.base, |
| 145 testcase.size)); |
| 146 |
| 147 CheckedRange<int32_t, uint32_t> range(testcase.base, testcase.size); |
| 148 EXPECT_EQ(testcase.valid, range.IsValid()); |
| 149 } |
| 150 } |
| 151 |
| 152 TEST(CheckedRange, ContainsValue) { |
| 153 const struct TestData { |
| 154 uint32_t value; |
| 155 bool valid; |
| 156 } kTestData[] = { |
| 157 {0, false}, |
| 158 {1, false}, |
| 159 {0x1fff, false}, |
| 160 {0x2000, true}, |
| 161 {0x2001, true}, |
| 162 {0x2ffe, true}, |
| 163 {0x2fff, true}, |
| 164 {0x3000, false}, |
| 165 {0x3001, false}, |
| 166 {0x7fffffff, false}, |
| 167 {0x80000000, false}, |
| 168 {0x80000001, false}, |
| 169 {0x80001fff, false}, |
| 170 {0x80002000, false}, |
| 171 {0x80002001, false}, |
| 172 {0x80002ffe, false}, |
| 173 {0x80002fff, false}, |
| 174 {0x80003000, false}, |
| 175 {0x80003001, false}, |
| 176 {0xffffcfff, false}, |
| 177 {0xffffdfff, false}, |
| 178 {0xffffefff, false}, |
| 179 {0xffffffff, false}, |
| 180 }; |
| 181 |
| 182 CheckedRange<uint32_t> parent_range(0x2000, 0x1000); |
| 183 ASSERT_TRUE(parent_range.IsValid()); |
| 184 |
| 185 for (size_t index = 0; index < arraysize(kTestData); ++index) { |
| 186 const TestData& testcase = kTestData[index]; |
| 187 SCOPED_TRACE( |
| 188 base::StringPrintf("index %zu, value 0x%x", index, testcase.value)); |
| 189 |
| 190 EXPECT_EQ(testcase.valid, parent_range.ContainsValue(testcase.value)); |
| 191 } |
| 192 } |
| 193 |
| 194 TEST(CheckedRange, ContainsRange) { |
| 195 const struct TestData { |
| 196 uint32_t base; |
| 197 uint32_t size; |
| 198 bool valid; |
| 199 } kTestData[] = { |
| 200 {0, 0, false}, |
| 201 {0, 1, false}, |
| 202 {0x2000, 0x1000, true}, |
| 203 {0, 0x2000, false}, |
| 204 {0x3000, 0x1000, false}, |
| 205 {0x1800, 0x1000, false}, |
| 206 {0x2800, 0x1000, false}, |
| 207 {0x2000, 0x800, true}, |
| 208 {0x2800, 0x800, true}, |
| 209 {0x2400, 0x800, true}, |
| 210 {0x2800, 0, true}, |
| 211 {0x2000, 0xffffdfff, false}, |
| 212 {0x2800, 0xffffd7ff, false}, |
| 213 {0x3000, 0xffffcfff, false}, |
| 214 {0xfffffffe, 1, false}, |
| 215 {0xffffffff, 0, false}, |
| 216 {0x1fff, 0, false}, |
| 217 {0x2000, 0, true}, |
| 218 {0x2001, 0, true}, |
| 219 {0x2fff, 0, true}, |
| 220 {0x3000, 0, true}, |
| 221 {0x3001, 0, false}, |
| 222 {0x1fff, 1, false}, |
| 223 {0x2000, 1, true}, |
| 224 {0x2001, 1, true}, |
| 225 {0x2fff, 1, true}, |
| 226 {0x3000, 1, false}, |
| 227 {0x3001, 1, false}, |
| 228 }; |
| 229 |
| 230 CheckedRange<uint32_t> parent_range(0x2000, 0x1000); |
| 231 ASSERT_TRUE(parent_range.IsValid()); |
| 232 |
| 233 for (size_t index = 0; index < arraysize(kTestData); ++index) { |
| 234 const TestData& testcase = kTestData[index]; |
| 235 SCOPED_TRACE(base::StringPrintf("index %zu, base 0x%x, size 0x%x", |
| 236 index, |
| 237 testcase.base, |
| 238 testcase.size)); |
| 239 |
| 240 CheckedRange<uint32_t> child_range(testcase.base, testcase.size); |
| 241 ASSERT_TRUE(child_range.IsValid()); |
| 242 EXPECT_EQ(testcase.valid, parent_range.ContainsRange(child_range)); |
| 243 } |
| 244 } |
| 245 |
| 246 } // namespace |
OLD | NEW |