| 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")), |
| 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 |
| 32 }; | 37 }; |
| 33 | 38 |
| 34 } // namespace | 39 } // namespace |
| 35 | 40 |
| 36 class IsolatedContextTest : public testing::Test { | 41 class IsolatedContextTest : public testing::Test { |
| 37 public: | 42 public: |
| 38 IsolatedContextTest() { | 43 IsolatedContextTest() { |
| 39 for (size_t i = 0; i < arraysize(kTestPaths); ++i) | 44 for (size_t i = 0; i < arraysize(kTestPaths); ++i) |
| 40 fileset_.insert(kTestPaths[i].NormalizePathSeparators()); | 45 fileset_.insert(kTestPaths[i].NormalizePathSeparators()); |
| 41 } | 46 } |
| 42 | 47 |
| 43 void SetUp() { | 48 void SetUp() { |
| 44 id_ = IsolatedContext::GetInstance()->RegisterIsolatedFileSystem(fileset_); | 49 std::vector<IsolatedContext::FileInfo> files; |
| 50 for (std::set<FilePath>::iterator iter = fileset_.begin(); |
| 51 iter != fileset_.end(); ++iter) { |
| 52 files.push_back(IsolatedContext::FileInfo( |
| 53 fileapi::IsolatedContext::GetNameForPath(*iter), |
| 54 iter->NormalizePathSeparators())); |
| 55 } |
| 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::set<FilePath> fileset_; |
| 59 | 71 |
| 60 private: | 72 private: |
| 61 DISALLOW_COPY_AND_ASSIGN(IsolatedContextTest); | 73 DISALLOW_COPY_AND_ASSIGN(IsolatedContextTest); |
| 62 }; | 74 }; |
| 63 | 75 |
| 64 TEST_F(IsolatedContextTest, RegisterAndRevokeTest) { | 76 TEST_F(IsolatedContextTest, RegisterAndRevokeTest) { |
| 65 // See if the returned top-level entries match with what we registered. | 77 // See if the returned top-level entries match with what we registered. |
| 66 std::vector<FilePath> toplevels; | 78 std::vector<FileInfo> toplevels; |
| 67 ASSERT_TRUE(isolated_context()->GetTopLevelPaths(id_, &toplevels)); | 79 ASSERT_TRUE(isolated_context()->GetRegisteredFileInfo(id_, &toplevels)); |
| 68 ASSERT_EQ(fileset_.size(), toplevels.size()); | 80 ASSERT_EQ(fileset_.size(), toplevels.size()); |
| 69 for (size_t i = 0; i < toplevels.size(); ++i) { | 81 for (size_t i = 0; i < toplevels.size(); ++i) { |
| 70 ASSERT_TRUE(fileset_.find(toplevels[i]) != fileset_.end()); | 82 ASSERT_TRUE(fileset_.find(toplevels[i].path) != fileset_.end()); |
| 71 } | 83 } |
| 72 | 84 |
| 73 // See if the basename of each registered kTestPaths (that is what we | 85 // See if the basename of each registered kTestPaths (that is what we |
| 74 // register in SetUp() by RegisterIsolatedFileSystem) is properly cracked as | 86 // register in SetUp() by RegisterFileSystem) is properly cracked as |
| 75 // a valid virtual path in the isolated filesystem. | 87 // a valid virtual path in the isolated filesystem. |
| 76 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { | 88 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { |
| 77 FilePath virtual_path = isolated_context()->CreateVirtualPath( | 89 FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id_) |
| 78 id_, kTestPaths[i].BaseName()); | 90 .AppendASCII(IsolatedContext::GetNameForPath(kTestPaths[i])); |
| 79 std::string cracked_id; | 91 std::string cracked_id; |
| 80 FilePath root_path, cracked_path; | 92 FileInfo root_info; |
| 93 FilePath cracked_path; |
| 81 ASSERT_TRUE(isolated_context()->CrackIsolatedPath( | 94 ASSERT_TRUE(isolated_context()->CrackIsolatedPath( |
| 82 virtual_path, &cracked_id, &root_path, &cracked_path)); | 95 virtual_path, &cracked_id, &root_info, &cracked_path)); |
| 83 ASSERT_EQ(kTestPaths[i].NormalizePathSeparators().value(), | 96 ASSERT_EQ(kTestPaths[i].NormalizePathSeparators().value(), |
| 84 cracked_path.value()); | 97 cracked_path.value()); |
| 85 ASSERT_TRUE(fileset_.find(root_path.NormalizePathSeparators()) | 98 ASSERT_TRUE(fileset_.find(root_info.path.NormalizePathSeparators()) |
| 86 != fileset_.end()); | 99 != fileset_.end()); |
| 87 ASSERT_EQ(id_, cracked_id); | 100 ASSERT_EQ(id_, cracked_id); |
| 88 } | 101 } |
| 89 | 102 |
| 90 // Revoking the current one and registering a new (empty) one. | 103 // Revoking the current one and registering a new (empty) one. |
| 91 isolated_context()->RevokeIsolatedFileSystem(id_); | 104 isolated_context()->RevokeFileSystem(id_); |
| 92 std::string id2 = isolated_context()->RegisterIsolatedFileSystem( | 105 std::string id2 = isolated_context()->RegisterFileSystem( |
| 93 std::set<FilePath>()); | 106 std::vector<FileInfo>()); |
| 94 | 107 |
| 95 // Make sure the GetTopLevelPaths returns true only for the new one. | 108 // Make sure the GetRegisteredFileInfo returns true only for the new one. |
| 96 ASSERT_TRUE(isolated_context()->GetTopLevelPaths(id2, &toplevels)); | 109 ASSERT_TRUE(isolated_context()->GetRegisteredFileInfo(id2, &toplevels)); |
| 97 ASSERT_FALSE(isolated_context()->GetTopLevelPaths(id_, &toplevels)); | 110 ASSERT_FALSE(isolated_context()->GetRegisteredFileInfo(id_, &toplevels)); |
| 98 | 111 |
| 99 isolated_context()->RevokeIsolatedFileSystem(id2); | 112 isolated_context()->RevokeFileSystem(id2); |
| 100 } | 113 } |
| 101 | 114 |
| 102 TEST_F(IsolatedContextTest, CrackWithRelativePaths) { | 115 TEST_F(IsolatedContextTest, CrackWithRelativePaths) { |
| 103 const struct { | 116 const struct { |
| 104 FilePath::StringType path; | 117 FilePath::StringType path; |
| 105 bool valid; | 118 bool valid; |
| 106 } relatives[] = { | 119 } relatives[] = { |
| 107 { FPL("foo"), true }, | 120 { FPL("foo"), true }, |
| 108 { FPL("foo/bar"), true }, | 121 { FPL("foo/bar"), true }, |
| 109 { FPL(".."), false }, | 122 { FPL(".."), false }, |
| 110 { FPL("foo/.."), false }, | 123 { FPL("foo/.."), false }, |
| 111 { FPL("foo/../bar"), false }, | 124 { FPL("foo/../bar"), false }, |
| 112 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 125 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 113 # define SHOULD_FAIL_WITH_WIN_SEPARATORS false | 126 # define SHOULD_FAIL_WITH_WIN_SEPARATORS false |
| 114 #else | 127 #else |
| 115 # define SHOULD_FAIL_WITH_WIN_SEPARATORS true | 128 # define SHOULD_FAIL_WITH_WIN_SEPARATORS true |
| 116 #endif | 129 #endif |
| 117 { FPL("foo\\..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, | 130 { FPL("foo\\..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, |
| 118 { FPL("foo/..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, | 131 { FPL("foo/..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, |
| 119 }; | 132 }; |
| 120 | 133 |
| 121 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { | 134 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { |
| 122 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(relatives); ++j) { | 135 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(relatives); ++j) { |
| 123 SCOPED_TRACE(testing::Message() << "Testing " | 136 SCOPED_TRACE(testing::Message() << "Testing " |
| 124 << kTestPaths[i].value() << " " << relatives[j].path); | 137 << kTestPaths[i].value() << " " << relatives[j].path); |
| 125 FilePath virtual_path = isolated_context()->CreateVirtualPath( | 138 FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id_) |
| 126 id_, kTestPaths[i].BaseName().Append(relatives[j].path)); | 139 .AppendASCII(IsolatedContext::GetNameForPath(kTestPaths[i])) |
| 140 .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 TEST_F(IsolatedContextTest, Writable) { | 182 TEST_F(IsolatedContextTest, Writable) { |
| 169 // By default the file system must be read-only. | 183 // By default the file system must be read-only. |
| 170 ASSERT_FALSE(isolated_context()->IsWritable(id_)); | 184 ASSERT_FALSE(isolated_context()->IsWritable(id_)); |
| 171 | 185 |
| 172 // Set writable. | 186 // Set writable. |
| 173 ASSERT_TRUE(isolated_context()->SetWritable(id_, true)); | 187 ASSERT_TRUE(isolated_context()->SetWritable(id_, true)); |
| 174 ASSERT_TRUE(isolated_context()->IsWritable(id_)); | 188 ASSERT_TRUE(isolated_context()->IsWritable(id_)); |
| 175 | 189 |
| 176 // Set non-writable. | 190 // Set non-writable. |
| 177 ASSERT_TRUE(isolated_context()->SetWritable(id_, false)); | 191 ASSERT_TRUE(isolated_context()->SetWritable(id_, false)); |
| 178 ASSERT_FALSE(isolated_context()->IsWritable(id_)); | 192 ASSERT_FALSE(isolated_context()->IsWritable(id_)); |
| 179 | 193 |
| 180 // Set writable again, and revoke the filesystem. | 194 // Set writable again, and revoke the filesystem. |
| 181 ASSERT_TRUE(isolated_context()->SetWritable(id_, true)); | 195 ASSERT_TRUE(isolated_context()->SetWritable(id_, true)); |
| 182 isolated_context()->RevokeIsolatedFileSystem(id_); | 196 isolated_context()->RevokeFileSystem(id_); |
| 183 | 197 |
| 184 // IsWritable should return false for non-registered file system. | 198 // IsWritable should return false for non-registered file system. |
| 185 ASSERT_FALSE(isolated_context()->IsWritable(id_)); | 199 ASSERT_FALSE(isolated_context()->IsWritable(id_)); |
| 186 // SetWritable should also return false for non-registered file system | 200 // SetWritable should also return false for non-registered file system |
| 187 // (no matter what value we give). | 201 // (no matter what value we give). |
| 188 ASSERT_FALSE(isolated_context()->SetWritable(id_, true)); | 202 ASSERT_FALSE(isolated_context()->SetWritable(id_, true)); |
| 189 ASSERT_FALSE(isolated_context()->SetWritable(id_, false)); | 203 ASSERT_FALSE(isolated_context()->SetWritable(id_, false)); |
| 190 } | 204 } |
| 191 | 205 |
| 192 } // namespace fileapi | 206 } // namespace fileapi |
| OLD | NEW |