1 // Copyright 2016 The Chromium Authors
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_x86.h"
13 #include "courgette/disassembler_win32_x64.h"
14 #include "courgette/disassembler_win32_x86.h"
20 class ProgramDetectorTest : public BaseTest {
22 void TestQuickDetect(const std::string& test_data,
23 ExecutableType expected_type) const;
24 void TestDetectDisassembler(const std::string& test_data,
25 ExecutableType expected_type) const;
28 void ProgramDetectorTest::TestQuickDetect(const std::string& test_data,
29 ExecutableType expected_type) const {
30 // QuickDetect() should return true only for the |expected_type|.
31 EXPECT_EQ(expected_type == EXE_WIN_32_X86,
32 DisassemblerWin32X86::QuickDetect(
33 reinterpret_cast<const uint8_t*>(test_data.data()),
35 EXPECT_EQ(expected_type == EXE_WIN_32_X64,
36 DisassemblerWin32X64::QuickDetect(
37 reinterpret_cast<const uint8_t*>(test_data.data()),
39 EXPECT_EQ(expected_type == EXE_ELF_32_X86,
40 DisassemblerElf32X86::QuickDetect(
41 reinterpret_cast<const uint8_t*>(test_data.data()),
45 void ProgramDetectorTest::TestDetectDisassembler(
46 const std::string& test_data,
47 ExecutableType expected_type) const {
48 ExecutableType detected_type = EXE_UNKNOWN;
49 size_t detected_length = 0;
50 DetectExecutableType(reinterpret_cast<const uint8_t*>(test_data.data()),
51 test_data.size(), &detected_type, &detected_length);
52 EXPECT_EQ(expected_type, detected_type);
53 EXPECT_EQ(test_data.size(), detected_length);
56 TEST_F(ProgramDetectorTest, All) {
57 std::string win32_x86 = FileContents("setup1.exe");
58 std::string win32_x64 = FileContents("chrome64_1.exe");
59 std::string elf_32 = FileContents("elf-32-1");
61 TestQuickDetect(win32_x86, EXE_WIN_32_X86);
62 TestQuickDetect(win32_x64, EXE_WIN_32_X64);
63 TestQuickDetect(elf_32, EXE_ELF_32_X86);
65 TestDetectDisassembler(win32_x86, EXE_WIN_32_X86);
66 TestDetectDisassembler(win32_x64, EXE_WIN_32_X64);
67 TestDetectDisassembler(elf_32, EXE_ELF_32_X86);
72 } // namespace courgette