Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: net/base/file_stream_unittest.cc

Issue 642403002: git cl format the first third of the net/base directory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nit Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/base/file_stream.h" 5 #include "net/base/file_stream.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 65
66 private: 66 private:
67 base::FilePath temp_file_path_; 67 base::FilePath temp_file_path_;
68 }; 68 };
69 69
70 namespace { 70 namespace {
71 71
72 TEST_F(FileStreamTest, OpenExplicitClose) { 72 TEST_F(FileStreamTest, OpenExplicitClose) {
73 TestCompletionCallback callback; 73 TestCompletionCallback callback;
74 FileStream stream(base::MessageLoopProxy::current()); 74 FileStream stream(base::MessageLoopProxy::current());
75 int flags = base::File::FLAG_OPEN | 75 int flags =
76 base::File::FLAG_READ | 76 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
77 base::File::FLAG_ASYNC;
78 int rv = stream.Open(temp_file_path(), flags, callback.callback()); 77 int rv = stream.Open(temp_file_path(), flags, callback.callback());
79 EXPECT_EQ(ERR_IO_PENDING, rv); 78 EXPECT_EQ(ERR_IO_PENDING, rv);
80 EXPECT_EQ(OK, callback.WaitForResult()); 79 EXPECT_EQ(OK, callback.WaitForResult());
81 EXPECT_TRUE(stream.IsOpen()); 80 EXPECT_TRUE(stream.IsOpen());
82 EXPECT_TRUE(stream.GetFileForTesting().IsValid()); 81 EXPECT_TRUE(stream.GetFileForTesting().IsValid());
83 EXPECT_EQ(ERR_IO_PENDING, stream.Close(callback.callback())); 82 EXPECT_EQ(ERR_IO_PENDING, stream.Close(callback.callback()));
84 EXPECT_EQ(OK, callback.WaitForResult()); 83 EXPECT_EQ(OK, callback.WaitForResult());
85 EXPECT_FALSE(stream.IsOpen()); 84 EXPECT_FALSE(stream.IsOpen());
86 EXPECT_FALSE(stream.GetFileForTesting().IsValid()); 85 EXPECT_FALSE(stream.GetFileForTesting().IsValid());
87 } 86 }
88 87
89 TEST_F(FileStreamTest, OpenExplicitCloseOrphaned) { 88 TEST_F(FileStreamTest, OpenExplicitCloseOrphaned) {
90 TestCompletionCallback callback; 89 TestCompletionCallback callback;
91 scoped_ptr<FileStream> stream(new FileStream( 90 scoped_ptr<FileStream> stream(
92 base::MessageLoopProxy::current())); 91 new FileStream(base::MessageLoopProxy::current()));
93 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | 92 int flags =
94 base::File::FLAG_ASYNC; 93 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
95 int rv = stream->Open(temp_file_path(), flags, callback.callback()); 94 int rv = stream->Open(temp_file_path(), flags, callback.callback());
96 EXPECT_EQ(ERR_IO_PENDING, rv); 95 EXPECT_EQ(ERR_IO_PENDING, rv);
97 EXPECT_EQ(OK, callback.WaitForResult()); 96 EXPECT_EQ(OK, callback.WaitForResult());
98 EXPECT_TRUE(stream->IsOpen()); 97 EXPECT_TRUE(stream->IsOpen());
99 EXPECT_TRUE(stream->GetFileForTesting().IsValid()); 98 EXPECT_TRUE(stream->GetFileForTesting().IsValid());
100 EXPECT_EQ(ERR_IO_PENDING, stream->Close(callback.callback())); 99 EXPECT_EQ(ERR_IO_PENDING, stream->Close(callback.callback()));
101 stream.reset(); 100 stream.reset();
102 // File isn't actually closed yet. 101 // File isn't actually closed yet.
103 base::RunLoop runloop; 102 base::RunLoop runloop;
104 runloop.RunUntilIdle(); 103 runloop.RunUntilIdle();
105 // The file should now be closed, though the callback has not been called. 104 // The file should now be closed, though the callback has not been called.
106 } 105 }
107 106
108 // Test the use of FileStream with a file handle provided at construction. 107 // Test the use of FileStream with a file handle provided at construction.
109 TEST_F(FileStreamTest, UseFileHandle) { 108 TEST_F(FileStreamTest, UseFileHandle) {
110 int rv = 0; 109 int rv = 0;
111 TestCompletionCallback callback; 110 TestCompletionCallback callback;
112 TestInt64CompletionCallback callback64; 111 TestInt64CompletionCallback callback64;
113 // 1. Test reading with a file handle. 112 // 1. Test reading with a file handle.
114 ASSERT_EQ(kTestDataSize, 113 ASSERT_EQ(kTestDataSize,
115 base::WriteFile(temp_file_path(), kTestData, kTestDataSize)); 114 base::WriteFile(temp_file_path(), kTestData, kTestDataSize));
116 int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | 115 int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ |
117 base::File::FLAG_ASYNC; 116 base::File::FLAG_ASYNC;
118 base::File file(temp_file_path(), flags); 117 base::File file(temp_file_path(), flags);
119 118
120 // Seek to the beginning of the file and read. 119 // Seek to the beginning of the file and read.
121 scoped_ptr<FileStream> read_stream( 120 scoped_ptr<FileStream> read_stream(
122 new FileStream(file.Pass(), base::MessageLoopProxy::current())); 121 new FileStream(file.Pass(), base::MessageLoopProxy::current()));
123 ASSERT_EQ(ERR_IO_PENDING, 122 ASSERT_EQ(ERR_IO_PENDING, read_stream->Seek(base::File::FROM_BEGIN, 0,
124 read_stream->Seek(base::File::FROM_BEGIN, 0, 123 callback64.callback()));
125 callback64.callback()));
126 ASSERT_EQ(0, callback64.WaitForResult()); 124 ASSERT_EQ(0, callback64.WaitForResult());
127 // Read into buffer and compare. 125 // Read into buffer and compare.
128 scoped_refptr<IOBufferWithSize> read_buffer = 126 scoped_refptr<IOBufferWithSize> read_buffer =
129 new IOBufferWithSize(kTestDataSize); 127 new IOBufferWithSize(kTestDataSize);
130 rv = read_stream->Read(read_buffer.get(), kTestDataSize, callback.callback()); 128 rv = read_stream->Read(read_buffer.get(), kTestDataSize, callback.callback());
131 ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); 129 ASSERT_EQ(kTestDataSize, callback.GetResult(rv));
132 ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); 130 ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize));
133 read_stream.reset(); 131 read_stream.reset();
134 132
135 // 2. Test writing with a file handle. 133 // 2. Test writing with a file handle.
136 base::DeleteFile(temp_file_path(), false); 134 base::DeleteFile(temp_file_path(), false);
137 flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE | 135 flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE |
138 base::File::FLAG_ASYNC; 136 base::File::FLAG_ASYNC;
139 file.Initialize(temp_file_path(), flags); 137 file.Initialize(temp_file_path(), flags);
140 138
141 scoped_ptr<FileStream> write_stream( 139 scoped_ptr<FileStream> write_stream(
142 new FileStream(file.Pass(), base::MessageLoopProxy::current())); 140 new FileStream(file.Pass(), base::MessageLoopProxy::current()));
143 ASSERT_EQ(ERR_IO_PENDING, 141 ASSERT_EQ(ERR_IO_PENDING, write_stream->Seek(base::File::FROM_BEGIN, 0,
144 write_stream->Seek(base::File::FROM_BEGIN, 0, 142 callback64.callback()));
145 callback64.callback()));
146 ASSERT_EQ(0, callback64.WaitForResult()); 143 ASSERT_EQ(0, callback64.WaitForResult());
147 scoped_refptr<IOBufferWithSize> write_buffer = CreateTestDataBuffer(); 144 scoped_refptr<IOBufferWithSize> write_buffer = CreateTestDataBuffer();
148 rv = write_stream->Write(write_buffer.get(), kTestDataSize, 145 rv = write_stream->Write(write_buffer.get(), kTestDataSize,
149 callback.callback()); 146 callback.callback());
150 ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); 147 ASSERT_EQ(kTestDataSize, callback.GetResult(rv));
151 write_stream.reset(); 148 write_stream.reset();
152 149
153 // Read into buffer and compare to make sure the handle worked fine. 150 // Read into buffer and compare to make sure the handle worked fine.
154 ASSERT_EQ(kTestDataSize, 151 ASSERT_EQ(kTestDataSize, base::ReadFile(temp_file_path(), read_buffer->data(),
155 base::ReadFile(temp_file_path(), read_buffer->data(), 152 kTestDataSize));
156 kTestDataSize));
157 ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); 153 ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize));
158 } 154 }
159 155
160 TEST_F(FileStreamTest, UseClosedStream) { 156 TEST_F(FileStreamTest, UseClosedStream) {
161 int rv = 0; 157 int rv = 0;
162 TestCompletionCallback callback; 158 TestCompletionCallback callback;
163 TestInt64CompletionCallback callback64; 159 TestInt64CompletionCallback callback64;
164 160
165 FileStream stream(base::MessageLoopProxy::current()); 161 FileStream stream(base::MessageLoopProxy::current());
166 162
167 EXPECT_FALSE(stream.IsOpen()); 163 EXPECT_FALSE(stream.IsOpen());
168 164
169 // Try seeking... 165 // Try seeking...
170 rv = stream.Seek(base::File::FROM_BEGIN, 5, callback64.callback()); 166 rv = stream.Seek(base::File::FROM_BEGIN, 5, callback64.callback());
171 EXPECT_EQ(ERR_UNEXPECTED, callback64.GetResult(rv)); 167 EXPECT_EQ(ERR_UNEXPECTED, callback64.GetResult(rv));
172 168
173 // Try reading... 169 // Try reading...
174 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(10); 170 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(10);
175 rv = stream.Read(buf.get(), buf->size(), callback.callback()); 171 rv = stream.Read(buf.get(), buf->size(), callback.callback());
176 EXPECT_EQ(ERR_UNEXPECTED, callback.GetResult(rv)); 172 EXPECT_EQ(ERR_UNEXPECTED, callback.GetResult(rv));
177 } 173 }
178 174
179 TEST_F(FileStreamTest, Read) { 175 TEST_F(FileStreamTest, Read) {
180 int64 file_size; 176 int64 file_size;
181 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); 177 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
182 178
183 FileStream stream(base::MessageLoopProxy::current()); 179 FileStream stream(base::MessageLoopProxy::current());
184 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | 180 int flags =
185 base::File::FLAG_ASYNC; 181 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
186 TestCompletionCallback callback; 182 TestCompletionCallback callback;
187 int rv = stream.Open(temp_file_path(), flags, callback.callback()); 183 int rv = stream.Open(temp_file_path(), flags, callback.callback());
188 EXPECT_EQ(OK, callback.GetResult(rv)); 184 EXPECT_EQ(OK, callback.GetResult(rv));
189 185
190 int total_bytes_read = 0; 186 int total_bytes_read = 0;
191 187
192 std::string data_read; 188 std::string data_read;
193 for (;;) { 189 for (;;) {
194 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 190 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
195 rv = stream.Read(buf.get(), buf->size(), callback.callback()); 191 rv = stream.Read(buf.get(), buf->size(), callback.callback());
196 rv = callback.GetResult(rv); 192 rv = callback.GetResult(rv);
197 EXPECT_LE(0, rv); 193 EXPECT_LE(0, rv);
198 if (rv <= 0) 194 if (rv <= 0)
199 break; 195 break;
200 total_bytes_read += rv; 196 total_bytes_read += rv;
201 data_read.append(buf->data(), rv); 197 data_read.append(buf->data(), rv);
202 } 198 }
203 EXPECT_EQ(file_size, total_bytes_read); 199 EXPECT_EQ(file_size, total_bytes_read);
204 EXPECT_EQ(kTestData, data_read); 200 EXPECT_EQ(kTestData, data_read);
205 } 201 }
206 202
207 TEST_F(FileStreamTest, Read_EarlyDelete) { 203 TEST_F(FileStreamTest, Read_EarlyDelete) {
208 int64 file_size; 204 int64 file_size;
209 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); 205 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
210 206
211 scoped_ptr<FileStream> stream( 207 scoped_ptr<FileStream> stream(
212 new FileStream(base::MessageLoopProxy::current())); 208 new FileStream(base::MessageLoopProxy::current()));
213 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | 209 int flags =
214 base::File::FLAG_ASYNC; 210 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
215 TestCompletionCallback callback; 211 TestCompletionCallback callback;
216 int rv = stream->Open(temp_file_path(), flags, callback.callback()); 212 int rv = stream->Open(temp_file_path(), flags, callback.callback());
217 EXPECT_EQ(ERR_IO_PENDING, rv); 213 EXPECT_EQ(ERR_IO_PENDING, rv);
218 EXPECT_EQ(OK, callback.WaitForResult()); 214 EXPECT_EQ(OK, callback.WaitForResult());
219 215
220 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 216 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
221 rv = stream->Read(buf.get(), buf->size(), callback.callback()); 217 rv = stream->Read(buf.get(), buf->size(), callback.callback());
222 stream.reset(); // Delete instead of closing it. 218 stream.reset(); // Delete instead of closing it.
223 if (rv < 0) { 219 if (rv < 0) {
224 EXPECT_EQ(ERR_IO_PENDING, rv); 220 EXPECT_EQ(ERR_IO_PENDING, rv);
225 // The callback should not be called if the request is cancelled. 221 // The callback should not be called if the request is cancelled.
226 base::RunLoop().RunUntilIdle(); 222 base::RunLoop().RunUntilIdle();
227 EXPECT_FALSE(callback.have_result()); 223 EXPECT_FALSE(callback.have_result());
228 } else { 224 } else {
229 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); 225 EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv));
230 } 226 }
231 } 227 }
232 228
233 TEST_F(FileStreamTest, Read_FromOffset) { 229 TEST_F(FileStreamTest, Read_FromOffset) {
234 int64 file_size; 230 int64 file_size;
235 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); 231 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
236 232
237 FileStream stream(base::MessageLoopProxy::current()); 233 FileStream stream(base::MessageLoopProxy::current());
238 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | 234 int flags =
239 base::File::FLAG_ASYNC; 235 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
240 TestCompletionCallback callback; 236 TestCompletionCallback callback;
241 int rv = stream.Open(temp_file_path(), flags, callback.callback()); 237 int rv = stream.Open(temp_file_path(), flags, callback.callback());
242 EXPECT_EQ(ERR_IO_PENDING, rv); 238 EXPECT_EQ(ERR_IO_PENDING, rv);
243 EXPECT_EQ(OK, callback.WaitForResult()); 239 EXPECT_EQ(OK, callback.WaitForResult());
244 240
245 TestInt64CompletionCallback callback64; 241 TestInt64CompletionCallback callback64;
246 const int64 kOffset = 3; 242 const int64 kOffset = 3;
247 rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback()); 243 rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback());
248 ASSERT_EQ(ERR_IO_PENDING, rv); 244 ASSERT_EQ(ERR_IO_PENDING, rv);
249 int64 new_offset = callback64.WaitForResult(); 245 int64 new_offset = callback64.WaitForResult();
(...skipping 12 matching lines...) Expand all
262 break; 258 break;
263 total_bytes_read += rv; 259 total_bytes_read += rv;
264 data_read.append(buf->data(), rv); 260 data_read.append(buf->data(), rv);
265 } 261 }
266 EXPECT_EQ(file_size - kOffset, total_bytes_read); 262 EXPECT_EQ(file_size - kOffset, total_bytes_read);
267 EXPECT_EQ(kTestData + kOffset, data_read); 263 EXPECT_EQ(kTestData + kOffset, data_read);
268 } 264 }
269 265
270 TEST_F(FileStreamTest, SeekAround) { 266 TEST_F(FileStreamTest, SeekAround) {
271 FileStream stream(base::MessageLoopProxy::current()); 267 FileStream stream(base::MessageLoopProxy::current());
272 int flags = base::File::FLAG_OPEN | base::File::FLAG_ASYNC | 268 int flags =
273 base::File::FLAG_READ; 269 base::File::FLAG_OPEN | base::File::FLAG_ASYNC | base::File::FLAG_READ;
274 TestCompletionCallback callback; 270 TestCompletionCallback callback;
275 int rv = stream.Open(temp_file_path(), flags, callback.callback()); 271 int rv = stream.Open(temp_file_path(), flags, callback.callback());
276 EXPECT_EQ(ERR_IO_PENDING, rv); 272 EXPECT_EQ(ERR_IO_PENDING, rv);
277 EXPECT_EQ(OK, callback.WaitForResult()); 273 EXPECT_EQ(OK, callback.WaitForResult());
278 274
279 TestInt64CompletionCallback callback64; 275 TestInt64CompletionCallback callback64;
280 276
281 const int64 kOffset = 3; 277 const int64 kOffset = 3;
282 rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback()); 278 rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback());
283 ASSERT_EQ(ERR_IO_PENDING, rv); 279 ASSERT_EQ(ERR_IO_PENDING, rv);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); 349 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
354 EXPECT_EQ(file_size, rv); 350 EXPECT_EQ(file_size, rv);
355 } 351 }
356 } 352 }
357 353
358 TEST_F(FileStreamTest, Write_FromOffset) { 354 TEST_F(FileStreamTest, Write_FromOffset) {
359 int64 file_size; 355 int64 file_size;
360 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); 356 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
361 357
362 FileStream stream(base::MessageLoopProxy::current()); 358 FileStream stream(base::MessageLoopProxy::current());
363 int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | 359 int flags =
364 base::File::FLAG_ASYNC; 360 base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
365 TestCompletionCallback callback; 361 TestCompletionCallback callback;
366 int rv = stream.Open(temp_file_path(), flags, callback.callback()); 362 int rv = stream.Open(temp_file_path(), flags, callback.callback());
367 EXPECT_EQ(ERR_IO_PENDING, rv); 363 EXPECT_EQ(ERR_IO_PENDING, rv);
368 EXPECT_EQ(OK, callback.WaitForResult()); 364 EXPECT_EQ(OK, callback.WaitForResult());
369 365
370 TestInt64CompletionCallback callback64; 366 TestInt64CompletionCallback callback64;
371 const int64 kOffset = 0; 367 const int64 kOffset = 0;
372 rv = stream.Seek(base::File::FROM_END, kOffset, callback64.callback()); 368 rv = stream.Seek(base::File::FROM_END, kOffset, callback64.callback());
373 ASSERT_EQ(ERR_IO_PENDING, rv); 369 ASSERT_EQ(ERR_IO_PENDING, rv);
374 int64 new_offset = callback64.WaitForResult(); 370 int64 new_offset = callback64.WaitForResult();
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 void OnComplete(int result) { 546 void OnComplete(int result) {
551 DCHECK_LT(0, result); 547 DCHECK_LT(0, result);
552 *total_bytes_written_ += result; 548 *total_bytes_written_ += result;
553 549
554 int rv; 550 int rv;
555 551
556 if (*total_bytes_written_ != kTestDataSize) { 552 if (*total_bytes_written_ != kTestDataSize) {
557 // Recurse to finish writing all data. 553 // Recurse to finish writing all data.
558 int total_bytes_written = 0, total_bytes_read = 0; 554 int total_bytes_written = 0, total_bytes_read = 0;
559 std::string data_read; 555 std::string data_read;
560 TestWriteReadCompletionCallback callback( 556 TestWriteReadCompletionCallback callback(stream_, &total_bytes_written,
561 stream_, &total_bytes_written, &total_bytes_read, &data_read); 557 &total_bytes_read, &data_read);
562 rv = stream_->Write( 558 rv = stream_->Write(drainable_.get(), drainable_->BytesRemaining(),
563 drainable_.get(), drainable_->BytesRemaining(), callback.callback()); 559 callback.callback());
564 DCHECK_EQ(ERR_IO_PENDING, rv); 560 DCHECK_EQ(ERR_IO_PENDING, rv);
565 rv = callback.WaitForResult(); 561 rv = callback.WaitForResult();
566 drainable_->DidConsume(total_bytes_written); 562 drainable_->DidConsume(total_bytes_written);
567 *total_bytes_written_ += total_bytes_written; 563 *total_bytes_written_ += total_bytes_written;
568 *total_bytes_read_ += total_bytes_read; 564 *total_bytes_read_ += total_bytes_read;
569 *data_read_ += data_read; 565 *data_read_ += data_read;
570 } else { // We're done writing all data. Start reading the data. 566 } else { // We're done writing all data. Start reading the data.
571 TestInt64CompletionCallback callback64; 567 TestInt64CompletionCallback callback64;
572 EXPECT_EQ(ERR_IO_PENDING, 568 EXPECT_EQ(ERR_IO_PENDING, stream_->Seek(base::File::FROM_BEGIN, 0,
573 stream_->Seek(base::File::FROM_BEGIN, 0, 569 callback64.callback()));
574 callback64.callback()));
575 { 570 {
576 base::MessageLoop::ScopedNestableTaskAllower allow( 571 base::MessageLoop::ScopedNestableTaskAllower allow(
577 base::MessageLoop::current()); 572 base::MessageLoop::current());
578 EXPECT_LE(0, callback64.WaitForResult()); 573 EXPECT_LE(0, callback64.WaitForResult());
579 } 574 }
580 575
581 TestCompletionCallback callback; 576 TestCompletionCallback callback;
582 for (;;) { 577 for (;;) {
583 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 578 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
584 rv = stream_->Read(buf.get(), buf->size(), callback.callback()); 579 rv = stream_->Read(buf.get(), buf->size(), callback.callback());
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 void OnComplete(int result) { 682 void OnComplete(int result) {
688 DCHECK_LT(0, result); 683 DCHECK_LT(0, result);
689 *total_bytes_written_ += result; 684 *total_bytes_written_ += result;
690 685
691 int rv; 686 int rv;
692 687
693 if (*total_bytes_written_ != kTestDataSize) { 688 if (*total_bytes_written_ != kTestDataSize) {
694 // Recurse to finish writing all data. 689 // Recurse to finish writing all data.
695 int total_bytes_written = 0; 690 int total_bytes_written = 0;
696 TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written); 691 TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written);
697 rv = stream_->Write( 692 rv = stream_->Write(drainable_.get(), drainable_->BytesRemaining(),
698 drainable_.get(), drainable_->BytesRemaining(), callback.callback()); 693 callback.callback());
699 DCHECK_EQ(ERR_IO_PENDING, rv); 694 DCHECK_EQ(ERR_IO_PENDING, rv);
700 rv = callback.WaitForResult(); 695 rv = callback.WaitForResult();
701 drainable_->DidConsume(total_bytes_written); 696 drainable_->DidConsume(total_bytes_written);
702 *total_bytes_written_ += total_bytes_written; 697 *total_bytes_written_ += total_bytes_written;
703 } 698 }
704 699
705 result_ = *total_bytes_written_; 700 result_ = *total_bytes_written_;
706 have_result_ = true; 701 have_result_ = true;
707 if (waiting_for_result_) 702 if (waiting_for_result_)
708 base::MessageLoop::current()->Quit(); 703 base::MessageLoop::current()->Quit();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); 748 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
754 EXPECT_EQ(kTestDataSize * 2, file_size); 749 EXPECT_EQ(kTestDataSize * 2, file_size);
755 } 750 }
756 751
757 TEST_F(FileStreamTest, OpenAndDelete) { 752 TEST_F(FileStreamTest, OpenAndDelete) {
758 scoped_refptr<base::SequencedWorkerPool> pool( 753 scoped_refptr<base::SequencedWorkerPool> pool(
759 new base::SequencedWorkerPool(1, "StreamTest")); 754 new base::SequencedWorkerPool(1, "StreamTest"));
760 755
761 bool prev = base::ThreadRestrictions::SetIOAllowed(false); 756 bool prev = base::ThreadRestrictions::SetIOAllowed(false);
762 scoped_ptr<FileStream> stream(new FileStream(pool.get())); 757 scoped_ptr<FileStream> stream(new FileStream(pool.get()));
763 int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | 758 int flags =
764 base::File::FLAG_ASYNC; 759 base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
765 TestCompletionCallback open_callback; 760 TestCompletionCallback open_callback;
766 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); 761 int rv = stream->Open(temp_file_path(), flags, open_callback.callback());
767 EXPECT_EQ(ERR_IO_PENDING, rv); 762 EXPECT_EQ(ERR_IO_PENDING, rv);
768 763
769 // Delete the stream without waiting for the open operation to be 764 // Delete the stream without waiting for the open operation to be
770 // complete. Should be safe. 765 // complete. Should be safe.
771 stream.reset(); 766 stream.reset();
772 767
773 // Force an operation through the pool. 768 // Force an operation through the pool.
774 scoped_ptr<FileStream> stream2(new FileStream(pool.get())); 769 scoped_ptr<FileStream> stream2(new FileStream(pool.get()));
775 TestCompletionCallback open_callback2; 770 TestCompletionCallback open_callback2;
776 rv = stream2->Open(temp_file_path(), flags, open_callback2.callback()); 771 rv = stream2->Open(temp_file_path(), flags, open_callback2.callback());
777 EXPECT_EQ(OK, open_callback2.GetResult(rv)); 772 EXPECT_EQ(OK, open_callback2.GetResult(rv));
778 stream2.reset(); 773 stream2.reset();
779 774
780 pool->Shutdown(); 775 pool->Shutdown();
781 776
782 // open_callback won't be called. 777 // open_callback won't be called.
783 base::RunLoop().RunUntilIdle(); 778 base::RunLoop().RunUntilIdle();
784 EXPECT_FALSE(open_callback.have_result()); 779 EXPECT_FALSE(open_callback.have_result());
785 base::ThreadRestrictions::SetIOAllowed(prev); 780 base::ThreadRestrictions::SetIOAllowed(prev);
786 } 781 }
787 782
788 // Verify that Write() errors are mapped correctly. 783 // Verify that Write() errors are mapped correctly.
789 TEST_F(FileStreamTest, WriteError) { 784 TEST_F(FileStreamTest, WriteError) {
790 // Try opening file as read-only and then writing to it using FileStream. 785 // Try opening file as read-only and then writing to it using FileStream.
791 uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_READ | 786 uint32 flags =
792 base::File::FLAG_ASYNC; 787 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
793 788
794 base::File file(temp_file_path(), flags); 789 base::File file(temp_file_path(), flags);
795 ASSERT_TRUE(file.IsValid()); 790 ASSERT_TRUE(file.IsValid());
796 791
797 scoped_ptr<FileStream> stream( 792 scoped_ptr<FileStream> stream(
798 new FileStream(file.Pass(), base::MessageLoopProxy::current())); 793 new FileStream(file.Pass(), base::MessageLoopProxy::current()));
799 794
800 scoped_refptr<IOBuffer> buf = new IOBuffer(1); 795 scoped_refptr<IOBuffer> buf = new IOBuffer(1);
801 buf->data()[0] = 0; 796 buf->data()[0] = 0;
802 797
803 TestCompletionCallback callback; 798 TestCompletionCallback callback;
804 int rv = stream->Write(buf.get(), 1, callback.callback()); 799 int rv = stream->Write(buf.get(), 1, callback.callback());
805 if (rv == ERR_IO_PENDING) 800 if (rv == ERR_IO_PENDING)
806 rv = callback.WaitForResult(); 801 rv = callback.WaitForResult();
807 EXPECT_LT(rv, 0); 802 EXPECT_LT(rv, 0);
808 803
809 stream.reset(); 804 stream.reset();
810 base::RunLoop().RunUntilIdle(); 805 base::RunLoop().RunUntilIdle();
811 } 806 }
812 807
813 // Verify that Read() errors are mapped correctly. 808 // Verify that Read() errors are mapped correctly.
814 TEST_F(FileStreamTest, ReadError) { 809 TEST_F(FileStreamTest, ReadError) {
815 // Try opening file for write and then reading from it using FileStream. 810 // Try opening file for write and then reading from it using FileStream.
816 uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | 811 uint32 flags =
817 base::File::FLAG_ASYNC; 812 base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
818 813
819 base::File file(temp_file_path(), flags); 814 base::File file(temp_file_path(), flags);
820 ASSERT_TRUE(file.IsValid()); 815 ASSERT_TRUE(file.IsValid());
821 816
822 scoped_ptr<FileStream> stream( 817 scoped_ptr<FileStream> stream(
823 new FileStream(file.Pass(), base::MessageLoopProxy::current())); 818 new FileStream(file.Pass(), base::MessageLoopProxy::current()));
824 819
825 scoped_refptr<IOBuffer> buf = new IOBuffer(1); 820 scoped_refptr<IOBuffer> buf = new IOBuffer(1);
826 TestCompletionCallback callback; 821 TestCompletionCallback callback;
827 int rv = stream->Read(buf.get(), 1, callback.callback()); 822 int rv = stream->Read(buf.get(), 1, callback.callback());
828 if (rv == ERR_IO_PENDING) 823 if (rv == ERR_IO_PENDING)
829 rv = callback.WaitForResult(); 824 rv = callback.WaitForResult();
830 EXPECT_LT(rv, 0); 825 EXPECT_LT(rv, 0);
831 826
832 stream.reset(); 827 stream.reset();
833 base::RunLoop().RunUntilIdle(); 828 base::RunLoop().RunUntilIdle();
(...skipping 12 matching lines...) Expand all
846 // Insert the image into MediaStore. MediaStore will do some conversions, and 841 // Insert the image into MediaStore. MediaStore will do some conversions, and
847 // return the content URI. 842 // return the content URI.
848 base::FilePath path = base::InsertImageIntoMediaStore(image_file); 843 base::FilePath path = base::InsertImageIntoMediaStore(image_file);
849 EXPECT_TRUE(path.IsContentUri()); 844 EXPECT_TRUE(path.IsContentUri());
850 EXPECT_TRUE(base::PathExists(path)); 845 EXPECT_TRUE(base::PathExists(path));
851 int64 file_size; 846 int64 file_size;
852 EXPECT_TRUE(base::GetFileSize(path, &file_size)); 847 EXPECT_TRUE(base::GetFileSize(path, &file_size));
853 EXPECT_LT(0, file_size); 848 EXPECT_LT(0, file_size);
854 849
855 FileStream stream(base::MessageLoopProxy::current()); 850 FileStream stream(base::MessageLoopProxy::current());
856 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | 851 int flags =
857 base::File::FLAG_ASYNC; 852 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC;
858 TestCompletionCallback callback; 853 TestCompletionCallback callback;
859 int rv = stream.Open(path, flags, callback.callback()); 854 int rv = stream.Open(path, flags, callback.callback());
860 EXPECT_EQ(ERR_IO_PENDING, rv); 855 EXPECT_EQ(ERR_IO_PENDING, rv);
861 EXPECT_EQ(OK, callback.WaitForResult()); 856 EXPECT_EQ(OK, callback.WaitForResult());
862 857
863 int total_bytes_read = 0; 858 int total_bytes_read = 0;
864 859
865 std::string data_read; 860 std::string data_read;
866 for (;;) { 861 for (;;) {
867 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); 862 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4);
868 rv = stream.Read(buf.get(), buf->size(), callback.callback()); 863 rv = stream.Read(buf.get(), buf->size(), callback.callback());
869 if (rv == ERR_IO_PENDING) 864 if (rv == ERR_IO_PENDING)
870 rv = callback.WaitForResult(); 865 rv = callback.WaitForResult();
871 EXPECT_LE(0, rv); 866 EXPECT_LE(0, rv);
872 if (rv <= 0) 867 if (rv <= 0)
873 break; 868 break;
874 total_bytes_read += rv; 869 total_bytes_read += rv;
875 data_read.append(buf->data(), rv); 870 data_read.append(buf->data(), rv);
876 } 871 }
877 EXPECT_EQ(file_size, total_bytes_read); 872 EXPECT_EQ(file_size, total_bytes_read);
878 } 873 }
879 #endif 874 #endif
880 875
881 } // namespace 876 } // namespace
882 877
883 } // namespace net 878 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698