| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/memory/scoped_ptr.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 class ImageInfoTest : public testing::Test { | |
| 16 public: | |
| 17 | |
| 18 void TestExe() const; | |
| 19 void TestResourceDll() const; | |
| 20 | |
| 21 private: | |
| 22 void SetUp() { | |
| 23 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir_); | |
| 24 test_dir_ = test_dir_.AppendASCII("courgette"); | |
| 25 test_dir_ = test_dir_.AppendASCII("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 FilePath test_dir_; | |
| 36 }; | |
| 37 | |
| 38 // Reads a test file into a string. | |
| 39 std::string ImageInfoTest::FileContents(const char* file_name) const { | |
| 40 FilePath file_path = test_dir_; | |
| 41 file_path = file_path.AppendASCII(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 scoped_ptr<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.get()); | |
| 67 EXPECT_EQ(449536U, 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 scoped_ptr<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 |