OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 25 matching lines...) Expand all Loading... |
36 #include "storage/browser/fileapi/file_system_context.h" | 36 #include "storage/browser/fileapi/file_system_context.h" |
37 #include "storage/browser/fileapi/file_system_file_util.h" | 37 #include "storage/browser/fileapi/file_system_file_util.h" |
38 #include "testing/gmock/include/gmock/gmock.h" | 38 #include "testing/gmock/include/gmock/gmock.h" |
39 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
40 #include "url/gurl.h" | 40 #include "url/gurl.h" |
41 | 41 |
42 using base::FilePath; | 42 using base::FilePath; |
43 using content::AsyncFileTestHelper; | 43 using content::AsyncFileTestHelper; |
44 using net::DrainableIOBuffer; | 44 using net::DrainableIOBuffer; |
45 using net::IOBuffer; | 45 using net::IOBuffer; |
| 46 using FileCreationInfo = storage::BlobMemoryController::FileCreationInfo; |
46 | 47 |
47 namespace storage { | 48 namespace storage { |
48 namespace { | 49 namespace { |
49 | 50 |
50 const int kTestDiskCacheStreamIndex = 0; | 51 const int kTestDiskCacheStreamIndex = 0; |
51 const int kTestDiskCacheSideStreamIndex = 1; | 52 const int kTestDiskCacheSideStreamIndex = 1; |
52 | 53 |
| 54 void SaveBlobStatusAndFiles(BlobStatus* status_ptr, |
| 55 std::vector<FileCreationInfo>* files_ptr, |
| 56 BlobStatus status, |
| 57 std::vector<FileCreationInfo> files) { |
| 58 *status_ptr = status; |
| 59 for (FileCreationInfo& info : files) { |
| 60 files_ptr->push_back(std::move(info)); |
| 61 } |
| 62 } |
| 63 |
53 // Our disk cache tests don't need a real data handle since the tests themselves | 64 // Our disk cache tests don't need a real data handle since the tests themselves |
54 // scope the disk cache and entries. | 65 // scope the disk cache and entries. |
55 class EmptyDataHandle : public storage::BlobDataBuilder::DataHandle { | 66 class EmptyDataHandle : public storage::BlobDataBuilder::DataHandle { |
56 private: | 67 private: |
57 ~EmptyDataHandle() override {} | 68 ~EmptyDataHandle() override {} |
58 }; | 69 }; |
59 | 70 |
60 // A disk_cache::Entry that arbitrarily delays the completion of a read | 71 // A disk_cache::Entry that arbitrarily delays the completion of a read |
61 // operation to allow testing some races without flake. This is particularly | 72 // operation to allow testing some races without flake. This is particularly |
62 // relevant in this unit test, which uses the always-synchronous MEMORY_CACHE. | 73 // relevant in this unit test, which uses the always-synchronous MEMORY_CACHE. |
(...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 const size_t kItemSize = 6; | 913 const size_t kItemSize = 6; |
903 const size_t kBufferSize = 10; | 914 const size_t kBufferSize = 10; |
904 const size_t kTotalSize = kNumItems * kItemSize; | 915 const size_t kTotalSize = kNumItems * kItemSize; |
905 char current_value = 0; | 916 char current_value = 0; |
906 // Create blob and reader. | 917 // Create blob and reader. |
907 for (size_t i = 0; i < kNumItems; i++) { | 918 for (size_t i = 0; i < kNumItems; i++) { |
908 current_value += kItemSize; | 919 current_value += kItemSize; |
909 FilePath path = kPathBase.Append( | 920 FilePath path = kPathBase.Append( |
910 FilePath::FromUTF8Unsafe(base::StringPrintf("%d", current_value))); | 921 FilePath::FromUTF8Unsafe(base::StringPrintf("%d", current_value))); |
911 uint64_t offset = i % 3 == 0 ? 1 : 0; | 922 uint64_t offset = i % 3 == 0 ? 1 : 0; |
912 uint64_t size = | 923 uint64_t size = kItemSize; |
913 i % 2 == 0 ? kItemSize : std::numeric_limits<uint64_t>::max(); | |
914 b.AppendFile(path, offset, size, kTime); | 924 b.AppendFile(path, offset, size, kTime); |
915 } | 925 } |
916 this->InitializeReader(&b); | 926 this->InitializeReader(&b); |
917 | 927 |
918 // Set expectations. | 928 // Set expectations. |
919 current_value = 0; | 929 current_value = 0; |
920 for (size_t i = 0; i < kNumItems; i++) { | 930 for (size_t i = 0; i < kNumItems; i++) { |
921 uint64_t offset = i % 3 == 0 ? 1 : 0; | 931 uint64_t offset = i % 3 == 0 ? 1 : 0; |
922 std::unique_ptr<char[]> buf(new char[kItemSize + offset]); | 932 std::unique_ptr<char[]> buf(new char[kItemSize + offset]); |
923 if (offset > 0) { | 933 if (offset > 0) { |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1167 EXPECT_EQ(BlobReader::Status::DONE, | 1177 EXPECT_EQ(BlobReader::Status::DONE, |
1168 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); | 1178 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); |
1169 buffer = CreateBuffer(kDataSize + 1); | 1179 buffer = CreateBuffer(kDataSize + 1); |
1170 EXPECT_EQ(BlobReader::Status::NET_ERROR, | 1180 EXPECT_EQ(BlobReader::Status::NET_ERROR, |
1171 reader_->SetReadRange(0, kDataSize + 1)); | 1181 reader_->SetReadRange(0, kDataSize + 1)); |
1172 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, reader_->net_error()); | 1182 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, reader_->net_error()); |
1173 } | 1183 } |
1174 | 1184 |
1175 TEST_F(BlobReaderTest, HandleBeforeAsyncCancel) { | 1185 TEST_F(BlobReaderTest, HandleBeforeAsyncCancel) { |
1176 const std::string kUuid("uuid1"); | 1186 const std::string kUuid("uuid1"); |
| 1187 const std::string kData("Hello!!!"); |
| 1188 const size_t kDataSize = 8ul; |
| 1189 std::vector<FileCreationInfo> files; |
1177 | 1190 |
1178 context_.CreatePendingBlob(kUuid, "", ""); | 1191 BlobDataBuilder b(kUuid); |
1179 blob_handle_ = context_.GetBlobDataFromUUID(kUuid); | 1192 b.AppendFutureData(kDataSize); |
| 1193 BlobStatus can_populate_status = |
| 1194 BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 1195 blob_handle_ = context_.BuildBlob( |
| 1196 b, base::Bind(&SaveBlobStatusAndFiles, &can_populate_status, &files)); |
| 1197 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, can_populate_status); |
1180 provider_ = new MockFileStreamReaderProvider(); | 1198 provider_ = new MockFileStreamReaderProvider(); |
1181 reader_.reset(new BlobReader(blob_handle_.get(), base::WrapUnique(provider_), | 1199 reader_.reset(new BlobReader(blob_handle_.get(), base::WrapUnique(provider_), |
1182 message_loop_.task_runner().get())); | 1200 message_loop_.task_runner().get())); |
1183 int size_result = -1; | 1201 int size_result = -1; |
1184 EXPECT_EQ(BlobReader::Status::IO_PENDING, | 1202 EXPECT_EQ(BlobReader::Status::IO_PENDING, |
1185 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); | 1203 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); |
1186 EXPECT_FALSE(reader_->IsInMemory()); | 1204 EXPECT_FALSE(reader_->IsInMemory()); |
1187 context_.CancelPendingBlob(kUuid, IPCBlobCreationCancelCode::UNKNOWN); | 1205 context_.BreakAndFinishPendingBlob( |
| 1206 kUuid, BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS); |
1188 base::RunLoop().RunUntilIdle(); | 1207 base::RunLoop().RunUntilIdle(); |
1189 EXPECT_EQ(net::ERR_FAILED, size_result); | 1208 EXPECT_EQ(net::ERR_FAILED, size_result); |
1190 } | 1209 } |
1191 | 1210 |
1192 TEST_F(BlobReaderTest, ReadFromIncompleteBlob) { | 1211 TEST_F(BlobReaderTest, ReadFromIncompleteBlob) { |
1193 const std::string kUuid("uuid1"); | 1212 const std::string kUuid("uuid1"); |
1194 const std::string kData("Hello!!!"); | 1213 const std::string kData("Hello!!!"); |
1195 const size_t kDataSize = 8ul; | 1214 const size_t kDataSize = 8ul; |
| 1215 std::vector<FileCreationInfo> files; |
1196 | 1216 |
1197 BlobDataBuilder b(kUuid); | 1217 BlobDataBuilder b(kUuid); |
1198 b.AppendData(kData); | 1218 b.AppendFutureData(kDataSize); |
1199 context_.CreatePendingBlob(kUuid, "", ""); | 1219 BlobStatus can_populate_status = |
1200 blob_handle_ = context_.GetBlobDataFromUUID(kUuid); | 1220 BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 1221 blob_handle_ = context_.BuildBlob( |
| 1222 b, base::Bind(&SaveBlobStatusAndFiles, &can_populate_status, &files)); |
| 1223 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, can_populate_status); |
1201 provider_ = new MockFileStreamReaderProvider(); | 1224 provider_ = new MockFileStreamReaderProvider(); |
1202 reader_.reset(new BlobReader(blob_handle_.get(), base::WrapUnique(provider_), | 1225 reader_.reset(new BlobReader(blob_handle_.get(), base::WrapUnique(provider_), |
1203 message_loop_.task_runner().get())); | 1226 message_loop_.task_runner().get())); |
1204 int size_result = -1; | 1227 int size_result = -1; |
1205 EXPECT_EQ(BlobReader::Status::IO_PENDING, | 1228 EXPECT_EQ(BlobReader::Status::IO_PENDING, |
1206 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); | 1229 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); |
1207 EXPECT_FALSE(reader_->IsInMemory()); | 1230 EXPECT_FALSE(reader_->IsInMemory()); |
1208 context_.CompletePendingBlob(b); | 1231 b.PopulateFutureData(0, kData.data(), 0, kDataSize); |
| 1232 context_.FinishedPopulatingPendingBlob(kUuid); |
1209 base::RunLoop().RunUntilIdle(); | 1233 base::RunLoop().RunUntilIdle(); |
1210 CheckSizeCalculatedAsynchronously(kDataSize, size_result); | 1234 CheckSizeCalculatedAsynchronously(kDataSize, size_result); |
1211 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kDataSize)); | 1235 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kDataSize)); |
1212 | 1236 |
1213 int bytes_read = 0; | 1237 int bytes_read = 0; |
1214 int async_bytes_read = 0; | 1238 int async_bytes_read = 0; |
1215 EXPECT_EQ(BlobReader::Status::DONE, | 1239 EXPECT_EQ(BlobReader::Status::DONE, |
1216 reader_->Read(buffer.get(), kDataSize, &bytes_read, | 1240 reader_->Read(buffer.get(), kDataSize, &bytes_read, |
1217 base::Bind(&SetValue<int>, &async_bytes_read))); | 1241 base::Bind(&SetValue<int>, &async_bytes_read))); |
1218 EXPECT_EQ(net::OK, reader_->net_error()); | 1242 EXPECT_EQ(net::OK, reader_->net_error()); |
1219 EXPECT_EQ(kDataSize, static_cast<size_t>(bytes_read)); | 1243 EXPECT_EQ(kDataSize, static_cast<size_t>(bytes_read)); |
1220 EXPECT_EQ(0, async_bytes_read); | 1244 EXPECT_EQ(0, async_bytes_read); |
1221 EXPECT_EQ(kData, std::string(buffer->data(), kDataSize)); | 1245 EXPECT_EQ(kData, std::string(buffer->data(), kDataSize)); |
1222 EXPECT_EQ(net::OK, size_result); | 1246 EXPECT_EQ(net::OK, size_result); |
1223 } | 1247 } |
1224 | 1248 |
1225 } // namespace storage | 1249 } // namespace storage |
OLD | NEW |