1 : // Copyright 2011 Google Inc.
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/pe/dia_util.h"
16 :
17 : #include <vector>
18 :
19 : #include "base/win/scoped_bstr.h"
20 : #include "base/win/scoped_comptr.h"
21 : #include "syzygy/core/unittest_util.h"
22 : #include "syzygy/pdb/pdb_data.h"
23 : #include "syzygy/pe/unittest_util.h"
24 :
25 : namespace pe {
26 :
27 : using base::win::ScopedBstr;
28 : using base::win::ScopedComPtr;
29 :
30 : static const wchar_t kNonsenseStreamName[] =
31 : L"ThisStreamNameCertainlyDoesNotExist";
32 :
33 : class DiaUtilTest : public testing::PELibUnitTest {
34 : };
35 :
36 E : TEST_F(DiaUtilTest, CreateDiaSource) {
37 E : ScopedComPtr<IDiaDataSource> dia_source;
38 E : EXPECT_TRUE(CreateDiaSource(dia_source.Receive()));
39 E : }
40 :
41 E : TEST_F(DiaUtilTest, CreateDiaSesssionDll) {
42 E : ScopedComPtr<IDiaDataSource> dia_source;
43 E : ASSERT_TRUE(CreateDiaSource(dia_source.Receive()));
44 :
45 E : ScopedComPtr<IDiaSession> dia_session;
46 : EXPECT_TRUE(CreateDiaSession(testing::GetExeRelativePath(kDllName),
47 : dia_source.get(),
48 E : dia_session.Receive()));
49 E : }
50 :
51 E : TEST_F(DiaUtilTest, CreateDiaSesssionPdb) {
52 E : ScopedComPtr<IDiaDataSource> dia_source;
53 E : ASSERT_TRUE(CreateDiaSource(dia_source.Receive()));
54 :
55 E : ScopedComPtr<IDiaSession> dia_session;
56 : EXPECT_TRUE(CreateDiaSession(testing::GetExeRelativePath(kDllPdbName),
57 : dia_source.get(),
58 E : dia_session.Receive()));
59 E : }
60 :
61 E : TEST_F(DiaUtilTest, FindDiaTableByIid) {
62 E : ScopedComPtr<IDiaDataSource> dia_source;
63 E : ASSERT_TRUE(CreateDiaSource(dia_source.Receive()));
64 :
65 E : ScopedComPtr<IDiaSession> dia_session;
66 : ASSERT_TRUE(CreateDiaSession(testing::GetExeRelativePath(kDllPdbName),
67 : dia_source.get(),
68 E : dia_session.Receive()));
69 :
70 E : ScopedComPtr<IDiaEnumSectionContribs> section_contribs;
71 : EXPECT_EQ(kSearchSucceeded,
72 : FindDiaTable(section_contribs.iid(),
73 : dia_session.get(),
74 E : reinterpret_cast<void**>(section_contribs.Receive())));
75 E : }
76 :
77 E : TEST_F(DiaUtilTest, FindDiaTableByType) {
78 E : ScopedComPtr<IDiaDataSource> dia_source;
79 E : ASSERT_TRUE(CreateDiaSource(dia_source.Receive()));
80 :
81 E : ScopedComPtr<IDiaSession> dia_session;
82 : ASSERT_TRUE(CreateDiaSession(testing::GetExeRelativePath(kDllPdbName),
83 : dia_source.get(),
84 E : dia_session.Receive()));
85 :
86 E : ScopedComPtr<IDiaEnumSectionContribs> section_contribs;
87 : EXPECT_EQ(kSearchSucceeded,
88 E : FindDiaTable(dia_session.get(), section_contribs.Receive()));
89 E : }
90 :
91 E : TEST_F(DiaUtilTest, FindDiaDebugStream) {
92 E : ScopedComPtr<IDiaDataSource> dia_source;
93 E : ASSERT_TRUE(CreateDiaSource(dia_source.Receive()));
94 :
95 E : ScopedComPtr<IDiaSession> dia_session;
96 : ASSERT_TRUE(CreateDiaSession(testing::GetExeRelativePath(kDllPdbName),
97 : dia_source.get(),
98 E : dia_session.Receive()));
99 :
100 E : ScopedComPtr<IDiaEnumDebugStreamData> debug_stream;
101 :
102 : EXPECT_EQ(kSearchFailed,
103 : FindDiaDebugStream(kNonsenseStreamName,
104 : dia_session.get(),
105 E : debug_stream.Receive()));
106 :
107 : EXPECT_EQ(kSearchSucceeded,
108 : FindDiaDebugStream(kFixupDiaDebugStreamName,
109 : dia_session.get(),
110 E : debug_stream.Receive()));
111 E : }
112 :
113 E : TEST_F(DiaUtilTest, LoadDiaDebugStream) {
114 E : ScopedComPtr<IDiaDataSource> dia_source;
115 E : ASSERT_TRUE(CreateDiaSource(dia_source.Receive()));
116 :
117 E : ScopedComPtr<IDiaSession> dia_session;
118 : ASSERT_TRUE(CreateDiaSession(testing::GetExeRelativePath(kDllPdbName),
119 : dia_source.get(),
120 E : dia_session.Receive()));
121 :
122 E : ScopedComPtr<IDiaEnumDebugStreamData> debug_stream;
123 : ASSERT_EQ(kSearchSucceeded,
124 : FindDiaDebugStream(kFixupDiaDebugStreamName,
125 : dia_session.get(),
126 E : debug_stream.Receive()));
127 :
128 E : std::vector<pdb::PdbFixup> fixups;
129 E : EXPECT_TRUE(LoadDiaDebugStream(debug_stream.get(), &fixups));
130 E : EXPECT_FALSE(fixups.empty());
131 E : }
132 :
133 E : TEST_F(DiaUtilTest, FindAndLoadDiaDebugStreamByName) {
134 E : ScopedComPtr<IDiaDataSource> dia_source;
135 E : ASSERT_TRUE(CreateDiaSource(dia_source.Receive()));
136 :
137 E : ScopedComPtr<IDiaSession> dia_session;
138 : ASSERT_TRUE(CreateDiaSession(testing::GetExeRelativePath(kDllPdbName),
139 : dia_source.get(),
140 E : dia_session.Receive()));
141 :
142 E : std::vector<pdb::PdbFixup> fixups;
143 :
144 : EXPECT_EQ(kSearchFailed,
145 : FindAndLoadDiaDebugStreamByName(kNonsenseStreamName,
146 : dia_session.get(),
147 E : &fixups));
148 E : EXPECT_TRUE(fixups.empty());
149 :
150 : EXPECT_EQ(kSearchSucceeded,
151 : FindAndLoadDiaDebugStreamByName(kFixupDiaDebugStreamName,
152 : dia_session.get(),
153 E : &fixups));
154 E : EXPECT_FALSE(fixups.empty());
155 E : }
156 :
157 : } // namespace pe
|