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.
5 #include "base/files/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/extensions/extension_creator_filter.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
14 class ExtensionCreatorFilterTest : public PlatformTest {
16 virtual void SetUp() {
17 PlatformTest::SetUp();
19 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
20 test_dir_ = temp_dir_.path();
22 filter_ = new extensions::ExtensionCreatorFilter();
25 base::FilePath CreateEmptyTestFile(const base::FilePath& file_path) {
26 base::FilePath test_file(test_dir_.Append(file_path));
27 base::FilePath temp_file;
28 EXPECT_TRUE(base::CreateTemporaryFileInDir(test_dir_, &temp_file));
29 EXPECT_TRUE(base::Move(temp_file, test_file));
33 base::FilePath CreateEmptyTestFileInDir(
34 const base::FilePath::StringType& file_name,
35 const base::FilePath::StringType& dir) {
36 base::FilePath temp_sub_dir(test_dir_.Append(dir));
37 base::FilePath test_file(temp_sub_dir.Append(file_name));
38 EXPECT_TRUE(base::CreateDirectory(temp_sub_dir));
39 base::FilePath temp_file;
40 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_sub_dir, &temp_file));
41 EXPECT_TRUE(base::Move(temp_file, test_file));
45 scoped_refptr<extensions::ExtensionCreatorFilter> filter_;
47 base::ScopedTempDir temp_dir_;
49 base::FilePath test_dir_;
52 struct UnaryBooleanTestData {
53 const base::FilePath::CharType* input;
57 TEST_F(ExtensionCreatorFilterTest, NormalCases) {
58 const struct UnaryBooleanTestData cases[] = {
59 { FILE_PATH_LITERAL("foo"), true },
60 { FILE_PATH_LITERAL(".foo"), false },
61 { FILE_PATH_LITERAL("~foo"), true },
62 { FILE_PATH_LITERAL("foo~"), false },
63 { FILE_PATH_LITERAL("#foo"), true },
64 { FILE_PATH_LITERAL("foo#"), true },
65 { FILE_PATH_LITERAL("#foo#"), false },
66 { FILE_PATH_LITERAL(".svn"), false },
67 { FILE_PATH_LITERAL("__MACOSX"), false },
68 { FILE_PATH_LITERAL(".DS_Store"), false },
69 { FILE_PATH_LITERAL("desktop.ini"), false },
70 { FILE_PATH_LITERAL("Thumbs.db"), false },
73 for (size_t i = 0; i < arraysize(cases); ++i) {
74 base::FilePath input(cases[i].input);
75 base::FilePath test_file(CreateEmptyTestFile(input));
76 bool observed = filter_->ShouldPackageFile(test_file);
78 EXPECT_EQ(cases[i].expected, observed) <<
79 "i: " << i << ", input: " << test_file.value();
83 struct StringStringWithBooleanTestData {
84 const base::FilePath::StringType file_name;
85 const base::FilePath::StringType dir;
89 // Ignore the files in special directories, including ".git", ".svn",
91 TEST_F(ExtensionCreatorFilterTest, IgnoreFilesInSpecialDir) {
92 const struct StringStringWithBooleanTestData cases[] = {
93 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL(".git"), false },
94 { FILE_PATH_LITERAL("goo"), FILE_PATH_LITERAL(".svn"), false },
95 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("__MACOSX"), false },
96 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("foo"), true },
97 { FILE_PATH_LITERAL("index.js"), FILE_PATH_LITERAL("scripts"), true },
100 for (size_t i = 0; i < arraysize(cases); ++i) {
101 base::FilePath test_file(CreateEmptyTestFileInDir(cases[i].file_name,
103 bool observed = filter_->ShouldPackageFile(test_file);
104 EXPECT_EQ(cases[i].expected, observed) <<
105 "i: " << i << ", input: " << test_file.value();
110 struct StringBooleanWithBooleanTestData {
111 const base::FilePath::CharType* input_char;
116 TEST_F(ExtensionCreatorFilterTest, WindowsHiddenFiles) {
117 const struct StringBooleanWithBooleanTestData cases[] = {
118 { FILE_PATH_LITERAL("a-normal-file"), false, true },
119 { FILE_PATH_LITERAL(".a-dot-file"), false, false },
120 { FILE_PATH_LITERAL(".a-dot-file-that-we-have-set-to-hidden"),
122 { FILE_PATH_LITERAL("a-file-that-we-have-set-to-hidden"), true, false },
123 { FILE_PATH_LITERAL("a-file-that-we-have-not-set-to-hidden"),
127 for (size_t i = 0; i < arraysize(cases); ++i) {
128 base::FilePath input(cases[i].input_char);
129 bool should_hide = cases[i].input_bool;
130 base::FilePath test_file(CreateEmptyTestFile(input));
133 SetFileAttributes(test_file.value().c_str(), FILE_ATTRIBUTE_HIDDEN);
135 bool observed = filter_->ShouldPackageFile(test_file);
136 EXPECT_EQ(cases[i].expected, observed) <<
137 "i: " << i << ", input: " << test_file.value();