14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file. 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/sandbox_file_system_backend.h" 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <set> 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/basictypes.h" 101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h" 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/files/scoped_temp_dir.h" 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/memory/scoped_ptr.h" 13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/message_loop/message_loop_proxy.h" 14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/run_loop.h" 154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/public/test/test_file_system_options.h" 161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_backend.h" 171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_url.h" 181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h" 191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/file_system_util.h" 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h" 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)using storage::FileSystemURL; 2403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)using storage::SandboxFileSystemBackend; 2503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)using storage::SandboxFileSystemBackendDelegate; 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// PS stands for path separator. 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(FILE_PATH_USES_WIN_SEPARATORS) 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define PS "\\" 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define PS "/" 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace content { 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace { 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const struct RootPathTest { 3903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::FileSystemType type; 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char* origin_url; 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char* expected_path; 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} kRootPathTestCases[] = { 4303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypeTemporary, "http://foo:1/", "000" PS "t"}, 4403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypePersistent, "http://foo:1/", "000" PS "p"}, 4503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypeTemporary, "http://bar.com/", "001" PS "t"}, 4603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypePersistent, "http://bar.com/", "001" PS "p"}, 4703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypeTemporary, "https://foo:2/", "002" PS "t"}, 4803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypePersistent, "https://foo:2/", "002" PS "p"}, 4903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypeTemporary, "https://bar.com/", "003" PS "t"}, 5003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypePersistent, "https://bar.com/", "003" PS "p"}, 5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const struct RootPathFileURITest { 5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::FileSystemType type; 5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char* origin_url; 5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char* expected_path; 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char* virtual_path; 5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} kRootPathFileURITestCases[] = { 5903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypeTemporary, "file:///", "000" PS "t", NULL}, 6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) {storage::kFileSystemTypePersistent, "file:///", "000" PS "p", NULL}, 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void DidOpenFileSystem(base::File::Error* error_out, 647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const GURL& origin_url, 657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch const std::string& name, 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::File::Error error) { 6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *error_out = error; 6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace 7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochclass SandboxFileSystemBackendTest : public testing::Test { 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) protected: 7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual void SetUp() { 7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) SetUpNewDelegate(CreateAllowFileAccessOptions()); 77a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) } 78a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void SetUpNewDelegate(const storage::FileSystemOptions& options) { 803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) delegate_.reset(new SandboxFileSystemBackendDelegate( 817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch NULL /* quota_manager_proxy */, 827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch base::MessageLoopProxy::current().get(), 837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch data_dir_.path(), 84a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) NULL /* special_storage_policy */, 85a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) options)); 8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void SetUpNewBackend(const storage::FileSystemOptions& options) { 893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) SetUpNewDelegate(options); 903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) backend_.reset(new SandboxFileSystemBackend(delegate_.get())); 9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::SandboxFileSystemBackendDelegate::OriginEnumerator* 943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) CreateOriginEnumerator() const { 957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch return backend_->CreateOriginEnumerator(); 9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void CreateOriginTypeDirectory(const GURL& origin, 9903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::FileSystemType type) { 1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) base::FilePath target = delegate_-> 10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) GetBaseDirectoryForOriginAndType(origin, type, true); 10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(!target.empty()); 1037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch ASSERT_TRUE(base::DirectoryExists(target)); 10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool GetRootPath(const GURL& origin_url, 10703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::FileSystemType type, 10803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::OpenFileSystemMode mode, 10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath* root_path) { 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::File::Error error = base::File::FILE_OK; 111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) backend_->ResolveURL( 112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) FileSystemURL::CreateForTest(origin_url, type, base::FilePath()), 113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) mode, 11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::Bind(&DidOpenFileSystem, &error)); 115d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) base::RunLoop().RunUntilIdle(); 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (error != base::File::FILE_OK) 11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath returned_root_path = 1193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) delegate_->GetBaseDirectoryForOriginAndType( 12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) origin_url, type, false /* create */); 12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (root_path) 12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *root_path = returned_root_path; 12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return !returned_root_path.empty(); 12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath file_system_path() const { 1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return data_dir_.path().Append( 1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) SandboxFileSystemBackendDelegate::kFileSystemDirectory); 12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 13190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::ScopedTempDir data_dir_; 13290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::MessageLoop message_loop_; 13303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) scoped_ptr<storage::SandboxFileSystemBackendDelegate> delegate_; 13403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) scoped_ptr<storage::SandboxFileSystemBackend> backend_; 13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1377dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(SandboxFileSystemBackendTest, Empty) { 1387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch SetUpNewBackend(CreateAllowFileAccessOptions()); 1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator( 14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) CreateOriginEnumerator()); 14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(enumerator->Next().is_empty()); 14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1447dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) { 1457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch SetUpNewBackend(CreateAllowFileAccessOptions()); 14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char* temporary_origins[] = { 14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "http://www.bar.com/", 14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "http://www.foo.com/", 14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "http://www.foo.com:1/", 15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "http://www.example.com:8080/", 15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "http://www.google.com:80/", 15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) }; 15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char* persistent_origins[] = { 15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "http://www.bar.com/", 15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "http://www.foo.com:8080/", 15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "http://www.foo.com:80/", 15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) }; 15890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t temporary_size = ARRAYSIZE_UNSAFE(temporary_origins); 15990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t persistent_size = ARRAYSIZE_UNSAFE(persistent_origins); 16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::set<GURL> temporary_set, persistent_set; 16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (size_t i = 0; i < temporary_size; ++i) { 16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) CreateOriginTypeDirectory(GURL(temporary_origins[i]), 16303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::kFileSystemTypeTemporary); 16490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) temporary_set.insert(GURL(temporary_origins[i])); 16590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 16690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (size_t i = 0; i < persistent_size; ++i) { 16790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) CreateOriginTypeDirectory(GURL(persistent_origins[i]), 16803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::kFileSystemTypePersistent); 16990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) persistent_set.insert(GURL(persistent_origins[i])); 17090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator( 17390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) CreateOriginEnumerator()); 17490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t temporary_actual_size = 0; 17590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t persistent_actual_size = 0; 17690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) GURL current; 17790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) while (!(current = enumerator->Next()).is_empty()) { 17890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec()); 17903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) { 18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(temporary_set.find(current) != temporary_set.end()); 18190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ++temporary_actual_size; 18290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 18303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) { 18490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(persistent_set.find(current) != persistent_set.end()); 18590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ++persistent_actual_size; 18690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 18790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 18890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 18990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(temporary_size, temporary_actual_size); 19090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(persistent_size, persistent_actual_size); 19190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 19290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1937dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(SandboxFileSystemBackendTest, GetRootPathCreateAndExamine) { 19490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> returned_root_path( 19590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ARRAYSIZE_UNSAFE(kRootPathTestCases)); 1967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch SetUpNewBackend(CreateAllowFileAccessOptions()); 19790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 19890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Create a new root directory. 19990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { 20090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " 20190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) << kRootPathTestCases[i].expected_path); 20290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 20390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath root_path; 20490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), 20590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) kRootPathTestCases[i].type, 20603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, 20790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) &root_path)); 20890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 20990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath expected = file_system_path().AppendASCII( 21090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) kRootPathTestCases[i].expected_path); 21190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(expected.value(), root_path.value()); 2127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch EXPECT_TRUE(base::DirectoryExists(root_path)); 21390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(returned_root_path.size() > i); 21490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) returned_root_path[i] = root_path; 21590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 21690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 21790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Get the root directory with create=false and see if we get the 21890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // same directory. 21990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { 22090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " 22190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) << kRootPathTestCases[i].expected_path); 22290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 22390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath root_path; 22490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), 22590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) kRootPathTestCases[i].type, 22603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, 22790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) &root_path)); 22890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(returned_root_path.size() > i); 22990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(returned_root_path[i].value(), root_path.value()); 23090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 23190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 23290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2337dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(SandboxFileSystemBackendTest, 2347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch GetRootPathCreateAndExamineWithNewBackend) { 23590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> returned_root_path( 23690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ARRAYSIZE_UNSAFE(kRootPathTestCases)); 2377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch SetUpNewBackend(CreateAllowFileAccessOptions()); 23890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 23990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) GURL origin_url("http://foo.com:1/"); 24090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 24190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath root_path1; 24203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) EXPECT_TRUE(GetRootPath(origin_url, 24303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::kFileSystemTypeTemporary, 24403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, 24590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) &root_path1)); 24690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch SetUpNewBackend(CreateDisallowFileAccessOptions()); 24890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath root_path2; 24903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) EXPECT_TRUE(GetRootPath(origin_url, 25003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::kFileSystemTypeTemporary, 25103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, 25290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) &root_path2)); 25390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 25490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(root_path1.value(), root_path2.value()); 25590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 25690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2577dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(SandboxFileSystemBackendTest, GetRootPathGetWithoutCreate) { 2587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch SetUpNewBackend(CreateDisallowFileAccessOptions()); 25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Try to get a root directory without creating. 26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { 26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " " 26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) << kRootPathTestCases[i].expected_path); 26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), 26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) kRootPathTestCases[i].type, 26603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, 26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NULL)); 26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2717dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(SandboxFileSystemBackendTest, GetRootPathInIncognito) { 2727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch SetUpNewBackend(CreateIncognitoFileSystemOptions()); 27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Try to get a root directory. 27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { 27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " " 27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) << kRootPathTestCases[i].expected_path); 27803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), 27903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) kRootPathTestCases[i].type, 28003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, 28103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) NULL)); 28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2857dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(SandboxFileSystemBackendTest, GetRootPathFileURI) { 2867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch SetUpNewBackend(CreateDisallowFileAccessOptions()); 28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { 28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #" 28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) << i << " " << kRootPathFileURITestCases[i].expected_path); 29003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) EXPECT_FALSE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), 29103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) kRootPathFileURITestCases[i].type, 29203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, 29303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) NULL)); 29490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 29590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 29690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2977dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochTEST_F(SandboxFileSystemBackendTest, GetRootPathFileURIWithAllowFlag) { 2987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch SetUpNewBackend(CreateAllowFileAccessOptions()); 29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { 30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" 30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) << i << " " << kRootPathFileURITestCases[i].expected_path); 30290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath root_path; 30390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), 30490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) kRootPathFileURITestCases[i].type, 30503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, 30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) &root_path)); 30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath expected = file_system_path().AppendASCII( 30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) kRootPathFileURITestCases[i].expected_path); 30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(expected.value(), root_path.value()); 3107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch EXPECT_TRUE(base::DirectoryExists(root_path)); 31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace content 315