[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 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 <algorithm> |
| 6 | |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 7 | #include "base/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 8 | #include "base/files/scoped_temp_dir.h" |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 9 | #include "base/path_service.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 10 | #include "extensions/common/constants.h" |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 11 | #include "extensions/common/extension_paths.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 12 | #include "extensions/common/extension_resource.h" |
| 13 | #include "extensions/common/id_util.h" |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 15 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 16 | |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 17 | namespace extensions { |
| 18 | |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 19 | TEST(ExtensionResourceTest, CreateEmptyResource) { |
| 20 | ExtensionResource resource; |
| 21 | |
| 22 | EXPECT_TRUE(resource.extension_root().empty()); |
| 23 | EXPECT_TRUE(resource.relative_path().empty()); |
| 24 | EXPECT_TRUE(resource.GetFilePath().empty()); |
| 25 | } |
| 26 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 27 | const base::FilePath::StringType ToLower( |
| 28 | const base::FilePath::StringType& in_str) { |
| 29 | base::FilePath::StringType str(in_str); |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 30 | std::transform(str.begin(), str.end(), str.begin(), tolower); |
| 31 | return str; |
| 32 | } |
| 33 | |
| 34 | TEST(ExtensionResourceTest, CreateWithMissingResourceOnDisk) { |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 35 | base::FilePath root_path; |
[email protected] | 32efb04 | 2013-03-29 00:23:21 | [diff] [blame] | 36 | ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &root_path)); |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 37 | base::FilePath relative_path; |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 38 | relative_path = relative_path.AppendASCII("cira.js"); |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 39 | std::string extension_id = id_util::GenerateId("test"); |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 40 | ExtensionResource resource(extension_id, root_path, relative_path); |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 41 | |
[email protected] | a14b16b | 2009-10-28 12:41:29 | [diff] [blame] | 42 | // The path doesn't exist on disk, we will be returned an empty path. |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 43 | EXPECT_EQ(root_path.value(), resource.extension_root().value()); |
| 44 | EXPECT_EQ(relative_path.value(), resource.relative_path().value()); |
[email protected] | a14b16b | 2009-10-28 12:41:29 | [diff] [blame] | 45 | EXPECT_TRUE(resource.GetFilePath().empty()); |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 48 | TEST(ExtensionResourceTest, ResourcesOutsideOfPath) { |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 49 | base::ScopedTempDir temp; |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 50 | ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 51 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 52 | base::FilePath inner_dir = temp.path().AppendASCII("directory"); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 53 | ASSERT_TRUE(base::CreateDirectory(inner_dir)); |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 54 | base::FilePath sub_dir = inner_dir.AppendASCII("subdir"); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 55 | ASSERT_TRUE(base::CreateDirectory(sub_dir)); |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 56 | base::FilePath inner_file = inner_dir.AppendASCII("inner"); |
| 57 | base::FilePath outer_file = temp.path().AppendASCII("outer"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 58 | ASSERT_TRUE(base::WriteFile(outer_file, "X", 1)); |
| 59 | ASSERT_TRUE(base::WriteFile(inner_file, "X", 1)); |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 60 | std::string extension_id = id_util::GenerateId("test"); |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 61 | |
| 62 | #if defined(OS_POSIX) |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 63 | base::FilePath symlink_file = inner_dir.AppendASCII("symlink"); |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 64 | base::CreateSymbolicLink( |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 65 | base::FilePath().AppendASCII("..").AppendASCII("outer"), |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 66 | symlink_file); |
| 67 | #endif |
| 68 | |
| 69 | // A non-packing extension should be able to access the file within the |
| 70 | // directory. |
| 71 | ExtensionResource r1(extension_id, inner_dir, |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 72 | base::FilePath().AppendASCII("inner")); |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 73 | EXPECT_FALSE(r1.GetFilePath().empty()); |
| 74 | |
| 75 | // ... but not a relative path that walks out of |inner_dir|. |
| 76 | ExtensionResource r2(extension_id, inner_dir, |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 77 | base::FilePath().AppendASCII("..").AppendASCII("outer")); |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 78 | EXPECT_TRUE(r2.GetFilePath().empty()); |
| 79 | |
| 80 | // A packing extension should also be able to access the file within the |
| 81 | // directory. |
| 82 | ExtensionResource r3(extension_id, inner_dir, |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 83 | base::FilePath().AppendASCII("inner")); |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 84 | r3.set_follow_symlinks_anywhere(); |
| 85 | EXPECT_FALSE(r3.GetFilePath().empty()); |
| 86 | |
| 87 | // ... but, again, not a relative path that walks out of |inner_dir|. |
| 88 | ExtensionResource r4(extension_id, inner_dir, |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 89 | base::FilePath().AppendASCII("..").AppendASCII("outer")); |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 90 | r4.set_follow_symlinks_anywhere(); |
| 91 | EXPECT_TRUE(r4.GetFilePath().empty()); |
| 92 | |
[email protected] | 730258b8 | 2012-11-28 15:27:22 | [diff] [blame] | 93 | // ... and not even when clever current-directory syntax is present. Note |
| 94 | // that the path for this test case can't start with the current directory |
| 95 | // component due to quirks in FilePath::Append(), and the path must exist. |
| 96 | ExtensionResource r4a( |
| 97 | extension_id, inner_dir, |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 98 | base::FilePath().AppendASCII("subdir").AppendASCII(".").AppendASCII(".."). |
[email protected] | 730258b8 | 2012-11-28 15:27:22 | [diff] [blame] | 99 | AppendASCII("..").AppendASCII("outer")); |
| 100 | r4a.set_follow_symlinks_anywhere(); |
| 101 | EXPECT_TRUE(r4a.GetFilePath().empty()); |
| 102 | |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 103 | #if defined(OS_POSIX) |
| 104 | // The non-packing extension should also not be able to access a resource that |
| 105 | // symlinks out of the directory. |
| 106 | ExtensionResource r5(extension_id, inner_dir, |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 107 | base::FilePath().AppendASCII("symlink")); |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 108 | EXPECT_TRUE(r5.GetFilePath().empty()); |
| 109 | |
| 110 | // ... but a packing extension can. |
| 111 | ExtensionResource r6(extension_id, inner_dir, |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 112 | base::FilePath().AppendASCII("symlink")); |
[email protected] | 3d41d43 | 2012-04-20 20:47:58 | [diff] [blame] | 113 | r6.set_follow_symlinks_anywhere(); |
| 114 | EXPECT_FALSE(r6.GetFilePath().empty()); |
| 115 | #endif |
| 116 | } |
| 117 | |
[email protected] | af958dd | 2009-10-22 20:56:18 | [diff] [blame] | 118 | TEST(ExtensionResourceTest, CreateWithAllResourcesOnDisk) { |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 119 | base::ScopedTempDir temp; |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 120 | ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 121 | |
[email protected] | af958dd | 2009-10-22 20:56:18 | [diff] [blame] | 122 | // Create resource in the extension root. |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 123 | const char* filename = "res.ico"; |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 124 | base::FilePath root_resource = temp.path().AppendASCII(filename); |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 125 | std::string data = "some foo"; |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 126 | ASSERT_TRUE(base::WriteFile(root_resource, data.c_str(), data.length())); |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 127 | |
[email protected] | af958dd | 2009-10-22 20:56:18 | [diff] [blame] | 128 | // Create l10n resources (for current locale and its parents). |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 129 | base::FilePath l10n_path = |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 130 | temp.path().Append(kLocaleFolder); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 131 | ASSERT_TRUE(base::CreateDirectory(l10n_path)); |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 132 | |
[email protected] | af958dd | 2009-10-22 20:56:18 | [diff] [blame] | 133 | std::vector<std::string> locales; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 134 | l10n_util::GetParentLocales(l10n_util::GetApplicationLocale(std::string()), |
| 135 | &locales); |
[email protected] | 14e101c0 | 2009-11-11 21:51:51 | [diff] [blame] | 136 | ASSERT_FALSE(locales.empty()); |
[email protected] | af958dd | 2009-10-22 20:56:18 | [diff] [blame] | 137 | for (size_t i = 0; i < locales.size(); i++) { |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 138 | base::FilePath make_path; |
[email protected] | af958dd | 2009-10-22 20:56:18 | [diff] [blame] | 139 | make_path = l10n_path.AppendASCII(locales[i]); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 140 | ASSERT_TRUE(base::CreateDirectory(make_path)); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 141 | ASSERT_TRUE(base::WriteFile(make_path.AppendASCII(filename), |
[email protected] | af958dd | 2009-10-22 20:56:18 | [diff] [blame] | 142 | data.c_str(), data.length())); |
| 143 | } |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 144 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 145 | base::FilePath path; |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 146 | std::string extension_id = id_util::GenerateId("test"); |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 147 | ExtensionResource resource(extension_id, temp.path(), |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 148 | base::FilePath().AppendASCII(filename)); |
| 149 | base::FilePath resolved_path = resource.GetFilePath(); |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 150 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 151 | base::FilePath expected_path; |
[email protected] | 14e101c0 | 2009-11-11 21:51:51 | [diff] [blame] | 152 | // Expect default path only, since fallback logic is disabled. |
| 153 | // See http://crbug.com/27359. |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 154 | expected_path = base::MakeAbsoluteFilePath(root_resource); |
| 155 | ASSERT_FALSE(expected_path.empty()); |
[email protected] | af958dd | 2009-10-22 20:56:18 | [diff] [blame] | 156 | |
| 157 | EXPECT_EQ(ToLower(expected_path.value()), ToLower(resolved_path.value())); |
[email protected] | 1288ba0 | 2009-10-15 00:02:24 | [diff] [blame] | 158 | EXPECT_EQ(ToLower(temp.path().value()), |
| 159 | ToLower(resource.extension_root().value())); |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 160 | EXPECT_EQ(ToLower(base::FilePath().AppendASCII(filename).value()), |
[email protected] | 1288ba0 | 2009-10-15 00:02:24 | [diff] [blame] | 161 | ToLower(resource.relative_path().value())); |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 162 | } |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 163 | |
| 164 | } // namespace extensions |