| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit) | 5 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit) |
| 6 // rather than as part of test_shell_tests because they rely on being able | 6 // rather than as part of test_shell_tests because they rely on being able |
| 7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses | 7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses |
| 8 // TYPE_UI, which URLRequest doesn't allow. | 8 // TYPE_UI, which URLRequest doesn't allow. |
| 9 // | 9 // |
| 10 | 10 |
| 11 #include "webkit/fileapi/file_system_dir_url_request_job.h" | 11 #include "webkit/fileapi/file_system_dir_url_request_job.h" |
| 12 | 12 |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 | 14 |
| 15 #include <string> | 15 #include <string> |
| 16 | 16 |
| 17 #include "base/file_path.h" | 17 #include "base/file_path.h" |
| 18 #include "base/format_macros.h" | 18 #include "base/format_macros.h" |
| 19 #include "base/message_loop.h" | 19 #include "base/message_loop.h" |
| 20 #include "base/platform_file.h" | 20 #include "base/platform_file.h" |
| 21 #include "base/scoped_temp_dir.h" | 21 #include "base/scoped_temp_dir.h" |
| 22 #include "base/string_piece.h" | 22 #include "base/string_piece.h" |
| 23 #include "base/utf_string_conversions.h" |
| 23 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 24 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
| 25 #include "net/http/http_request_headers.h" | 26 #include "net/http/http_request_headers.h" |
| 26 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
| 27 #include "net/url_request/url_request_test_util.h" | 28 #include "net/url_request/url_request_test_util.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #include "unicode/regex.h" |
| 29 #include "webkit/fileapi/file_system_context.h" | 31 #include "webkit/fileapi/file_system_context.h" |
| 30 #include "webkit/fileapi/file_system_file_util.h" | 32 #include "webkit/fileapi/file_system_file_util.h" |
| 31 #include "webkit/fileapi/file_system_operation_context.h" | 33 #include "webkit/fileapi/file_system_operation_context.h" |
| 32 #include "webkit/fileapi/file_system_path_manager.h" | 34 #include "webkit/fileapi/file_system_path_manager.h" |
| 33 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 35 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
| 34 | 36 |
| 35 namespace fileapi { | 37 namespace fileapi { |
| 36 namespace { | 38 namespace { |
| 37 | 39 |
| 38 // We always use the TEMPORARY FileSystem in this test. | 40 // We always use the TEMPORARY FileSystem in this test. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 } | 118 } |
| 117 | 119 |
| 118 void TestRequest(const GURL& url) { | 120 void TestRequest(const GURL& url) { |
| 119 TestRequestHelper(url, true); | 121 TestRequestHelper(url, true); |
| 120 } | 122 } |
| 121 | 123 |
| 122 void TestRequestNoRun(const GURL& url) { | 124 void TestRequestNoRun(const GURL& url) { |
| 123 TestRequestHelper(url, false); | 125 TestRequestHelper(url, false); |
| 124 } | 126 } |
| 125 | 127 |
| 128 FileSystemOperationContext* NewOperationContext(const FilePath& path) { |
| 129 FileSystemOperationContext* context(new FileSystemOperationContext( |
| 130 file_system_context_, file_util())); |
| 131 |
| 132 context->set_src_origin_url(GURL("http://remote")); |
| 133 context->set_src_virtual_path(path); |
| 134 context->set_src_type(fileapi::kFileSystemTypeTemporary); |
| 135 context->set_allowed_bytes_growth(1024); |
| 136 return context; |
| 137 } |
| 138 |
| 126 void CreateDirectory(const base::StringPiece dir_name) { | 139 void CreateDirectory(const base::StringPiece dir_name) { |
| 127 FilePath path = FilePath().AppendASCII(dir_name); | 140 FilePath path = FilePath().AppendASCII(dir_name); |
| 128 FileSystemFileUtil* file_util = file_system_context_->path_manager()-> | 141 scoped_ptr<FileSystemOperationContext> context(NewOperationContext(path)); |
| 129 sandbox_provider()->GetFileSystemFileUtil(); | 142 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util()->CreateDirectory( |
| 130 FileSystemOperationContext context(file_system_context_, file_util); | 143 context.get(), |
| 131 context.set_src_origin_url(GURL("http://remote")); | |
| 132 context.set_src_virtual_path(path); | |
| 133 context.set_src_type(fileapi::kFileSystemTypeTemporary); | |
| 134 context.set_allowed_bytes_growth(1024); | |
| 135 | |
| 136 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util->CreateDirectory( | |
| 137 &context, | |
| 138 path, | 144 path, |
| 139 false /* exclusive */, | 145 false /* exclusive */, |
| 140 false /* recursive */)); | 146 false /* recursive */)); |
| 141 } | 147 } |
| 142 | 148 |
| 149 void EnsureFileExists(const base::StringPiece file_name) { |
| 150 FilePath path = FilePath().AppendASCII(file_name); |
| 151 scoped_ptr<FileSystemOperationContext> context(NewOperationContext(path)); |
| 152 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util()->EnsureFileExists( |
| 153 context.get(), path, NULL)); |
| 154 } |
| 155 |
| 156 void TruncateFile(const base::StringPiece file_name, int64 length) { |
| 157 FilePath path = FilePath().AppendASCII(file_name); |
| 158 scoped_ptr<FileSystemOperationContext> context(NewOperationContext(path)); |
| 159 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util()->Truncate( |
| 160 context.get(), path, length)); |
| 161 } |
| 162 |
| 163 PlatformFileError GetFileInfo(const FilePath& path, |
| 164 base::PlatformFileInfo* file_info, |
| 165 FilePath* platform_file_path) { |
| 166 scoped_ptr<FileSystemOperationContext> context(NewOperationContext(path)); |
| 167 return file_util()->GetFileInfo(context.get(), path, |
| 168 file_info, platform_file_path); |
| 169 } |
| 170 |
| 171 void VerifyListingEntry(const std::string& entry_line, |
| 172 const std::string& name, |
| 173 const std::string& url, |
| 174 bool is_directory, |
| 175 int64 size) { |
| 176 #define STR "([^\"]*)" |
| 177 icu::UnicodeString pattern("^<script>addRow\\(\"" STR "\",\"" STR |
| 178 "\",(0|1),\"" STR "\",\"" STR "\"\\);</script>"); |
| 179 #undef STR |
| 180 icu::UnicodeString input(entry_line.c_str()); |
| 181 |
| 182 UErrorCode status = U_ZERO_ERROR; |
| 183 icu::RegexMatcher match(pattern, input, 0, status); |
| 184 |
| 185 EXPECT_TRUE(match.find()); |
| 186 EXPECT_EQ(5, match.groupCount()); |
| 187 EXPECT_EQ(icu::UnicodeString(name.c_str()), match.group(1, status)); |
| 188 EXPECT_EQ(icu::UnicodeString(url.c_str()), match.group(2, status)); |
| 189 EXPECT_EQ(icu::UnicodeString(is_directory ? "1" : "0"), |
| 190 match.group(3, status)); |
| 191 icu::UnicodeString size_string(FormatBytesUnlocalized(size).c_str()); |
| 192 EXPECT_EQ(size_string, match.group(4, status)); |
| 193 |
| 194 base::Time date; |
| 195 icu::UnicodeString date_ustr(match.group(5, status)); |
| 196 std::wstring date_wstr; |
| 197 UTF16ToWide(date_ustr.getBuffer(), date_ustr.length(), &date_wstr); |
| 198 EXPECT_TRUE(base::Time::FromString(date_wstr.c_str(), &date)); |
| 199 EXPECT_FALSE(date.is_null()); |
| 200 } |
| 201 |
| 143 GURL CreateFileSystemURL(const std::string path) { | 202 GURL CreateFileSystemURL(const std::string path) { |
| 144 return GURL(kFileSystemURLPrefix + path); | 203 return GURL(kFileSystemURLPrefix + path); |
| 145 } | 204 } |
| 146 | 205 |
| 147 static net::URLRequestJob* FileSystemDirURLRequestJobFactory( | 206 static net::URLRequestJob* FileSystemDirURLRequestJobFactory( |
| 148 net::URLRequest* request, | 207 net::URLRequest* request, |
| 149 const std::string& scheme) { | 208 const std::string& scheme) { |
| 150 DCHECK(job_); | 209 DCHECK(job_); |
| 151 net::URLRequestJob* temp = job_; | 210 net::URLRequestJob* temp = job_; |
| 152 job_ = NULL; | 211 job_ = NULL; |
| 153 return temp; | 212 return temp; |
| 154 } | 213 } |
| 155 | 214 |
| 215 FileSystemFileUtil* file_util() { |
| 216 return file_system_context_->path_manager()->sandbox_provider()-> |
| 217 GetFileSystemFileUtil(); |
| 218 } |
| 219 |
| 156 // Put the message loop at the top, so that it's the last thing deleted. | 220 // Put the message loop at the top, so that it's the last thing deleted. |
| 157 MessageLoop message_loop_; | 221 MessageLoop message_loop_; |
| 158 // Delete all MessageLoopProxy objects before the MessageLoop, to help prevent | 222 // Delete all MessageLoopProxy objects before the MessageLoop, to help prevent |
| 159 // leaks caused by tasks posted during shutdown. | 223 // leaks caused by tasks posted during shutdown. |
| 160 scoped_refptr<base::MessageLoopProxy> file_thread_proxy_; | 224 scoped_refptr<base::MessageLoopProxy> file_thread_proxy_; |
| 161 | 225 |
| 162 ScopedTempDir temp_dir_; | 226 ScopedTempDir temp_dir_; |
| 163 FilePath root_path_; | 227 FilePath root_path_; |
| 164 scoped_ptr<net::URLRequest> request_; | 228 scoped_ptr<net::URLRequest> request_; |
| 165 scoped_ptr<TestDelegate> delegate_; | 229 scoped_ptr<TestDelegate> delegate_; |
| 166 scoped_refptr<TestSpecialStoragePolicy> special_storage_policy_; | 230 scoped_refptr<TestSpecialStoragePolicy> special_storage_policy_; |
| 167 scoped_refptr<FileSystemContext> file_system_context_; | 231 scoped_refptr<FileSystemContext> file_system_context_; |
| 168 base::ScopedCallbackFactory<FileSystemDirURLRequestJobTest> callback_factory_; | 232 base::ScopedCallbackFactory<FileSystemDirURLRequestJobTest> callback_factory_; |
| 169 | 233 |
| 170 static net::URLRequestJob* job_; | 234 static net::URLRequestJob* job_; |
| 171 }; | 235 }; |
| 172 | 236 |
| 173 // static | 237 // static |
| 174 net::URLRequestJob* FileSystemDirURLRequestJobTest::job_ = NULL; | 238 net::URLRequestJob* FileSystemDirURLRequestJobTest::job_ = NULL; |
| 175 | 239 |
| 176 // TODO(adamk): Write tighter tests once we've decided on a format for directory | |
| 177 // listing responses. | |
| 178 TEST_F(FileSystemDirURLRequestJobTest, DirectoryListing) { | 240 TEST_F(FileSystemDirURLRequestJobTest, DirectoryListing) { |
| 179 CreateDirectory("foo"); | 241 CreateDirectory("foo"); |
| 180 CreateDirectory("foo/bar"); | 242 CreateDirectory("foo/bar"); |
| 181 CreateDirectory("foo/bar/baz"); | 243 CreateDirectory("foo/bar/baz"); |
| 182 | 244 |
| 245 EnsureFileExists("foo/bar/hoge"); |
| 246 TruncateFile("foo/bar/hoge", 10); |
| 247 |
| 183 TestRequest(CreateFileSystemURL("foo/bar/")); | 248 TestRequest(CreateFileSystemURL("foo/bar/")); |
| 184 | 249 |
| 185 ASSERT_FALSE(request_->is_pending()); | 250 ASSERT_FALSE(request_->is_pending()); |
| 186 EXPECT_EQ(1, delegate_->response_started_count()); | 251 EXPECT_EQ(1, delegate_->response_started_count()); |
| 187 EXPECT_FALSE(delegate_->received_data_before_response()); | 252 EXPECT_FALSE(delegate_->received_data_before_response()); |
| 188 EXPECT_GT(delegate_->bytes_received(), 0); | 253 EXPECT_GT(delegate_->bytes_received(), 0); |
| 254 |
| 255 std::istringstream in(delegate_->data_received()); |
| 256 std::string line; |
| 257 EXPECT_TRUE(std::getline(in, line)); |
| 258 |
| 259 #if defined(OS_WIN) |
| 260 EXPECT_EQ("<script>start(\"foo\\\\bar\");</script>", line); |
| 261 #elif defined(OS_POSIX) |
| 262 EXPECT_EQ("<script>start(\"/foo/bar\");</script>", line); |
| 263 #endif |
| 264 |
| 265 EXPECT_TRUE(std::getline(in, line)); |
| 266 VerifyListingEntry(line, "baz", "baz", true, 0); |
| 267 |
| 268 EXPECT_TRUE(std::getline(in, line)); |
| 269 VerifyListingEntry(line, "hoge", "hoge", false, 10); |
| 189 } | 270 } |
| 190 | 271 |
| 191 TEST_F(FileSystemDirURLRequestJobTest, InvalidURL) { | 272 TEST_F(FileSystemDirURLRequestJobTest, InvalidURL) { |
| 192 TestRequest(GURL("filesystem:/foo/bar/baz")); | 273 TestRequest(GURL("filesystem:/foo/bar/baz")); |
| 193 ASSERT_FALSE(request_->is_pending()); | 274 ASSERT_FALSE(request_->is_pending()); |
| 194 EXPECT_TRUE(delegate_->request_failed()); | 275 EXPECT_TRUE(delegate_->request_failed()); |
| 195 ASSERT_FALSE(request_->status().is_success()); | 276 ASSERT_FALSE(request_->status().is_success()); |
| 196 EXPECT_EQ(net::ERR_INVALID_URL, request_->status().os_error()); | 277 EXPECT_EQ(net::ERR_INVALID_URL, request_->status().os_error()); |
| 197 } | 278 } |
| 198 | 279 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 224 MessageLoop::current()->PostTask(FROM_HERE, new QuitNowTask); | 305 MessageLoop::current()->PostTask(FROM_HERE, new QuitNowTask); |
| 225 MessageLoop::current()->Run(); | 306 MessageLoop::current()->Run(); |
| 226 | 307 |
| 227 request_.reset(); | 308 request_.reset(); |
| 228 MessageLoop::current()->RunAllPending(); | 309 MessageLoop::current()->RunAllPending(); |
| 229 // If we get here, success! we didn't crash! | 310 // If we get here, success! we didn't crash! |
| 230 } | 311 } |
| 231 | 312 |
| 232 } // namespace (anonymous) | 313 } // namespace (anonymous) |
| 233 } // namespace fileapi | 314 } // namespace fileapi |
| OLD | NEW |