| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "courgette/disassembler_win32_x86.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/path_service.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/string_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 class DisassemblerWin32X86Test : public testing::Test { |
| 16 public: |
| 17 |
| 18 void TestExe() const; |
| 19 void TestResourceDll() const; |
| 20 |
| 21 private: |
| 22 void SetUp() { |
| 23 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir_); |
| 24 test_dir_ = test_dir_.AppendASCII("courgette"); |
| 25 test_dir_ = test_dir_.AppendASCII("testdata"); |
| 26 } |
| 27 |
| 28 void TearDown() { |
| 29 } |
| 30 |
| 31 void ExpectExecutable(courgette::DisassemblerWin32X86* disassembler) const; |
| 32 |
| 33 std::string FileContents(const char* file_name) const; |
| 34 |
| 35 FilePath test_dir_; |
| 36 }; |
| 37 |
| 38 // Reads a test file into a string. |
| 39 std::string DisassemblerWin32X86Test::FileContents( |
| 40 const char* file_name) const { |
| 41 |
| 42 FilePath file_path = test_dir_; |
| 43 file_path = file_path.AppendASCII(file_name); |
| 44 std::string file_bytes; |
| 45 if (!file_util::ReadFileToString(file_path, &file_bytes)) { |
| 46 EXPECT_TRUE(!"Could not read test data"); |
| 47 } |
| 48 return file_bytes; |
| 49 } |
| 50 |
| 51 void DisassemblerWin32X86Test::ExpectExecutable( |
| 52 courgette::DisassemblerWin32X86* disassembler) const { |
| 53 |
| 54 EXPECT_TRUE(disassembler->ok()); |
| 55 EXPECT_TRUE(disassembler->has_text_section()); |
| 56 } |
| 57 |
| 58 void DisassemblerWin32X86Test::TestExe() const { |
| 59 std::string file1 = FileContents("setup1.exe"); |
| 60 |
| 61 scoped_ptr<courgette::DisassemblerWin32X86> disassembler( |
| 62 new courgette::DisassemblerWin32X86(file1.c_str(), file1.length())); |
| 63 |
| 64 bool can_parse_header = disassembler->ParseHeader(); |
| 65 EXPECT_TRUE(can_parse_header); |
| 66 |
| 67 // The executable is the whole file, not 'embedded' with the file |
| 68 EXPECT_EQ(file1.length(), disassembler->length()); |
| 69 |
| 70 ExpectExecutable(disassembler.get()); |
| 71 EXPECT_EQ(449536U, disassembler->size_of_code()); |
| 72 EXPECT_EQ(courgette::DisassemblerWin32X86::SectionName( |
| 73 disassembler->RVAToSection(0x00401234 - 0x00400000)), |
| 74 std::string(".text")); |
| 75 |
| 76 EXPECT_EQ(0, disassembler->RVAToFileOffset(0)); |
| 77 EXPECT_EQ(1024, disassembler->RVAToFileOffset(4096)); |
| 78 EXPECT_EQ(46928, disassembler->RVAToFileOffset(50000)); |
| 79 |
| 80 std::vector<courgette::RVA> relocs; |
| 81 bool can_parse_relocs = disassembler->ParseRelocs(&relocs); |
| 82 EXPECT_TRUE(can_parse_relocs); |
| 83 |
| 84 const uint8* offset_p = disassembler->OffsetToPointer(0); |
| 85 EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()), |
| 86 reinterpret_cast<const void*>(offset_p)); |
| 87 EXPECT_EQ('M', offset_p[0]); |
| 88 EXPECT_EQ('Z', offset_p[1]); |
| 89 |
| 90 const uint8* rva_p = disassembler->RVAToPointer(0); |
| 91 EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()), |
| 92 reinterpret_cast<const void*>(rva_p)); |
| 93 EXPECT_EQ('M', rva_p[0]); |
| 94 EXPECT_EQ('Z', rva_p[1]); |
| 95 } |
| 96 |
| 97 void DisassemblerWin32X86Test::TestResourceDll() const { |
| 98 std::string file1 = FileContents("en-US.dll"); |
| 99 |
| 100 scoped_ptr<courgette::DisassemblerWin32X86> disassembler( |
| 101 new courgette::DisassemblerWin32X86(file1.c_str(), file1.length())); |
| 102 |
| 103 bool can_parse_header = disassembler->ParseHeader(); |
| 104 EXPECT_TRUE(can_parse_header); |
| 105 |
| 106 // The executable is the whole file, not 'embedded' with the file |
| 107 EXPECT_EQ(file1.length(), disassembler->length()); |
| 108 |
| 109 EXPECT_TRUE(disassembler->ok()); |
| 110 EXPECT_FALSE(disassembler->has_text_section()); |
| 111 EXPECT_EQ(0U, disassembler->size_of_code()); |
| 112 } |
| 113 |
| 114 TEST_F(DisassemblerWin32X86Test, All) { |
| 115 TestExe(); |
| 116 TestResourceDll(); |
| 117 } |
| OLD | NEW |