OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "courgette/program_detector.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 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" |
| 16 |
| 17 namespace courgette { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class ProgramDetectorTest : public BaseTest { |
| 22 public: |
| 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; |
| 27 }; |
| 28 |
| 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()), |
| 35 test_data.size())); |
| 36 EXPECT_EQ(expected_type == EXE_WIN_32_X64, |
| 37 DisassemblerWin32X64::QuickDetect( |
| 38 reinterpret_cast<const uint8_t*>(test_data.data()), |
| 39 test_data.size())); |
| 40 EXPECT_EQ(expected_type == EXE_ELF_32_X86, |
| 41 DisassemblerElf32X86::QuickDetect( |
| 42 reinterpret_cast<const uint8_t*>(test_data.data()), |
| 43 test_data.size())); |
| 44 EXPECT_EQ(expected_type == EXE_ELF_32_ARM, |
| 45 DisassemblerElf32ARM::QuickDetect( |
| 46 reinterpret_cast<const uint8_t*>(test_data.data()), |
| 47 test_data.size())); |
| 48 } |
| 49 |
| 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(test_data.data(), test_data.size(), &detected_type, |
| 56 &detected_length); |
| 57 EXPECT_EQ(expected_type, detected_type); |
| 58 EXPECT_EQ(test_data.size(), detected_length); |
| 59 } |
| 60 |
| 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"); |
| 66 |
| 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); |
| 71 |
| 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); |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 |
| 80 } // namespace courgette |
OLD | NEW |