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.
7 #include "base/file_util.h"
8 #include "base/path_service.h"
9 #include "base/scoped_temp_dir.h"
10 #include "chrome/common/chrome_paths.h"
11 #include "chrome/common/extensions/extension.h"
12 #include "chrome/common/extensions/extension_l10n_util.h"
13 #include "chrome/common/extensions/extension_resource.h"
14 #include "chrome/common/extensions/extension_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/l10n/l10n_util.h"
18 TEST(ExtensionResourceTest
, CreateEmptyResource
) {
19 ExtensionResource resource
;
21 EXPECT_TRUE(resource
.extension_root().empty());
22 EXPECT_TRUE(resource
.relative_path().empty());
23 EXPECT_TRUE(resource
.GetFilePath().empty());
26 const FilePath::StringType
ToLower(const FilePath::StringType
& in_str
) {
27 FilePath::StringType
str(in_str
);
28 std::transform(str
.begin(), str
.end(), str
.begin(), tolower
);
32 TEST(ExtensionResourceTest
, CreateWithMissingResourceOnDisk
) {
34 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &root_path
));
35 FilePath relative_path
;
36 relative_path
= relative_path
.AppendASCII("cira.js");
37 std::string extension_id
= extension_test_util::MakeId("test");
38 ExtensionResource
resource(extension_id
, root_path
, relative_path
);
40 // The path doesn't exist on disk, we will be returned an empty path.
41 EXPECT_EQ(root_path
.value(), resource
.extension_root().value());
42 EXPECT_EQ(relative_path
.value(), resource
.relative_path().value());
43 EXPECT_TRUE(resource
.GetFilePath().empty());
46 TEST(ExtensionResourceTest
, ResourcesOutsideOfPath
) {
48 ASSERT_TRUE(temp
.CreateUniqueTempDir());
50 FilePath inner_dir
= temp
.path().AppendASCII("directory");
51 ASSERT_TRUE(file_util::CreateDirectory(inner_dir
));
52 FilePath inner_file
= inner_dir
.AppendASCII("inner");
53 FilePath outer_file
= temp
.path().AppendASCII("outer");
54 ASSERT_TRUE(file_util::WriteFile(outer_file
, "X", 1));
55 ASSERT_TRUE(file_util::WriteFile(inner_file
, "X", 1));
56 std::string extension_id
= extension_test_util::MakeId("test");
59 FilePath symlink_file
= inner_dir
.AppendASCII("symlink");
60 file_util::CreateSymbolicLink(
61 FilePath().AppendASCII("..").AppendASCII("outer"),
65 // A non-packing extension should be able to access the file within the
67 ExtensionResource
r1(extension_id
, inner_dir
,
68 FilePath().AppendASCII("inner"));
69 EXPECT_FALSE(r1
.GetFilePath().empty());
71 // ... but not a relative path that walks out of |inner_dir|.
72 ExtensionResource
r2(extension_id
, inner_dir
,
73 FilePath().AppendASCII("..").AppendASCII("outer"));
74 EXPECT_TRUE(r2
.GetFilePath().empty());
76 // A packing extension should also be able to access the file within the
78 ExtensionResource
r3(extension_id
, inner_dir
,
79 FilePath().AppendASCII("inner"));
80 r3
.set_follow_symlinks_anywhere();
81 EXPECT_FALSE(r3
.GetFilePath().empty());
83 // ... but, again, not a relative path that walks out of |inner_dir|.
84 ExtensionResource
r4(extension_id
, inner_dir
,
85 FilePath().AppendASCII("..").AppendASCII("outer"));
86 r4
.set_follow_symlinks_anywhere();
87 EXPECT_TRUE(r4
.GetFilePath().empty());
90 // The non-packing extension should also not be able to access a resource that
91 // symlinks out of the directory.
92 ExtensionResource
r5(extension_id
, inner_dir
,
93 FilePath().AppendASCII("symlink"));
94 EXPECT_TRUE(r5
.GetFilePath().empty());
96 // ... but a packing extension can.
97 ExtensionResource
r6(extension_id
, inner_dir
,
98 FilePath().AppendASCII("symlink"));
99 r6
.set_follow_symlinks_anywhere();
100 EXPECT_FALSE(r6
.GetFilePath().empty());
104 // crbug.com/108721. Disabled on Windows due to crashing on Vista.
106 #define CreateWithAllResourcesOnDisk DISABLED_CreateWithAllResourcesOnDisk
108 TEST(ExtensionResourceTest
, CreateWithAllResourcesOnDisk
) {
110 ASSERT_TRUE(temp
.CreateUniqueTempDir());
112 // Create resource in the extension root.
113 const char* filename
= "res.ico";
114 FilePath root_resource
= temp
.path().AppendASCII(filename
);
115 std::string data
= "some foo";
116 ASSERT_TRUE(file_util::WriteFile(root_resource
, data
.c_str(), data
.length()));
118 // Create l10n resources (for current locale and its parents).
119 FilePath l10n_path
= temp
.path().Append(extensions::Extension::kLocaleFolder
);
120 ASSERT_TRUE(file_util::CreateDirectory(l10n_path
));
122 std::vector
<std::string
> locales
;
123 l10n_util::GetParentLocales(l10n_util::GetApplicationLocale(""), &locales
);
124 ASSERT_FALSE(locales
.empty());
125 for (size_t i
= 0; i
< locales
.size(); i
++) {
127 make_path
= l10n_path
.AppendASCII(locales
[i
]);
128 ASSERT_TRUE(file_util::CreateDirectory(make_path
));
129 ASSERT_TRUE(file_util::WriteFile(make_path
.AppendASCII(filename
),
130 data
.c_str(), data
.length()));
134 std::string extension_id
= extension_test_util::MakeId("test");
135 ExtensionResource
resource(extension_id
, temp
.path(),
136 FilePath().AppendASCII(filename
));
137 FilePath resolved_path
= resource
.GetFilePath();
139 FilePath expected_path
;
140 // Expect default path only, since fallback logic is disabled.
141 // See http://crbug.com/27359.
142 expected_path
= root_resource
;
143 ASSERT_TRUE(file_util::AbsolutePath(&expected_path
));
145 EXPECT_EQ(ToLower(expected_path
.value()), ToLower(resolved_path
.value()));
146 EXPECT_EQ(ToLower(temp
.path().value()),
147 ToLower(resource
.extension_root().value()));
148 EXPECT_EQ(ToLower(FilePath().AppendASCII(filename
).value()),
149 ToLower(resource
.relative_path().value()));