Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4102)

Unified Diff: courgette/rel32_finder_unittest.cc

Issue 2008253004: Refactor rel32 searching process for x64 to make it more similar to x86. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve rel32_finder doc Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: courgette/rel32_finder_unittest.cc
diff --git a/courgette/rel32_finder_win32_x86_unittest.cc b/courgette/rel32_finder_unittest.cc
similarity index 75%
rename from courgette/rel32_finder_win32_x86_unittest.cc
rename to courgette/rel32_finder_unittest.cc
index 496f0b94bb249bb837bad08f0ca3bcbc9e4a8272..a6b3fb6faa958f1e8f88898c3e916821d6162387 100644
--- a/courgette/rel32_finder_win32_x86_unittest.cc
+++ b/courgette/rel32_finder_unittest.cc
@@ -2,8 +2,6 @@
// 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>
@@ -14,16 +12,18 @@
#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),
@@ -32,17 +32,30 @@ class Rel32FinderWin32X86TestCase {
LoadTestFromString(test_data);
}
+ template <class Finder>
huangs 2016/05/30 05:48:34 Pretty cool use of template. :) In this instance
etiennep 2016/05/30 17:07:30 Done.
void RunTestBasic(std::string name) {
- Rel32FinderWin32X86_Basic finder(relocs_start_rva_, relocs_end_rva_);
+ Finder finder = CreateFinder<Finder>();
ASSERT_FALSE(text_data_.empty());
finder.Find(&text_data_[0], &text_data_[0] + text_data_.size(),
- text_start_rva_, text_end_rva_, abs32_locations_);
+ text_start_rva_, text_end_rva_, abs32_locations_);
std::vector<RVA> rel32_locations;
finder.SwapRel32Locations(&rel32_locations);
EXPECT_EQ(expected_rel32_locations_, rel32_locations)
<< "From test case " << name << " (addresses are in hex)";
}
+ template <class Finder>
+ Finder CreateFinder();
+
+ template <>
+ Rel32FinderX86 CreateFinder<Rel32FinderX86>() {
+ return Rel32FinderX86(relocs_start_rva_, relocs_end_rva_);
+ }
+ template <>
+ Rel32FinderX64 CreateFinder<Rel32FinderX64>() {
+ return Rel32FinderX64(relocs_start_rva_, relocs_end_rva_, image_end_rva_);
+ }
+
private:
RVA text_start_rva_;
RVA text_end_rva_;
@@ -83,7 +96,7 @@ 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);
@@ -107,7 +120,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));
}
@@ -131,19 +144,24 @@ class Rel32FinderWin32X86TestCase {
}
};
-class Rel32FinderWin32X86Test : public BaseTest {
+class Rel32FinderTest : public BaseTest {
public:
+ template <class Finder>
void RunTest(const char* test_case_file) {
- Rel32FinderWin32X86TestCase test_case(FileContents(test_case_file));
- test_case.RunTestBasic(test_case_file);
+ Rel32FinderTestCase test_case(FileContents(test_case_file));
+ test_case.RunTestBasic<Finder>(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<Rel32FinderX86>("rel32_x86_01.txt");
+ RunTest<Rel32FinderX86>("rel32_x86_02.txt");
+ RunTest<Rel32FinderX86>("rel32_x86_03.txt");
+ RunTest<Rel32FinderX86>("rel32_x86_04.txt");
+
+ RunTest<Rel32FinderX64>("rel32_x64_01.txt");
+ RunTest<Rel32FinderX64>("rel32_x64_02.txt");
+ RunTest<Rel32FinderX64>("rel32_x64_03.txt");
}
huangs 2016/05/30 05:48:34 Curious why there's no rel32_x64_04.txt?
etiennep 2016/05/30 17:07:30 I don't think testing false positive is relevant.
} // namespace

Powered by Google App Engine
This is Rietveld 408576698