fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 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 "base/file_version_info_win.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | |
| 9 | #include <stddef.h> |
| 10 | |
| 11 | #include <memory> |
| 12 | |
| 13 | #include "base/file_version_info.h" |
| 14 | #include "base/files/file_path.h" |
| 15 | #include "base/macros.h" |
| 16 | #include "base/memory/ptr_util.h" |
| 17 | #include "base/path_service.h" |
| 18 | #include "base/scoped_native_library.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
| 21 | using base::FilePath; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | FilePath GetTestDataPath() { |
| 26 | FilePath path; |
| 27 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 28 | path = path.AppendASCII("base"); |
| 29 | path = path.AppendASCII("test"); |
| 30 | path = path.AppendASCII("data"); |
| 31 | path = path.AppendASCII("file_version_info_unittest"); |
| 32 | return path; |
| 33 | } |
| 34 | |
| 35 | class FileVersionInfoFactory { |
| 36 | public: |
| 37 | explicit FileVersionInfoFactory(const FilePath& path) : path_(path) {} |
| 38 | |
| 39 | std::unique_ptr<FileVersionInfo> Create() const { |
| 40 | return base::WrapUnique(FileVersionInfo::CreateFileVersionInfo(path_)); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | const FilePath path_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(FileVersionInfoFactory); |
| 47 | }; |
| 48 | |
| 49 | class FileVersionInfoForModuleFactory { |
| 50 | public: |
| 51 | explicit FileVersionInfoForModuleFactory(const FilePath& path) |
| 52 | // Load the library with LOAD_LIBRARY_AS_IMAGE_RESOURCE since it shouldn't |
| 53 | // be executed. |
| 54 | : library_(::LoadLibraryEx(path.value().c_str(), |
| 55 | nullptr, |
| 56 | LOAD_LIBRARY_AS_IMAGE_RESOURCE)) { |
| 57 | EXPECT_TRUE(library_.is_valid()); |
| 58 | } |
| 59 | |
| 60 | std::unique_ptr<FileVersionInfo> Create() const { |
| 61 | return base::WrapUnique( |
| 62 | FileVersionInfo::CreateFileVersionInfoForModule(library_.get())); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | const base::ScopedNativeLibrary library_; |
| 67 | |
| 68 | DISALLOW_COPY_AND_ASSIGN(FileVersionInfoForModuleFactory); |
| 69 | }; |
| 70 | |
| 71 | template <typename T> |
| 72 | class FileVersionInfoTest : public testing::Test {}; |
| 73 | |
| 74 | using FileVersionInfoFactories = |
| 75 | ::testing::Types<FileVersionInfoFactory, FileVersionInfoForModuleFactory>; |
| 76 | |
| 77 | } // namespace |
| 78 | |
| 79 | TYPED_TEST_CASE(FileVersionInfoTest, FileVersionInfoFactories); |
| 80 | |
| 81 | TYPED_TEST(FileVersionInfoTest, HardCodedProperties) { |
| 82 | const wchar_t kDLLName[] = {L"FileVersionInfoTest1.dll"}; |
| 83 | |
| 84 | const wchar_t* const kExpectedValues[15] = { |
| 85 | // FileVersionInfoTest.dll |
| 86 | L"Goooooogle", // company_name |
| 87 | L"Google", // company_short_name |
| 88 | L"This is the product name", // product_name |
| 89 | L"This is the product short name", // product_short_name |
| 90 | L"The Internal Name", // internal_name |
| 91 | L"4.3.2.1", // product_version |
| 92 | L"Private build property", // private_build |
| 93 | L"Special build property", // special_build |
| 94 | L"This is a particularly interesting comment", // comments |
| 95 | L"This is the original filename", // original_filename |
| 96 | L"This is my file description", // file_description |
| 97 | L"1.2.3.4", // file_version |
| 98 | L"This is the legal copyright", // legal_copyright |
| 99 | L"This is the legal trademarks", // legal_trademarks |
| 100 | L"This is the last change", // last_change |
| 101 | }; |
| 102 | |
| 103 | FilePath dll_path = GetTestDataPath(); |
| 104 | dll_path = dll_path.Append(kDLLName); |
| 105 | |
| 106 | TypeParam factory(dll_path); |
| 107 | std::unique_ptr<FileVersionInfo> version_info(factory.Create()); |
| 108 | ASSERT_TRUE(version_info); |
| 109 | |
| 110 | int j = 0; |
| 111 | EXPECT_EQ(kExpectedValues[j++], version_info->company_name()); |
| 112 | EXPECT_EQ(kExpectedValues[j++], version_info->company_short_name()); |
| 113 | EXPECT_EQ(kExpectedValues[j++], version_info->product_name()); |
| 114 | EXPECT_EQ(kExpectedValues[j++], version_info->product_short_name()); |
| 115 | EXPECT_EQ(kExpectedValues[j++], version_info->internal_name()); |
| 116 | EXPECT_EQ(kExpectedValues[j++], version_info->product_version()); |
| 117 | EXPECT_EQ(kExpectedValues[j++], version_info->private_build()); |
| 118 | EXPECT_EQ(kExpectedValues[j++], version_info->special_build()); |
| 119 | EXPECT_EQ(kExpectedValues[j++], version_info->comments()); |
| 120 | EXPECT_EQ(kExpectedValues[j++], version_info->original_filename()); |
| 121 | EXPECT_EQ(kExpectedValues[j++], version_info->file_description()); |
| 122 | EXPECT_EQ(kExpectedValues[j++], version_info->file_version()); |
| 123 | EXPECT_EQ(kExpectedValues[j++], version_info->legal_copyright()); |
| 124 | EXPECT_EQ(kExpectedValues[j++], version_info->legal_trademarks()); |
| 125 | EXPECT_EQ(kExpectedValues[j++], version_info->last_change()); |
| 126 | } |
| 127 | |
| 128 | TYPED_TEST(FileVersionInfoTest, IsOfficialBuild) { |
| 129 | constexpr struct { |
| 130 | const wchar_t* const dll_name; |
| 131 | const bool is_official_build; |
| 132 | } kTestItems[]{ |
| 133 | {L"FileVersionInfoTest1.dll", true}, {L"FileVersionInfoTest2.dll", false}, |
| 134 | }; |
| 135 | |
| 136 | for (const auto& test_item : kTestItems) { |
| 137 | const FilePath dll_path = GetTestDataPath().Append(test_item.dll_name); |
| 138 | |
| 139 | TypeParam factory(dll_path); |
| 140 | std::unique_ptr<FileVersionInfo> version_info(factory.Create()); |
| 141 | ASSERT_TRUE(version_info); |
| 142 | |
| 143 | EXPECT_EQ(test_item.is_official_build, version_info->is_official_build()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | TYPED_TEST(FileVersionInfoTest, CustomProperties) { |
| 148 | FilePath dll_path = GetTestDataPath(); |
| 149 | dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll"); |
| 150 | |
| 151 | TypeParam factory(dll_path); |
| 152 | std::unique_ptr<FileVersionInfo> version_info(factory.Create()); |
| 153 | ASSERT_TRUE(version_info); |
| 154 | |
| 155 | // Test few existing properties. |
| 156 | std::wstring str; |
| 157 | FileVersionInfoWin* version_info_win = |
| 158 | static_cast<FileVersionInfoWin*>(version_info.get()); |
| 159 | EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 1", &str)); |
| 160 | EXPECT_EQ(L"Un", str); |
| 161 | EXPECT_EQ(L"Un", version_info_win->GetStringValue(L"Custom prop 1")); |
| 162 | |
| 163 | EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 2", &str)); |
| 164 | EXPECT_EQ(L"Deux", str); |
| 165 | EXPECT_EQ(L"Deux", version_info_win->GetStringValue(L"Custom prop 2")); |
| 166 | |
| 167 | EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 3", &str)); |
| 168 | EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", str); |
| 169 | EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", |
| 170 | version_info_win->GetStringValue(L"Custom prop 3")); |
| 171 | |
| 172 | // Test an non-existing property. |
| 173 | EXPECT_FALSE(version_info_win->GetValue(L"Unknown property", &str)); |
| 174 | EXPECT_EQ(L"", version_info_win->GetStringValue(L"Unknown property")); |
| 175 | } |