1 // Copyright 2016 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.
5 #include "courgette/program_detector.h"
9 #include "courgette/base_test_unittest.h"
10 #include "courgette/courgette.h"
11 #include "courgette/disassembler.h"
12 #include "courgette/disassembler_elf_32_arm.h"
13 #include "courgette/disassembler_elf_32_x86.h"
14 #include "courgette/disassembler_win32_x64.h"
15 #include "courgette/disassembler_win32_x86.h"
21 class ProgramDetectorTest : public BaseTest {
23 void TestQuickDetect(const std::string& test_data,
24 ExecutableType expected_type) const;
25 void TestDetectDisassembler(const std::string& test_data,
26 ExecutableType expected_type) const;
29 void ProgramDetectorTest::TestQuickDetect(const std::string& test_data,
30 ExecutableType expected_type) const {
31 // QuickDetect() should return true only for the |expected_type|.
32 EXPECT_EQ(expected_type == EXE_WIN_32_X86,
33 DisassemblerWin32X86::QuickDetect(
34 reinterpret_cast<const uint8_t*>(test_data.data()),
36 EXPECT_EQ(expected_type == EXE_WIN_32_X64,
37 DisassemblerWin32X64::QuickDetect(
38 reinterpret_cast<const uint8_t*>(test_data.data()),
40 EXPECT_EQ(expected_type == EXE_ELF_32_X86,
41 DisassemblerElf32X86::QuickDetect(
42 reinterpret_cast<const uint8_t*>(test_data.data()),
44 EXPECT_EQ(expected_type == EXE_ELF_32_ARM,
45 DisassemblerElf32ARM::QuickDetect(
46 reinterpret_cast<const uint8_t*>(test_data.data()),
50 void ProgramDetectorTest::TestDetectDisassembler(
51 const std::string& test_data,
52 ExecutableType expected_type) const {
53 ExecutableType detected_type = EXE_UNKNOWN;
54 size_t detected_length = 0;
55 DetectExecutableType(reinterpret_cast<const uint8_t*>(test_data.data()),
56 test_data.size(), &detected_type, &detected_length);
57 EXPECT_EQ(expected_type, detected_type);
58 EXPECT_EQ(test_data.size(), detected_length);
61 TEST_F(ProgramDetectorTest, All) {
62 std::string win32_x86 = FileContents("setup1.exe");
63 std::string win32_x64 = FileContents("chrome64_1.exe");
64 std::string elf_32 = FileContents("elf-32-1");
65 std::string elf_arm = FileContents("elf-armv7");
67 TestQuickDetect(win32_x86, EXE_WIN_32_X86);
68 TestQuickDetect(win32_x64, EXE_WIN_32_X64);
69 TestQuickDetect(elf_32, EXE_ELF_32_X86);
70 TestQuickDetect(elf_arm, EXE_ELF_32_ARM);
72 TestDetectDisassembler(win32_x86, EXE_WIN_32_X86);
73 TestDetectDisassembler(win32_x64, EXE_WIN_32_X64);
74 TestDetectDisassembler(elf_32, EXE_ELF_32_X86);
75 TestDetectDisassembler(elf_arm, EXE_ELF_32_ARM);
80 } // namespace courgette