summaryrefslogtreecommitdiffstats
path: root/net/base/upload_disk_cache_entry_element_reader_unittest.cc
blob: 7321acc9cd5b54bc544cc984c4b0cc4a734988b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/upload_disk_cache_entry_element_reader.h"

#include <stdint.h>

#include <algorithm>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/disk_cache.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

namespace net {
namespace {

const int kTestDiskCacheStreamIndex = 0;

const char kDataKey[] = "a key";

const char kData[] = "this is data in a disk cache entry";
const size_t kDataSize = arraysize(kData) - 1;

// A disk_cache::Entry that arbitrarily delays the completion of a read
// operation to allow testing some races without flake. This is particularly
// relevant in this unit test, which uses the always-synchronous MEMORY_CACHE.
class DelayedReadEntry : public disk_cache::Entry {
 public:
  explicit DelayedReadEntry(disk_cache::ScopedEntryPtr entry)
      : entry_(entry.Pass()) {}
  ~DelayedReadEntry() override { EXPECT_FALSE(HasPendingReadCallbacks()); }

  bool HasPendingReadCallbacks() { return !pending_read_callbacks_.empty(); }

  void RunPendingReadCallbacks() {
    std::vector<base::Callback<void(void)>> callbacks;
    pending_read_callbacks_.swap(callbacks);
    for (const auto& callback : callbacks)
      callback.Run();
  }

  // From disk_cache::Entry:
  void Doom() override { entry_->Doom(); }

  void Close() override { delete this; }  // Note this is required by the API.

  std::string GetKey() const override { return entry_->GetKey(); }

  base::Time GetLastUsed() const override { return entry_->GetLastUsed(); }

  base::Time GetLastModified() const override {
    return entry_->GetLastModified();
  }

  int32 GetDataSize(int index) const override {
    return entry_->GetDataSize(index);
  }

  int ReadData(int index,
               int offset,
               IOBuffer* buf,
               int buf_len,
               const CompletionCallback& original_callback) override {
    TestCompletionCallback callback;
    int rv = entry_->ReadData(index, offset, buf, buf_len, callback.callback());
    DCHECK_NE(rv, ERR_IO_PENDING)
        << "Test expects to use a MEMORY_CACHE instance, which is synchronous.";
    pending_read_callbacks_.push_back(base::Bind(original_callback, rv));
    return ERR_IO_PENDING;
  }

  int WriteData(int index,
                int offset,
                IOBuffer* buf,
                int buf_len,
                const CompletionCallback& callback,
                bool truncate) override {
    return entry_->WriteData(index, offset, buf, buf_len, callback, truncate);
  }

  int ReadSparseData(int64 offset,
                     IOBuffer* buf,
                     int buf_len,
                     const CompletionCallback& callback) override {
    return entry_->ReadSparseData(offset, buf, buf_len, callback);
  }

  int WriteSparseData(int64 offset,
                      IOBuffer* buf,
                      int buf_len,
                      const CompletionCallback& callback) override {
    return entry_->WriteSparseData(offset, buf, buf_len, callback);
  }

  int GetAvailableRange(int64 offset,
                        int len,
                        int64* start,
                        const CompletionCallback& callback) override {
    return entry_->GetAvailableRange(offset, len, start, callback);
  }

  bool CouldBeSparse() const override { return entry_->CouldBeSparse(); }

  void CancelSparseIO() override { entry_->CancelSparseIO(); }

  int ReadyForSparseIO(const CompletionCallback& callback) override {
    return entry_->ReadyForSparseIO(callback);
  }

 private:
  disk_cache::ScopedEntryPtr entry_;
  std::vector<base::Callback<void(void)>> pending_read_callbacks_;
};

class UploadDiskCacheEntryElementReaderTest : public PlatformTest {
 public:
  UploadDiskCacheEntryElementReaderTest() {}

  ~UploadDiskCacheEntryElementReaderTest() override {}

