diff options
author | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-12 04:33:11 +0000 |
---|---|---|
committer | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-12 04:33:11 +0000 |
commit | 363b4e8ff0472b65b4b0716ce0057a65a39acd8d (patch) | |
tree | 5b9bafe6f9ff7f8f55f8afb21d2af838da2851bc /webkit | |
parent | 4f9b3357b78af29c31086e13c55e45d435bd8a75 (diff) | |
download | chromium_src-363b4e8ff0472b65b4b0716ce0057a65a39acd8d.zip chromium_src-363b4e8ff0472b65b4b0716ce0057a65a39acd8d.tar.gz chromium_src-363b4e8ff0472b65b4b0716ce0057a65a39acd8d.tar.bz2 |
Implement write support in NativeMediaFileUtil.
BUG=144509
Review URL: https://chromiumcodereview.appspot.com/11744016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@176511 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/fileapi/media/native_media_file_util.cc | 119 | ||||
-rw-r--r-- | webkit/fileapi/media/native_media_file_util.h | 20 | ||||
-rw-r--r-- | webkit/fileapi/media/native_media_file_util_unittest.cc | 393 |
3 files changed, 507 insertions, 25 deletions
diff --git a/webkit/fileapi/media/native_media_file_util.cc b/webkit/fileapi/media/native_media_file_util.cc index 4ccbc34..4e99c82 100644 --- a/webkit/fileapi/media/native_media_file_util.cc +++ b/webkit/fileapi/media/native_media_file_util.cc @@ -7,6 +7,7 @@ #include "webkit/fileapi/file_system_operation_context.h" #include "webkit/fileapi/media/media_path_filter.h" #include "webkit/fileapi/media/filtering_file_enumerator.h" +#include "webkit/fileapi/native_file_util.h" using base::PlatformFile; using base::PlatformFileError; @@ -23,17 +24,18 @@ PlatformFileError NativeMediaFileUtil::CreateOrOpen( int file_flags, PlatformFile* file_handle, bool* created) { - // TODO(tzik): Apply context()->media_path_filter() here when we support write - // access. + // Only called by NaCl, which should not have access to media file systems. return base::PLATFORM_FILE_ERROR_SECURITY; } PlatformFileError NativeMediaFileUtil::EnsureFileExists( FileSystemOperationContext* context, const FileSystemURL& url, bool* created) { - // TODO(tzik): Apply context()->media_path_filter() here when we support write - // access. - return base::PLATFORM_FILE_ERROR_SECURITY; + FilePath file_path; + PlatformFileError error = GetFilteredLocalFilePath(context, url, &file_path); + if (error != base::PLATFORM_FILE_OK) + return error; + return NativeFileUtil::EnsureFileExists(file_path, created); } scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> @@ -53,18 +55,32 @@ PlatformFileError NativeMediaFileUtil::Touch( const FileSystemURL& url, const base::Time& last_access_time, const base::Time& last_modified_time) { - // TODO(tzik): Apply context()->media_path_filter() here when we support write - // access. - return base::PLATFORM_FILE_ERROR_SECURITY; + FilePath file_path; + PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( + context, + url, + // Touch fails for non-existent paths and filtered paths. + base::PLATFORM_FILE_ERROR_FAILED, + &file_path); + if (error != base::PLATFORM_FILE_OK) + return error; + return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time); } PlatformFileError NativeMediaFileUtil::Truncate( FileSystemOperationContext* context, const FileSystemURL& url, int64 length) { - // TODO(tzik): Apply context()->media_path_filter() here when we support write - // access. - return base::PLATFORM_FILE_ERROR_SECURITY; + FilePath file_path; + PlatformFileError error = GetFilteredLocalFilePathForExistingFileOrDirectory( + context, + url, + // Cannot truncate paths that do not exist, or are filtered. + base::PLATFORM_FILE_ERROR_NOT_FOUND, + &file_path); + if (error != base::PLATFORM_FILE_OK) + return error; + return NativeFileUtil::Truncate(file_path, length); } bool NativeMediaFileUtil::IsDirectoryEmpty( @@ -89,24 +105,45 @@ PlatformFileError NativeMediaFileUtil::CopyOrMoveFile( const FileSystemURL& src_url, const FileSystemURL& dest_url, bool copy) { - // TODO(tzik): Apply context()->media_path_filter() here when we support write - // access. - return base::PLATFORM_FILE_ERROR_SECURITY; + FilePath src_file_path; + PlatformFileError error = + GetFilteredLocalFilePath(context, src_url, &src_file_path); + if (error != base::PLATFORM_FILE_OK) + return error; + + FilePath dest_file_path; + error = GetFilteredLocalFilePath(context, dest_url, &dest_file_path); + if (error != base::PLATFORM_FILE_OK) + return error; + + return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy); } PlatformFileError NativeMediaFileUtil::CopyInForeignFile( FileSystemOperationContext* context, const FilePath& src_file_path, const FileSystemURL& dest_url) { - // TODO(tzik): Apply context()->media_path_filter() here when we support write - // access. - return base::PLATFORM_FILE_ERROR_SECURITY; + if (src_file_path.empty()) + return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; + + FilePath dest_file_path; + PlatformFileError error = + GetFilteredLocalFilePath(context, dest_url, &dest_file_path); + if (error != base::PLATFORM_FILE_OK) + return error; + return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true); } PlatformFileError NativeMediaFileUtil::DeleteFile( FileSystemOperationContext* context, const FileSystemURL& url) { - return base::PLATFORM_FILE_ERROR_SECURITY; + FilePath file_path; + PlatformFileError error = GetLocalFilePath(context, url, &file_path); + if (error != base::PLATFORM_FILE_OK) + return error; + if (!context->media_path_filter()->Match(file_path)) + return base::PLATFORM_FILE_ERROR_NOT_FOUND; + return NativeFileUtil::DeleteFile(file_path); } PlatformFileError NativeMediaFileUtil::GetFileInfo( @@ -125,9 +162,53 @@ PlatformFileError NativeMediaFileUtil::GetFileInfo( return error; if (file_info->is_directory || - context->media_path_filter()->Match(*platform_path)) + context->media_path_filter()->Match(*platform_path)) { return base::PLATFORM_FILE_OK; + } return base::PLATFORM_FILE_ERROR_NOT_FOUND; } +PlatformFileError NativeMediaFileUtil::GetFilteredLocalFilePath( + FileSystemOperationContext* context, + const FileSystemURL& file_system_url, + FilePath* local_file_path) { + FilePath file_path; + PlatformFileError error = + IsolatedFileUtil::GetLocalFilePath(context, file_system_url, &file_path); + if (error != base::PLATFORM_FILE_OK) + return error; + if (!context->media_path_filter()->Match(file_path)) + return base::PLATFORM_FILE_ERROR_SECURITY; + + *local_file_path = file_path; + return base::PLATFORM_FILE_OK; +} + +PlatformFileError +NativeMediaFileUtil::GetFilteredLocalFilePathForExistingFileOrDirectory( + FileSystemOperationContext* context, + const FileSystemURL& file_system_url, + PlatformFileError failure_error, + FilePath* local_file_path) { + FilePath file_path; + PlatformFileError error = + GetLocalFilePath(context, file_system_url, &file_path); + if (error != base::PLATFORM_FILE_OK) + return error; + + if (!file_util::PathExists(file_path)) + return failure_error; + PlatformFileInfo file_info; + if (!file_util::GetFileInfo(file_path, &file_info)) + return base::PLATFORM_FILE_ERROR_FAILED; + + if (!file_info.is_directory && + !context->media_path_filter()->Match(file_path)) { + return failure_error; + } + + *local_file_path = file_path; + return base::PLATFORM_FILE_OK; +} + } // namespace fileapi diff --git a/webkit/fileapi/media/native_media_file_util.h b/webkit/fileapi/media/native_media_file_util.h index 6b86e1d..4987a05 100644 --- a/webkit/fileapi/media/native_media_file_util.h +++ b/webkit/fileapi/media/native_media_file_util.h @@ -63,6 +63,26 @@ class WEBKIT_STORAGE_EXPORT_PRIVATE NativeMediaFileUtil FilePath* platform_path) OVERRIDE; private: + // Like GetLocalFilePath(), but always take media_path_filter() into + // consideration. If the media_path_filter() check fails, return + // PLATFORM_FILE_ERROR_SECURITY. |local_file_path| does not have to exist. + base::PlatformFileError GetFilteredLocalFilePath( + FileSystemOperationContext* context, + const FileSystemURL& file_system_url, + FilePath* local_file_path); + + // Like GetLocalFilePath(), but if the file does not exist, then return + // |failure_error|. + // If |local_file_path| is a file, then take media_path_filter() into + // consideration. + // If the media_path_filter() check fails, return |failure_error|. + // If |local_file_path| is a directory, return PLATFORM_FILE_OK. + base::PlatformFileError GetFilteredLocalFilePathForExistingFileOrDirectory( + FileSystemOperationContext* context, + const FileSystemURL& file_system_url, + base::PlatformFileError failure_error, + FilePath* local_file_path); + DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtil); }; diff --git a/webkit/fileapi/media/native_media_file_util_unittest.cc b/webkit/fileapi/media/native_media_file_util_unittest.cc index ff18801..071922c 100644 --- a/webkit/fileapi/media/native_media_file_util_unittest.cc +++ b/webkit/fileapi/media/native_media_file_util_unittest.cc @@ -7,7 +7,10 @@ #include "base/bind.h" #include "base/files/scoped_temp_dir.h" +#include "base/format_macros.h" #include "base/message_loop.h" +#include "base/stringprintf.h" +#include "base/time.h" #include "testing/gtest/include/gtest/gtest.h" #include "webkit/fileapi/file_system_context.h" #include "webkit/fileapi/file_system_operation.h" @@ -46,9 +49,21 @@ const FilteringTestCase kFilteringTestCases[] = { { FPL("foobar.cod"), false, false }, // Unsupported media file. }; -void ExpectEqHelper(base::PlatformFileError expected, +void ExpectEqHelper(const std::string& test_name, + base::PlatformFileError expected, base::PlatformFileError actual) { - EXPECT_EQ(expected, actual); + EXPECT_EQ(expected, actual) << test_name; +} + +void ExpectMetadataEqHelper(const std::string& test_name, + base::PlatformFileError expected, + bool expected_is_directory, + base::PlatformFileError actual, + const base::PlatformFileInfo& file_info, + const FilePath& /*platform_path*/) { + EXPECT_EQ(expected, actual) << test_name; + if (actual == base::PLATFORM_FILE_OK) + EXPECT_EQ(expected_is_directory, file_info.is_directory) << test_name; } void DidReadDirectory(std::set<FilePath::StringType>* content, @@ -89,6 +104,7 @@ class NativeMediaFileUtilTest : public testing::Test { void SetUp() { ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); + ASSERT_TRUE(file_util::CreateDirectory(root_path())); scoped_refptr<quota::SpecialStoragePolicy> storage_policy = new quota::MockSpecialStoragePolicy(); @@ -170,10 +186,15 @@ TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { base::PLATFORM_FILE_OK : base::PLATFORM_FILE_ERROR_NOT_FOUND; - if (kFilteringTestCases[i].is_directory) - operation->DirectoryExists(url, base::Bind(&ExpectEqHelper, expectation)); - else - operation->FileExists(url, base::Bind(&ExpectEqHelper, expectation)); + std::string test_name = + base::StringPrintf("DirectoryExistsAndFileExistsFiltering %" PRIuS, i); + if (kFilteringTestCases[i].is_directory) { + operation->DirectoryExists( + url, base::Bind(&ExpectEqHelper, test_name, expectation)); + } else { + operation->FileExists( + url, base::Bind(&ExpectEqHelper, test_name, expectation)); + } MessageLoop::current()->RunUntilIdle(); } } @@ -199,3 +220,363 @@ TEST_F(NativeMediaFileUtilTest, ReadDirectoryFiltering) { EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); } } + +TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { + // Run the loop twice. The second loop attempts to create files that are + // pre-existing. Though the result should be the same. + for (int loop_count = 0; loop_count < 2; ++loop_count) { + for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { + FileSystemURL root_url(origin(), type(), root_path()); + FileSystemOperation* operation = NewOperation(root_url); + + FilePath path = root_path().Append(kFilteringTestCases[i].path); + FileSystemURL url(origin(), type(), path); + + std::string test_name = base::StringPrintf( + "CreateFileAndCreateDirectoryFiltering run %d, test %" PRIuS, + loop_count, i); + base::PlatformFileError expectation = + kFilteringTestCases[i].visible ? + base::PLATFORM_FILE_OK : + base::PLATFORM_FILE_ERROR_SECURITY; + if (kFilteringTestCases[i].is_directory) { + operation->CreateDirectory( + url, false, false, + base::Bind(&ExpectEqHelper, test_name, expectation)); + } else { + operation->CreateFile( + url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); + } + MessageLoop::current()->RunUntilIdle(); + } + } +} + +TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) { + FilePath dest_path = root_path().AppendASCII("dest"); + FileSystemURL dest_url(origin(), type(), dest_path); + + // Run the loop twice. The first run has no source files. The second run does. + for (int loop_count = 0; loop_count < 2; ++loop_count) { + if (loop_count == 1) { + PopulateDirectoryWithTestCases(root_path(), + kFilteringTestCases, + arraysize(kFilteringTestCases)); + } + for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { + // Always start with an empty destination directory. + // Copying to a non-empty destination directory is an invalid operation. + ASSERT_TRUE(file_util::Delete(dest_path, true)); + ASSERT_TRUE(file_util::CreateDirectory(dest_path)); + + FileSystemURL root_url(origin(), type(), root_path()); + FileSystemOperation* operation = NewOperation(root_url); + + FilePath path = root_path().Append(kFilteringTestCases[i].path); + FileSystemURL url(origin(), type(), path); + + std::string test_name = base::StringPrintf( + "CopySourceFiltering run %d test %" PRIuS, loop_count, i); + base::PlatformFileError expectation = base::PLATFORM_FILE_OK; + if (loop_count == 0 || !kFilteringTestCases[i].visible) { + // If the source does not exist or is not visible. + expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; + } else if (!kFilteringTestCases[i].is_directory) { + // Cannot copy a visible file to a directory. + expectation = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; + } + operation->Copy( + url, dest_url, base::Bind(&ExpectEqHelper, test_name, expectation)); + MessageLoop::current()->RunUntilIdle(); + } + } +} + +TEST_F(NativeMediaFileUtilTest, CopyDestFiltering) { + // Run the loop twice. The first run has no destination files. + // The second run does. + for (int loop_count = 0; loop_count < 2; ++loop_count) { + if (loop_count == 1) { + // Reset the test directory between the two loops to remove old + // directories and create new ones that should pre-exist. + ASSERT_TRUE(file_util::Delete(root_path(), true)); + ASSERT_TRUE(file_util::CreateDirectory(root_path())); + PopulateDirectoryWithTestCases(root_path(), + kFilteringTestCases, + arraysize(kFilteringTestCases)); + } + + // Always create a dummy source data file. + FilePath src_path = root_path().AppendASCII("foo.jpg"); + FileSystemURL src_url(origin(), type(), src_path); + static const char kDummyData[] = "dummy"; + ASSERT_TRUE(file_util::WriteFile(src_path, kDummyData, strlen(kDummyData))); + + for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { + FileSystemURL root_url(origin(), type(), root_path()); + FileSystemOperation* operation = NewOperation(root_url); + + FilePath path = root_path().Append(kFilteringTestCases[i].path); + FileSystemURL url(origin(), type(), path); + + std::string test_name = base::StringPrintf( + "CopyDestFiltering run %d test %" PRIuS, loop_count, i); + base::PlatformFileError expectation; + if (loop_count == 0) { + // These directories do not exist in this case, so Copy() will not + // treat them as directories. Thus invalidating these test cases. + if (kFilteringTestCases[i].is_directory) + continue; + // If the destination path does not exist and is not visible, then + // creating it would be a security violation. + expectation = + kFilteringTestCases[i].visible ? + base::PLATFORM_FILE_OK : + base::PLATFORM_FILE_ERROR_SECURITY; + } else { + if (!kFilteringTestCases[i].visible) { + // If the destination path exist and is not visible, then to the copy + // operation, it looks like the file needs to be created, which is a + // security violation. + expectation = base::PLATFORM_FILE_ERROR_SECURITY; + } else if (kFilteringTestCases[i].is_directory) { + // Cannot copy a file to a directory. + expectation = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; + } else { + // Copying from a file to a visible file that exists is ok. + expectation = base::PLATFORM_FILE_OK; + } + } + operation->Copy( + src_url, url, base::Bind(&ExpectEqHelper, test_name, expectation)); + MessageLoop::current()->RunUntilIdle(); + } + } +} + +TEST_F(NativeMediaFileUtilTest, MoveSourceFiltering) { + FilePath dest_path = root_path().AppendASCII("dest"); + FileSystemURL dest_url(origin(), type(), dest_path); + + // Run the loop twice. The first run has no source files. The second run does. + for (int loop_count = 0; loop_count < 2; ++loop_count) { + if (loop_count == 1) { + PopulateDirectoryWithTestCases(root_path(), + kFilteringTestCases, + arraysize(kFilteringTestCases)); + } + for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { + // Always start with an empty destination directory. + // Moving to a non-empty destination directory is an invalid operation. + ASSERT_TRUE(file_util::Delete(dest_path, true)); + ASSERT_TRUE(file_util::CreateDirectory(dest_path)); + + FileSystemURL root_url(origin(), type(), root_path()); + FileSystemOperation* operation = NewOperation(root_url); + + FilePath path = root_path().Append(kFilteringTestCases[i].path); + FileSystemURL url(origin(), type(), path); + + std::string test_name = base::StringPrintf( + "MoveSourceFiltering run %d test %" PRIuS, loop_count, i); + base::PlatformFileError expectation = base::PLATFORM_FILE_OK; + if (loop_count == 0 || !kFilteringTestCases[i].visible) { + // If the source does not exist or is not visible. + expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; + } else if (!kFilteringTestCases[i].is_directory) { + // Cannot move a visible file to a directory. + expectation = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; + } + operation->Move( + url, dest_url, base::Bind(&ExpectEqHelper, test_name, expectation)); + MessageLoop::current()->RunUntilIdle(); + } + } +} + +TEST_F(NativeMediaFileUtilTest, MoveDestFiltering) { + // Run the loop twice. The first run has no destination files. + // The second run does. + for (int loop_count = 0; loop_count < 2; ++loop_count) { + if (loop_count == 1) { + // Reset the test directory between the two loops to remove old + // directories and create new ones that should pre-exist. + ASSERT_TRUE(file_util::Delete(root_path(), true)); + ASSERT_TRUE(file_util::CreateDirectory(root_path())); + PopulateDirectoryWithTestCases(root_path(), + kFilteringTestCases, + arraysize(kFilteringTestCases)); + } + + for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { + // Create the source file for every test case because it might get moved. + FilePath src_path = root_path().AppendASCII("foo.jpg"); + FileSystemURL src_url(origin(), type(), src_path); + static const char kDummyData[] = "dummy"; + ASSERT_TRUE( + file_util::WriteFile(src_path, kDummyData, strlen(kDummyData))); + + FileSystemURL root_url(origin(), type(), root_path()); + FileSystemOperation* operation = NewOperation(root_url); + + FilePath path = root_path().Append(kFilteringTestCases[i].path); + FileSystemURL url(origin(), type(), path); + + std::string test_name = base::StringPrintf( + "MoveDestFiltering run %d test %" PRIuS, loop_count, i); + base::PlatformFileError expectation; + if (loop_count == 0) { + // These directories do not exist in this case, so Move() will not + // treat them as directories. Thus invalidating these test cases. + if (kFilteringTestCases[i].is_directory) + continue; + // If the destination path does not exist and is not visible, then + // creating it would be a security violation. + expectation = + kFilteringTestCases[i].visible ? + base::PLATFORM_FILE_OK : + base::PLATFORM_FILE_ERROR_SECURITY; + } else { + if (!kFilteringTestCases[i].visible) { + // If the destination path exist and is not visible, then to the move + // operation, it looks like the file needs to be created, which is a + // security violation. + expectation = base::PLATFORM_FILE_ERROR_SECURITY; + } else if (kFilteringTestCases[i].is_directory) { + // Cannot move a file to a directory. + expectation = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; + } else { + // Moving from a file to a visible file that exists is ok. + expectation = base::PLATFORM_FILE_OK; + } + } + operation->Move( + src_url, url, base::Bind(&ExpectEqHelper, test_name, expectation)); + MessageLoop::current()->RunUntilIdle(); + } + } +} + +TEST_F(NativeMediaFileUtilTest, GetMetadataFiltering) { + // Run the loop twice. The first run has no files. The second run does. + for (int loop_count = 0; loop_count < 2; ++loop_count) { + if (loop_count == 1) { + PopulateDirectoryWithTestCases(root_path(), + kFilteringTestCases, + arraysize(kFilteringTestCases)); + } + for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { + FileSystemURL root_url(origin(), type(), root_path()); + FileSystemOperation* operation = NewOperation(root_url); + + FilePath path = root_path().Append(kFilteringTestCases[i].path); + FileSystemURL url(origin(), type(), path); + + std::string test_name = base::StringPrintf( + "GetMetadataFiltering run %d test %" PRIuS, loop_count, i); + base::PlatformFileError expectation = base::PLATFORM_FILE_OK; + if (loop_count == 0 || !kFilteringTestCases[i].visible) { + // Cannot get metadata from files that do not exist or are not visible. + expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; + } + operation->GetMetadata(url, + base::Bind(&ExpectMetadataEqHelper, + test_name, + expectation, + kFilteringTestCases[i].is_directory)); + MessageLoop::current()->RunUntilIdle(); + } + } +} + +TEST_F(NativeMediaFileUtilTest, RemoveFiltering) { + // Run the loop twice. The first run has no files. The second run does. + for (int loop_count = 0; loop_count < 2; ++loop_count) { + if (loop_count == 1) { + PopulateDirectoryWithTestCases(root_path(), + kFilteringTestCases, + arraysize(kFilteringTestCases)); + } + for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { + FileSystemURL root_url(origin(), type(), root_path()); + FileSystemOperation* operation = NewOperation(root_url); + + FilePath path = root_path().Append(kFilteringTestCases[i].path); + FileSystemURL url(origin(), type(), path); + + std::string test_name = base::StringPrintf( + "RemoveFiltering run %d test %" PRIuS, loop_count, i); + base::PlatformFileError expectation = base::PLATFORM_FILE_OK; + if (loop_count == 0 || !kFilteringTestCases[i].visible) { + // Cannot remove files that do not exist or are not visible. + expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; + } + operation->Remove( + url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); + MessageLoop::current()->RunUntilIdle(); + } + } +} + +TEST_F(NativeMediaFileUtilTest, TruncateFiltering) { + // Run the loop twice. The first run has no files. The second run does. + for (int loop_count = 0; loop_count < 2; ++loop_count) { + if (loop_count == 1) { + PopulateDirectoryWithTestCases(root_path(), + kFilteringTestCases, + arraysize(kFilteringTestCases)); + } + for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { + FileSystemURL root_url(origin(), type(), root_path()); + FileSystemOperation* operation = NewOperation(root_url); + + FilePath path = root_path().Append(kFilteringTestCases[i].path); + FileSystemURL url(origin(), type(), path); + + std::string test_name = base::StringPrintf( + "TruncateFiltering run %d test %" PRIuS, loop_count, i); + base::PlatformFileError expectation = base::PLATFORM_FILE_OK; + if (loop_count == 0 || !kFilteringTestCases[i].visible) { + // Cannot truncate files that do not exist or are not visible. + expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; + } else if (kFilteringTestCases[i].is_directory) { + // Cannot truncate directories. + expectation = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; + } + operation->Truncate( + url, 0, base::Bind(&ExpectEqHelper, test_name, expectation)); + MessageLoop::current()->RunUntilIdle(); + } + } +} + +TEST_F(NativeMediaFileUtilTest, TouchFileFiltering) { + base::Time time = base::Time::Now(); + + // Run the loop twice. The first run has no files. The second run does. + for (int loop_count = 0; loop_count < 2; ++loop_count) { + if (loop_count == 1) { + PopulateDirectoryWithTestCases(root_path(), + kFilteringTestCases, + arraysize(kFilteringTestCases)); + } + for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { + FileSystemURL root_url(origin(), type(), root_path()); + FileSystemOperation* operation = NewOperation(root_url); + + FilePath path = root_path().Append(kFilteringTestCases[i].path); + FileSystemURL url(origin(), type(), path); + + std::string test_name = base::StringPrintf( + "TouchFileFiltering run %d test %" PRIuS, loop_count, i); + base::PlatformFileError expectation = base::PLATFORM_FILE_OK; + if (loop_count == 0 || !kFilteringTestCases[i].visible) { + // Files do not exists. Touch fails. + expectation = base::PLATFORM_FILE_ERROR_FAILED; + } + operation->TouchFile( + url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); + MessageLoop::current()->RunUntilIdle(); + } + } +} |