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