1a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// found in the LICENSE file.
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
5a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/basictypes.h"
6a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/file_util.h"
7a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/files/file_path.h"
8a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/files/scoped_temp_dir.h"
9a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
10ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/message_loop/message_loop.h"
11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/platform_file.h"
12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/file_system_context.h"
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/file_system_operation_context.h"
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/isolated_context.h"
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/mock_file_system_context.h"
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/transient_file_util.h"
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/common/blob/scoped_file.h"
19a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace fileapi {
21a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
22a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)class TransientFileUtilTest : public testing::Test {
23a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) public:
24a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  TransientFileUtilTest() {}
25a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  virtual ~TransientFileUtilTest() {}
26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  virtual void SetUp() OVERRIDE {
28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    file_system_context_ = CreateFileSystemContextForTesting(
29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        NULL, base::FilePath(FILE_PATH_LITERAL("dummy")));
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    transient_file_util_.reset(new TransientFileUtil);
31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  virtual void TearDown() OVERRIDE {
36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    file_system_context_ = NULL;
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    base::MessageLoop::current()->RunUntilIdle();
38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  void CreateAndRegisterTemporaryFile(
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      FileSystemURL* file_url,
42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      base::FilePath* file_path) {
43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    EXPECT_TRUE(
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        file_util::CreateTemporaryFileInDir(data_dir_.path(), file_path));
45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    IsolatedContext* isolated_context = IsolatedContext::GetInstance();
46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    std::string name = "tmp";
47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    std::string fsid = isolated_context->RegisterFileSystemForPath(
48a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        kFileSystemTypeForTransientFile,
49a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        *file_path,
50a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        &name);
51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    ASSERT_TRUE(!fsid.empty());
52a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    base::FilePath virtual_path = isolated_context->CreateVirtualRootPath(
53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        fsid).AppendASCII(name);
54a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    *file_url = file_system_context_->CreateCrackedFileSystemURL(
55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        GURL("http://foo"),
56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        kFileSystemTypeIsolated,
57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        virtual_path);
58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<FileSystemOperationContext> NewOperationContext() {
61a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return make_scoped_ptr(
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        new FileSystemOperationContext(file_system_context_.get()));
63a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
64a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
65a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  FileSystemFileUtil* file_util() { return transient_file_util_.get(); }
66a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
67a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) private:
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::MessageLoop message_loop_;
69a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::ScopedTempDir data_dir_;
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_refptr<FileSystemContext> file_system_context_;
71a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  scoped_ptr<TransientFileUtil> transient_file_util_;
72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
73a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TransientFileUtilTest);
74a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)};
75a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)TEST_F(TransientFileUtilTest, TransientFile) {
77a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  FileSystemURL temp_url;
78a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::FilePath temp_path;
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  CreateAndRegisterTemporaryFile(&temp_url, &temp_path);
81a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
82a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::PlatformFileError error;
83a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::PlatformFileInfo file_info;
84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::FilePath path;
85a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
86a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Make sure the file is there.
87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  ASSERT_TRUE(temp_url.is_valid());
887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_TRUE(base::PathExists(temp_path));
897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_FALSE(base::DirectoryExists(temp_path));
90a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
91a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Create a snapshot file.
92a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  {
93a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    webkit_blob::ScopedFile scoped_file =
94a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        file_util()->CreateSnapshotFile(NewOperationContext().get(),
95a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                        temp_url,
96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                        &error,
97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                        &file_info,
98a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                        &path);
99a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    ASSERT_EQ(base::PLATFORM_FILE_OK, error);
100a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    ASSERT_EQ(temp_path, path);
101a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    ASSERT_FALSE(file_info.is_directory);
102a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
103a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // The file should be still there.
1047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ASSERT_TRUE(base::PathExists(temp_path));
105a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    ASSERT_EQ(base::PLATFORM_FILE_OK,
106a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)              file_util()->GetFileInfo(NewOperationContext().get(),
107a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                       temp_url, &file_info, &path));
108a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    ASSERT_EQ(temp_path, path);
109a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    ASSERT_FALSE(file_info.is_directory);
110a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
111a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
112a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // The file's now scoped out.
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::MessageLoop::current()->RunUntilIdle();
114a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
115a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Now the temporary file and the transient filesystem must be gone too.
1167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_FALSE(base::PathExists(temp_path));
117a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
118a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)            file_util()->GetFileInfo(NewOperationContext().get(),
119a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                     temp_url, &file_info, &path));
120a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
121a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
122a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}  // namespace fileapi
123