1 : // Copyright 2012 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/pdb/omap.h"
16 :
17 : #include "base/path_service.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/core/unittest_util.h"
20 : #include "syzygy/pdb/unittest_util.h"
21 :
22 : namespace pdb {
23 :
24 : using core::RelativeAddress;
25 :
26 E : TEST(OmapTest, CreateOmap) {
27 E : OMAP omap = CreateOmap(523, 644);
28 E : EXPECT_EQ(523u, omap.rva);
29 E : EXPECT_EQ(644u, omap.rvaTo);
30 E : }
31 :
32 E : TEST(OmapTest, OmapLess) {
33 E : OMAP omap1 = CreateOmap(0, 0);
34 E : OMAP omap2 = CreateOmap(1, 1);
35 E : OMAP omap3 = CreateOmap(1, 2);
36 :
37 E : EXPECT_TRUE(OmapLess(omap1, omap2));
38 E : EXPECT_FALSE(OmapLess(omap2, omap1));
39 E : EXPECT_FALSE(OmapLess(omap1, omap1));
40 E : EXPECT_FALSE(OmapLess(omap2, omap2));
41 E : EXPECT_FALSE(OmapLess(omap2, omap3));
42 E : EXPECT_FALSE(OmapLess(omap3, omap2));
43 E : }
44 :
45 E : TEST(OmapTest, OmapVectorIsValid) {
46 E : std::vector<OMAP> omaps;
47 E : EXPECT_TRUE(OmapVectorIsValid(omaps));
48 :
49 E : omaps.push_back(CreateOmap(0, 0));
50 E : EXPECT_TRUE(OmapVectorIsValid(omaps));
51 :
52 E : omaps.push_back(CreateOmap(1, 0));
53 E : EXPECT_TRUE(OmapVectorIsValid(omaps));
54 :
55 E : omaps.push_back(CreateOmap(1, 1));
56 E : EXPECT_FALSE(OmapVectorIsValid(omaps));
57 :
58 E : omaps.back().rva = 0;
59 E : EXPECT_FALSE(OmapVectorIsValid(omaps));
60 E : }
61 :
62 E : TEST(OmapTest, Translate) {
63 E : std::vector<OMAP> omaps;
64 :
65 : // We create mapping that sends [1000, 2000) to [2000, 3000) and
66 : // [2000, 3000) to [1000, 2000). Addresses < 1000 and >= 3000 remain fixed.
67 E : omaps.push_back(CreateOmap(1000, 2000));
68 E : omaps.push_back(CreateOmap(2000, 1000));
69 E : omaps.push_back(CreateOmap(3000, 3000));
70 :
71 E : ASSERT_TRUE(OmapVectorIsValid(omaps));
72 :
73 : // Try a mapping in each of the 4 distinct regions that the OMAP vector
74 : // imposes on the address space.
75 : EXPECT_EQ(RelativeAddress(500),
76 E : TranslateAddressViaOmap(omaps, RelativeAddress(500)));
77 : EXPECT_EQ(RelativeAddress(2500),
78 E : TranslateAddressViaOmap(omaps, RelativeAddress(1500)));
79 : EXPECT_EQ(RelativeAddress(1500),
80 E : TranslateAddressViaOmap(omaps, RelativeAddress(2500)));
81 : EXPECT_EQ(RelativeAddress(3500),
82 E : TranslateAddressViaOmap(omaps, RelativeAddress(3500)));
83 E : }
84 :
85 E : TEST(OmapTest, ReadOmapsFromPdbFile) {
86 E : std::vector<OMAP> omap_to, omap_from;
87 :
88 : // We only test ReadOmapsFromPdbFile as this wraps ReadOmapsFromPdbReader and
89 : // inherently tests both.
90 :
91 : // We expect this to be false, as the original test_dll has no OMAP
92 : // information in it.
93 : base::FilePath pdb_path = testing::GetSrcRelativePath(
94 E : testing::kTestDllFilePath);
95 E : EXPECT_FALSE(ReadOmapsFromPdbFile(pdb_path, NULL, NULL));
96 :
97 : pdb_path = testing::GetSrcRelativePath(
98 E : testing::kOmappedTestPdbFilePath);
99 E : EXPECT_TRUE(ReadOmapsFromPdbFile(pdb_path, NULL, NULL));
100 E : EXPECT_TRUE(ReadOmapsFromPdbFile(pdb_path, NULL, &omap_from));
101 E : EXPECT_TRUE(ReadOmapsFromPdbFile(pdb_path, &omap_to, NULL));
102 E : EXPECT_TRUE(ReadOmapsFromPdbFile(pdb_path, &omap_to, &omap_from));
103 E : EXPECT_FALSE(omap_to.empty());
104 E : EXPECT_FALSE(omap_from.empty());
105 E : }
106 :
107 : } // namespace pdb
|