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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
|
// Copyright (c) 2012 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/file_stream.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/sequenced_worker_pool_owner.h"
#include "base/test/test_timeouts.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread_restrictions.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/log/test_net_log.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#if defined(OS_ANDROID)
#include "base/test/test_file_util.h"
#endif
namespace net {
namespace {
const char kTestData[] = "0123456789";
const int kTestDataSize = arraysize(kTestData) - 1;
// Creates an IOBufferWithSize that contains the kTestDataSize.
IOBufferWithSize* CreateTestDataBuffer() {
IOBufferWithSize* buf = new IOBufferWithSize(kTestDataSize);
memcpy(buf->data(), kTestData, kTestDataSize);
return buf;
}
} // namespace
class FileStreamTest : public PlatformTest {
public:
void SetUp() override {
PlatformTest::SetUp();
base::CreateTemporaryFile(&temp_file_path_);
base::WriteFile(temp_file_path_, kTestData, kTestDataSize);
}
void TearDown() override {
// FileStreamContexts must be asynchronously closed on the file task runner
// before they can be deleted. Pump the RunLoop to avoid leaks.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(base::DeleteFile(temp_file_path_, false));
PlatformTest::TearDown();
}
const base::FilePath temp_file_path() const { return temp_file_path_; }
private:
base::FilePath temp_file_path_;
};
namespace {
TEST_F(FileStreamTest, OpenExplicitClose) {
TestCompletionCallback callback;
FileStream stream(base::ThreadTaskRunnerHandle::Get());
int flags = base::File::FLAG_OPEN |
base::File::FLAG_READ |
base::File::FLAG_ASYNC;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(stream.IsOpen());
EXPECT_EQ(ERR_IO_PENDING, stream.Close(callback.callback()));
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_FALSE(stream.IsOpen());
}
TEST_F(FileStreamTest, OpenExplicitCloseOrphaned) {
TestCompletionCallback callback;
scoped_ptr<FileStream> stream(
new FileStream(base::ThreadTaskRunnerHandle::Get()));
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_ASYNC;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(stream->IsOpen());
EXPECT_EQ(ERR_IO_PENDING, stream->Close(callback.callback()));
stream.reset();
// File isn't actually closed yet.
base::RunLoop runloop;
runloop.RunUntilIdle();
// The file should now be closed, though the callback has not been called.
}
// Test the use of FileStream with a file handle provided at construction.
TEST_F(FileStreamTest, UseFileHandle) {
int rv = 0;
TestCompletionCallback callback;
TestInt64CompletionCallback callback64;
// 1. Test reading with a file handle.
ASSERT_EQ(kTestDataSize,
base::WriteFile(temp_file_path(), kTestData, kTestDataSize));
int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ |
base::File::FLAG_ASYNC;
base::File file(temp_file_path(), flags);
// Seek to the beginning of the file and read.
scoped_ptr<FileStream> read_stream(
new FileStream(file.Pass(), base::ThreadTaskRunnerHandle::Get()));
ASSERT_EQ(ERR_IO_PENDING, read_stream->Seek(0, callback64.callback()));
ASSERT_EQ(0, callback64.WaitForResult());
// Read into buffer and compare.
scoped_refptr<IOBufferWithSize> read_buffer =
new IOBufferWithSize(kTestDataSize);
rv = read_stream->Read(read_buffer.get(), kTestDataSize, callback.callback());
ASSERT_EQ(kTestDataSize, callback.GetResult(rv));
ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize));
read_stream.reset();
// 2. Test writing with a file handle.
base::DeleteFile(temp_file_path(), false);
flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE |
base::File::FLAG_ASYNC;
file.Initialize(temp_file_path(), flags);
scoped_ptr<FileStream> write_stream(
new FileStream(file.Pass(), base::ThreadTaskRunnerHandle::Get()));
ASSERT_EQ(ERR_IO_PENDING, write_stream->Seek(0, callback64.callback()));
ASSERT_EQ(0, callback64.WaitForResult());
scoped_refptr<IOBufferWithSize> write_buffer = CreateTestDataBuffer();
rv = write_stream->Write(write_buffer.get(), kTestDataSize,
callback.callback());
ASSERT_EQ(kTestDataSize, callback.GetResult(rv));
write_stream.reset();
// Read into buffer and compare to make sure the handle worked fine.
ASSERT_EQ(kTestDataSize,
base::ReadFile(temp_file_path(), read_buffer->data(),
kTestDataSize));
ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize));
}
TEST_F(FileStreamTest, UseClosedStream) {
int rv = 0;
TestCompletionCallback callback;
TestInt64CompletionCallback callback64;
FileStream stream(base::ThreadTaskRunnerHandle::Get());
EXPECT_FALSE(stream.IsOpen());
// Try seeking...
rv = stream.Seek(5, callback64.callback());
EXPECT_EQ(ERR_UNEXPECTED, callback64.GetResult(rv));
// Try reading...
scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(10);
rv = stream.Read(buf.get(), buf->size(), callback.callback());
EXPECT_EQ(ERR_UNEXPECTED, callback.GetResult(rv));
}
TEST_F(FileStreamTest, Read) {
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
FileStream stream(base::ThreadTaskRunnerHandle::Get());
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
int total_bytes_read = 0;
std::string data_read;
for (;;) {
scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
rv = stream.Read(buf.get(), buf->size(), callback.callback());
rv = callback.GetResult(rv);
EXPECT_LE(0, rv);
if (rv <= 0)
break;
total_bytes_read += rv;
data_read.append(buf->data(), rv);
}
EXPECT_EQ(file_size, total_bytes_read);
EXPECT_EQ(kTestData, data_read);
}
TEST_F(FileStreamTest, Read_EarlyDelete) {
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(base::ThreadTaskRunnerHandle::Get()));
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
rv = stream->Read(buf.get(), buf->size(), callback.callback());
stream.reset(); // Delete instead of closing it.
if (rv < 0) {
EXPECT_EQ(ERR_IO_PENDING, rv);
// The callback should not be called if the request is cancelled.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(callback.have_result());
} else {
EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv));
}
}
TEST_F(FileStreamTest, Read_FromOffset) {
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
FileStream stream(base::ThreadTaskRunnerHandle::Get());
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
TestInt64CompletionCallback callback64;
const int64_t kOffset = 3;
rv = stream.Seek(kOffset, callback64.callback());
ASSERT_EQ(ERR_IO_PENDING, rv);
int64_t new_offset = callback64.WaitForResult();
EXPECT_EQ(kOffset, new_offset);
int total_bytes_read = 0;
std::string data_read;
for (;;) {
scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
rv = stream.Read(buf.get(), buf->size(), callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LE(0, rv);
if (rv <= 0)
break;
total_bytes_read += rv;
data_read.append(buf->data(), rv);
}
EXPECT_EQ(file_size - kOffset, total_bytes_read);
EXPECT_EQ(kTestData + kOffset, data_read);
}
TEST_F(FileStreamTest, Write) {
FileStream stream(base::ThreadTaskRunnerHandle::Get());
int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(OK, callback.GetResult(rv));
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(0, file_size);
scoped_refptr<IOBuffer> buf = CreateTestDataBuffer();
rv = stream.Write(buf.get(), kTestDataSize, callback.callback());
rv = callback.GetResult(rv);
EXPECT_EQ(kTestDataSize, rv);
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize, file_size);
std::string data_read;
EXPECT_TRUE(base::ReadFileToString(temp_file_path(), &data_read));
EXPECT_EQ(kTestData, data_read);
}
TEST_F(FileStreamTest, Write_EarlyDelete) {
scoped_ptr<FileStream> stream(
new FileStream(base::ThreadTaskRunnerHandle::Get()));
int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(0, file_size);
scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
rv = stream->Write(buf.get(), buf->size(), callback.callback());
stream.reset();
if (rv < 0) {
EXPECT_EQ(ERR_IO_PENDING, rv);
// The callback should not be called if the request is cancelled.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(callback.have_result());
} else {
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(file_size, rv);
}
}
TEST_F(FileStreamTest, Write_FromOffset) {
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
FileStream stream(base::ThreadTaskRunnerHandle::Get());
int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE |
base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
TestInt64CompletionCallback callback64;
const int64_t kOffset = kTestDataSize;
rv = stream.Seek(kOffset, callback64.callback());
ASSERT_EQ(ERR_IO_PENDING, rv);
int64_t new_offset = callback64.WaitForResult();
EXPECT_EQ(kTestDataSize, new_offset);
int total_bytes_written = 0;
scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
scoped_refptr<DrainableIOBuffer> drainable =
new DrainableIOBuffer(buf.get(), buf->size());
while (total_bytes_written != kTestDataSize) {
rv = stream.Write(drainable.get(), drainable->BytesRemaining(),
callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(0, rv);
if (rv <= 0)
break;
drainable->DidConsume(rv);
total_bytes_written += rv;
}
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(file_size, kTestDataSize * 2);
}
TEST_F(FileStreamTest, BasicReadWrite) {
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(base::ThreadTaskRunnerHandle::Get()));
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
int64_t total_bytes_read = 0;
std::string data_read;
for (;;) {
scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
rv = stream->Read(buf.get(), buf->size(), callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LE(0, rv);
if (rv <= 0)
break;
total_bytes_read += rv;
data_read.append(buf->data(), rv);
}
EXPECT_EQ(file_size, total_bytes_read);
EXPECT_TRUE(data_read == kTestData);
int total_bytes_written = 0;
scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
scoped_refptr<DrainableIOBuffer> drainable =
new DrainableIOBuffer(buf.get(), buf->size());
while (total_bytes_written != kTestDataSize) {
rv = stream->Write(drainable.get(), drainable->BytesRemaining(),
callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(0, rv);
if (rv <= 0)
break;
drainable->DidConsume(rv);
total_bytes_written += rv;
}
stream.reset();
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
}
TEST_F(FileStreamTest, BasicWriteRead) {
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(base::ThreadTaskRunnerHandle::Get()));
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
TestInt64CompletionCallback callback64;
rv = stream->Seek(file_size, callback64.callback());
ASSERT_EQ(ERR_IO_PENDING, rv);
int64_t offset = callback64.WaitForResult();
EXPECT_EQ(offset, file_size);
int total_bytes_written = 0;
scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
scoped_refptr<DrainableIOBuffer> drainable =
new DrainableIOBuffer(buf.get(), buf->size());
while (total_bytes_written != kTestDataSize) {
rv = stream->Write(drainable.get(), drainable->BytesRemaining(),
callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(0, rv);
if (rv <= 0)
break;
drainable->DidConsume(rv);
total_bytes_written += rv;
}
EXPECT_EQ(kTestDataSize, total_bytes_written);
rv = stream->Seek(0, callback64.callback());
ASSERT_EQ(ERR_IO_PENDING, rv);
offset = callback64.WaitForResult();
EXPECT_EQ(0, offset);
int total_bytes_read = 0;
std::string data_read;
for (;;) {
scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
rv = stream->Read(buf.get(), buf->size(), callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LE(0, rv);
if (rv <= 0)
break;
total_bytes_read += rv;
data_read.append(buf->data(), rv);
}
stream.reset();
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
EXPECT_EQ(kTestDataSize * 2, total_bytes_read);
const std::string kExpectedFileData =
std::string(kTestData) + std::string(kTestData);
EXPECT_EQ(kExpectedFileData, data_read);
}
class TestWriteReadCompletionCallback {
public:
TestWriteReadCompletionCallback(FileStream* stream,
int* total_bytes_written,
int* total_bytes_read,
std::string* data_read)
: result_(0),
have_result_(false),
waiting_for_result_(false),
stream_(stream),
total_bytes_written_(total_bytes_written),
total_bytes_read_(total_bytes_read),
data_read_(data_read),
callback_(base::Bind(&TestWriteReadCompletionCallback::OnComplete,
base::Unretained(this))),
test_data_(CreateTestDataBuffer()),
drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) {}
int WaitForResult() {
DCHECK(!waiting_for_result_);
while (!have_result_) {
waiting_for_result_ = true;
base::RunLoop().Run();
waiting_for_result_ = false;
}
have_result_ = false; // auto-reset for next callback
return result_;
}
const CompletionCallback& callback() const { return callback_; }
void ValidateWrittenData() {
TestCompletionCallback callback;
int rv = 0;
for (;;) {
scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
rv = stream_->Read(buf.get(), buf->size(), callback.callback());
if (rv == ERR_IO_PENDING) {
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current());
rv = callback.WaitForResult();
}
EXPECT_LE(0, rv);
if (rv <= 0)
break;
*total_bytes_read_ += rv;
data_read_->append(buf->data(), rv);
}
}
private:
void OnComplete(int result) {
DCHECK_LT(0, result);
*total_bytes_written_ += result;
int rv;
if (*total_bytes_written_ != kTestDataSize) {
// Recurse to finish writing all data.
int total_bytes_written = 0, total_bytes_read = 0;
std::string data_read;
TestWriteReadCompletionCallback callback(
stream_, &total_bytes_written, &total_bytes_read, &data_read);
rv = stream_->Write(
drainable_.get(), drainable_->BytesRemaining(), callback.callback());
DCHECK_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
drainable_->DidConsume(total_bytes_written);
*total_bytes_written_ += total_bytes_written;
*total_bytes_read_ += total_bytes_read;
*data_read_ += data_read;
} else { // We're done writing all data. Start reading the data.
TestInt64CompletionCallback callback64;
EXPECT_EQ(ERR_IO_PENDING, stream_->Seek(0, callback64.callback()));
{
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current());
EXPECT_LE(0, callback64.WaitForResult());
}
}
result_ = *total_bytes_written_;
have_result_ = true;
if (waiting_for_result_)
base::MessageLoop::current()->QuitWhenIdle();
}
int result_;
bool have_result_;
bool waiting_for_result_;
FileStream* stream_;
int* total_bytes_written_;
int* total_bytes_read_;
std::string* data_read_;
const CompletionCallback callback_;
scoped_refptr<IOBufferWithSize> test_data_;
scoped_refptr<DrainableIOBuffer> drainable_;
DISALLOW_COPY_AND_ASSIGN(TestWriteReadCompletionCallback);
};
TEST_F(FileStreamTest, WriteRead) {
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(base::ThreadTaskRunnerHandle::Get()));
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
TestCompletionCallback open_callback;
int rv = stream->Open(temp_file_path(), flags, open_callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, open_callback.WaitForResult());
TestInt64CompletionCallback callback64;
EXPECT_EQ(ERR_IO_PENDING, stream->Seek(file_size, callback64.callback()));
EXPECT_EQ(file_size, callback64.WaitForResult());
int total_bytes_written = 0;
int total_bytes_read = 0;
std::string data_read;
TestWriteReadCompletionCallback callback(stream.get(), &total_bytes_written,
&total_bytes_read, &data_read);
scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
rv = stream->Write(buf.get(), buf->size(), callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(0, rv);
EXPECT_EQ(kTestDataSize, total_bytes_written);
callback.ValidateWrittenData();
stream.reset();
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
EXPECT_EQ(kTestDataSize * 2, total_bytes_read);
const std::string kExpectedFileData =
std::string(kTestData) + std::string(kTestData);
EXPECT_EQ(kExpectedFileData, data_read);
}
class TestWriteCloseCompletionCallback {
public:
TestWriteCloseCompletionCallback(FileStream* stream, int* total_bytes_written)
: result_(0),
have_result_(false),
waiting_for_result_(false),
stream_(stream),
total_bytes_written_(total_bytes_written),
callback_(base::Bind(&TestWriteCloseCompletionCallback::OnComplete,
base::Unretained(this))),
test_data_(CreateTestDataBuffer()),
drainable_(new DrainableIOBuffer(test_data_.get(), kTestDataSize)) {}
int WaitForResult() {
DCHECK(!waiting_for_result_);
while (!have_result_) {
waiting_for_result_ = true;
base::RunLoop().Run();
waiting_for_result_ = false;
}
have_result_ = false; // auto-reset for next callback
return result_;
}
const CompletionCallback& callback() const { return callback_; }
private:
void OnComplete(int result) {
DCHECK_LT(0, result);
*total_bytes_written_ += result;
int rv;
if (*total_bytes_written_ != kTestDataSize) {
// Recurse to finish writing all data.
int total_bytes_written = 0;
TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written);
rv = stream_->Write(
drainable_.get(), drainable_->BytesRemaining(), callback.callback());
DCHECK_EQ(ERR_IO_PENDING, rv);
rv = callback.WaitForResult();
drainable_->DidConsume(total_bytes_written);
*total_bytes_written_ += total_bytes_written;
}
result_ = *total_bytes_written_;
have_result_ = true;
if (waiting_for_result_)
base::MessageLoop::current()->QuitWhenIdle();
}
int result_;
bool have_result_;
bool waiting_for_result_;
FileStream* stream_;
int* total_bytes_written_;
const CompletionCallback callback_;
scoped_refptr<IOBufferWithSize> test_data_;
scoped_refptr<DrainableIOBuffer> drainable_;
DISALLOW_COPY_AND_ASSIGN(TestWriteCloseCompletionCallback);
};
TEST_F(FileStreamTest, WriteClose) {
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(base::ThreadTaskRunnerHandle::Get()));
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
TestCompletionCallback open_callback;
int rv = stream->Open(temp_file_path(), flags, open_callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, open_callback.WaitForResult());
TestInt64CompletionCallback callback64;
EXPECT_EQ(ERR_IO_PENDING, stream->Seek(file_size, callback64.callback()));
EXPECT_EQ(file_size, callback64.WaitForResult());
int total_bytes_written = 0;
TestWriteCloseCompletionCallback callback(stream.get(), &total_bytes_written);
scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
rv = stream->Write(buf.get(), buf->size(), callback.callback());
if (rv == ERR_IO_PENDING)
total_bytes_written = callback.WaitForResult();
EXPECT_LT(0, total_bytes_written);
EXPECT_EQ(kTestDataSize, total_bytes_written);
stream.reset();
EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
}
TEST_F(FileStreamTest, OpenAndDelete) {
base::SequencedWorkerPoolOwner pool_owner(1, "StreamTest");
bool prev = base::ThreadRestrictions::SetIOAllowed(false);
scoped_ptr<FileStream> stream(new FileStream(pool_owner.pool()));
int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE |
base::File::FLAG_ASYNC;
TestCompletionCallback open_callback;
int rv = stream->Open(temp_file_path(), flags, open_callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
// Delete the stream without waiting for the open operation to be
// complete. Should be safe.
stream.reset();
// Force an operation through the pool.
scoped_ptr<FileStream> stream2(new FileStream(pool_owner.pool()));
TestCompletionCallback open_callback2;
rv = stream2->Open(temp_file_path(), flags, open_callback2.callback());
EXPECT_EQ(OK, open_callback2.GetResult(rv));
stream2.reset();
// open_callback won't be called.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(open_callback.have_result());
base::ThreadRestrictions::SetIOAllowed(prev);
}
// Verify that Write() errors are mapped correctly.
TEST_F(FileStreamTest, WriteError) {
// Try opening file as read-only and then writing to it using FileStream.
uint32_t flags =
base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
base::File file(temp_file_path(), flags);
ASSERT_TRUE(file.IsValid());
scoped_ptr<FileStream> stream(
new FileStream(file.Pass(), base::ThreadTaskRunnerHandle::Get()));
scoped_refptr<IOBuffer> buf = new IOBuffer(1);
buf->data()[0] = 0;
TestCompletionCallback callback;
int rv = stream->Write(buf.get(), 1, callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(rv, 0);
stream.reset();
base::RunLoop().RunUntilIdle();
}
// Verify that Read() errors are mapped correctly.
TEST_F(FileStreamTest, ReadError) {
// Try opening file for write and then reading from it using FileStream.
uint32_t flags =
base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
base::File file(temp_file_path(), flags);
ASSERT_TRUE(file.IsValid());
scoped_ptr<FileStream> stream(
new FileStream(file.Pass(), base::ThreadTaskRunnerHandle::Get()));
scoped_refptr<IOBuffer> buf = new IOBuffer(1);
TestCompletionCallback callback;
int rv = stream->Read(buf.get(), 1, callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(rv, 0);
stream.reset();
base::RunLoop().RunUntilIdle();
}
#if defined(OS_ANDROID)
TEST_F(FileStreamTest, ContentUriRead) {
base::FilePath test_dir;
PathService::Get(base::DIR_SOURCE_ROOT, &test_dir);
test_dir = test_dir.AppendASCII("net");
test_dir = test_dir.AppendASCII("data");
test_dir = test_dir.AppendASCII("file_stream_unittest");
ASSERT_TRUE(base::PathExists(test_dir));
base::FilePath image_file = test_dir.Append(FILE_PATH_LITERAL("red.png"));
// Insert the image into MediaStore. MediaStore will do some conversions, and
// return the content URI.
base::FilePath path = base::InsertImageIntoMediaStore(image_file);
EXPECT_TRUE(path.IsContentUri());
EXPECT_TRUE(base::PathExists(path));
int64_t file_size;
EXPECT_TRUE(base::GetFileSize(path, &file_size));
EXPECT_LT(0, file_size);
FileStream stream(base::ThreadTaskRunnerHandle::Get());
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(path, flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
int total_bytes_read = 0;
std::string data_read;
for (;;) {
scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
rv = stream.Read(buf.get(), buf->size(), callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LE(0, rv);
if (rv <= 0)
break;
total_bytes_read += rv;
data_read.append(buf->data(), rv);
}
EXPECT_EQ(file_size, total_bytes_read);
}
#endif
} // namespace
} // namespace net
|