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 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" 11 12namespace { 13 14class ExtensionCreatorFilterTest : public PlatformTest { 15 protected: 16 virtual void SetUp() { 17 PlatformTest::SetUp(); 18 19 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 20 test_dir_ = temp_dir_.path(); 21 22 filter_ = new extensions::ExtensionCreatorFilter(); 23 } 24 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)); 30 return test_file; 31 } 32 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)); 42 return test_file; 43 } 44 45 scoped_refptr<extensions::ExtensionCreatorFilter> filter_; 46 47 base::ScopedTempDir temp_dir_; 48 49 base::FilePath test_dir_; 50}; 51 52struct UnaryBooleanTestData { 53 const base::FilePath::CharType* input; 54 bool expected; 55}; 56 57TEST_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 }, 71 }; 72 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); 77 78 EXPECT_EQ(cases[i].expected, observed) << 79 "i: " << i << ", input: " << test_file.value(); 80 } 81} 82 83struct StringStringWithBooleanTestData { 84 const base::FilePath::StringType file_name; 85 const base::FilePath::StringType dir; 86 bool expected; 87}; 88 89// Ignore the files in special directories, including ".git", ".svn", 90// "__MACOSX". 91TEST_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 }, 98 }; 99 100 for (size_t i = 0; i < arraysize(cases); ++i) { 101 base::FilePath test_file(CreateEmptyTestFileInDir(cases[i].file_name, 102 cases[i].dir)); 103 bool observed = filter_->ShouldPackageFile(test_file); 104 EXPECT_EQ(cases[i].expected, observed) << 105 "i: " << i << ", input: " << test_file.value(); 106 } 107} 108 109#if defined(OS_WIN) 110struct StringBooleanWithBooleanTestData { 111 const base::FilePath::CharType* input_char; 112 bool input_bool; 113 bool expected; 114}; 115 116TEST_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"), 121 true, false }, 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"), 124 false, true }, 125 }; 126 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)); 131 132 if (should_hide) { 133 SetFileAttributes(test_file.value().c_str(), FILE_ATTRIBUTE_HIDDEN); 134 } 135 bool observed = filter_->ShouldPackageFile(test_file); 136 EXPECT_EQ(cases[i].expected, observed) << 137 "i: " << i << ", input: " << test_file.value(); 138 } 139} 140#endif 141 142} // namespace 143