OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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/image_info.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/path_service.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/string_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 class ImageInfoTest : public testing::Test { |
| 15 public: |
| 16 |
| 17 void TestExe() const; |
| 18 void TestResourceDll() const; |
| 19 |
| 20 private: |
| 21 void SetUp() { |
| 22 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir_); |
| 23 file_util::AppendToPath(&test_dir_, L"courgette"); |
| 24 file_util::AppendToPath(&test_dir_, L"testdata"); |
| 25 } |
| 26 |
| 27 void TearDown() { |
| 28 } |
| 29 |
| 30 void ExpectExecutable(courgette::PEInfo *info) const; |
| 31 |
| 32 std::string FileContents(const char *file_name) const; |
| 33 |
| 34 std::wstring test_dir_; |
| 35 }; |
| 36 |
| 37 // Reads a test file into a string. |
| 38 std::string ImageInfoTest::FileContents(const char *file_name) const { |
| 39 std::wstring file_path = test_dir_; |
| 40 file_util::AppendToPath(&file_path, UTF8ToWide(file_name)); |
| 41 std::string file_bytes; |
| 42 if (!file_util::ReadFileToString(file_path, &file_bytes)) { |
| 43 EXPECT_TRUE(!"Could not read test data"); |
| 44 } |
| 45 return file_bytes; |
| 46 } |
| 47 |
| 48 void ImageInfoTest::ExpectExecutable(courgette::PEInfo *info) const { |
| 49 EXPECT_TRUE(info->ok()); |
| 50 EXPECT_TRUE(info->has_text_section()); |
| 51 } |
| 52 |
| 53 void ImageInfoTest::TestExe() const { |
| 54 std::string file1 = FileContents("setup1.exe"); |
| 55 |
| 56 courgette::PEInfo *info = new courgette::PEInfo(); |
| 57 info->Init(reinterpret_cast<const uint8*>(file1.c_str()), file1.length()); |
| 58 |
| 59 bool can_parse_header = info->ParseHeader(); |
| 60 EXPECT_TRUE(can_parse_header); |
| 61 |
| 62 // The executable is the whole file, not 'embedded' with the file |
| 63 EXPECT_EQ(file1.length(), info->length()); |
| 64 |
| 65 ExpectExecutable(info); |
| 66 EXPECT_EQ(449536, info->size_of_code()); |
| 67 EXPECT_EQ(SectionName(info->RVAToSection(0x00401234 - 0x00400000)), |
| 68 std::string(".text")); |
| 69 |
| 70 EXPECT_EQ(0, info->RVAToFileOffset(0)); |
| 71 EXPECT_EQ(1024, info->RVAToFileOffset(4096)); |
| 72 EXPECT_EQ(46928, info->RVAToFileOffset(50000)); |
| 73 |
| 74 std::vector<courgette::RVA> relocs; |
| 75 bool can_parse_relocs = info->ParseRelocs(&relocs); |
| 76 EXPECT_TRUE(can_parse_relocs); |
| 77 |
| 78 const uint8* p = info->RVAToPointer(0); |
| 79 EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()), |
| 80 reinterpret_cast<const void*>(p)); |
| 81 EXPECT_EQ('M', p[0]); |
| 82 EXPECT_EQ('Z', p[1]); |
| 83 } |
| 84 |
| 85 void ImageInfoTest::TestResourceDll() const { |
| 86 std::string file1 = FileContents("en-US.dll"); |
| 87 |
| 88 courgette::PEInfo *info = new courgette::PEInfo(); |
| 89 info->Init(reinterpret_cast<const uint8*>(file1.c_str()), file1.length()); |
| 90 |
| 91 bool can_parse_header = info->ParseHeader(); |
| 92 EXPECT_TRUE(can_parse_header); |
| 93 |
| 94 // The executable is the whole file, not 'embedded' with the file |
| 95 EXPECT_EQ(file1.length(), info->length()); |
| 96 |
| 97 EXPECT_TRUE(info->ok()); |
| 98 EXPECT_FALSE(info->has_text_section()); |
| 99 EXPECT_EQ(0u, info->size_of_code()); |
| 100 } |
| 101 |
| 102 TEST_F(ImageInfoTest, All) { |
| 103 TestExe(); |
| 104 TestResourceDll(); |
| 105 } |
OLD | NEW |