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