  void SetUp() override {
    TestCompletionCallback callback;
    int rv = disk_cache::CreateCacheBackend(
        MEMORY_CACHE, CACHE_BACKEND_DEFAULT, base::FilePath(), 0, false,
        nullptr, nullptr, &cache_, callback.callback());
    ASSERT_EQ(OK, callback.GetResult(rv));

    disk_cache::Entry* tmp_entry = nullptr;
    rv = cache_->CreateEntry(kDataKey, &tmp_entry, callback.callback());
    ASSERT_EQ(OK, callback.GetResult(rv));
    entry_.reset(tmp_entry);

    scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(kData);
    rv = entry_->WriteData(kTestDiskCacheStreamIndex, 0, io_buffer.get(),
                           kDataSize, callback.callback(), false);
    EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv));
  }

  void set_entry(disk_cache::ScopedEntryPtr entry) { entry_.swap(entry); }
  disk_cache::Entry* entry() { return entry_.get(); }
  disk_cache::ScopedEntryPtr release_entry() { return entry_.Pass(); }

 private:
  scoped_ptr<disk_cache::Backend> cache_;
  disk_cache::ScopedEntryPtr entry_;
};

TEST_F(UploadDiskCacheEntryElementReaderTest, ReadAll) {
  UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex,
                                           0, kDataSize);
  EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining());

  char read_buffer[kDataSize];
  std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0');

  scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer);
  TestCompletionCallback callback;
  int rv = reader.Read(io_buffer.get(), kDataSize, callback.callback());
  EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv));
  EXPECT_EQ(0U, reader.BytesRemaining())
      << "Expected a single read of |kDataSize| to retrieve entire entry.";
  EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize));
}

TEST_F(UploadDiskCacheEntryElementReaderTest, ReadPartially) {
  UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex,
                                           0, kDataSize);
  EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining());

  const size_t kReadBuffer1Size = kDataSize / 3;
  char read_buffer1[kReadBuffer1Size];
  std::fill(read_buffer1, read_buffer1 + arraysize(read_buffer1), '\0');

  scoped_refptr<IOBuffer> io_buffer1 = new WrappedIOBuffer(read_buffer1);

  const size_t kReadBuffer2Size = kDataSize - kReadBuffer1Size;
  char read_buffer2[kReadBuffer2Size];
  scoped_refptr<IOBuffer> io_buffer2 = new WrappedIOBuffer(read_buffer2);

  TestCompletionCallback callback;
  int rv = reader.Read(io_buffer1.get(), kReadBuffer1Size, callback.callback());
  EXPECT_EQ(static_cast<int>(kReadBuffer1Size), callback.GetResult(rv));
  EXPECT_EQ(static_cast<uint64_t>(kReadBuffer2Size), reader.BytesRemaining());

  rv = reader.Read(io_buffer2.get(), kReadBuffer2Size, callback.callback());
  EXPECT_EQ(static_cast<int>(kReadBuffer2Size), callback.GetResult(rv));
  EXPECT_EQ(0U, reader.BytesRemaining());

  EXPECT_EQ(std::string(kData, kDataSize),
            std::string(read_buffer1, kReadBuffer1Size) +
                std::string(read_buffer2, kReadBuffer2Size));
}

TEST_F(UploadDiskCacheEntryElementReaderTest, ReadTooMuch) {
  UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex,
                                           0, kDataSize);
  EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining());

  const size_t kTooLargeSize = kDataSize + kDataSize / 2;

  char read_buffer[kTooLargeSize];
  std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0');

  scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer);
  TestCompletionCallback callback;
  int rv = reader.Read(io_buffer.get(), kTooLargeSize, callback.callback());
  EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv));
  EXPECT_EQ(0U, reader.BytesRemaining());
  EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize));
}

TEST_F(UploadDiskCacheEntryElementReaderTest, ReadAsync) {
  DelayedReadEntry* delayed_read_entry = new DelayedReadEntry(release_entry());
  set_entry(disk_cache::ScopedEntryPtr(delayed_read_entry));

  UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex,
                                           0, kDataSize);

  char read_buffer[kDataSize];
  std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0');

  scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer);
  TestCompletionCallback callback;
  int rv = reader.Read(io_buffer.get(), kDataSize, callback.callback());
  EXPECT_EQ(ERR_IO_PENDING, rv);
  EXPECT_TRUE(delayed_read_entry->HasPendingReadCallbacks());
  EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining());

  delayed_read_entry->RunPendingReadCallbacks();
  EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv));
  EXPECT_EQ(0U, reader.BytesRemaining())
      << "Expected a single read of |kDataSize| to retrieve entire entry.";
  EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize));
}

