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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 | 46 |
47 namespace storage { | 47 namespace storage { |
48 namespace { | 48 namespace { |
49 | 49 |
50 const int kTestDiskCacheStreamIndex = 0; | 50 const int kTestDiskCacheStreamIndex = 0; |
51 const int kTestDiskCacheSideStreamIndex = 1; | 51 const int kTestDiskCacheSideStreamIndex = 1; |
52 | 52 |
| 53 void CanPopulateDataCallback(BlobStatus* result_pointer, BlobStatus result) { |
| 54 *result_pointer = result; |
| 55 } |
| 56 |
53 // Our disk cache tests don't need a real data handle since the tests themselves | 57 // Our disk cache tests don't need a real data handle since the tests themselves |
54 // scope the disk cache and entries. | 58 // scope the disk cache and entries. |
55 class EmptyDataHandle : public storage::BlobDataBuilder::DataHandle { | 59 class EmptyDataHandle : public storage::BlobDataBuilder::DataHandle { |
56 private: | 60 private: |
57 ~EmptyDataHandle() override {} | 61 ~EmptyDataHandle() override {} |
58 }; | 62 }; |
59 | 63 |
60 // A disk_cache::Entry that arbitrarily delays the completion of a read | 64 // A disk_cache::Entry that arbitrarily delays the completion of a read |
61 // operation to allow testing some races without flake. This is particularly | 65 // operation to allow testing some races without flake. This is particularly |
62 // relevant in this unit test, which uses the always-synchronous MEMORY_CACHE. | 66 // 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; | 906 const size_t kItemSize = 6; |
903 const size_t kBufferSize = 10; | 907 const size_t kBufferSize = 10; |
904 const size_t kTotalSize = kNumItems * kItemSize; | 908 const size_t kTotalSize = kNumItems * kItemSize; |
905 char current_value = 0; | 909 char current_value = 0; |
906 // Create blob and reader. | 910 // Create blob and reader. |
907 for (size_t i = 0; i < kNumItems; i++) { | 911 for (size_t i = 0; i < kNumItems; i++) { |
908 current_value += kItemSize; | 912 current_value += kItemSize; |
909 FilePath path = kPathBase.Append( | 913 FilePath path = kPathBase.Append( |
910 FilePath::FromUTF8Unsafe(base::StringPrintf("%d", current_value))); | 914 FilePath::FromUTF8Unsafe(base::StringPrintf("%d", current_value))); |
911 uint64_t offset = i % 3 == 0 ? 1 : 0; | 915 uint64_t offset = i % 3 == 0 ? 1 : 0; |
912 uint64_t size = | 916 uint64_t size = kItemSize; |
913 i % 2 == 0 ? kItemSize : std::numeric_limits<uint64_t>::max(); | |
914 b.AppendFile(path, offset, size, kTime); | 917 b.AppendFile(path, offset, size, kTime); |
915 } | 918 } |
916 this->InitializeReader(&b); | 919 this->InitializeReader(&b); |
917 | 920 |
918 // Set expectations. | 921 // Set expectations. |
919 current_value = 0; | 922 current_value = 0; |
920 for (size_t i = 0; i < kNumItems; i++) { | 923 for (size_t i = 0; i < kNumItems; i++) { |
921 uint64_t offset = i % 3 == 0 ? 1 : 0; | 924 uint64_t offset = i % 3 == 0 ? 1 : 0; |
922 std::unique_ptr<char[]> buf(new char[kItemSize + offset]); | 925 std::unique_ptr<char[]> buf(new char[kItemSize + offset]); |
923 if (offset > 0) { | 926 if (offset > 0) { |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1167 EXPECT_EQ(BlobReader::Status::DONE, | 1170 EXPECT_EQ(BlobReader::Status::DONE, |
1168 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); | 1171 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); |
1169 buffer = CreateBuffer(kDataSize + 1); | 1172 buffer = CreateBuffer(kDataSize + 1); |
1170 EXPECT_EQ(BlobReader::Status::NET_ERROR, | 1173 EXPECT_EQ(BlobReader::Status::NET_ERROR, |
1171 reader_->SetReadRange(0, kDataSize + 1)); | 1174 reader_->SetReadRange(0, kDataSize + 1)); |
1172 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, reader_->net_error()); | 1175 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, reader_->net_error()); |
1173 } | 1176 } |
1174 | 1177 |
1175 TEST_F(BlobReaderTest, HandleBeforeAsyncCancel) { | 1178 TEST_F(BlobReaderTest, HandleBeforeAsyncCancel) { |
1176 const std::string kUuid("uuid1"); | 1179 const std::string kUuid("uuid1"); |
| 1180 const std::string kData("Hello!!!"); |
| 1181 const size_t kDataSize = 8ul; |
1177 | 1182 |
1178 context_.CreatePendingBlob(kUuid, "", ""); | 1183 BlobDataBuilder b(kUuid); |
1179 blob_handle_ = context_.GetBlobDataFromUUID(kUuid); | 1184 b.AppendFutureData(kDataSize); |
| 1185 BlobStatus can_populate_status = BlobStatus::INVALID_CONSTRUCTION_ARGUMENTS; |
| 1186 blob_handle_ = context_.BuildBlob( |
| 1187 b, base::Bind(&CanPopulateDataCallback, &can_populate_status)); |
| 1188 EXPECT_EQ(BlobStatus::PENDING, can_populate_status); |
1180 provider_ = new MockFileStreamReaderProvider(); | 1189 provider_ = new MockFileStreamReaderProvider(); |
1181 reader_.reset(new BlobReader(blob_handle_.get(), base::WrapUnique(provider_), | 1190 reader_.reset(new BlobReader(blob_handle_.get(), base::WrapUnique(provider_), |
1182 message_loop_.task_runner().get())); | 1191 message_loop_.task_runner().get())); |
1183 int size_result = -1; | 1192 int size_result = -1; |
1184 EXPECT_EQ(BlobReader::Status::IO_PENDING, | 1193 EXPECT_EQ(BlobReader::Status::IO_PENDING, |
1185 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); | 1194 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); |
1186 EXPECT_FALSE(reader_->IsInMemory()); | 1195 EXPECT_FALSE(reader_->IsInMemory()); |
1187 context_.CancelPendingBlob(kUuid, IPCBlobCreationCancelCode::UNKNOWN); | 1196 context_.BreakAndFinishBlob(kUuid, |
| 1197 BlobStatus::INVALID_CONSTRUCTION_ARGUMENTS); |
1188 base::RunLoop().RunUntilIdle(); | 1198 base::RunLoop().RunUntilIdle(); |
1189 EXPECT_EQ(net::ERR_FAILED, size_result); | 1199 EXPECT_EQ(net::ERR_FAILED, size_result); |
1190 } | 1200 } |
1191 | 1201 |
1192 TEST_F(BlobReaderTest, ReadFromIncompleteBlob) { | 1202 TEST_F(BlobReaderTest, ReadFromIncompleteBlob) { |
1193 const std::string kUuid("uuid1"); | 1203 const std::string kUuid("uuid1"); |
1194 const std::string kData("Hello!!!"); | 1204 const std::string kData("Hello!!!"); |
1195 const size_t kDataSize = 8ul; | 1205 const size_t kDataSize = 8ul; |
1196 | 1206 |
1197 BlobDataBuilder b(kUuid); | 1207 BlobDataBuilder b(kUuid); |
1198 b.AppendData(kData); | 1208 b.AppendFutureData(kDataSize); |
1199 context_.CreatePendingBlob(kUuid, "", ""); | 1209 BlobStatus can_populate_status = BlobStatus::INVALID_CONSTRUCTION_ARGUMENTS; |
1200 blob_handle_ = context_.GetBlobDataFromUUID(kUuid); | 1210 blob_handle_ = context_.BuildBlob( |
| 1211 b, base::Bind(&CanPopulateDataCallback, &can_populate_status)); |
| 1212 EXPECT_EQ(BlobStatus::PENDING, can_populate_status); |
1201 provider_ = new MockFileStreamReaderProvider(); | 1213 provider_ = new MockFileStreamReaderProvider(); |
1202 reader_.reset(new BlobReader(blob_handle_.get(), base::WrapUnique(provider_), | 1214 reader_.reset(new BlobReader(blob_handle_.get(), base::WrapUnique(provider_), |
1203 message_loop_.task_runner().get())); | 1215 message_loop_.task_runner().get())); |
1204 int size_result = -1; | 1216 int size_result = -1; |
1205 EXPECT_EQ(BlobReader::Status::IO_PENDING, | 1217 EXPECT_EQ(BlobReader::Status::IO_PENDING, |
1206 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); | 1218 reader_->CalculateSize(base::Bind(&SetValue<int>, &size_result))); |
1207 EXPECT_FALSE(reader_->IsInMemory()); | 1219 EXPECT_FALSE(reader_->IsInMemory()); |
1208 context_.CompletePendingBlob(b); | 1220 b.PopulateFutureData(0, kData.data(), 0, kDataSize); |
| 1221 context_.FinishedPopulatingBlob(kUuid); |
1209 base::RunLoop().RunUntilIdle(); | 1222 base::RunLoop().RunUntilIdle(); |
1210 CheckSizeCalculatedAsynchronously(kDataSize, size_result); | 1223 CheckSizeCalculatedAsynchronously(kDataSize, size_result); |
1211 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kDataSize)); | 1224 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kDataSize)); |
1212 | 1225 |
1213 int bytes_read = 0; | 1226 int bytes_read = 0; |
1214 int async_bytes_read = 0; | 1227 int async_bytes_read = 0; |
1215 EXPECT_EQ(BlobReader::Status::DONE, | 1228 EXPECT_EQ(BlobReader::Status::DONE, |
1216 reader_->Read(buffer.get(), kDataSize, &bytes_read, | 1229 reader_->Read(buffer.get(), kDataSize, &bytes_read, |
1217 base::Bind(&SetValue<int>, &async_bytes_read))); | 1230 base::Bind(&SetValue<int>, &async_bytes_read))); |
1218 EXPECT_EQ(net::OK, reader_->net_error()); | 1231 EXPECT_EQ(net::OK, reader_->net_error()); |
1219 EXPECT_EQ(kDataSize, static_cast<size_t>(bytes_read)); | 1232 EXPECT_EQ(kDataSize, static_cast<size_t>(bytes_read)); |
1220 EXPECT_EQ(0, async_bytes_read); | 1233 EXPECT_EQ(0, async_bytes_read); |
1221 EXPECT_EQ(kData, std::string(buffer->data(), kDataSize)); | 1234 EXPECT_EQ(kData, std::string(buffer->data(), kDataSize)); |
1222 EXPECT_EQ(net::OK, size_result); | 1235 EXPECT_EQ(net::OK, size_result); |
1223 } | 1236 } |
1224 | 1237 |
1225 } // namespace storage | 1238 } // namespace storage |
OLD | NEW |