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