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