TEST_F(UploadDiskCacheEntryElementReaderTest, MultipleInit) {
  UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex,
                                           0, kDataSize);
  char read_buffer[kDataSize];
  std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0');

  scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer);
  TestCompletionCallback callback;
  int rv = reader.Read(io_buffer.get(), kDataSize, callback.callback());
  EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv));
  EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize));

  rv = reader.Init(callback.callback());
  EXPECT_EQ(OK, callback.GetResult(rv));
  EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining());
  rv = reader.Read(io_buffer.get(), kDataSize, callback.callback());
  EXPECT_EQ(static_cast<int>(kDataSize), callback.GetResult(rv));
  EXPECT_EQ(std::string(kData, kDataSize), std::string(read_buffer, kDataSize));
}

TEST_F(UploadDiskCacheEntryElementReaderTest, InitDuringAsyncOperation) {
  DelayedReadEntry* delayed_read_entry = new DelayedReadEntry(release_entry());
  set_entry(disk_cache::ScopedEntryPtr(delayed_read_entry));

  UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex,
                                           0, kDataSize);
  char read_buffer[kDataSize];
  std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0');

  scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer);
  TestCompletionCallback read_callback;
  int rv = reader.Read(io_buffer.get(), kDataSize, read_callback.callback());
  EXPECT_EQ(ERR_IO_PENDING, rv);
  EXPECT_TRUE(delayed_read_entry->HasPendingReadCallbacks());
  EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining());

  TestCompletionCallback init_callback;
  rv = reader.Init(init_callback.callback());
  EXPECT_EQ(OK, init_callback.GetResult(rv));

  delayed_read_entry->RunPendingReadCallbacks();
  EXPECT_FALSE(delayed_read_entry->HasPendingReadCallbacks());
  EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining());

  char read_buffer2[kDataSize];
  std::fill(read_buffer2, read_buffer2 + arraysize(read_buffer2), '\0');
  scoped_refptr<IOBuffer> io_buffer2 = new WrappedIOBuffer(read_buffer2);
  TestCompletionCallback read_callback2;
  rv = reader.Read(io_buffer2.get(), kDataSize, read_callback2.callback());
  EXPECT_EQ(ERR_IO_PENDING, rv);
  EXPECT_TRUE(delayed_read_entry->HasPendingReadCallbacks());
  EXPECT_EQ(static_cast<uint64_t>(kDataSize), reader.BytesRemaining());

  delayed_read_entry->RunPendingReadCallbacks();
  EXPECT_FALSE(delayed_read_entry->HasPendingReadCallbacks());
  read_callback2.WaitForResult();  // Succeeds if this does not deadlock.
  EXPECT_EQ(std::string(kData, kDataSize),
            std::string(read_buffer2, kDataSize));
}

TEST_F(UploadDiskCacheEntryElementReaderTest, Range) {
  const size_t kOffset = kDataSize / 4;
  const size_t kLength = kDataSize / 3;

  UploadDiskCacheEntryElementReader reader(entry(), kTestDiskCacheStreamIndex,
                                           kOffset, kLength);
  EXPECT_EQ(static_cast<uint64_t>(kLength), reader.BytesRemaining());

  char read_buffer[kLength];
  std::fill(read_buffer, read_buffer + arraysize(read_buffer), '\0');

  scoped_refptr<IOBuffer> io_buffer = new WrappedIOBuffer(read_buffer);
  TestCompletionCallback callback;
  int rv = reader.Read(io_buffer.get(), kLength, callback.callback());
  EXPECT_EQ(static_cast<int>(kLength), callback.GetResult(rv));
  EXPECT_EQ(0U, reader.BytesRemaining());
  EXPECT_EQ(std::string(kData + kOffset, kLength),
            std::string(read_buffer, kLength));
}

}  // namespace
}  // namespace net