truncate_operation_unittest.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
1eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Copyright 2013 The Chromium Authors. All rights reserved. 2eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// found in the LICENSE file. 4eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/chromeos/drive/file_system/truncate_operation.h" 6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/file_util.h" 8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/files/file_path.h" 9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/chromeos/drive/drive.pb.h" 10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" 11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" 12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/google_apis/test_util.h" 13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "testing/gtest/include/gtest/gtest.h" 14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace drive { 16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace file_system { 17eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass TruncateOperationTest : public OperationTestBase { 19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch protected: 20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch virtual void SetUp() { 21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch OperationTestBase::SetUp(); 22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 23eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch operation_.reset(new TruncateOperation( 24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch blocking_task_runner(), observer(), scheduler(), 25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch metadata(), cache(), temp_dir())); 26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch scoped_ptr<TruncateOperation> operation_; 29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}; 30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(TruncateOperationTest, Truncate) { 32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); 33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ResourceEntry src_entry; 34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); 35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const int64 file_size = src_entry.file_info().size(); 36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Make sure the file has at least 2 bytes. 38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_GE(file_size, 2); 39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FileError error = FILE_ERROR_FAILED; 41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch operation_->Truncate( 42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch file_in_root, 43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 1, // Truncate to 1 byte. 44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch google_apis::test_util::CreateCopyResultCallback(&error)); 457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch test_util::RunBlockingPoolTask(); 46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(FILE_ERROR_OK, error); 47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath local_path; 49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch error = FILE_ERROR_FAILED; 50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch cache()->GetFileOnUIThread( 51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch src_entry.resource_id(), src_entry.file_specific_info().md5(), 52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch google_apis::test_util::CreateCopyResultCallback(&error, &local_path)); 537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch test_util::RunBlockingPoolTask(); 54eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_EQ(FILE_ERROR_OK, error); 55eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // The local file should be truncated. 57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch int64 local_file_size = 0; 58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch file_util::GetFileSize(local_path, &local_file_size); 59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(1, local_file_size); 60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(TruncateOperationTest, NegativeSize) { 63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); 64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ResourceEntry src_entry; 65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); 66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const int64 file_size = src_entry.file_info().size(); 67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 68eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Make sure the file has at least 2 bytes. 69eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_GE(file_size, 2); 70eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 71eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FileError error = FILE_ERROR_FAILED; 72eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch operation_->Truncate( 73eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch file_in_root, 74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch -1, // Truncate to "-1" byte. 75eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch google_apis::test_util::CreateCopyResultCallback(&error)); 767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch test_util::RunBlockingPoolTask(); 77eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error); 78eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 79eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(TruncateOperationTest, HostedDocument) { 81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath file_in_root(FILE_PATH_LITERAL( 82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch "drive/root/Document 1 excludeDir-test.gdoc")); 83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 84eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FileError error = FILE_ERROR_FAILED; 85eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch operation_->Truncate( 86eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch file_in_root, 87eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 1, // Truncate to 1 byte. 88eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch google_apis::test_util::CreateCopyResultCallback(&error)); 897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch test_util::RunBlockingPoolTask(); 90eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error); 91eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 92eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 93eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(TruncateOperationTest, Extend) { 94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); 95eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ResourceEntry src_entry; 96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); 97eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const int64 file_size = src_entry.file_info().size(); 98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FileError error = FILE_ERROR_FAILED; 100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch operation_->Truncate( 101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch file_in_root, 102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch file_size + 10, // Extend to 10 bytes. 103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch google_apis::test_util::CreateCopyResultCallback(&error)); 1047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch test_util::RunBlockingPoolTask(); 105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(FILE_ERROR_OK, error); 106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath local_path; 108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch error = FILE_ERROR_FAILED; 109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch cache()->GetFileOnUIThread( 110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch src_entry.resource_id(), src_entry.file_specific_info().md5(), 111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch google_apis::test_util::CreateCopyResultCallback(&error, &local_path)); 1127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch test_util::RunBlockingPoolTask(); 113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_EQ(FILE_ERROR_OK, error); 114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // The local file should be truncated. 116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string content; 117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_TRUE(file_util::ReadFileToString(local_path, &content)); 118eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 119eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(file_size + 10, static_cast<int64>(content.size())); 120eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // All trailing 10 bytes should be '\0'. 121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(std::string(10, '\0'), content.substr(file_size)); 122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} // namespace file_system 125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} // namespace drive 126