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