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