Index: courgette/rel32_finder_unittest.cc |
diff --git a/courgette/rel32_finder_win32_x86_unittest.cc b/courgette/rel32_finder_unittest.cc |
similarity index 72% |
rename from courgette/rel32_finder_win32_x86_unittest.cc |
rename to courgette/rel32_finder_unittest.cc |
index 496f0b94bb249bb837bad08f0ca3bcbc9e4a8272..7f6588098a5be451e858afe0fcc86597a6bf8214 100644 |
--- a/courgette/rel32_finder_win32_x86_unittest.cc |
+++ b/courgette/rel32_finder_unittest.cc |
@@ -2,28 +2,29 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "courgette/rel32_finder_win32_x86.h" |
- |
#include <stddef.h> |
#include <stdint.h> |
#include <algorithm> |
+#include <memory> |
#include <sstream> |
#include <string> |
#include "base/macros.h" |
#include "courgette/base_test_unittest.h" |
#include "courgette/image_utils.h" |
+#include "courgette/rel32_finder_x64.h" |
+#include "courgette/rel32_finder_x86.h" |
#include "testing/gtest/include/gtest/gtest.h" |
namespace courgette { |
namespace { |
-// Helper class to load and execute a Rel32FinderWin32X86 test case. |
-class Rel32FinderWin32X86TestCase { |
+// Helper class to load and execute a Rel32Finder test case. |
+class Rel32FinderTestCase { |
public: |
- Rel32FinderWin32X86TestCase(const std::string& test_data) |
+ Rel32FinderTestCase(const std::string& test_data) |
: text_start_rva_(0), |
text_end_rva_(0), |
relocs_start_rva_(0), |
@@ -33,17 +34,29 @@ class Rel32FinderWin32X86TestCase { |
} |
void RunTestBasic(std::string name) { |
- Rel32FinderWin32X86_Basic finder(relocs_start_rva_, relocs_end_rva_); |
ASSERT_FALSE(text_data_.empty()); |
- finder.Find(&text_data_[0], &text_data_[0] + text_data_.size(), |
- text_start_rva_, text_end_rva_, abs32_locations_); |
+ finder_->Find(&text_data_[0], &text_data_[0] + text_data_.size(), |
+ text_start_rva_, text_end_rva_, abs32_locations_); |
std::vector<RVA> rel32_locations; |
- finder.SwapRel32Locations(&rel32_locations); |
+ finder_->SwapRel32Locations(&rel32_locations); |
EXPECT_EQ(expected_rel32_locations_, rel32_locations) |
<< "From test case " << name << " (addresses are in hex)"; |
} |
+ void CreateFinder(const std::string& processor_type) { |
+ if (processor_type == "x64") { |
+ finder_ = std::unique_ptr<Rel32Finder>(new Rel32FinderX64( |
+ relocs_start_rva_, relocs_end_rva_, image_end_rva_)); |
+ } else if (processor_type == "x86") { |
+ finder_ = std::unique_ptr<Rel32Finder>( |
+ new Rel32FinderX86(relocs_start_rva_, relocs_end_rva_)); |
+ } else { |
+ NOTREACHED(); |
+ } |
+ } |
+ |
private: |
+ std::unique_ptr<Rel32Finder> finder_; |
RVA text_start_rva_; |
RVA text_end_rva_; |
RVA relocs_start_rva_; |
@@ -61,7 +74,7 @@ class Rel32FinderWin32X86TestCase { |
while (std::getline(iss, line)) { |
// Trim comments and trailing spaces. |
size_t end_pos = std::min(line.find("#"), line.length()); |
- while (end_pos > 0 && line[end_pos] == ' ') |
+ while (end_pos > 0 && line[end_pos - 1] == ' ') |
--end_pos; |
line.resize(end_pos); |
if (!line.empty()) |
@@ -83,10 +96,12 @@ class Rel32FinderWin32X86TestCase { |
} |
// Initializes the test case by parsing the multi-line string |test_data| |
- // to extract Rel32FinderWin32X86 parameters, and read expected values. |
+ // to extract Rel32Finder parameters, and read expected values. |
void LoadTestFromString(const std::string& test_data) { |
// The first lines (ignoring empty ones) specify RVA bounds. |
std::istringstream iss(test_data); |
+ std::string processor_type; |
+ ASSERT_TRUE(ReadNonEmptyLine(iss, &processor_type)); |
ASSERT_TRUE(ReadHexUInt32(iss, &text_start_rva_)); |
ASSERT_TRUE(ReadHexUInt32(iss, &text_end_rva_)); |
ASSERT_TRUE(ReadHexUInt32(iss, &relocs_start_rva_)); |
@@ -107,7 +122,7 @@ class Rel32FinderWin32X86TestCase { |
std::string toks = line.substr(kBytesBegin, kBytesEnd); |
uint32_t vals[6]; |
int num_read = sscanf(toks.c_str(), "%X %X %X %X %X %X", &vals[0], |
- &vals[1], &vals[2], &vals[3], &vals[4], &vals[5]); |
+ &vals[1], &vals[2], &vals[3], &vals[4], &vals[5]); |
for (int i = 0; i < num_read; ++i) |
text_data_.push_back(static_cast<uint8_t>(vals[i] & 0xFF)); |
} |
@@ -128,22 +143,27 @@ class Rel32FinderWin32X86TestCase { |
ASSERT_EQ(1, sscanf(line.c_str(), "%X", &rel32_location)); |
expected_rel32_locations_.push_back(rel32_location); |
} |
+ CreateFinder(processor_type); |
} |
}; |
-class Rel32FinderWin32X86Test : public BaseTest { |
+class Rel32FinderTest : public BaseTest { |
public: |
void RunTest(const char* test_case_file) { |
- Rel32FinderWin32X86TestCase test_case(FileContents(test_case_file)); |
+ Rel32FinderTestCase test_case(FileContents(test_case_file)); |
test_case.RunTestBasic(test_case_file); |
} |
}; |
-TEST_F(Rel32FinderWin32X86Test, TestBasic) { |
- RunTest("rel32_win32_x86_01.txt"); |
- RunTest("rel32_win32_x86_02.txt"); |
- RunTest("rel32_win32_x86_03.txt"); |
- RunTest("rel32_win32_x86_04.txt"); |
+TEST_F(Rel32FinderTest, TestBasic) { |
+ RunTest("rel32_x86_01.txt"); |
+ RunTest("rel32_x86_02.txt"); |
+ RunTest("rel32_x86_03.txt"); |
+ RunTest("rel32_x86_04.txt"); |
+ |
+ RunTest("rel32_x64_01.txt"); |
+ RunTest("rel32_x64_02.txt"); |
+ RunTest("rel32_x64_03.txt"); |
} |
} // namespace |