OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "extensions/common/file_util.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/file_util.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "url/gurl.h" |
| 11 |
| 12 typedef testing::Test FileUtilTest; |
| 13 |
| 14 TEST_F(FileUtilTest, ExtensionURLToRelativeFilePath) { |
| 15 #define URL_PREFIX "chrome-extension://extension-id/" |
| 16 struct TestCase { |
| 17 const char* url; |
| 18 const char* expected_relative_path; |
| 19 } test_cases[] = { |
| 20 { URL_PREFIX "simple.html", |
| 21 "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" }, |
| 28 #if defined(OS_WIN) |
| 29 { URL_PREFIX "C%3A/simple.html", |
| 30 "" }, |
| 31 #endif |
| 32 { URL_PREFIX "////simple.html", |
| 33 "simple.html" }, |
| 34 { URL_PREFIX "/simple.html", |
| 35 "simple.html" }, |
| 36 { URL_PREFIX "\\simple.html", |
| 37 "simple.html" }, |
| 38 { URL_PREFIX "\\\\foo\\simple.html", |
| 39 "foo/simple.html" }, |
| 40 }; |
| 41 #undef URL_PREFIX |
| 42 |
| 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; |
| 53 } |
| 54 } |
| 55 |
| 56 TEST_F(FileUtilTest, ExtensionResourceURLToFilePath) { |
| 57 // Setup filesystem for testing. |
| 58 base::FilePath root_path; |
| 59 ASSERT_TRUE(file_util::CreateNewTempDirectory( |
| 60 base::FilePath::StringType(), &root_path)); |
| 61 root_path = base::MakeAbsoluteFilePath(root_path); |
| 62 ASSERT_FALSE(root_path.empty()); |
| 63 |
| 64 base::FilePath api_path = root_path.Append(FILE_PATH_LITERAL("apiname")); |
| 65 ASSERT_TRUE(file_util::CreateDirectory(api_path)); |
| 66 |
| 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))); |
| 72 |
| 73 #ifdef FILE_PATH_USES_WIN_SEPARATORS |
| 74 #define SEP "\\" |
| 75 #else |
| 76 #define SEP "/" |
| 77 #endif |
| 78 #define URL_PREFIX "chrome-extension-resource://" |
| 79 struct TestCase { |
| 80 const char* url; |
| 81 const base::FilePath::CharType* expected_path; |
| 82 } test_cases[] = { |
| 83 { URL_PREFIX "apiname/test.js", |
| 84 FILE_PATH_LITERAL("test.js") }, |
| 85 { URL_PREFIX "/apiname/test.js", |
| 86 FILE_PATH_LITERAL("test.js") }, |
| 87 // Test % escape |
| 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", |
| 94 NULL }, |
| 95 // Test apiname/../../test.js |
| 96 { URL_PREFIX "apiname/../../test.js", |
| 97 FILE_PATH_LITERAL("test.js") }, |
| 98 { URL_PREFIX "apiname/..%2F../test.js", |
| 99 NULL }, |
| 100 { URL_PREFIX "apiname/f/../../../test.js", |
| 101 FILE_PATH_LITERAL("test.js") }, |
| 102 { URL_PREFIX "apiname/f%2F..%2F..%2F../test.js", |
| 103 NULL }, |
| 104 }; |
| 105 #undef SEP |
| 106 #undef URL_PREFIX |
| 107 |
| 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; |
| 118 } |
| 119 // Remove temp files. |
| 120 ASSERT_TRUE(base::DeleteFile(root_path, true)); |
| 121 } |
OLD | NEW |