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