program_detector_unittest.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "courgette/program_detector.h"
  5. #include <string>
  6. #include "courgette/base_test_unittest.h"
  7. #include "courgette/courgette.h"
  8. #include "courgette/disassembler.h"
  9. #include "courgette/disassembler_elf_32_x86.h"
  10. #include "courgette/disassembler_win32_x64.h"
  11. #include "courgette/disassembler_win32_x86.h"
  12. namespace courgette {
  13. namespace {
  14. class ProgramDetectorTest : public BaseTest {
  15. public:
  16. void TestQuickDetect(const std::string& test_data,
  17. ExecutableType expected_type) const;
  18. void TestDetectDisassembler(const std::string& test_data,
  19. ExecutableType expected_type) const;
  20. };
  21. void ProgramDetectorTest::TestQuickDetect(const std::string& test_data,
  22. ExecutableType expected_type) const {
  23. // QuickDetect() should return true only for the |expected_type|.
  24. EXPECT_EQ(expected_type == EXE_WIN_32_X86,
  25. DisassemblerWin32X86::QuickDetect(
  26. reinterpret_cast<const uint8_t*>(test_data.data()),
  27. test_data.size()));
  28. EXPECT_EQ(expected_type == EXE_WIN_32_X64,
  29. DisassemblerWin32X64::QuickDetect(
  30. reinterpret_cast<const uint8_t*>(test_data.data()),
  31. test_data.size()));
  32. EXPECT_EQ(expected_type == EXE_ELF_32_X86,
  33. DisassemblerElf32X86::QuickDetect(
  34. reinterpret_cast<const uint8_t*>(test_data.data()),
  35. test_data.size()));
  36. }
  37. void ProgramDetectorTest::TestDetectDisassembler(
  38. const std::string& test_data,
  39. ExecutableType expected_type) const {
  40. ExecutableType detected_type = EXE_UNKNOWN;
  41. size_t detected_length = 0;
  42. DetectExecutableType(reinterpret_cast<const uint8_t*>(test_data.data()),
  43. test_data.size(), &detected_type, &detected_length);
  44. EXPECT_EQ(expected_type, detected_type);
  45. EXPECT_EQ(test_data.size(), detected_length);
  46. }
  47. TEST_F(ProgramDetectorTest, All) {
  48. std::string win32_x86 = FileContents("setup1.exe");
  49. std::string win32_x64 = FileContents("chrome64_1.exe");
  50. std::string elf_32 = FileContents("elf-32-1");
  51. TestQuickDetect(win32_x86, EXE_WIN_32_X86);
  52. TestQuickDetect(win32_x64, EXE_WIN_32_X64);
  53. TestQuickDetect(elf_32, EXE_ELF_32_X86);
  54. TestDetectDisassembler(win32_x86, EXE_WIN_32_X86);
  55. TestDetectDisassembler(win32_x64, EXE_WIN_32_X64);
  56. TestDetectDisassembler(elf_32, EXE_ELF_32_X86);
  57. }
  58. } // namespace
  59. } // namespace courgette