1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
13 #include "base/macros.h"
14 #include "courgette/base_test_unittest.h"
15 #include "courgette/image_utils.h"
16 #include "courgette/rel32_finder_x64.h"
17 #include "courgette/rel32_finder_x86.h"
18 #include "testing/gtest/include/gtest/gtest.h"
24 // Helper class to load and execute a Rel32Finder test case.
25 class Rel32FinderTestCase {
27 Rel32FinderTestCase(const std::string& test_data)
33 LoadTestFromString(test_data);
36 void RunTestBasic(std::string name) {
37 ASSERT_FALSE(text_data_.empty());
38 finder_->Find(&text_data_[0], &text_data_[0] + text_data_.size(),
39 text_start_rva_, text_end_rva_, abs32_locations_);
40 std::vector<RVA> rel32_locations;
41 finder_->SwapRel32Locations(&rel32_locations);
42 EXPECT_EQ(expected_rel32_locations_, rel32_locations)
43 << "From test case " << name << " (addresses are in hex)";
46 void CreateFinder(const std::string& processor_type) {
47 if (processor_type == "x64") {
48 finder_ = std::unique_ptr<Rel32Finder>(new Rel32FinderX64(
49 relocs_start_rva_, relocs_end_rva_, image_end_rva_));
50 } else if (processor_type == "x86") {
51 finder_ = std::unique_ptr<Rel32Finder>(
52 new Rel32FinderX86(relocs_start_rva_, relocs_end_rva_));
59 std::unique_ptr<Rel32Finder> finder_;
62 RVA relocs_start_rva_;
65 std::vector<uint8_t> text_data_;
66 std::vector<RVA> abs32_locations_;
67 std::vector<RVA> expected_rel32_locations_;
69 // Scans |iss| for the next non-empty line, after removing "#"-style comments
70 // and stripping trailing spaces. On success, returns true and writes the
71 // result to |line_out|. Otherwise returns false.
72 bool ReadNonEmptyLine(std::istringstream& iss, std::string* line_out) {
74 while (std::getline(iss, line)) {
75 // Trim comments and trailing spaces.
76 size_t end_pos = std::min(line.find("#"), line.length());
77 while (end_pos > 0 && line[end_pos - 1] == ' ')
89 // Scans |iss| for the next non-empty line, and reads (hex) uint32_t into |v|.
90 // Returns true iff successful.
91 bool ReadHexUInt32(std::istringstream& iss, uint32_t* v) {
93 if (!ReadNonEmptyLine(iss, &line))
95 return sscanf(line.c_str(), "%X", v) == 1;
98 // Initializes the test case by parsing the multi-line string |test_data|
99 // to extract Rel32Finder parameters, and read expected values.
100 void LoadTestFromString(const std::string& test_data) {
101 // The first lines (ignoring empty ones) specify RVA bounds.
102 std::istringstream iss(test_data);
103 std::string processor_type;
104 ASSERT_TRUE(ReadNonEmptyLine(iss, &processor_type));
105 ASSERT_TRUE(ReadHexUInt32(iss, &text_start_rva_));
106 ASSERT_TRUE(ReadHexUInt32(iss, &text_end_rva_));
107 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_start_rva_));
108 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_end_rva_));
109 ASSERT_TRUE(ReadHexUInt32(iss, &image_end_rva_));
112 // The Program section specifies instruction bytes. We require lines to be
113 // formatted in "DUMPBIN /DISASM" style, i.e.,
114 // "00401003: E8 00 00 00 00 call 00401008"
116 // We extract up to 6 bytes per line. The remaining are ignored.
117 const int kBytesBegin = 12;
118 const int kBytesEnd = 17;
119 ReadNonEmptyLine(iss, &line);
120 ASSERT_EQ("Program:", line);
121 while (ReadNonEmptyLine(iss, &line) && line != "Abs32:") {
122 std::string toks = line.substr(kBytesBegin, kBytesEnd);
124 int num_read = sscanf(toks.c_str(), "%X %X %X %X %X %X", &vals[0],
125 &vals[1], &vals[2], &vals[3], &vals[4], &vals[5]);
126 for (int i = 0; i < num_read; ++i)
127 text_data_.push_back(static_cast<uint8_t>(vals[i] & 0xFF));
129 ASSERT_FALSE(text_data_.empty());
131 // The Abs32 section specifies hex RVAs, one per line.
132 ASSERT_EQ("Abs32:", line);
133 while (ReadNonEmptyLine(iss, &line) && line != "Expected:") {
135 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &abs32_location));
136 abs32_locations_.push_back(abs32_location);
139 // The Expected section specifies hex Rel32 RVAs, one per line.
140 ASSERT_EQ("Expected:", line);
141 while (ReadNonEmptyLine(iss, &line)) {
143 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &rel32_location));
144 expected_rel32_locations_.push_back(rel32_location);
146 CreateFinder(processor_type);
150 class Rel32FinderTest : public BaseTest {
152 void RunTest(const char* test_case_file) {
153 Rel32FinderTestCase test_case(FileContents(test_case_file));
154 test_case.RunTestBasic(test_case_file);
158 TEST_F(Rel32FinderTest, TestBasic) {
159 RunTest("rel32_x86_01.txt");
160 RunTest("rel32_x86_02.txt");
161 RunTest("rel32_x86_03.txt");
162 RunTest("rel32_x86_04.txt");
164 RunTest("rel32_x64_01.txt");
165 RunTest("rel32_x64_02.txt");
166 RunTest("rel32_x64_03.txt");
171 } // namespace courgette