1 // Copyright 2013 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_x64.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "courgette/base_test_unittest.h"
9
10 class DisassemblerWin32X64Test : public BaseTest {
11 public:
12 void TestExe() const;
13 void TestExe32() const;
14 void TestResourceDll() const;
15 };
16
TestExe() const17 void DisassemblerWin32X64Test::TestExe() const {
18 std::string file1 = FileContents("chrome64_1.exe");
19
20 scoped_ptr<courgette::DisassemblerWin32X64> disassembler(
21 new courgette::DisassemblerWin32X64(file1.c_str(), file1.length()));
22
23 bool can_parse_header = disassembler->ParseHeader();
24 EXPECT_TRUE(can_parse_header);
25
26 // The executable is the whole file, not 'embedded' with the file
27 EXPECT_EQ(file1.length(), disassembler->length());
28
29 EXPECT_TRUE(disassembler->ok());
30 EXPECT_TRUE(disassembler->has_text_section());
31 EXPECT_EQ(488448U, disassembler->size_of_code());
32 EXPECT_FALSE(disassembler->is_32bit());
33 EXPECT_EQ(courgette::DisassemblerWin32X64::SectionName(
34 disassembler->RVAToSection(0x00401234 - 0x00400000)),
35 std::string(".text"));
36
37 EXPECT_EQ(0, disassembler->RVAToFileOffset(0));
38 EXPECT_EQ(1024, disassembler->RVAToFileOffset(4096));
39 EXPECT_EQ(46928, disassembler->RVAToFileOffset(50000));
40
41 std::vector<courgette::RVA> relocs;
42 bool can_parse_relocs = disassembler->ParseRelocs(&relocs);
43 EXPECT_TRUE(can_parse_relocs);
44
45 const uint8* offset_p = disassembler->OffsetToPointer(0);
46 EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()),
47 reinterpret_cast<const void*>(offset_p));
48 EXPECT_EQ('M', offset_p[0]);
49 EXPECT_EQ('Z', offset_p[1]);
50
51 const uint8* rva_p = disassembler->RVAToPointer(0);
52 EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()),
53 reinterpret_cast<const void*>(rva_p));
54 EXPECT_EQ('M', rva_p[0]);
55 EXPECT_EQ('Z', rva_p[1]);
56 }
57
TestExe32() const58 void DisassemblerWin32X64Test::TestExe32() const {
59 std::string file1 = FileContents("setup1.exe");
60
61 scoped_ptr<courgette::DisassemblerWin32X64> disassembler(
62 new courgette::DisassemblerWin32X64(file1.c_str(), file1.length()));
63
64 bool can_parse_header = disassembler->ParseHeader();
65 EXPECT_FALSE(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 EXPECT_FALSE(disassembler->ok());
71 EXPECT_TRUE(disassembler->has_text_section());
72 EXPECT_EQ(449536U, disassembler->size_of_code());
73 EXPECT_TRUE(disassembler->is_32bit());
74 }
75
TestResourceDll() const76 void DisassemblerWin32X64Test::TestResourceDll() const {
77 std::string file1 = FileContents("en-US-64.dll");
78
79 scoped_ptr<courgette::DisassemblerWin32X64> disassembler(
80 new courgette::DisassemblerWin32X64(file1.c_str(), file1.length()));
81
82 bool can_parse_header = disassembler->ParseHeader();
83 EXPECT_FALSE(can_parse_header);
84
85 // The executable is the whole file, not 'embedded' with the file
86 EXPECT_EQ(file1.length(), disassembler->length());
87
88 EXPECT_FALSE(disassembler->ok());
89 EXPECT_FALSE(disassembler->has_text_section());
90 EXPECT_EQ(0U, disassembler->size_of_code());
91 EXPECT_FALSE(disassembler->is_32bit());
92 }
93
TEST_F(DisassemblerWin32X64Test,All)94 TEST_F(DisassemblerWin32X64Test, All) {
95 TestExe();
96 TestExe32();
97 TestResourceDll();
98 }
99