file_version_info_win_unittest.cc 6.32 KB
Newer Older
1
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
initial.commit's avatar
initial.commit committed
4

5 6 7
#include "base/file_version_info_win.h"

#include <windows.h>
dcheng's avatar
dcheng committed
8

9 10
#include <stddef.h>

dcheng's avatar
dcheng committed
11 12
#include <memory>

13
#include "base/file_version_info.h"
14
#include "base/files/file_path.h"
15
#include "base/macros.h"
16
#include "base/memory/ptr_util.h"
initial.commit's avatar
initial.commit committed
17
#include "base/path_service.h"
18
#include "base/scoped_native_library.h"
initial.commit's avatar
initial.commit committed
19 20
#include "testing/gtest/include/gtest/gtest.h"

21 22
using base::FilePath;

initial.commit's avatar
initial.commit committed
23 24
namespace {

25 26
FilePath GetTestDataPath() {
  FilePath path;
initial.commit's avatar
initial.commit committed
27
  PathService::Get(base::DIR_SOURCE_ROOT, &path);
28
  path = path.AppendASCII("base");
29
  path = path.AppendASCII("test");
30 31
  path = path.AppendASCII("data");
  path = path.AppendASCII("file_version_info_unittest");
initial.commit's avatar
initial.commit committed
32 33
  return path;
}
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

class FileVersionInfoFactory {
 public:
  explicit FileVersionInfoFactory(const FilePath& path) : path_(path) {}

  std::unique_ptr<FileVersionInfo> Create() const {
    return base::WrapUnique(FileVersionInfo::CreateFileVersionInfo(path_));
  }

 private:
  const FilePath path_;

  DISALLOW_COPY_AND_ASSIGN(FileVersionInfoFactory);
};

class FileVersionInfoForModuleFactory {
 public:
  explicit FileVersionInfoForModuleFactory(const FilePath& path)
      // Load the library with LOAD_LIBRARY_AS_IMAGE_RESOURCE since it shouldn't
      // be executed.
      : library_(::LoadLibraryEx(path.value().c_str(),
                                 nullptr,
                                 LOAD_LIBRARY_AS_IMAGE_RESOURCE)) {
    EXPECT_TRUE(library_.is_valid());
  }

  std::unique_ptr<FileVersionInfo> Create() const {
    return base::WrapUnique(
        FileVersionInfo::CreateFileVersionInfoForModule(library_.get()));
  }

 private:
  const base::ScopedNativeLibrary library_;

  DISALLOW_COPY_AND_ASSIGN(FileVersionInfoForModuleFactory);
};

template <typename T>
class FileVersionInfoTest : public testing::Test {};

using FileVersionInfoFactories =
    ::testing::Types<FileVersionInfoFactory, FileVersionInfoForModuleFactory>;
initial.commit's avatar
initial.commit committed
76

77
}  // namespace
initial.commit's avatar
initial.commit committed
78

79 80 81
TYPED_TEST_CASE(FileVersionInfoTest, FileVersionInfoFactories);

