1 // Copyright 2013 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.
5 #include "extensions/common/file_util.h"
7 #include "base/basictypes.h"
8 #include "base/file_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
12 typedef testing::Test FileUtilTest
;
14 TEST_F(FileUtilTest
, ExtensionURLToRelativeFilePath
) {
15 #define URL_PREFIX "chrome-extension://extension-id/"
18 const char* expected_relative_path
;
20 { URL_PREFIX
"simple.html",
22 { URL_PREFIX
"directory/to/file.html",
23 "directory/to/file.html" },
24 { URL_PREFIX
"escape%20spaces.html",
25 "escape spaces.html" },
26 { URL_PREFIX
"%C3%9Cber.html",
27 "\xC3\x9C" "ber.html" },
29 { URL_PREFIX
"C%3A/simple.html",
32 { URL_PREFIX
"////simple.html",
34 { URL_PREFIX
"/simple.html",
36 { URL_PREFIX
"\\simple.html",
38 { URL_PREFIX
"\\\\foo\\simple.html",
43 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_cases
); ++i
) {
44 GURL
url(test_cases
[i
].url
);
45 base::FilePath expected_path
=
46 base::FilePath::FromUTF8Unsafe(test_cases
[i
].expected_relative_path
);
47 base::FilePath actual_path
=
48 extensions::file_util::ExtensionURLToRelativeFilePath(url
);
49 EXPECT_FALSE(actual_path
.IsAbsolute()) <<
50 " For the path " << actual_path
.value();
51 EXPECT_EQ(expected_path
.value(), actual_path
.value()) <<
52 " For the path " << url
;
56 TEST_F(FileUtilTest
, ExtensionResourceURLToFilePath
) {
57 // Setup filesystem for testing.
58 base::FilePath root_path
;
59 ASSERT_TRUE(base::CreateNewTempDirectory(
60 base::FilePath::StringType(), &root_path
));
61 root_path
= base::MakeAbsoluteFilePath(root_path
);
62 ASSERT_FALSE(root_path
.empty());
64 base::FilePath api_path
= root_path
.Append(FILE_PATH_LITERAL("apiname"));
65 ASSERT_TRUE(base::CreateDirectory(api_path
));
67 const char data
[] = "Test Data";
68 base::FilePath resource_path
= api_path
.Append(FILE_PATH_LITERAL("test.js"));
69 ASSERT_TRUE(file_util::WriteFile(resource_path
, data
, sizeof(data
)));
70 resource_path
= api_path
.Append(FILE_PATH_LITERAL("escape spaces.js"));
71 ASSERT_TRUE(file_util::WriteFile(resource_path
, data
, sizeof(data
)));
73 #ifdef FILE_PATH_USES_WIN_SEPARATORS
78 #define URL_PREFIX "chrome-extension-resource://"
81 const base::FilePath::CharType
* expected_path
;
83 { URL_PREFIX
"apiname/test.js",
84 FILE_PATH_LITERAL("test.js") },
85 { URL_PREFIX
"/apiname/test.js",
86 FILE_PATH_LITERAL("test.js") },
88 { URL_PREFIX
"apiname/%74%65st.js",
89 FILE_PATH_LITERAL("test.js") },
90 { URL_PREFIX
"apiname/escape%20spaces.js",
91 FILE_PATH_LITERAL("escape spaces.js") },
92 // Test file does not exist.
93 { URL_PREFIX
"apiname/directory/to/file.js",
95 // Test apiname/../../test.js
96 { URL_PREFIX
"apiname/../../test.js",
97 FILE_PATH_LITERAL("test.js") },
98 { URL_PREFIX
"apiname/..%2F../test.js",
100 { URL_PREFIX
"apiname/f/../../../test.js",
101 FILE_PATH_LITERAL("test.js") },
102 { URL_PREFIX
"apiname/f%2F..%2F..%2F../test.js",
108 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_cases
); ++i
) {
109 GURL
url(test_cases
[i
].url
);
110 base::FilePath expected_path
;
111 if (test_cases
[i
].expected_path
)
112 expected_path
= root_path
.Append(FILE_PATH_LITERAL("apiname")).Append(
113 test_cases
[i
].expected_path
);
114 base::FilePath actual_path
=
115 extensions::file_util::ExtensionResourceURLToFilePath(url
, root_path
);
116 EXPECT_EQ(expected_path
.value(), actual_path
.value()) <<
117 " For the path " << url
;
119 // Remove temp files.
120 ASSERT_TRUE(base::DeleteFile(root_path
, true));