| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/fileapi/isolated_context.h" | 5 #include "webkit/fileapi/isolated_context.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "webkit/fileapi/isolated_context.h" |
| 12 | 13 |
| 13 #define FPL(x) FILE_PATH_LITERAL(x) | 14 #define FPL(x) FILE_PATH_LITERAL(x) |
| 14 | 15 |
| 15 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | 16 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
| 16 #define DRIVE FPL("C:") | 17 #define DRIVE FPL("C:") |
| 17 #else | 18 #else |
| 18 #define DRIVE | 19 #define DRIVE |
| 19 #endif | 20 #endif |
| 20 | 21 |
| 21 namespace fileapi { | 22 namespace fileapi { |
| 22 | 23 |
| 24 typedef IsolatedContext::FileInfo FileInfo; |
| 25 |
| 23 namespace { | 26 namespace { |
| 24 | 27 |
| 25 const FilePath kTestPaths[] = { | 28 const FilePath kTestPaths[] = { |
| 26 FilePath(DRIVE FPL("/a/b")), | 29 FilePath(DRIVE FPL("/a/b.txt")), |
| 27 FilePath(DRIVE FPL("/c/d/e")), | 30 FilePath(DRIVE FPL("/c/d/e")), |
| 28 FilePath(DRIVE FPL("/h/")), | 31 FilePath(DRIVE FPL("/h/")), |
| 32 FilePath(DRIVE FPL("/")), |
| 29 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 33 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 30 FilePath(DRIVE FPL("\\foo\\bar")), | 34 FilePath(DRIVE FPL("\\foo\\bar")), |
| 35 FilePath(DRIVE FPL("\\")), |
| 31 #endif | 36 #endif |
| 37 // For duplicated base name test. |
| 38 FilePath(DRIVE FPL("/")), |
| 39 FilePath(DRIVE FPL("/f/e")), |
| 40 FilePath(DRIVE FPL("/f/b.txt")), |
| 32 }; | 41 }; |
| 33 | 42 |
| 34 } // namespace | 43 } // namespace |
| 35 | 44 |
| 36 class IsolatedContextTest : public testing::Test { | 45 class IsolatedContextTest : public testing::Test { |
| 37 public: | 46 public: |
| 38 IsolatedContextTest() { | 47 IsolatedContextTest() { |
| 39 for (size_t i = 0; i < arraysize(kTestPaths); ++i) | 48 for (size_t i = 0; i < arraysize(kTestPaths); ++i) |
| 40 fileset_.insert(kTestPaths[i].NormalizePathSeparators()); | 49 fileset_.insert(kTestPaths[i].NormalizePathSeparators()); |
| 41 } | 50 } |
| 42 | 51 |
| 43 void SetUp() { | 52 void SetUp() { |
| 44 id_ = IsolatedContext::GetInstance()->RegisterIsolatedFileSystem(fileset_); | 53 IsolatedContext::FileInfoSet files; |
| 54 for (size_t i = 0; i < arraysize(kTestPaths); ++i) |
| 55 names_.push_back(files.AddPath(kTestPaths[i].NormalizePathSeparators())); |
| 56 id_ = IsolatedContext::GetInstance()->RegisterFileSystem(files); |
| 45 ASSERT_FALSE(id_.empty()); | 57 ASSERT_FALSE(id_.empty()); |
| 46 } | 58 } |
| 47 | 59 |
| 48 void TearDown() { | 60 void TearDown() { |
| 49 IsolatedContext::GetInstance()->RevokeIsolatedFileSystem(id_); | 61 IsolatedContext::GetInstance()->RevokeFileSystem(id_); |
| 50 } | 62 } |
| 51 | 63 |
| 52 IsolatedContext* isolated_context() const { | 64 IsolatedContext* isolated_context() const { |
| 53 return IsolatedContext::GetInstance(); | 65 return IsolatedContext::GetInstance(); |
| 54 } | 66 } |
| 55 | 67 |
| 56 protected: | 68 protected: |
| 57 std::string id_; | 69 std::string id_; |
| 58 std::set<FilePath> fileset_; | 70 std::multiset<FilePath> fileset_; |
| 71 std::vector<std::string> names_; |
| 59 | 72 |
| 60 private: | 73 private: |
| 61 DISALLOW_COPY_AND_ASSIGN(IsolatedContextTest); | 74 DISALLOW_COPY_AND_ASSIGN(IsolatedContextTest); |
| 62 }; | 75 }; |
| 63 | 76 |
| 64 TEST_F(IsolatedContextTest, RegisterAndRevokeTest) { | 77 TEST_F(IsolatedContextTest, RegisterAndRevokeTest) { |
| 65 // See if the returned top-level entries match with what we registered. | 78 // See if the returned top-level entries match with what we registered. |
| 66 std::vector<FilePath> toplevels; | 79 std::vector<FileInfo> toplevels; |
| 67 ASSERT_TRUE(isolated_context()->GetTopLevelPaths(id_, &toplevels)); | 80 ASSERT_TRUE(isolated_context()->GetRegisteredFileInfo(id_, &toplevels)); |
| 68 ASSERT_EQ(fileset_.size(), toplevels.size()); | 81 ASSERT_EQ(fileset_.size(), toplevels.size()); |
| 69 for (size_t i = 0; i < toplevels.size(); ++i) { | 82 for (size_t i = 0; i < toplevels.size(); ++i) { |
| 70 ASSERT_TRUE(fileset_.find(toplevels[i]) != fileset_.end()); | 83 ASSERT_TRUE(fileset_.find(toplevels[i].path) != fileset_.end()); |
| 71 } | 84 } |
| 72 | 85 |
| 73 // See if the basename of each registered kTestPaths (that is what we | 86 // See if the name of each registered kTestPaths (that is what we |
| 74 // register in SetUp() by RegisterIsolatedFileSystem) is properly cracked as | 87 // register in SetUp() by RegisterFileSystem) is properly cracked as |
| 75 // a valid virtual path in the isolated filesystem. | 88 // a valid virtual path in the isolated filesystem. |
| 76 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { | 89 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { |
| 77 FilePath virtual_path = isolated_context()->CreateVirtualPath( | 90 FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id_) |
| 78 id_, kTestPaths[i].BaseName()); | 91 .AppendASCII(names_[i]); |
| 79 std::string cracked_id; | 92 std::string cracked_id; |
| 80 FilePath root_path, cracked_path; | 93 FileInfo root_info; |
| 94 FilePath cracked_path; |
| 81 ASSERT_TRUE(isolated_context()->CrackIsolatedPath( | 95 ASSERT_TRUE(isolated_context()->CrackIsolatedPath( |
| 82 virtual_path, &cracked_id, &root_path, &cracked_path)); | 96 virtual_path, &cracked_id, &root_info, &cracked_path)); |
| 83 ASSERT_EQ(kTestPaths[i].NormalizePathSeparators().value(), | 97 ASSERT_EQ(kTestPaths[i].NormalizePathSeparators().value(), |
| 84 cracked_path.value()); | 98 cracked_path.value()); |
| 85 ASSERT_TRUE(fileset_.find(root_path.NormalizePathSeparators()) | 99 ASSERT_TRUE(fileset_.find(root_info.path.NormalizePathSeparators()) |
| 86 != fileset_.end()); | 100 != fileset_.end()); |
| 87 ASSERT_EQ(id_, cracked_id); | 101 ASSERT_EQ(id_, cracked_id); |
| 88 } | 102 } |
| 89 | 103 |
| 90 // Revoking the current one and registering a new (empty) one. | 104 // Revoking the current one and registering a new (empty) one. |
| 91 isolated_context()->RevokeIsolatedFileSystem(id_); | 105 isolated_context()->RevokeFileSystem(id_); |
| 92 std::string id2 = isolated_context()->RegisterIsolatedFileSystem( | 106 std::string id2 = isolated_context()->RegisterFileSystem( |
| 93 std::set<FilePath>()); | 107 IsolatedContext::FileInfoSet()); |
| 94 | 108 |
| 95 // Make sure the GetTopLevelPaths returns true only for the new one. | 109 // Make sure the GetRegisteredFileInfo returns true only for the new one. |
| 96 ASSERT_TRUE(isolated_context()->GetTopLevelPaths(id2, &toplevels)); | 110 ASSERT_TRUE(isolated_context()->GetRegisteredFileInfo(id2, &toplevels)); |
| 97 ASSERT_FALSE(isolated_context()->GetTopLevelPaths(id_, &toplevels)); | 111 ASSERT_FALSE(isolated_context()->GetRegisteredFileInfo(id_, &toplevels)); |
| 98 | 112 |
| 99 isolated_context()->RevokeIsolatedFileSystem(id2); | 113 isolated_context()->RevokeFileSystem(id2); |
| 100 } | 114 } |
| 101 | 115 |
| 102 TEST_F(IsolatedContextTest, CrackWithRelativePaths) { | 116 TEST_F(IsolatedContextTest, CrackWithRelativePaths) { |
| 103 const struct { | 117 const struct { |
| 104 FilePath::StringType path; | 118 FilePath::StringType path; |
| 105 bool valid; | 119 bool valid; |
| 106 } relatives[] = { | 120 } relatives[] = { |
| 107 { FPL("foo"), true }, | 121 { FPL("foo"), true }, |
| 108 { FPL("foo/bar"), true }, | 122 { FPL("foo/bar"), true }, |
| 109 { FPL(".."), false }, | 123 { FPL(".."), false }, |
| 110 { FPL("foo/.."), false }, | 124 { FPL("foo/.."), false }, |
| 111 { FPL("foo/../bar"), false }, | 125 { FPL("foo/../bar"), false }, |
| 112 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 126 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 113 # define SHOULD_FAIL_WITH_WIN_SEPARATORS false | 127 # define SHOULD_FAIL_WITH_WIN_SEPARATORS false |
| 114 #else | 128 #else |
| 115 # define SHOULD_FAIL_WITH_WIN_SEPARATORS true | 129 # define SHOULD_FAIL_WITH_WIN_SEPARATORS true |
| 116 #endif | 130 #endif |
| 117 { FPL("foo\\..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, | 131 { FPL("foo\\..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, |
| 118 { FPL("foo/..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, | 132 { FPL("foo/..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, |
| 119 }; | 133 }; |
| 120 | 134 |
| 121 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { | 135 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { |
| 122 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(relatives); ++j) { | 136 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(relatives); ++j) { |
| 123 SCOPED_TRACE(testing::Message() << "Testing " | 137 SCOPED_TRACE(testing::Message() << "Testing " |
| 124 << kTestPaths[i].value() << " " << relatives[j].path); | 138 << kTestPaths[i].value() << " " << relatives[j].path); |
| 125 FilePath virtual_path = isolated_context()->CreateVirtualPath( | 139 FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id_) |
| 126 id_, kTestPaths[i].BaseName().Append(relatives[j].path)); | 140 .AppendASCII(names_[i]).Append(relatives[j].path); |
| 127 std::string cracked_id; | 141 std::string cracked_id; |
| 128 FilePath root_path, cracked_path; | 142 FileInfo root_info; |
| 143 FilePath cracked_path; |
| 129 if (!relatives[j].valid) { | 144 if (!relatives[j].valid) { |
| 130 ASSERT_FALSE(isolated_context()->CrackIsolatedPath( | 145 ASSERT_FALSE(isolated_context()->CrackIsolatedPath( |
| 131 virtual_path, &cracked_id, &root_path, &cracked_path)); | 146 virtual_path, &cracked_id, &root_info, &cracked_path)); |
| 132 continue; | 147 continue; |
| 133 } | 148 } |
| 134 ASSERT_TRUE(isolated_context()->CrackIsolatedPath( | 149 ASSERT_TRUE(isolated_context()->CrackIsolatedPath( |
| 135 virtual_path, &cracked_id, &root_path, &cracked_path)); | 150 virtual_path, &cracked_id, &root_info, &cracked_path)); |
| 136 ASSERT_TRUE(fileset_.find(root_path.NormalizePathSeparators()) | 151 ASSERT_TRUE(fileset_.find(root_info.path.NormalizePathSeparators()) |
| 137 != fileset_.end()); | 152 != fileset_.end()); |
| 138 ASSERT_EQ(kTestPaths[i].Append(relatives[j].path) | 153 ASSERT_EQ(kTestPaths[i].Append(relatives[j].path) |
| 139 .NormalizePathSeparators().value(), | 154 .NormalizePathSeparators().value(), |
| 140 cracked_path.value()); | 155 cracked_path.value()); |
| 141 ASSERT_EQ(id_, cracked_id); | 156 ASSERT_EQ(id_, cracked_id); |
| 142 } | 157 } |
| 143 } | 158 } |
| 144 } | 159 } |
| 145 | 160 |
| 146 TEST_F(IsolatedContextTest, TestWithVirtualRoot) { | 161 TEST_F(IsolatedContextTest, TestWithVirtualRoot) { |
| 147 std::string cracked_id; | 162 std::string cracked_id; |
| 148 FilePath root_path, cracked_path; | 163 FilePath cracked_path; |
| 149 const FilePath root(FPL("/")); | |
| 150 | 164 |
| 151 // Trying to crack virtual root "/" returns true but with empty cracked path | 165 // Trying to crack virtual root "/" returns true but with empty cracked path |
| 152 // as "/" of the isolated filesystem is a pure virtual directory | 166 // as "/" of the isolated filesystem is a pure virtual directory |
| 153 // that has no corresponding platform directory. | 167 // that has no corresponding platform directory. |
| 154 FilePath virtual_path = isolated_context()->CreateVirtualPath(id_, root); | 168 FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id_); |
| 155 ASSERT_TRUE(isolated_context()->CrackIsolatedPath( | 169 ASSERT_TRUE(isolated_context()->CrackIsolatedPath( |
| 156 virtual_path, &cracked_id, &root_path, &cracked_path)); | 170 virtual_path, &cracked_id, NULL, &cracked_path)); |
| 157 ASSERT_EQ(FPL(""), cracked_path.value()); | 171 ASSERT_EQ(FPL(""), cracked_path.value()); |
| 158 ASSERT_EQ(id_, cracked_id); | 172 ASSERT_EQ(id_, cracked_id); |
| 159 | 173 |
| 160 // Trying to crack "/foo" should fail (because "foo" is not the one | 174 // Trying to crack "/foo" should fail (because "foo" is not the one |
| 161 // included in the kTestPaths). | 175 // included in the kTestPaths). |
| 162 virtual_path = isolated_context()->CreateVirtualPath( | 176 virtual_path = isolated_context()->CreateVirtualRootPath( |
| 163 id_, FilePath(FPL("foo"))); | 177 id_).AppendASCII("foo"); |
| 164 ASSERT_FALSE(isolated_context()->CrackIsolatedPath( | 178 ASSERT_FALSE(isolated_context()->CrackIsolatedPath( |
| 165 virtual_path, &cracked_id, &root_path, &cracked_path)); | 179 virtual_path, &cracked_id, NULL, &cracked_path)); |
| 166 } | 180 } |
| 167 | 181 |
| 168 } // namespace fileapi | 182 } // namespace fileapi |
| OLD | NEW |