| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "components/drive/file_system/truncate_operation.h" | 5 #include "components/drive/file_system/truncate_operation.h" |
| 6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 7 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 9 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
| 10 #include "components/drive/drive.pb.h" | 12 #include "components/drive/drive.pb.h" |
| 11 #include "components/drive/fake_free_disk_space_getter.h" | 13 #include "components/drive/fake_free_disk_space_getter.h" |
| 12 #include "components/drive/file_system/operation_test_base.h" | 14 #include "components/drive/file_system/operation_test_base.h" |
| 13 #include "content/public/test/test_utils.h" | 15 #include "content/public/test/test_utils.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 17 |
| 16 namespace drive { | 18 namespace drive { |
| 17 namespace file_system { | 19 namespace file_system { |
| 18 | 20 |
| 19 class TruncateOperationTest : public OperationTestBase { | 21 class TruncateOperationTest : public OperationTestBase { |
| 20 protected: | 22 protected: |
| 21 void SetUp() override { | 23 void SetUp() override { |
| 22 OperationTestBase::SetUp(); | 24 OperationTestBase::SetUp(); |
| 23 | 25 |
| 24 operation_.reset(new TruncateOperation( | 26 operation_.reset(new TruncateOperation( |
| 25 blocking_task_runner(), delegate(), scheduler(), | 27 blocking_task_runner(), delegate(), scheduler(), |
| 26 metadata(), cache(), temp_dir())); | 28 metadata(), cache(), temp_dir())); |
| 27 } | 29 } |
| 28 | 30 |
| 29 scoped_ptr<TruncateOperation> operation_; | 31 scoped_ptr<TruncateOperation> operation_; |
| 30 }; | 32 }; |
| 31 | 33 |
| 32 TEST_F(TruncateOperationTest, Truncate) { | 34 TEST_F(TruncateOperationTest, Truncate) { |
| 33 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 35 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 34 ResourceEntry src_entry; | 36 ResourceEntry src_entry; |
| 35 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 37 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 36 const int64 file_size = src_entry.file_info().size(); | 38 const int64_t file_size = src_entry.file_info().size(); |
| 37 | 39 |
| 38 // Make sure the file has at least 2 bytes. | 40 // Make sure the file has at least 2 bytes. |
| 39 ASSERT_GE(file_size, 2); | 41 ASSERT_GE(file_size, 2); |
| 40 | 42 |
| 41 FileError error = FILE_ERROR_FAILED; | 43 FileError error = FILE_ERROR_FAILED; |
| 42 operation_->Truncate( | 44 operation_->Truncate( |
| 43 file_in_root, | 45 file_in_root, |
| 44 1, // Truncate to 1 byte. | 46 1, // Truncate to 1 byte. |
| 45 google_apis::test_util::CreateCopyResultCallback(&error)); | 47 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 46 content::RunAllBlockingPoolTasksUntilIdle(); | 48 content::RunAllBlockingPoolTasksUntilIdle(); |
| 47 EXPECT_EQ(FILE_ERROR_OK, error); | 49 EXPECT_EQ(FILE_ERROR_OK, error); |
| 48 | 50 |
| 49 base::FilePath local_path; | 51 base::FilePath local_path; |
| 50 error = FILE_ERROR_FAILED; | 52 error = FILE_ERROR_FAILED; |
| 51 base::PostTaskAndReplyWithResult( | 53 base::PostTaskAndReplyWithResult( |
| 52 blocking_task_runner(), | 54 blocking_task_runner(), |
| 53 FROM_HERE, | 55 FROM_HERE, |
| 54 base::Bind(&internal::FileCache::GetFile, | 56 base::Bind(&internal::FileCache::GetFile, |
| 55 base::Unretained(cache()), | 57 base::Unretained(cache()), |
| 56 GetLocalId(file_in_root), &local_path), | 58 GetLocalId(file_in_root), &local_path), |
| 57 google_apis::test_util::CreateCopyResultCallback(&error)); | 59 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 58 content::RunAllBlockingPoolTasksUntilIdle(); | 60 content::RunAllBlockingPoolTasksUntilIdle(); |
| 59 ASSERT_EQ(FILE_ERROR_OK, error); | 61 ASSERT_EQ(FILE_ERROR_OK, error); |
| 60 | 62 |
| 61 // The local file should be truncated. | 63 // The local file should be truncated. |
| 62 int64 local_file_size = 0; | 64 int64_t local_file_size = 0; |
| 63 base::GetFileSize(local_path, &local_file_size); | 65 base::GetFileSize(local_path, &local_file_size); |
| 64 EXPECT_EQ(1, local_file_size); | 66 EXPECT_EQ(1, local_file_size); |
| 65 } | 67 } |
| 66 | 68 |
| 67 TEST_F(TruncateOperationTest, NegativeSize) { | 69 TEST_F(TruncateOperationTest, NegativeSize) { |
| 68 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 70 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 69 ResourceEntry src_entry; | 71 ResourceEntry src_entry; |
| 70 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 72 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 71 const int64 file_size = src_entry.file_info().size(); | 73 const int64_t file_size = src_entry.file_info().size(); |
| 72 | 74 |
| 73 // Make sure the file has at least 2 bytes. | 75 // Make sure the file has at least 2 bytes. |
| 74 ASSERT_GE(file_size, 2); | 76 ASSERT_GE(file_size, 2); |
| 75 | 77 |
| 76 FileError error = FILE_ERROR_FAILED; | 78 FileError error = FILE_ERROR_FAILED; |
| 77 operation_->Truncate( | 79 operation_->Truncate( |
| 78 file_in_root, | 80 file_in_root, |
| 79 -1, // Truncate to "-1" byte. | 81 -1, // Truncate to "-1" byte. |
| 80 google_apis::test_util::CreateCopyResultCallback(&error)); | 82 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 81 content::RunAllBlockingPoolTasksUntilIdle(); | 83 content::RunAllBlockingPoolTasksUntilIdle(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 92 1, // Truncate to 1 byte. | 94 1, // Truncate to 1 byte. |
| 93 google_apis::test_util::CreateCopyResultCallback(&error)); | 95 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 94 content::RunAllBlockingPoolTasksUntilIdle(); | 96 content::RunAllBlockingPoolTasksUntilIdle(); |
| 95 EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error); | 97 EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error); |
| 96 } | 98 } |
| 97 | 99 |
| 98 TEST_F(TruncateOperationTest, Extend) { | 100 TEST_F(TruncateOperationTest, Extend) { |
| 99 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 101 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 100 ResourceEntry src_entry; | 102 ResourceEntry src_entry; |
| 101 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 103 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 102 const int64 file_size = src_entry.file_info().size(); | 104 const int64_t file_size = src_entry.file_info().size(); |
| 103 | 105 |
| 104 FileError error = FILE_ERROR_FAILED; | 106 FileError error = FILE_ERROR_FAILED; |
| 105 operation_->Truncate( | 107 operation_->Truncate( |
| 106 file_in_root, | 108 file_in_root, |
| 107 file_size + 10, // Extend to 10 bytes. | 109 file_size + 10, // Extend to 10 bytes. |
| 108 google_apis::test_util::CreateCopyResultCallback(&error)); | 110 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 109 content::RunAllBlockingPoolTasksUntilIdle(); | 111 content::RunAllBlockingPoolTasksUntilIdle(); |
| 110 EXPECT_EQ(FILE_ERROR_OK, error); | 112 EXPECT_EQ(FILE_ERROR_OK, error); |
| 111 | 113 |
| 112 base::FilePath local_path; | 114 base::FilePath local_path; |
| 113 error = FILE_ERROR_FAILED; | 115 error = FILE_ERROR_FAILED; |
| 114 base::PostTaskAndReplyWithResult( | 116 base::PostTaskAndReplyWithResult( |
| 115 blocking_task_runner(), | 117 blocking_task_runner(), |
| 116 FROM_HERE, | 118 FROM_HERE, |
| 117 base::Bind(&internal::FileCache::GetFile, | 119 base::Bind(&internal::FileCache::GetFile, |
| 118 base::Unretained(cache()), | 120 base::Unretained(cache()), |
| 119 GetLocalId(file_in_root), &local_path), | 121 GetLocalId(file_in_root), &local_path), |
| 120 google_apis::test_util::CreateCopyResultCallback(&error)); | 122 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 121 content::RunAllBlockingPoolTasksUntilIdle(); | 123 content::RunAllBlockingPoolTasksUntilIdle(); |
| 122 ASSERT_EQ(FILE_ERROR_OK, error); | 124 ASSERT_EQ(FILE_ERROR_OK, error); |
| 123 | 125 |
| 124 // The local file should be truncated. | 126 // The local file should be truncated. |
| 125 std::string content; | 127 std::string content; |
| 126 ASSERT_TRUE(base::ReadFileToString(local_path, &content)); | 128 ASSERT_TRUE(base::ReadFileToString(local_path, &content)); |
| 127 | 129 |
| 128 EXPECT_EQ(file_size + 10, static_cast<int64>(content.size())); | 130 EXPECT_EQ(file_size + 10, static_cast<int64_t>(content.size())); |
| 129 // All trailing 10 bytes should be '\0'. | 131 // All trailing 10 bytes should be '\0'. |
| 130 EXPECT_EQ(std::string(10, '\0'), content.substr(file_size)); | 132 EXPECT_EQ(std::string(10, '\0'), content.substr(file_size)); |
| 131 } | 133 } |
| 132 | 134 |
| 133 } // namespace file_system | 135 } // namespace file_system |
| 134 } // namespace drive | 136 } // namespace drive |
| OLD | NEW |