Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(539)

Side by Side Diff: chrome/common/extensions/extension_resource_unittest.cc

Issue 12578008: Move CrxFile, FileReader, ExtensionResource to src/extensions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 <algorithm>
6
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/path_service.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"
17
18 TEST(ExtensionResourceTest, CreateEmptyResource) {
19 ExtensionResource resource;
20
21 EXPECT_TRUE(resource.extension_root().empty());
22 EXPECT_TRUE(resource.relative_path().empty());
23 EXPECT_TRUE(resource.GetFilePath().empty());
24 }
25
26 const base::FilePath::StringType ToLower(
27 const base::FilePath::StringType& in_str) {
28 base::FilePath::StringType str(in_str);
29 std::transform(str.begin(), str.end(), str.begin(), tolower);
30 return str;
31 }
32
33 TEST(ExtensionResourceTest, CreateWithMissingResourceOnDisk) {
34 base::FilePath root_path;
35 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &root_path));
36 base::FilePath relative_path;
37 relative_path = relative_path.AppendASCII("cira.js");
38 std::string extension_id = extension_test_util::MakeId("test");
39 ExtensionResource resource(extension_id, root_path, relative_path);
40
41 // The path doesn't exist on disk, we will be returned an empty path.
42 EXPECT_EQ(root_path.value(), resource.extension_root().value());
43 EXPECT_EQ(relative_path.value(), resource.relative_path().value());
44 EXPECT_TRUE(resource.GetFilePath().empty());
45 }
46
47 TEST(ExtensionResourceTest, ResourcesOutsideOfPath) {
48 base::ScopedTempDir temp;
49 ASSERT_TRUE(temp.CreateUniqueTempDir());
50
51 base::FilePath inner_dir = temp.path().AppendASCII("directory");
52 ASSERT_TRUE(file_util::CreateDirectory(inner_dir));
53 base::FilePath sub_dir = inner_dir.AppendASCII("subdir");
54 ASSERT_TRUE(file_util::CreateDirectory(sub_dir));
55 base::FilePath inner_file = inner_dir.AppendASCII("inner");
56 base::FilePath outer_file = temp.path().AppendASCII("outer");
57 ASSERT_TRUE(file_util::WriteFile(outer_file, "X", 1));
58 ASSERT_TRUE(file_util::WriteFile(inner_file, "X", 1));
59 std::string extension_id = extension_test_util::MakeId("test");
60
61 #if defined(OS_POSIX)
62 base::FilePath symlink_file = inner_dir.AppendASCII("symlink");
63 file_util::CreateSymbolicLink(
64 base::FilePath().AppendASCII("..").AppendASCII("outer"),
65 symlink_file);
66 #endif
67
68 // A non-packing extension should be able to access the file within the
69 // directory.
70 ExtensionResource r1(extension_id, inner_dir,
71 base::FilePath().AppendASCII("inner"));
72 EXPECT_FALSE(r1.GetFilePath().empty());
73
74 // ... but not a relative path that walks out of |inner_dir|.
75 ExtensionResource r2(extension_id, inner_dir,
76 base::FilePath().AppendASCII("..").AppendASCII("outer"));
77 EXPECT_TRUE(r2.GetFilePath().empty());
78
79 // A packing extension should also be able to access the file within the
80 // directory.
81 ExtensionResource r3(extension_id, inner_dir,
82 base::FilePath().AppendASCII("inner"));
83 r3.set_follow_symlinks_anywhere();
84 EXPECT_FALSE(r3.GetFilePath().empty());
85
86 // ... but, again, not a relative path that walks out of |inner_dir|.
87 ExtensionResource r4(extension_id, inner_dir,
88 base::FilePath().AppendASCII("..").AppendASCII("outer"));
89 r4.set_follow_symlinks_anywhere();
90 EXPECT_TRUE(r4.GetFilePath().empty());
91
92 // ... and not even when clever current-directory syntax is present. Note
93 // that the path for this test case can't start with the current directory
94 // component due to quirks in FilePath::Append(), and the path must exist.
95 ExtensionResource r4a(
96 extension_id, inner_dir,
97 base::FilePath().AppendASCII("subdir").AppendASCII(".").AppendASCII("..").
98 AppendASCII("..").AppendASCII("outer"));
99 r4a.set_follow_symlinks_anywhere();
100 EXPECT_TRUE(r4a.GetFilePath().empty());
101
102 #if defined(OS_POSIX)
103 // The non-packing extension should also not be able to access a resource that
104 // symlinks out of the directory.
105 ExtensionResource r5(extension_id, inner_dir,
106 base::FilePath().AppendASCII("symlink"));
107 EXPECT_TRUE(r5.GetFilePath().empty());
108
109 // ... but a packing extension can.
110 ExtensionResource r6(extension_id, inner_dir,
111 base::FilePath().AppendASCII("symlink"));
112 r6.set_follow_symlinks_anywhere();
113 EXPECT_FALSE(r6.GetFilePath().empty());
114 #endif
115 }
116
117 TEST(ExtensionResourceTest, CreateWithAllResourcesOnDisk) {
118 base::ScopedTempDir temp;
119 ASSERT_TRUE(temp.CreateUniqueTempDir());
120
121 // Create resource in the extension root.
122 const char* filename = "res.ico";
123 base::FilePath root_resource = temp.path().AppendASCII(filename);
124 std::string data = "some foo";
125 ASSERT_TRUE(file_util::WriteFile(root_resource, data.c_str(), data.length()));
126
127 // Create l10n resources (for current locale and its parents).
128 base::FilePath l10n_path =
129 temp.path().Append(extensions::Extension::kLocaleFolder);
130 ASSERT_TRUE(file_util::CreateDirectory(l10n_path));
131
132 std::vector<std::string> locales;
133 l10n_util::GetParentLocales(l10n_util::GetApplicationLocale(""), &locales);
134 ASSERT_FALSE(locales.empty());
135 for (size_t i = 0; i < locales.size(); i++) {
136 base::FilePath make_path;
137 make_path = l10n_path.AppendASCII(locales[i]);
138 ASSERT_TRUE(file_util::CreateDirectory(make_path));
139 ASSERT_TRUE(file_util::WriteFile(make_path.AppendASCII(filename),
140 data.c_str(), data.length()));
141 }
142
143 base::FilePath path;
144 std::string extension_id = extension_test_util::MakeId("test");
145 ExtensionResource resource(extension_id, temp.path(),
146 base::FilePath().AppendASCII(filename));
147 base::FilePath resolved_path = resource.GetFilePath();
148
149 base::FilePath expected_path;
150 // Expect default path only, since fallback logic is disabled.
151 // See http://crbug.com/27359.
152 expected_path = root_resource;
153 ASSERT_TRUE(file_util::AbsolutePath(&expected_path));
154
155 EXPECT_EQ(ToLower(expected_path.value()), ToLower(resolved_path.value()));
156 EXPECT_EQ(ToLower(temp.path().value()),
157 ToLower(resource.extension_root().value()));
158 EXPECT_EQ(ToLower(base::FilePath().AppendASCII(filename).value()),
159 ToLower(resource.relative_path().value()));
160 }
OLDNEW
« no previous file with comments | « chrome/common/extensions/extension_resource.cc ('k') | chrome/common/extensions/extension_test_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698