blob: 6c81bb7cb309e54c331913ab16afb7cac0f9014b [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"
jdoerrie5c4dc4e2019-02-01 18:02:3318#include "base/strings/string_util.h"
fdoray5b7de9e92016-06-29 23:13:1119#include "testing/gtest/include/gtest/gtest.h"
20
21using base::FilePath;
22
23namespace {
24
25FilePath GetTestDataPath() {
26 FilePath path;
Avi Drissmanea15ea02018-05-07 18:55:1227 base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
fdoray5b7de9e92016-06-29 23:13:1128 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
35class FileVersionInfoFactory {
36 public:
37 explicit FileVersionInfoFactory(const FilePath& path) : path_(path) {}
38
39 std::unique_ptr<FileVersionInfo> Create() const {
David Benjamin04cc2b42019-01-29 05:30:3340 return FileVersionInfo::CreateFileVersionInfo(path_);
fdoray5b7de9e92016-06-29 23:13:1141 }
42
43 private:
44 const FilePath path_;
45
46 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoFactory);
47};
48
49class 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.
jdoerriebacc1962019-02-07 13:39:2254 : library_(::LoadLibraryEx(base::as_wcstr(path.value()),
fdoray5b7de9e92016-06-29 23:13:1155 nullptr,
56 LOAD_LIBRARY_AS_IMAGE_RESOURCE)) {
57 EXPECT_TRUE(library_.is_valid());
58 }
59
60 std::unique_ptr<FileVersionInfo> Create() const {
David Benjamin04cc2b42019-01-29 05:30:3361 return FileVersionInfo::CreateFileVersionInfoForModule(library_.get());
fdoray5b7de9e92016-06-29 23:13:1162 }
63
64 private:
65 const base::ScopedNativeLibrary library_;
66
67 DISALLOW_COPY_AND_ASSIGN(FileVersionInfoForModuleFactory);
68};
69
70template <typename T>
71class FileVersionInfoTest : public testing::Test {};
72
73using FileVersionInfoFactories =
74 ::testing::Types<FileVersionInfoFactory, FileVersionInfoForModuleFactory>;
75
76} // namespace
77
Victor Costanebc52732019-02-15 02:39:4778TYPED_TEST_SUITE(FileVersionInfoTest, FileVersionInfoFactories);
fdoray5b7de9e92016-06-29 23:13:1179
80TYPED_TEST(FileVersionInfoTest, HardCodedProperties) {
jdoerrie5c4dc4e2019-02-01 18:02:3381 const base::char16 kDLLName[] = STRING16_LITERAL("FileVersionInfoTest1.dll");
fdoray5b7de9e92016-06-29 23:13:1182
jdoerrie5c4dc4e2019-02-01 18:02:3383 const base::char16* const kExpectedValues[15] = {
fdoray5b7de9e92016-06-29 23:13:1184 // FileVersionInfoTest.dll
jdoerrie5c4dc4e2019-02-01 18:02:3385 STRING16_LITERAL("Goooooogle"), // company_name
86 STRING16_LITERAL("Google"), // company_short_name
87 STRING16_LITERAL("This is the product name"), // product_name
88 STRING16_LITERAL("This is the product short name"), // product_short_name
89 STRING16_LITERAL("The Internal Name"), // internal_name
90 STRING16_LITERAL("4.3.2.1"), // product_version
jdoerrie5c4dc4e2019-02-01 18:02:3391 STRING16_LITERAL("Special build property"), // special_build
jdoerrie5c4dc4e2019-02-01 18:02:3392 STRING16_LITERAL("This is the original filename"), // original_filename
93 STRING16_LITERAL("This is my file description"), // file_description
94 STRING16_LITERAL("1.2.3.4"), // file_version
fdoray5b7de9e92016-06-29 23:13:1195 };
96
97 FilePath dll_path = GetTestDataPath();
98 dll_path = dll_path.Append(kDLLName);
99
100 TypeParam factory(dll_path);
101 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
102 ASSERT_TRUE(version_info);
103
104 int j = 0;
105 EXPECT_EQ(kExpectedValues[j++], version_info->company_name());
106 EXPECT_EQ(kExpectedValues[j++], version_info->company_short_name());
107 EXPECT_EQ(kExpectedValues[j++], version_info->product_name());
108 EXPECT_EQ(kExpectedValues[j++], version_info->product_short_name());
109 EXPECT_EQ(kExpectedValues[j++], version_info->internal_name());
110 EXPECT_EQ(kExpectedValues[j++], version_info->product_version());
fdoray5b7de9e92016-06-29 23:13:11111 EXPECT_EQ(kExpectedValues[j++], version_info->special_build());
fdoray5b7de9e92016-06-29 23:13:11112 EXPECT_EQ(kExpectedValues[j++], version_info->original_filename());
113 EXPECT_EQ(kExpectedValues[j++], version_info->file_description());
114 EXPECT_EQ(kExpectedValues[j++], version_info->file_version());
fdoray5b7de9e92016-06-29 23:13:11115}
116
117TYPED_TEST(FileVersionInfoTest, CustomProperties) {
118 FilePath dll_path = GetTestDataPath();
119 dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll");
120
121 TypeParam factory(dll_path);
122 std::unique_ptr<FileVersionInfo> version_info(factory.Create());
123 ASSERT_TRUE(version_info);
124
125 // Test few existing properties.
jdoerrie5c4dc4e2019-02-01 18:02:33126 base::string16 str;
fdoray5b7de9e92016-06-29 23:13:11127 FileVersionInfoWin* version_info_win =
128 static_cast<FileVersionInfoWin*>(version_info.get());
jdoerrie5c4dc4e2019-02-01 18:02:33129 EXPECT_TRUE(
130 version_info_win->GetValue(STRING16_LITERAL("Custom prop 1"), &str));
131 EXPECT_EQ(STRING16_LITERAL("Un"), str);
132 EXPECT_EQ(STRING16_LITERAL("Un"), version_info_win->GetStringValue(
133 STRING16_LITERAL("Custom prop 1")));
fdoray5b7de9e92016-06-29 23:13:11134
jdoerrie5c4dc4e2019-02-01 18:02:33135 EXPECT_TRUE(
136 version_info_win->GetValue(STRING16_LITERAL("Custom prop 2"), &str));
137 EXPECT_EQ(STRING16_LITERAL("Deux"), str);
138 EXPECT_EQ(STRING16_LITERAL("Deux"), version_info_win->GetStringValue(
139 STRING16_LITERAL("Custom prop 2")));
fdoray5b7de9e92016-06-29 23:13:11140
jdoerrie5c4dc4e2019-02-01 18:02:33141 EXPECT_TRUE(
142 version_info_win->GetValue(STRING16_LITERAL("Custom prop 3"), &str));
143 EXPECT_EQ(
144 STRING16_LITERAL("1600 Amphitheatre Parkway Mountain View, CA 94043"),
145 str);
146 EXPECT_EQ(
147 STRING16_LITERAL("1600 Amphitheatre Parkway Mountain View, CA 94043"),
148 version_info_win->GetStringValue(STRING16_LITERAL("Custom prop 3")));
fdoray5b7de9e92016-06-29 23:13:11149
150 // Test an non-existing property.
jdoerrie5c4dc4e2019-02-01 18:02:33151 EXPECT_FALSE(
152 version_info_win->GetValue(STRING16_LITERAL("Unknown property"), &str));
153 EXPECT_EQ(base::string16(), version_info_win->GetStringValue(
154 STRING16_LITERAL("Unknown property")));
Alan Screene93de3a2019-10-02 13:56:09155
156 EXPECT_EQ(base::Version(std::vector<uint32_t>{1, 0, 0, 1}),
157 version_info_win->GetFileVersion());
fdoray5b7de9e92016-06-29 23:13:11158}
Lei Zhang16fa1acb2019-10-04 16:05:37159
160TYPED_TEST(FileVersionInfoTest, NoVersionInfo) {
161 FilePath dll_path = GetTestDataPath();
162 dll_path = dll_path.AppendASCII("no_version_info.dll");
163
164 TypeParam factory(dll_path);
165 ASSERT_FALSE(factory.Create());
166}