TYPED_TEST(FileVersionInfoTest, HardCodedProperties) {
82
  const wchar_t kDLLName[] = {L"FileVersionInfoTest1.dll"};
initial.commit's avatar
initial.commit committed
83

84
  const wchar_t* const kExpectedValues[15] = {
initial.commit's avatar
initial.commit committed
85
      // FileVersionInfoTest.dll
86 87 88 89 90 91 92 93
      L"Goooooogle",                                  // company_name
      L"Google",                                      // company_short_name
      L"This is the product name",                    // product_name
      L"This is the product short name",              // product_short_name
      L"The Internal Name",                           // internal_name
      L"4.3.2.1",                                     // product_version
      L"Private build property",                      // private_build
      L"Special build property",                      // special_build
initial.commit's avatar
initial.commit committed
94
      L"This is a particularly interesting comment",  // comments
95 96 97 98 99 100
      L"This is the original filename",               // original_filename
      L"This is my file description",                 // file_description
      L"1.2.3.4",                                     // file_version
      L"This is the legal copyright",                 // legal_copyright
      L"This is the legal trademarks",                // legal_trademarks
      L"This is the last change",                     // last_change
initial.commit's avatar
initial.commit committed
101 102
  };

103 104
  FilePath dll_path = GetTestDataPath();
  dll_path = dll_path.Append(kDLLName);
initial.commit's avatar
initial.commit committed
105

106 107 108
  TypeParam factory(dll_path);
  std::unique_ptr<FileVersionInfo> version_info(factory.Create());
  ASSERT_TRUE(version_info);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

  int j = 0;
  EXPECT_EQ(kExpectedValues[j++], version_info->company_name());
  EXPECT_EQ(kExpectedValues[j++], version_info->company_short_name());
  EXPECT_EQ(kExpectedValues[j++], version_info->product_name());
  EXPECT_EQ(kExpectedValues[j++], version_info->product_short_name());
  EXPECT_EQ(kExpectedValues[j++], version_info->internal_name());
  EXPECT_EQ(kExpectedValues[j++], version_info->product_version());
  EXPECT_EQ(kExpectedValues[j++], version_info->private_build());
  EXPECT_EQ(kExpectedValues[j++], version_info->special_build());
  EXPECT_EQ(kExpectedValues[j++], version_info->comments());
  EXPECT_EQ(kExpectedValues[j++], version_info->original_filename());
  EXPECT_EQ(kExpectedValues[j++], version_info->file_description());
  EXPECT_EQ(kExpectedValues[j++], version_info->file_version());
  EXPECT_EQ(kExpectedValues[j++], version_info->legal_copyright());
  EXPECT_EQ(kExpectedValues[j++], version_info->legal_trademarks());
  EXPECT_EQ(kExpectedValues[j++], version_info->last_change());
initial.commit's avatar
initial.commit committed
126 127
}

128 129 130 131 132 133
TYPED_TEST(FileVersionInfoTest, IsOfficialBuild) {
  constexpr struct {
    const wchar_t* const dll_name;
    const bool is_official_build;
  } kTestItems[]{
      {L"FileVersionInfoTest1.dll", true}, {L"FileVersionInfoTest2.dll", false},
initial.commit's avatar
initial.commit committed
134 135
  };

136 137
  for (const auto& test_item : kTestItems) {
    const FilePath dll_path = GetTestDataPath().Append(test_item.dll_name);
initial.commit's avatar
initial.commit committed
138

139 140 141
    TypeParam factory(dll_path);
    std::unique_ptr<FileVersionInfo> version_info(factory.Create());
    ASSERT_TRUE(version_info);
initial.commit's avatar
initial.commit committed
142

143
    EXPECT_EQ(test_item.is_official_build, version_info->is_official_build());
initial.commit's avatar
initial.commit committed
144 145 146
  }
}

147
TYPED_TEST(FileVersionInfoTest, CustomProperties) {
148 149
  FilePath dll_path = GetTestDataPath();
  dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll");
initial.commit's avatar
initial.commit committed
150

151 152 153
  TypeParam factory(dll_path);
  std::unique_ptr<FileVersionInfo> version_info(factory.Create());
  ASSERT_TRUE(version_info);
initial.commit's avatar
initial.commit committed
154 155 156

  // Test few existing properties.
  std::wstring str;
157 158
  FileVersionInfoWin* version_info_win =
      static_cast<FileVersionInfoWin*>(version_info.get());
159
  EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 1", &str));
initial.commit's avatar
initial.commit committed
160
  EXPECT_EQ(L"Un", str);
161
  EXPECT_EQ(L"Un", version_info_win->GetStringValue(L"Custom prop 1"));
initial.commit's avatar
initial.commit committed
162

163
  EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 2", &str));
initial.commit's avatar
initial.commit committed
164
  EXPECT_EQ(L"Deux", str);
165
  EXPECT_EQ(L"Deux", version_info_win->GetStringValue(L"Custom prop 2"));
initial.commit's avatar
initial.commit committed
166

167
  EXPECT_TRUE(version_info_win->GetValue(L"Custom prop 3", &str));
initial.commit's avatar
initial.commit committed
168 169
  EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043", str);
  EXPECT_EQ(L"1600 Amphitheatre Parkway Mountain View, CA 94043",
170
            version_info_win->GetStringValue(L"Custom prop 3"));
initial.commit's avatar
initial.commit committed
171 172

  // Test an non-existing property.
173
  EXPECT_FALSE(version_info_win->GetValue(L"Unknown property", &str));
174
  EXPECT_EQ(L"", version_info_win->GetStringValue(L"Unknown property"));
initial.commit's avatar
initial.commit committed
175
}