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/pe_file_writer.h"
16 :
17 : #include "base/file_util.h"
18 : #include "base/path_service.h"
19 : #include "gtest/gtest.h"
20 : #include "gmock/gmock.h"
21 : #include "syzygy/core/unittest_util.h"
22 : #include "syzygy/pe/decomposer.h"
23 : #include "syzygy/pe/pe_file.h"
24 : #include "syzygy/pe/unittest_util.h"
25 :
26 : namespace pe {
27 :
28 : namespace {
29 :
30 : class PEFileWriterTest: public testing::PELibUnitTest {
31 : // Add customizations here.
32 : };
33 :
34 : } // namespace
35 :
36 E : TEST_F(PEFileWriterTest, LoadOriginalImage) {
37 : // This test baselines the other test(s) that operate on mutated, copied
38 : // versions of the DLLs.
39 E : FilePath image_path(testing::GetExeRelativePath(kDllName));
40 E : ASSERT_NO_FATAL_FAILURE(CheckTestDll(image_path));
41 E : }
42 :
43 E : TEST_F(PEFileWriterTest, RewriteAndLoadImage) {
44 : // Create a temporary file we can write the new image to.
45 E : FilePath temp_dir;
46 E : ASSERT_NO_FATAL_FAILURE(CreateTemporaryDir(&temp_dir));
47 E : FilePath temp_file = temp_dir.Append(kDllName);
48 :
49 : // Decompose the original test image.
50 E : PEFile image_file;
51 E : FilePath image_path(testing::GetExeRelativePath(kDllName));
52 E : ASSERT_TRUE(image_file.Init(image_path));
53 :
54 E : Decomposer decomposer(image_file);
55 E : block_graph::BlockGraph block_graph;
56 E : pe::ImageLayout image_layout(&block_graph);
57 E : ASSERT_TRUE(decomposer.Decompose(&image_layout));
58 :
59 E : PEFileWriter writer(image_layout);
60 :
61 E : ASSERT_TRUE(writer.WriteImage(temp_file));
62 E : ASSERT_NO_FATAL_FAILURE(CheckTestDll(temp_file));
63 E : }
64 :
65 E : TEST_F(PEFileWriterTest, UpdateFileChecksum) {
66 E : FilePath temp_dir;
67 E : ASSERT_NO_FATAL_FAILURE(CreateTemporaryDir(&temp_dir));
68 :
69 : // Verify that the function fails on non-existent paths.
70 E : FilePath executable = temp_dir.Append(L"executable_file.exe");
71 E : EXPECT_FALSE(PEFileWriter::UpdateFileChecksum(executable));
72 :
73 : // Verify that the function fails for non-image files.
74 E : file_util::ScopedFILE file(file_util::OpenFile(executable, "wb"));
75 : // Grow the file to 16K.
76 E : ASSERT_EQ(0, fseek(file.get(), 16 * 1024, SEEK_SET));
77 E : file.reset();
78 E : EXPECT_FALSE(PEFileWriter::UpdateFileChecksum(executable));
79 :
80 : // Make a copy of our test DLL and check that we work on that.
81 E : FilePath input_path(testing::GetExeRelativePath(kDllName));
82 E : FilePath image_path(temp_dir.Append(kDllName));
83 E : EXPECT_TRUE(file_util::CopyFile(input_path, image_path));
84 E : EXPECT_TRUE(PEFileWriter::UpdateFileChecksum(image_path));
85 E : }
86 :
87 : } // namespace pe
|