file_version_info_win_unittest.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "base/file_version_info_win.h"
  5. #include <windows.h>
  6. #include <stddef.h>
  7. #include <memory>
  8. #include "base/file_version_info.h"
  9. #include "base/files/file_path.h"
  10. #include "base/path_service.h"
  11. #include "base/scoped_native_library.h"
  12. #include "base/strings/string_util.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. using base::FilePath;
  15. namespace {
  16. FilePath GetTestDataPath() {
  17. FilePath path;
  18. base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
  19. path = path.AppendASCII("base");
  20. path = path.AppendASCII("test");
  21. path = path.AppendASCII("data");
  22. path = path.AppendASCII("file_version_info_unittest");
  23. return path;
  24. }
  25. class FileVersionInfoFactory {
  26. public:
  27. explicit FileVersionInfoFactory(const FilePath& path) : path_(path) {}
  28. FileVersionInfoFactory(const FileVersionInfoFactory&) = delete;
  29. FileVersionInfoFactory& operator=(const FileVersionInfoFactory&) = delete;
  30. std::unique_ptr<FileVersionInfo> Create() const {
  31. return FileVersionInfo::CreateFileVersionInfo(path_);
  32. }
  33. private:
  34. const FilePath path_;
  35. };
  36. class FileVersionInfoForModuleFactory {
  37. public:
  38. explicit FileVersionInfoForModuleFactory(const FilePath& path)
  39. // Load the library with LOAD_LIBRARY_AS_IMAGE_RESOURCE since it shouldn't
  40. // be executed.
  41. : library_(::LoadLibraryEx(path.value().c_str(),
  42. nullptr,
  43. LOAD_LIBRARY_AS_IMAGE_RESOURCE)) {
  44. EXPECT_TRUE(library_.is_valid());
  45. }
  46. FileVersionInfoForModuleFactory(const FileVersionInfoForModuleFactory&) =
  47. delete;
  48. FileVersionInfoForModuleFactory& operator=(
  49. const FileVersionInfoForModuleFactory&) = delete;
  50. std::unique_ptr<FileVersionInfo> Create() const {
  51. return FileVersionInfo::CreateFileVersionInfoForModule(library_.get());
  52. }
  53. private:
  54. const base::ScopedNativeLibrary library_;
  55. };
  56. template <typename T>
  57. class FileVersionInfoTest : public testing::Test {};
  58. using FileVersionInfoFactories =
  59. ::testing::Types<FileVersionInfoFactory, FileVersionInfoForModuleFactory>;
  60. } // namespace
  61. TYPED_TEST_SUITE(FileVersionInfoTest, FileVersionInfoFactories);
  62. TYPED_TEST(FileVersionInfoTest, HardCodedProperties) {
  63. const base::FilePath::CharType kDLLName[] =
  64. FILE_PATH_LITERAL("FileVersionInfoTest1.dll");
  65. const wchar_t* const kExpectedValues[15] = {
  66. // FileVersionInfoTest.dll
  67. L"Goooooogle", // company_name
  68. L"Google", // company_short_name
  69. L"This is the product name", // product_name
  70. L"This is the product short name", // product_short_name
  71. L"The Internal Name", // internal_name
  72. L"4.3.2.1", // product_version
  73. L"Special build property", // special_build
  74. L"This is the original filename", // original_filename
  75. L"This is my file description", // file_description
  76. L"1.2.3.4", // file_version
  77. };
  78. FilePath dll_path = GetTestDataPath();
  79. dll_path = dll_path.Append(kDLLName);
  80. TypeParam factory(dll_path);
  81. std::unique_ptr<FileVersionInfo> version_info(factory.Create());
  82. ASSERT_TRUE(version_info);
  83. int j = 0;
  84. EXPECT_EQ(kExpectedValues[j++],
  85. base::AsWStringPiece(version_info->company_name()));
  86. EXPECT_EQ(kExpectedValues[j++],
  87. base::AsWStringPiece(version_info->company_short_name()));
  88. EXPECT_EQ(kExpectedValues[j++],
  89. base::AsWStringPiece(version_info->product_name()));
  90. EXPECT_EQ(kExpectedValues[j++],
  91. base::AsWStringPiece(version_info->product_short_name()));
  92. EXPECT_EQ(kExpectedValues[j++],
  93. base::AsWStringPiece(version_info->internal_name()));
  94. EXPECT_EQ(kExpectedValues[j++],
  95. base::AsWStringPiece(version_info->product_version()));
  96. EXPECT_EQ(kExpectedValues[j++],
  97. base::AsWStringPiece(version_info->special_build()));
  98. EXPECT_EQ(kExpectedValues[j++],
  99. base::AsWStringPiece(version_info->original_filename()));
  100. EXPECT_EQ(kExpectedValues[j++],
  101. base::AsWStringPiece(version_info->file_description()));
  102. EXPECT_EQ(kExpectedValues[j++],
  103. base::AsWStringPiece(version_info->file_version()));
  104. }
  105. TYPED_TEST(FileVersionInfoTest, CustomProperties) {
  106. FilePath dll_path = GetTestDataPath();
  107. dll_path = dll_path.AppendASCII("FileVersionInfoTest1.dll");
  108. TypeParam factory(dll_path);
  109. std::unique_ptr<FileVersionInfo> version_info(factory.Create());
  110. ASSERT_TRUE(version_info);
  111. // Test few existing properties.
  112. std::u16string str;
  113. FileVersionInfoWin* version_info_win =
  114. static_cast<FileVersionInfoWin*>(version_info.get());
  115. EXPECT_TRUE(version_info_win->GetValue(u"Custom prop 1", &str));
  116. EXPECT_EQ(u"Un", str);
  117. EXPECT_EQ(u"Un", version_info_win->GetStringValue(u"Custom prop 1"));
  118. EXPECT_TRUE(version_info_win->GetValue(u"Custom prop 2", &str));
  119. EXPECT_EQ(u"Deux", str);
  120. EXPECT_EQ(u"Deux", version_info_win->GetStringValue(u"Custom prop 2"));
  121. EXPECT_TRUE(version_info_win->GetValue(u"Custom prop 3", &str));
  122. EXPECT_EQ(u"1600 Amphitheatre Parkway Mountain View, CA 94043", str);
  123. EXPECT_EQ(u"1600 Amphitheatre Parkway Mountain View, CA 94043",
  124. version_info_win->GetStringValue(u"Custom prop 3"));
  125. // Test an non-existing property.
  126. EXPECT_FALSE(version_info_win->GetValue(u"Unknown property", &str));
  127. EXPECT_EQ(std::u16string(),
  128. version_info_win->GetStringValue(u"Unknown property"));
  129. EXPECT_EQ(base::Version(std::vector<uint32_t>{1, 0, 0, 1}),
  130. version_info_win->GetFileVersion());
  131. }
  132. TYPED_TEST(FileVersionInfoTest, NoVersionInfo) {
  133. FilePath dll_path = GetTestDataPath();
  134. dll_path = dll_path.AppendASCII("no_version_info.dll");
  135. TypeParam factory(dll_path);
  136. ASSERT_FALSE(factory.Create());
  137. }