blob: eb16e5bb28345803b09dfb88ab1ea08e40d42037 [file] [log] [blame]
[email protected]3d41d432012-04-20 20:47:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ecabe6ee2009-10-07 22:49:102// 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]ecabe6ee2009-10-07 22:49:107#include "base/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:238#include "base/files/scoped_temp_dir.h"
[email protected]ecabe6ee2009-10-07 22:49:109#include "base/path_service.h"
[email protected]993da5e2013-03-23 21:25:1610#include "extensions/common/constants.h"
[email protected]32efb042013-03-29 00:23:2111#include "extensions/common/extension_paths.h"
[email protected]993da5e2013-03-23 21:25:1612#include "extensions/common/extension_resource.h"
13#include "extensions/common/id_util.h"
[email protected]ecabe6ee2009-10-07 22:49:1014#include "testing/gtest/include/gtest/gtest.h"
[email protected]c051a1b2011-01-21 23:30:1715#include "ui/base/l10n/l10n_util.h"
[email protected]ecabe6ee2009-10-07 22:49:1016
[email protected]993da5e2013-03-23 21:25:1617namespace extensions {
18
[email protected]ecabe6ee2009-10-07 22:49:1019TEST(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]a7329162013-02-07 19:21:4827const base::FilePath::StringType ToLower(
28 const base::FilePath::StringType& in_str) {
29 base::FilePath::StringType str(in_str);
[email protected]ecabe6ee2009-10-07 22:49:1030 std::transform(str.begin(), str.end(), str.begin(), tolower);
31 return str;
32}
33
34TEST(ExtensionResourceTest, CreateWithMissingResourceOnDisk) {
[email protected]a7329162013-02-07 19:21:4835 base::FilePath root_path;
[email protected]32efb042013-03-29 00:23:2136 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &root_path));
[email protected]a7329162013-02-07 19:21:4837 base::FilePath relative_path;
[email protected]ecabe6ee2009-10-07 22:49:1038 relative_path = relative_path.AppendASCII("cira.js");
[email protected]993da5e2013-03-23 21:25:1639 std::string extension_id = id_util::GenerateId("test");
[email protected]052c92702010-06-25 07:25:5240 ExtensionResource resource(extension_id, root_path, relative_path);
[email protected]ecabe6ee2009-10-07 22:49:1041
[email protected]a14b16b2009-10-28 12:41:2942 // The path doesn't exist on disk, we will be returned an empty path.
[email protected]ecabe6ee2009-10-07 22:49:1043 EXPECT_EQ(root_path.value(), resource.extension_root().value());
44 EXPECT_EQ(relative_path.value(), resource.relative_path().value());
[email protected]a14b16b2009-10-28 12:41:2945 EXPECT_TRUE(resource.GetFilePath().empty());
[email protected]ecabe6ee2009-10-07 22:49:1046}
47
[email protected]3d41d432012-04-20 20:47:5848TEST(ExtensionResourceTest, ResourcesOutsideOfPath) {
[email protected]ea1a3f62012-11-16 20:34:2349 base::ScopedTempDir temp;
[email protected]3d41d432012-04-20 20:47:5850 ASSERT_TRUE(temp.CreateUniqueTempDir());
51
[email protected]a7329162013-02-07 19:21:4852 base::FilePath inner_dir = temp.path().AppendASCII("directory");
[email protected]426d1c92013-12-03 20:08:5453 ASSERT_TRUE(base::CreateDirectory(inner_dir));
[email protected]a7329162013-02-07 19:21:4854 base::FilePath sub_dir = inner_dir.AppendASCII("subdir");
[email protected]426d1c92013-12-03 20:08:5455 ASSERT_TRUE(base::CreateDirectory(sub_dir));
[email protected]a7329162013-02-07 19:21:4856 base::FilePath inner_file = inner_dir.AppendASCII("inner");
57 base::FilePath outer_file = temp.path().AppendASCII("outer");
[email protected]e5c2a22e2014-03-06 20:42:3058 ASSERT_TRUE(base::WriteFile(outer_file, "X", 1));
59 ASSERT_TRUE(base::WriteFile(inner_file, "X", 1));
[email protected]993da5e2013-03-23 21:25:1660 std::string extension_id = id_util::GenerateId("test");
[email protected]3d41d432012-04-20 20:47:5861
62#if defined(OS_POSIX)
[email protected]a7329162013-02-07 19:21:4863 base::FilePath symlink_file = inner_dir.AppendASCII("symlink");
[email protected]b264eab2013-11-27 23:22:0864 base::CreateSymbolicLink(
[email protected]a7329162013-02-07 19:21:4865 base::FilePath().AppendASCII("..").AppendASCII("outer"),
[email protected]3d41d432012-04-20 20:47:5866 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]a7329162013-02-07 19:21:4872 base::FilePath().AppendASCII("inner"));
[email protected]3d41d432012-04-20 20:47:5873 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]a7329162013-02-07 19:21:4877 base::FilePath().AppendASCII("..").AppendASCII("outer"));
[email protected]3d41d432012-04-20 20:47:5878 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]a7329162013-02-07 19:21:4883 base::FilePath().AppendASCII("inner"));
[email protected]3d41d432012-04-20 20:47:5884 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]a7329162013-02-07 19:21:4889 base::FilePath().AppendASCII("..").AppendASCII("outer"));
[email protected]3d41d432012-04-20 20:47:5890 r4.set_follow_symlinks_anywhere();
91 EXPECT_TRUE(r4.GetFilePath().empty());
92
[email protected]730258b82012-11-28 15:27:2293 // ... 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]a7329162013-02-07 19:21:4898 base::FilePath().AppendASCII("subdir").AppendASCII(".").AppendASCII("..").
[email protected]730258b82012-11-28 15:27:2299 AppendASCII("..").AppendASCII("outer"));
100 r4a.set_follow_symlinks_anywhere();
101 EXPECT_TRUE(r4a.GetFilePath().empty());
102
[email protected]3d41d432012-04-20 20:47:58103#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]a7329162013-02-07 19:21:48107 base::FilePath().AppendASCII("symlink"));
[email protected]3d41d432012-04-20 20:47:58108 EXPECT_TRUE(r5.GetFilePath().empty());
109
110 // ... but a packing extension can.
111 ExtensionResource r6(extension_id, inner_dir,
[email protected]a7329162013-02-07 19:21:48112 base::FilePath().AppendASCII("symlink"));
[email protected]3d41d432012-04-20 20:47:58113 r6.set_follow_symlinks_anywhere();
114 EXPECT_FALSE(r6.GetFilePath().empty());
115#endif
116}
117
[email protected]af958dd2009-10-22 20:56:18118TEST(ExtensionResourceTest, CreateWithAllResourcesOnDisk) {
[email protected]ea1a3f62012-11-16 20:34:23119 base::ScopedTempDir temp;
[email protected]ecabe6ee2009-10-07 22:49:10120 ASSERT_TRUE(temp.CreateUniqueTempDir());
121
[email protected]af958dd2009-10-22 20:56:18122 // Create resource in the extension root.
[email protected]ecabe6ee2009-10-07 22:49:10123 const char* filename = "res.ico";
[email protected]a7329162013-02-07 19:21:48124 base::FilePath root_resource = temp.path().AppendASCII(filename);
[email protected]ecabe6ee2009-10-07 22:49:10125 std::string data = "some foo";
[email protected]e5c2a22e2014-03-06 20:42:30126 ASSERT_TRUE(base::WriteFile(root_resource, data.c_str(), data.length()));
[email protected]ecabe6ee2009-10-07 22:49:10127
[email protected]af958dd2009-10-22 20:56:18128 // Create l10n resources (for current locale and its parents).
[email protected]a7329162013-02-07 19:21:48129 base::FilePath l10n_path =
[email protected]993da5e2013-03-23 21:25:16130 temp.path().Append(kLocaleFolder);
[email protected]426d1c92013-12-03 20:08:54131 ASSERT_TRUE(base::CreateDirectory(l10n_path));
[email protected]ecabe6ee2009-10-07 22:49:10132
[email protected]af958dd2009-10-22 20:56:18133 std::vector<std::string> locales;
[email protected]007b3f82013-04-09 08:46:45134 l10n_util::GetParentLocales(l10n_util::GetApplicationLocale(std::string()),
135 &locales);
[email protected]14e101c02009-11-11 21:51:51136 ASSERT_FALSE(locales.empty());
[email protected]af958dd2009-10-22 20:56:18137 for (size_t i = 0; i < locales.size(); i++) {
[email protected]a7329162013-02-07 19:21:48138 base::FilePath make_path;
[email protected]af958dd2009-10-22 20:56:18139 make_path = l10n_path.AppendASCII(locales[i]);
[email protected]426d1c92013-12-03 20:08:54140 ASSERT_TRUE(base::CreateDirectory(make_path));
[email protected]e5c2a22e2014-03-06 20:42:30141 ASSERT_TRUE(base::WriteFile(make_path.AppendASCII(filename),
[email protected]af958dd2009-10-22 20:56:18142 data.c_str(), data.length()));
143 }
[email protected]ecabe6ee2009-10-07 22:49:10144
[email protected]a7329162013-02-07 19:21:48145 base::FilePath path;
[email protected]993da5e2013-03-23 21:25:16146 std::string extension_id = id_util::GenerateId("test");
[email protected]052c92702010-06-25 07:25:52147 ExtensionResource resource(extension_id, temp.path(),
[email protected]a7329162013-02-07 19:21:48148 base::FilePath().AppendASCII(filename));
149 base::FilePath resolved_path = resource.GetFilePath();
[email protected]ecabe6ee2009-10-07 22:49:10150
[email protected]a7329162013-02-07 19:21:48151 base::FilePath expected_path;
[email protected]14e101c02009-11-11 21:51:51152 // Expect default path only, since fallback logic is disabled.
153 // See http://crbug.com/27359.
[email protected]15476932013-04-12 05:17:15154 expected_path = base::MakeAbsoluteFilePath(root_resource);
155 ASSERT_FALSE(expected_path.empty());
[email protected]af958dd2009-10-22 20:56:18156
157 EXPECT_EQ(ToLower(expected_path.value()), ToLower(resolved_path.value()));
[email protected]1288ba02009-10-15 00:02:24158 EXPECT_EQ(ToLower(temp.path().value()),
159 ToLower(resource.extension_root().value()));
[email protected]a7329162013-02-07 19:21:48160 EXPECT_EQ(ToLower(base::FilePath().AppendASCII(filename).value()),
[email protected]1288ba02009-10-15 00:02:24161 ToLower(resource.relative_path().value()));
[email protected]ecabe6ee2009-10-07 22:49:10162}
[email protected]993da5e2013-03-23 21:25:16163
164} // namespace extensions