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/symbols/symbol_provider.h"
16 :
17 : #include <string>
18 : #include <vector>
19 :
20 : #include "base/files/file_path.h"
21 : #include "base/strings/utf_string_conversions.h"
22 : #include "gtest/gtest.h"
23 : #include "syzygy/core/unittest_util.h"
24 : #include "syzygy/pe/pe_file.h"
25 : #include "syzygy/refinery/core/address.h"
26 : #include "syzygy/refinery/process_state/process_state.h"
27 : #include "syzygy/refinery/process_state/process_state_util.h"
28 : #include "syzygy/refinery/types/type.h"
29 : #include "syzygy/refinery/types/type_repository.h"
30 :
31 : namespace refinery {
32 :
33 : namespace {
34 :
35 : const Address kAddress = 0x0000CAFE; // Fits 32-bit.
36 : const Size kSize = 42U;
37 : const uint32 kChecksum = 11U;
38 : const uint32 kTimestamp = 22U;
39 :
40 : } // namespace
41 :
42 E : TEST(SymbolProviderTest, FindOrCreateTypeRepository) {
43 E : ProcessState process_state;
44 E : scoped_refptr<SymbolProvider> provider = new SymbolProvider();
45 :
46 : // Get the signature for test_types.dll.
47 : const base::FilePath module_path(testing::GetSrcRelativePath(
48 E : L"syzygy\\refinery\\test_data\\test_types.dll"));
49 E : pe::PEFile pe_file;
50 E : ASSERT_TRUE(pe_file.Init(module_path));
51 E : pe::PEFile::Signature module_signature;
52 E : pe_file.GetSignature(&module_signature);
53 :
54 : // Successfully retrieve the repository.
55 E : scoped_refptr<TypeRepository> repository;
56 : ASSERT_TRUE(
57 E : provider->FindOrCreateTypeRepository(module_signature, &repository));
58 E : ASSERT_TRUE(repository != nullptr);
59 E : ASSERT_GT(repository->size(), 0);
60 :
61 : // Ensure a second call retrieves the same object.
62 E : scoped_refptr<TypeRepository> second_repository;
63 : ASSERT_TRUE(provider->FindOrCreateTypeRepository(module_signature,
64 E : &second_repository));
65 E : ASSERT_EQ(repository.get(), second_repository.get());
66 E : }
67 :
68 E : TEST(SymbolProviderTest, FindOrCreateTypeNameIndex) {
69 E : ProcessState process_state;
70 E : scoped_refptr<SymbolProvider> provider = new SymbolProvider();
71 :
72 : // Get the signature for test_types.dll.
73 : const base::FilePath module_path(testing::GetSrcRelativePath(
74 E : L"syzygy\\refinery\\test_data\\test_types.dll"));
75 E : pe::PEFile pe_file;
76 E : ASSERT_TRUE(pe_file.Init(module_path));
77 E : pe::PEFile::Signature module_signature;
78 E : pe_file.GetSignature(&module_signature);
79 :
80 : // Successfully retrieve the type name index.
81 E : scoped_refptr<TypeNameIndex> index;
82 E : ASSERT_TRUE(provider->FindOrCreateTypeNameIndex(module_signature, &index));
83 E : ASSERT_TRUE(index != nullptr);
84 E : std::vector<TypePtr> matching_types;
85 E : index->GetTypes(L"testing::TestSimpleUDT", &matching_types);
86 E : ASSERT_EQ(1, matching_types.size());
87 E : }
88 :
89 : } // namespace refinery
|