extension_creator_filter_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (c) 2012 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 "extensions/browser/extension_creator_filter.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include "base/files/file_util.h"
  8. #include "base/files/scoped_temp_dir.h"
  9. #include "build/build_config.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. #include "testing/platform_test.h"
  12. #if BUILDFLAG(IS_WIN)
  13. #include <windows.h>
  14. #endif
  15. namespace {
  16. class ExtensionCreatorFilterTest : public PlatformTest {
  17. protected:
  18. void SetUp() override {
  19. PlatformTest::SetUp();
  20. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  21. extension_dir_ = temp_dir_.GetPath();
  22. filter_ = base::MakeRefCounted<extensions::ExtensionCreatorFilter>(
  23. extension_dir_);
  24. }
  25. base::FilePath CreateTestFile(const base::FilePath& file_path) {
  26. return CreateRelativeFilePath(file_path);
  27. }
  28. // Creates an empty file with the given relative path. Creates parent
  29. // directories if needed.
  30. base::FilePath CreateRelativeFilePath(
  31. const base::FilePath& relative_file_path) {
  32. base::FilePath path = extension_dir_.Append(relative_file_path);
  33. EXPECT_TRUE(base::CreateDirectory(path.DirName()));
  34. std::string contents = "test";
  35. EXPECT_EQ(static_cast<int>(contents.size()),
  36. base::WriteFile(path, contents.c_str(), contents.size()));
  37. return path;
  38. }
  39. base::FilePath CreateTestFileInDir(
  40. const base::FilePath::StringType& file_name,
  41. const base::FilePath::StringType& dir) {
  42. return CreateRelativeFilePath(base::FilePath(dir).Append(file_name));
  43. }
  44. scoped_refptr<extensions::ExtensionCreatorFilter> filter_;
  45. base::ScopedTempDir temp_dir_;
  46. base::FilePath extension_dir_;
  47. };
  48. struct UnaryBooleanTestData {
  49. const base::FilePath::CharType* input;
  50. bool expected;
  51. };
  52. TEST_F(ExtensionCreatorFilterTest, NormalCases) {
  53. const struct UnaryBooleanTestData cases[] = {
  54. {FILE_PATH_LITERAL("foo"), true},
  55. {FILE_PATH_LITERAL(".foo"), false},
  56. {FILE_PATH_LITERAL("~foo"), true},
  57. {FILE_PATH_LITERAL("foo~"), false},
  58. {FILE_PATH_LITERAL("#foo"), true},
  59. {FILE_PATH_LITERAL("foo#"), true},
  60. {FILE_PATH_LITERAL("#foo#"), false},
  61. {FILE_PATH_LITERAL(".svn"), false},
  62. {FILE_PATH_LITERAL("__MACOSX"), false},
  63. {FILE_PATH_LITERAL(".DS_Store"), false},
  64. {FILE_PATH_LITERAL("desktop.ini"), false},
  65. {FILE_PATH_LITERAL("Thumbs.db"), false},
  66. };
  67. for (size_t i = 0; i < std::size(cases); ++i) {
  68. base::FilePath input(cases[i].input);
  69. base::FilePath test_file(CreateTestFile(input));
  70. bool observed = filter_->ShouldPackageFile(test_file);
  71. EXPECT_EQ(cases[i].expected, observed)
  72. << "i: " << i << ", input: " << test_file.value();
  73. }
  74. }
  75. TEST_F(ExtensionCreatorFilterTest, MetadataFolderExcluded) {
  76. const struct UnaryBooleanTestData cases[] = {
  77. {FILE_PATH_LITERAL("_metadata/foo"), false},
  78. {FILE_PATH_LITERAL("_metadata/abc/foo"), false},
  79. {FILE_PATH_LITERAL("_metadata/abc/xyz/foo"), false},
  80. {FILE_PATH_LITERAL("abc/_metadata/xyz"), true},
  81. {FILE_PATH_LITERAL("xyz/_metadata"), true},
  82. };
  83. // Create and test the filepaths.
  84. for (size_t i = 0; i < std::size(cases); ++i) {
  85. base::FilePath test_file =
  86. CreateRelativeFilePath(base::FilePath(cases[i].input));
  87. bool observed = filter_->ShouldPackageFile(test_file);
  88. EXPECT_EQ(cases[i].expected, observed)
  89. << "i: " << i << ", input: " << test_file.value();
  90. }
  91. // Also test directories.
  92. const struct UnaryBooleanTestData directory_cases[] = {
  93. {FILE_PATH_LITERAL("_metadata"), false},
  94. {FILE_PATH_LITERAL("_metadata/abc"), false},
  95. {FILE_PATH_LITERAL("_metadata/abc/xyz"), false},
  96. {FILE_PATH_LITERAL("abc"), true},
  97. {FILE_PATH_LITERAL("abc/_metadata"), true},
  98. {FILE_PATH_LITERAL("xyz"), true},
  99. };
  100. for (size_t i = 0; i < std::size(directory_cases); ++i) {
  101. base::FilePath directory = extension_dir_.Append(directory_cases[i].input);
  102. bool observed = filter_->ShouldPackageFile(directory);
  103. EXPECT_EQ(directory_cases[i].expected, observed)
  104. << "i: " << i << ", input: " << directory.value();
  105. }
  106. }
  107. struct StringStringWithBooleanTestData {
  108. const base::FilePath::StringType file_name;
  109. const base::FilePath::StringType dir;
  110. bool expected;
  111. };
  112. // Ignore the files in special directories, including ".git", ".svn",
  113. // "__MACOSX".
  114. TEST_F(ExtensionCreatorFilterTest, IgnoreFilesInSpecialDir) {
  115. const struct StringStringWithBooleanTestData cases[] = {
  116. {FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL(".git"), false},
  117. {FILE_PATH_LITERAL("goo"), FILE_PATH_LITERAL(".svn"), false},
  118. {FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("__MACOSX"), false},
  119. {FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("foo"), true},
  120. {FILE_PATH_LITERAL("index.js"), FILE_PATH_LITERAL("scripts"), true},
  121. };
  122. for (size_t i = 0; i < std::size(cases); ++i) {
  123. base::FilePath test_file(
  124. CreateTestFileInDir(cases[i].file_name, cases[i].dir));
  125. bool observed = filter_->ShouldPackageFile(test_file);
  126. EXPECT_EQ(cases[i].expected, observed)
  127. << "i: " << i << ", input: " << test_file.value();
  128. }
  129. }
  130. #if BUILDFLAG(IS_WIN)
  131. struct StringBooleanWithBooleanTestData {
  132. const base::FilePath::CharType* input_char;
  133. bool input_bool;
  134. bool expected;
  135. };
  136. TEST_F(ExtensionCreatorFilterTest, WindowsHiddenFiles) {
  137. const struct StringBooleanWithBooleanTestData cases[] = {
  138. {FILE_PATH_LITERAL("a-normal-file"), false, true},
  139. {FILE_PATH_LITERAL(".a-dot-file"), false, false},
  140. {FILE_PATH_LITERAL(".a-dot-file-that-we-have-set-to-hidden"), true,
  141. false},
  142. {FILE_PATH_LITERAL("a-file-that-we-have-set-to-hidden"), true, false},
  143. {FILE_PATH_LITERAL("a-file-that-we-have-not-set-to-hidden"), false, true},
  144. };
  145. for (size_t i = 0; i < std::size(cases); ++i) {
  146. base::FilePath input(cases[i].input_char);
  147. bool should_hide = cases[i].input_bool;
  148. base::FilePath test_file(CreateTestFile(input));
  149. if (should_hide) {
  150. SetFileAttributes(test_file.value().c_str(), FILE_ATTRIBUTE_HIDDEN);
  151. }
  152. bool observed = filter_->ShouldPackageFile(test_file);
  153. EXPECT_EQ(cases[i].expected, observed)
  154. << "i: " << i << ", input: " << test_file.value();
  155. }
  156. }
  157. #endif
  158. } // namespace