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.
5 #include "base/file_version_info_win.h"
13 #include "base/file_version_info.h"
14 #include "base/files/file_path.h"
15 #include "base/macros.h"
16 #include "base/path_service.h"
17 #include "base/scoped_native_library.h"
18 #include "base/strings/string_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 FilePath GetTestDataPath() {
27 base::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");
35 class FileVersionInfoFactory {
37 explicit FileVersionInfoFactory(const FilePath& path) : path_(path) {}
39 std::unique_ptr<FileVersionInfo> Create() const {
40 return FileVersionInfo::CreateFileVersionInfo(path_);
46 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoFactory);
49 class FileVersionInfoForModuleFactory {
51 explicit FileVersionInfoForModuleFactory(const FilePath& path)
52 // Load the library with LOAD_LIBRARY_AS_IMAGE_RESOURCE since it shouldn't
54 : library_(::LoadLibraryEx(path.value().c_str(),
56 LOAD_LIBRARY_AS_IMAGE_RESOURCE)) {
57 EXPECT_TRUE(library_.is_valid());
60 std::unique_ptr<FileVersionInfo> Create() const {
61 return FileVersionInfo::CreateFileVersionInfoForModule(library_.get());
65 const base::ScopedNativeLibrary library_;
67 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoForModuleFactory);
71 class FileVersionInfoTest : public testing::Test {};
73 using FileVersionInfoFactories =
74 ::testing::Types<FileVersionInfoFactory, FileVersionInfoForModuleFactory>;
78 TYPED_TEST_SUITE(FileVersionInfoTest, FileVersionInfoFactories);
80 TYPED_TEST(FileVersionInfoTest, HardCodedProperties) {
81 const base::FilePath::CharType kDLLName[] =
82 FILE_PATH_LITERAL("FileVersionInfoTest1.dll");
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"Special build property", // special_build
93 L"This is the original filename", // original_filename
94 L"This is my file description", // file_description
95 L"1.2.3.4", // file_version
98 FilePath dll_path = GetTestDataPath();
99 dll_path = dll_path.Append(kDLLName);
101 TypeParam factory(dll_path);
102 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
103 ASSERT_TRUE(version_info);
106 EXPECT_EQ(kExpectedValues[j++],
107 base::AsWStringPiece(version_info->company_name()));
108 EXPECT_EQ(kExpectedValues[j++],
109 base::AsWStringPiece(version_info->company_short_name()));
110 EXPECT_EQ(kExpectedValues[j++],
111 base::AsWStringPiece(version_info->product_name()));
112 EXPECT_EQ(kExpectedValues[j++],
113 base::AsWStringPiece(version_info->product_short_name()));
114 EXPECT_EQ(kExpectedValues[j++],
115 base::AsWStringPiece(version_info->internal_name()));
116 EXPECT_EQ(kExpectedValues[j++],
117 base::AsWStringPiece(version_info->product_version()));
118 EXPECT_EQ(kExpectedValues[j++],
119 base::AsWStringPiece(version_info->special_build()));
120 EXPECT_EQ(kExpectedValues[j++],
121 base::AsWStringPiece(version_info->original_filename()));
122 EXPECT_EQ(kExpectedValues[j++],
123 base::AsWStringPiece(version_info->file_description()));
124 EXPECT_EQ(kExpectedValues[j++],
125 base::AsWStringPiece(version_info->file_version()));
128 TYPED_TEST(FileVersionInfoTest, CustomProperties) {
129 FilePath dll_path = GetTestDataPath();
130 dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll");
132 TypeParam factory(dll_path);
133 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
134 ASSERT_TRUE(version_info);
136 // Test few existing properties.
138 FileVersionInfoWin* version_info_win =
139 static_cast<FileVersionInfoWin*>(version_info.get());
141 version_info_win->GetValue(STRING16_LITERAL("Custom prop 1"), &str));
142 EXPECT_EQ(STRING16_LITERAL("Un"), str);
143 EXPECT_EQ(STRING16_LITERAL("Un"), version_info_win->GetStringValue(
144 STRING16_LITERAL("Custom prop 1")));
147 version_info_win->GetValue(STRING16_LITERAL("Custom prop 2"), &str));
148 EXPECT_EQ(STRING16_LITERAL("Deux"), str);
149 EXPECT_EQ(STRING16_LITERAL("Deux"), version_info_win->GetStringValue(
150 STRING16_LITERAL("Custom prop 2")));
153 version_info_win->GetValue(STRING16_LITERAL("Custom prop 3"), &str));
155 STRING16_LITERAL("1600 Amphitheatre Parkway Mountain View, CA 94043"),
158 STRING16_LITERAL("1600 Amphitheatre Parkway Mountain View, CA 94043"),
159 version_info_win->GetStringValue(STRING16_LITERAL("Custom prop 3")));
161 // Test an non-existing property.
163 version_info_win->GetValue(STRING16_LITERAL("Unknown property"), &str));
164 EXPECT_EQ(base::string16(), version_info_win->GetStringValue(
165 STRING16_LITERAL("Unknown property")));
167 EXPECT_EQ(base::Version(std::vector<uint32_t>{1, 0, 0, 1}),
168 version_info_win->GetFileVersion());
171 TYPED_TEST(FileVersionInfoTest, NoVersionInfo) {
172 FilePath dll_path = GetTestDataPath();
173 dll_path = dll_path.AppendASCII("no_version_info.dll");
175 TypeParam factory(dll_path);
176 ASSERT_FALSE(factory.Create());