1 : // Copyright 2015 Google Inc. 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 "syzygy/refinery/core/address.h"
16 :
17 : #include "gtest/gtest.h"
18 :
19 : namespace refinery {
20 :
21 E : TEST(AddressTest, ConversionTest) {
22 : // An integer with its most significant bit set.
23 E : uint32_t integer = 0xffffffff;
24 :
25 : // The expected address corresponding to this integer.
26 E : Address expected_address = static_cast<Address>(integer);
27 E : ASSERT_EQ(0xffffffffULL, expected_address);
28 :
29 : // The coresponding pointer with its most significant bit set (note: this is a
30 : // 32 bit test).
31 E : int* ptr = reinterpret_cast<int*>(integer);
32 :
33 : // Validate conversion is as expected.
34 : ASSERT_EQ(expected_address,
35 E : static_cast<Address>(reinterpret_cast<uintptr_t>(ptr)));
36 E : }
37 :
38 E : TEST(AddressRangeTest, IsValid) {
39 : // TODO(siggi): IsValid is sort of a nonsense check, as the size of the
40 : // address space needs to go into it.
41 : // Move this check to the ProcessState.
42 :
43 : // The empty range is not valid.
44 E : AddressRange range;
45 E : EXPECT_TRUE(range.IsEmpty());
46 E : EXPECT_FALSE(range.IsValid());
47 :
48 E : range = AddressRange(UINT64_MAX, 1);
49 E : EXPECT_FALSE(range.IsValid());
50 :
51 E : range = AddressRange(UINT64_MAX - UINT32_MAX + 1, UINT32_MAX);
52 E : EXPECT_FALSE(range.IsValid());
53 :
54 E : range = AddressRange(UINT64_MAX - UINT32_MAX, UINT32_MAX);
55 E : EXPECT_TRUE(range.IsValid());
56 :
57 : // Empty range anywhere is not valid.
58 E : range = AddressRange(100, 0);
59 E : EXPECT_FALSE(range.IsValid());
60 :
61 E : range = AddressRange(100, 1);
62 E : EXPECT_TRUE(range.IsValid());
63 E : }
64 :
65 : } // namespace refinery
|