| OLD | NEW | 
|---|
| 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  Loading... | 
| 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( | 
| 124             read_stream->Seek(base::File::FROM_BEGIN, 0, | 123       ERR_IO_PENDING, | 
| 125                               callback64.callback())); | 124       read_stream->Seek(base::File::FROM_BEGIN, 0, callback64.callback())); | 
| 126   ASSERT_EQ(0, callback64.WaitForResult()); | 125   ASSERT_EQ(0, callback64.WaitForResult()); | 
| 127   // Read into buffer and compare. | 126   // Read into buffer and compare. | 
| 128   scoped_refptr<IOBufferWithSize> read_buffer = | 127   scoped_refptr<IOBufferWithSize> read_buffer = | 
| 129       new IOBufferWithSize(kTestDataSize); | 128       new IOBufferWithSize(kTestDataSize); | 
| 130   rv = read_stream->Read(read_buffer.get(), kTestDataSize, callback.callback()); | 129   rv = read_stream->Read(read_buffer.get(), kTestDataSize, callback.callback()); | 
| 131   ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); | 130   ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); | 
| 132   ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); | 131   ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); | 
| 133   read_stream.reset(); | 132   read_stream.reset(); | 
| 134 | 133 | 
| 135   // 2. Test writing with a file handle. | 134   // 2. Test writing with a file handle. | 
| 136   base::DeleteFile(temp_file_path(), false); | 135   base::DeleteFile(temp_file_path(), false); | 
| 137   flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE | | 136   flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE | | 
| 138           base::File::FLAG_ASYNC; | 137           base::File::FLAG_ASYNC; | 
| 139   file.Initialize(temp_file_path(), flags); | 138   file.Initialize(temp_file_path(), flags); | 
| 140 | 139 | 
| 141   scoped_ptr<FileStream> write_stream( | 140   scoped_ptr<FileStream> write_stream( | 
| 142       new FileStream(file.Pass(), base::MessageLoopProxy::current())); | 141       new FileStream(file.Pass(), base::MessageLoopProxy::current())); | 
| 143   ASSERT_EQ(ERR_IO_PENDING, | 142   ASSERT_EQ( | 
| 144             write_stream->Seek(base::File::FROM_BEGIN, 0, | 143       ERR_IO_PENDING, | 
| 145                                callback64.callback())); | 144       write_stream->Seek(base::File::FROM_BEGIN, 0, callback64.callback())); | 
| 146   ASSERT_EQ(0, callback64.WaitForResult()); | 145   ASSERT_EQ(0, callback64.WaitForResult()); | 
| 147   scoped_refptr<IOBufferWithSize> write_buffer = CreateTestDataBuffer(); | 146   scoped_refptr<IOBufferWithSize> write_buffer = CreateTestDataBuffer(); | 
| 148   rv = write_stream->Write(write_buffer.get(), kTestDataSize, | 147   rv = write_stream->Write( | 
| 149                            callback.callback()); | 148       write_buffer.get(), kTestDataSize, callback.callback()); | 
| 150   ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); | 149   ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); | 
| 151   write_stream.reset(); | 150   write_stream.reset(); | 
| 152 | 151 | 
| 153   // Read into buffer and compare to make sure the handle worked fine. | 152   // Read into buffer and compare to make sure the handle worked fine. | 
| 154   ASSERT_EQ(kTestDataSize, | 153   ASSERT_EQ( | 
| 155             base::ReadFile(temp_file_path(), read_buffer->data(), | 154       kTestDataSize, | 
| 156                            kTestDataSize)); | 155       base::ReadFile(temp_file_path(), read_buffer->data(), kTestDataSize)); | 
| 157   ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); | 156   ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); | 
| 158 } | 157 } | 
| 159 | 158 | 
| 160 TEST_F(FileStreamTest, UseClosedStream) { | 159 TEST_F(FileStreamTest, UseClosedStream) { | 
| 161   int rv = 0; | 160   int rv = 0; | 
| 162   TestCompletionCallback callback; | 161   TestCompletionCallback callback; | 
| 163   TestInt64CompletionCallback callback64; | 162   TestInt64CompletionCallback callback64; | 
| 164 | 163 | 
| 165   FileStream stream(base::MessageLoopProxy::current()); | 164   FileStream stream(base::MessageLoopProxy::current()); | 
| 166 | 165 | 
| 167   EXPECT_FALSE(stream.IsOpen()); | 166   EXPECT_FALSE(stream.IsOpen()); | 
| 168 | 167 | 
| 169   // Try seeking... | 168   // Try seeking... | 
| 170   rv = stream.Seek(base::File::FROM_BEGIN, 5, callback64.callback()); | 169   rv = stream.Seek(base::File::FROM_BEGIN, 5, callback64.callback()); | 
| 171   EXPECT_EQ(ERR_UNEXPECTED, callback64.GetResult(rv)); | 170   EXPECT_EQ(ERR_UNEXPECTED, callback64.GetResult(rv)); | 
| 172 | 171 | 
| 173   // Try reading... | 172   // Try reading... | 
| 174   scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(10); | 173   scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(10); | 
| 175   rv = stream.Read(buf.get(), buf->size(), callback.callback()); | 174   rv = stream.Read(buf.get(), buf->size(), callback.callback()); | 
| 176   EXPECT_EQ(ERR_UNEXPECTED, callback.GetResult(rv)); | 175   EXPECT_EQ(ERR_UNEXPECTED, callback.GetResult(rv)); | 
| 177 } | 176 } | 
| 178 | 177 | 
| 179 TEST_F(FileStreamTest, Read) { | 178 TEST_F(FileStreamTest, Read) { | 
| 180   int64 file_size; | 179   int64 file_size; | 
| 181   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 180   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 
| 182 | 181 | 
| 183   FileStream stream(base::MessageLoopProxy::current()); | 182   FileStream stream(base::MessageLoopProxy::current()); | 
| 184   int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 183   int flags = | 
| 185               base::File::FLAG_ASYNC; | 184       base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC; | 
| 186   TestCompletionCallback callback; | 185   TestCompletionCallback callback; | 
| 187   int rv = stream.Open(temp_file_path(), flags, callback.callback()); | 186   int rv = stream.Open(temp_file_path(), flags, callback.callback()); | 
| 188   EXPECT_EQ(OK, callback.GetResult(rv)); | 187   EXPECT_EQ(OK, callback.GetResult(rv)); | 
| 189 | 188 | 
| 190   int total_bytes_read = 0; | 189   int total_bytes_read = 0; | 
| 191 | 190 | 
| 192   std::string data_read; | 191   std::string data_read; | 
| 193   for (;;) { | 192   for (;;) { | 
| 194     scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 193     scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 
| 195     rv = stream.Read(buf.get(), buf->size(), callback.callback()); | 194     rv = stream.Read(buf.get(), buf->size(), callback.callback()); | 
| 196     rv = callback.GetResult(rv); | 195     rv = callback.GetResult(rv); | 
| 197     EXPECT_LE(0, rv); | 196     EXPECT_LE(0, rv); | 
| 198     if (rv <= 0) | 197     if (rv <= 0) | 
| 199       break; | 198       break; | 
| 200     total_bytes_read += rv; | 199     total_bytes_read += rv; | 
| 201     data_read.append(buf->data(), rv); | 200     data_read.append(buf->data(), rv); | 
| 202   } | 201   } | 
| 203   EXPECT_EQ(file_size, total_bytes_read); | 202   EXPECT_EQ(file_size, total_bytes_read); | 
| 204   EXPECT_EQ(kTestData, data_read); | 203   EXPECT_EQ(kTestData, data_read); | 
| 205 } | 204 } | 
| 206 | 205 | 
| 207 TEST_F(FileStreamTest, Read_EarlyDelete) { | 206 TEST_F(FileStreamTest, Read_EarlyDelete) { | 
| 208   int64 file_size; | 207   int64 file_size; | 
| 209   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 208   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 
| 210 | 209 | 
| 211   scoped_ptr<FileStream> stream( | 210   scoped_ptr<FileStream> stream( | 
| 212       new FileStream(base::MessageLoopProxy::current())); | 211       new FileStream(base::MessageLoopProxy::current())); | 
| 213   int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 212   int flags = | 
| 214               base::File::FLAG_ASYNC; | 213       base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC; | 
| 215   TestCompletionCallback callback; | 214   TestCompletionCallback callback; | 
| 216   int rv = stream->Open(temp_file_path(), flags, callback.callback()); | 215   int rv = stream->Open(temp_file_path(), flags, callback.callback()); | 
| 217   EXPECT_EQ(ERR_IO_PENDING, rv); | 216   EXPECT_EQ(ERR_IO_PENDING, rv); | 
| 218   EXPECT_EQ(OK, callback.WaitForResult()); | 217   EXPECT_EQ(OK, callback.WaitForResult()); | 
| 219 | 218 | 
| 220   scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 219   scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 
| 221   rv = stream->Read(buf.get(), buf->size(), callback.callback()); | 220   rv = stream->Read(buf.get(), buf->size(), callback.callback()); | 
| 222   stream.reset();  // Delete instead of closing it. | 221   stream.reset();  // Delete instead of closing it. | 
| 223   if (rv < 0) { | 222   if (rv < 0) { | 
| 224     EXPECT_EQ(ERR_IO_PENDING, rv); | 223     EXPECT_EQ(ERR_IO_PENDING, rv); | 
| 225     // The callback should not be called if the request is cancelled. | 224     // The callback should not be called if the request is cancelled. | 
| 226     base::RunLoop().RunUntilIdle(); | 225     base::RunLoop().RunUntilIdle(); | 
| 227     EXPECT_FALSE(callback.have_result()); | 226     EXPECT_FALSE(callback.have_result()); | 
| 228   } else { | 227   } else { | 
| 229     EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); | 228     EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); | 
| 230   } | 229   } | 
| 231 } | 230 } | 
| 232 | 231 | 
| 233 TEST_F(FileStreamTest, Read_FromOffset) { | 232 TEST_F(FileStreamTest, Read_FromOffset) { | 
| 234   int64 file_size; | 233   int64 file_size; | 
| 235   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 234   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 
| 236 | 235 | 
| 237   FileStream stream(base::MessageLoopProxy::current()); | 236   FileStream stream(base::MessageLoopProxy::current()); | 
| 238   int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 237   int flags = | 
| 239               base::File::FLAG_ASYNC; | 238       base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC; | 
| 240   TestCompletionCallback callback; | 239   TestCompletionCallback callback; | 
| 241   int rv = stream.Open(temp_file_path(), flags, callback.callback()); | 240   int rv = stream.Open(temp_file_path(), flags, callback.callback()); | 
| 242   EXPECT_EQ(ERR_IO_PENDING, rv); | 241   EXPECT_EQ(ERR_IO_PENDING, rv); | 
| 243   EXPECT_EQ(OK, callback.WaitForResult()); | 242   EXPECT_EQ(OK, callback.WaitForResult()); | 
| 244 | 243 | 
| 245   TestInt64CompletionCallback callback64; | 244   TestInt64CompletionCallback callback64; | 
| 246   const int64 kOffset = 3; | 245   const int64 kOffset = 3; | 
| 247   rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback()); | 246   rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback()); | 
| 248   ASSERT_EQ(ERR_IO_PENDING, rv); | 247   ASSERT_EQ(ERR_IO_PENDING, rv); | 
| 249   int64 new_offset = callback64.WaitForResult(); | 248   int64 new_offset = callback64.WaitForResult(); | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 262       break; | 261       break; | 
| 263     total_bytes_read += rv; | 262     total_bytes_read += rv; | 
| 264     data_read.append(buf->data(), rv); | 263     data_read.append(buf->data(), rv); | 
| 265   } | 264   } | 
| 266   EXPECT_EQ(file_size - kOffset, total_bytes_read); | 265   EXPECT_EQ(file_size - kOffset, total_bytes_read); | 
| 267   EXPECT_EQ(kTestData + kOffset, data_read); | 266   EXPECT_EQ(kTestData + kOffset, data_read); | 
| 268 } | 267 } | 
| 269 | 268 | 
| 270 TEST_F(FileStreamTest, SeekAround) { | 269 TEST_F(FileStreamTest, SeekAround) { | 
| 271   FileStream stream(base::MessageLoopProxy::current()); | 270   FileStream stream(base::MessageLoopProxy::current()); | 
| 272   int flags = base::File::FLAG_OPEN | base::File::FLAG_ASYNC | | 271   int flags = | 
| 273               base::File::FLAG_READ; | 272       base::File::FLAG_OPEN | base::File::FLAG_ASYNC | base::File::FLAG_READ; | 
| 274   TestCompletionCallback callback; | 273   TestCompletionCallback callback; | 
| 275   int rv = stream.Open(temp_file_path(), flags, callback.callback()); | 274   int rv = stream.Open(temp_file_path(), flags, callback.callback()); | 
| 276   EXPECT_EQ(ERR_IO_PENDING, rv); | 275   EXPECT_EQ(ERR_IO_PENDING, rv); | 
| 277   EXPECT_EQ(OK, callback.WaitForResult()); | 276   EXPECT_EQ(OK, callback.WaitForResult()); | 
| 278 | 277 | 
| 279   TestInt64CompletionCallback callback64; | 278   TestInt64CompletionCallback callback64; | 
| 280 | 279 | 
| 281   const int64 kOffset = 3; | 280   const int64 kOffset = 3; | 
| 282   rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback()); | 281   rv = stream.Seek(base::File::FROM_BEGIN, kOffset, callback64.callback()); | 
| 283   ASSERT_EQ(ERR_IO_PENDING, rv); | 282   ASSERT_EQ(ERR_IO_PENDING, rv); | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 353     EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 352     EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 
| 354     EXPECT_EQ(file_size, rv); | 353     EXPECT_EQ(file_size, rv); | 
| 355   } | 354   } | 
| 356 } | 355 } | 
| 357 | 356 | 
| 358 TEST_F(FileStreamTest, Write_FromOffset) { | 357 TEST_F(FileStreamTest, Write_FromOffset) { | 
| 359   int64 file_size; | 358   int64 file_size; | 
| 360   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 359   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 
| 361 | 360 | 
| 362   FileStream stream(base::MessageLoopProxy::current()); | 361   FileStream stream(base::MessageLoopProxy::current()); | 
| 363   int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | | 362   int flags = | 
| 364               base::File::FLAG_ASYNC; | 363       base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC; | 
| 365   TestCompletionCallback callback; | 364   TestCompletionCallback callback; | 
| 366   int rv = stream.Open(temp_file_path(), flags, callback.callback()); | 365   int rv = stream.Open(temp_file_path(), flags, callback.callback()); | 
| 367   EXPECT_EQ(ERR_IO_PENDING, rv); | 366   EXPECT_EQ(ERR_IO_PENDING, rv); | 
| 368   EXPECT_EQ(OK, callback.WaitForResult()); | 367   EXPECT_EQ(OK, callback.WaitForResult()); | 
| 369 | 368 | 
| 370   TestInt64CompletionCallback callback64; | 369   TestInt64CompletionCallback callback64; | 
| 371   const int64 kOffset = 0; | 370   const int64 kOffset = 0; | 
| 372   rv = stream.Seek(base::File::FROM_END, kOffset, callback64.callback()); | 371   rv = stream.Seek(base::File::FROM_END, kOffset, callback64.callback()); | 
| 373   ASSERT_EQ(ERR_IO_PENDING, rv); | 372   ASSERT_EQ(ERR_IO_PENDING, rv); | 
| 374   int64 new_offset = callback64.WaitForResult(); | 373   int64 new_offset = callback64.WaitForResult(); | 
| 375   EXPECT_EQ(kTestDataSize, new_offset); | 374   EXPECT_EQ(kTestDataSize, new_offset); | 
| 376 | 375 | 
| 377   int total_bytes_written = 0; | 376   int total_bytes_written = 0; | 
| 378 | 377 | 
| 379   scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 378   scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 
| 380   scoped_refptr<DrainableIOBuffer> drainable = | 379   scoped_refptr<DrainableIOBuffer> drainable = | 
| 381       new DrainableIOBuffer(buf.get(), buf->size()); | 380       new DrainableIOBuffer(buf.get(), buf->size()); | 
| 382   while (total_bytes_written != kTestDataSize) { | 381   while (total_bytes_written != kTestDataSize) { | 
| 383     rv = stream.Write(drainable.get(), drainable->BytesRemaining(), | 382     rv = stream.Write( | 
| 384                       callback.callback()); | 383         drainable.get(), drainable->BytesRemaining(), callback.callback()); | 
| 385     if (rv == ERR_IO_PENDING) | 384     if (rv == ERR_IO_PENDING) | 
| 386       rv = callback.WaitForResult(); | 385       rv = callback.WaitForResult(); | 
| 387     EXPECT_LT(0, rv); | 386     EXPECT_LT(0, rv); | 
| 388     if (rv <= 0) | 387     if (rv <= 0) | 
| 389       break; | 388       break; | 
| 390     drainable->DidConsume(rv); | 389     drainable->DidConsume(rv); | 
| 391     total_bytes_written += rv; | 390     total_bytes_written += rv; | 
| 392   } | 391   } | 
| 393   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 392   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 
| 394   EXPECT_EQ(file_size, kTestDataSize * 2); | 393   EXPECT_EQ(file_size, kTestDataSize * 2); | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
| 423   } | 422   } | 
| 424   EXPECT_EQ(file_size, total_bytes_read); | 423   EXPECT_EQ(file_size, total_bytes_read); | 
| 425   EXPECT_TRUE(data_read == kTestData); | 424   EXPECT_TRUE(data_read == kTestData); | 
| 426 | 425 | 
| 427   int total_bytes_written = 0; | 426   int total_bytes_written = 0; | 
| 428 | 427 | 
| 429   scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 428   scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 
| 430   scoped_refptr<DrainableIOBuffer> drainable = | 429   scoped_refptr<DrainableIOBuffer> drainable = | 
| 431       new DrainableIOBuffer(buf.get(), buf->size()); | 430       new DrainableIOBuffer(buf.get(), buf->size()); | 
| 432   while (total_bytes_written != kTestDataSize) { | 431   while (total_bytes_written != kTestDataSize) { | 
| 433     rv = stream->Write(drainable.get(), drainable->BytesRemaining(), | 432     rv = stream->Write( | 
| 434                        callback.callback()); | 433         drainable.get(), drainable->BytesRemaining(), callback.callback()); | 
| 435     if (rv == ERR_IO_PENDING) | 434     if (rv == ERR_IO_PENDING) | 
| 436       rv = callback.WaitForResult(); | 435       rv = callback.WaitForResult(); | 
| 437     EXPECT_LT(0, rv); | 436     EXPECT_LT(0, rv); | 
| 438     if (rv <= 0) | 437     if (rv <= 0) | 
| 439       break; | 438       break; | 
| 440     drainable->DidConsume(rv); | 439     drainable->DidConsume(rv); | 
| 441     total_bytes_written += rv; | 440     total_bytes_written += rv; | 
| 442   } | 441   } | 
| 443 | 442 | 
| 444   stream.reset(); | 443   stream.reset(); | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 465   ASSERT_EQ(ERR_IO_PENDING, rv); | 464   ASSERT_EQ(ERR_IO_PENDING, rv); | 
| 466   int64 offset = callback64.WaitForResult(); | 465   int64 offset = callback64.WaitForResult(); | 
| 467   EXPECT_EQ(offset, file_size); | 466   EXPECT_EQ(offset, file_size); | 
| 468 | 467 | 
| 469   int total_bytes_written = 0; | 468   int total_bytes_written = 0; | 
| 470 | 469 | 
| 471   scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 470   scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 
| 472   scoped_refptr<DrainableIOBuffer> drainable = | 471   scoped_refptr<DrainableIOBuffer> drainable = | 
| 473       new DrainableIOBuffer(buf.get(), buf->size()); | 472       new DrainableIOBuffer(buf.get(), buf->size()); | 
| 474   while (total_bytes_written != kTestDataSize) { | 473   while (total_bytes_written != kTestDataSize) { | 
| 475     rv = stream->Write(drainable.get(), drainable->BytesRemaining(), | 474     rv = stream->Write( | 
| 476                        callback.callback()); | 475         drainable.get(), drainable->BytesRemaining(), callback.callback()); | 
| 477     if (rv == ERR_IO_PENDING) | 476     if (rv == ERR_IO_PENDING) | 
| 478       rv = callback.WaitForResult(); | 477       rv = callback.WaitForResult(); | 
| 479     EXPECT_LT(0, rv); | 478     EXPECT_LT(0, rv); | 
| 480     if (rv <= 0) | 479     if (rv <= 0) | 
| 481       break; | 480       break; | 
| 482     drainable->DidConsume(rv); | 481     drainable->DidConsume(rv); | 
| 483     total_bytes_written += rv; | 482     total_bytes_written += rv; | 
| 484   } | 483   } | 
| 485 | 484 | 
| 486   EXPECT_EQ(kTestDataSize, total_bytes_written); | 485   EXPECT_EQ(kTestDataSize, total_bytes_written); | 
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 562       rv = stream_->Write( | 561       rv = stream_->Write( | 
| 563           drainable_.get(), drainable_->BytesRemaining(), callback.callback()); | 562           drainable_.get(), drainable_->BytesRemaining(), callback.callback()); | 
| 564       DCHECK_EQ(ERR_IO_PENDING, rv); | 563       DCHECK_EQ(ERR_IO_PENDING, rv); | 
| 565       rv = callback.WaitForResult(); | 564       rv = callback.WaitForResult(); | 
| 566       drainable_->DidConsume(total_bytes_written); | 565       drainable_->DidConsume(total_bytes_written); | 
| 567       *total_bytes_written_ += total_bytes_written; | 566       *total_bytes_written_ += total_bytes_written; | 
| 568       *total_bytes_read_ += total_bytes_read; | 567       *total_bytes_read_ += total_bytes_read; | 
| 569       *data_read_ += data_read; | 568       *data_read_ += data_read; | 
| 570     } else {  // We're done writing all data.  Start reading the data. | 569     } else {  // We're done writing all data.  Start reading the data. | 
| 571       TestInt64CompletionCallback callback64; | 570       TestInt64CompletionCallback callback64; | 
| 572       EXPECT_EQ(ERR_IO_PENDING, | 571       EXPECT_EQ( | 
| 573                 stream_->Seek(base::File::FROM_BEGIN, 0, | 572           ERR_IO_PENDING, | 
| 574                               callback64.callback())); | 573           stream_->Seek(base::File::FROM_BEGIN, 0, callback64.callback())); | 
| 575       { | 574       { | 
| 576         base::MessageLoop::ScopedNestableTaskAllower allow( | 575         base::MessageLoop::ScopedNestableTaskAllower allow( | 
| 577             base::MessageLoop::current()); | 576             base::MessageLoop::current()); | 
| 578         EXPECT_LE(0, callback64.WaitForResult()); | 577         EXPECT_LE(0, callback64.WaitForResult()); | 
| 579       } | 578       } | 
| 580 | 579 | 
| 581       TestCompletionCallback callback; | 580       TestCompletionCallback callback; | 
| 582       for (;;) { | 581       for (;;) { | 
| 583         scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 582         scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 
| 584         rv = stream_->Read(buf.get(), buf->size(), callback.callback()); | 583         rv = stream_->Read(buf.get(), buf->size(), callback.callback()); | 
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 629   EXPECT_EQ(OK, open_callback.WaitForResult()); | 628   EXPECT_EQ(OK, open_callback.WaitForResult()); | 
| 630 | 629 | 
| 631   TestInt64CompletionCallback callback64; | 630   TestInt64CompletionCallback callback64; | 
| 632   EXPECT_EQ(ERR_IO_PENDING, | 631   EXPECT_EQ(ERR_IO_PENDING, | 
| 633             stream->Seek(base::File::FROM_END, 0, callback64.callback())); | 632             stream->Seek(base::File::FROM_END, 0, callback64.callback())); | 
| 634   EXPECT_EQ(file_size, callback64.WaitForResult()); | 633   EXPECT_EQ(file_size, callback64.WaitForResult()); | 
| 635 | 634 | 
| 636   int total_bytes_written = 0; | 635   int total_bytes_written = 0; | 
| 637   int total_bytes_read = 0; | 636   int total_bytes_read = 0; | 
| 638   std::string data_read; | 637   std::string data_read; | 
| 639   TestWriteReadCompletionCallback callback(stream.get(), &total_bytes_written, | 638   TestWriteReadCompletionCallback callback( | 
| 640                                            &total_bytes_read, &data_read); | 639       stream.get(), &total_bytes_written, &total_bytes_read, &data_read); | 
| 641 | 640 | 
| 642   scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 641   scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer(); | 
| 643   rv = stream->Write(buf.get(), buf->size(), callback.callback()); | 642   rv = stream->Write(buf.get(), buf->size(), callback.callback()); | 
| 644   if (rv == ERR_IO_PENDING) | 643   if (rv == ERR_IO_PENDING) | 
| 645     rv = callback.WaitForResult(); | 644     rv = callback.WaitForResult(); | 
| 646   EXPECT_LT(0, rv); | 645   EXPECT_LT(0, rv); | 
| 647   EXPECT_EQ(kTestDataSize, total_bytes_written); | 646   EXPECT_EQ(kTestDataSize, total_bytes_written); | 
| 648 | 647 | 
| 649   stream.reset(); | 648   stream.reset(); | 
| 650 | 649 | 
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 753   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 752   EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 
| 754   EXPECT_EQ(kTestDataSize * 2, file_size); | 753   EXPECT_EQ(kTestDataSize * 2, file_size); | 
| 755 } | 754 } | 
| 756 | 755 | 
| 757 TEST_F(FileStreamTest, OpenAndDelete) { | 756 TEST_F(FileStreamTest, OpenAndDelete) { | 
| 758   scoped_refptr<base::SequencedWorkerPool> pool( | 757   scoped_refptr<base::SequencedWorkerPool> pool( | 
| 759       new base::SequencedWorkerPool(1, "StreamTest")); | 758       new base::SequencedWorkerPool(1, "StreamTest")); | 
| 760 | 759 | 
| 761   bool prev = base::ThreadRestrictions::SetIOAllowed(false); | 760   bool prev = base::ThreadRestrictions::SetIOAllowed(false); | 
| 762   scoped_ptr<FileStream> stream(new FileStream(pool.get())); | 761   scoped_ptr<FileStream> stream(new FileStream(pool.get())); | 
| 763   int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | | 762   int flags = | 
| 764               base::File::FLAG_ASYNC; | 763       base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC; | 
| 765   TestCompletionCallback open_callback; | 764   TestCompletionCallback open_callback; | 
| 766   int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); | 765   int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); | 
| 767   EXPECT_EQ(ERR_IO_PENDING, rv); | 766   EXPECT_EQ(ERR_IO_PENDING, rv); | 
| 768 | 767 | 
| 769   // Delete the stream without waiting for the open operation to be | 768   // Delete the stream without waiting for the open operation to be | 
| 770   // complete. Should be safe. | 769   // complete. Should be safe. | 
| 771   stream.reset(); | 770   stream.reset(); | 
| 772 | 771 | 
| 773   // Force an operation through the pool. | 772   // Force an operation through the pool. | 
| 774   scoped_ptr<FileStream> stream2(new FileStream(pool.get())); | 773   scoped_ptr<FileStream> stream2(new FileStream(pool.get())); | 
| 775   TestCompletionCallback open_callback2; | 774   TestCompletionCallback open_callback2; | 
| 776   rv = stream2->Open(temp_file_path(), flags, open_callback2.callback()); | 775   rv = stream2->Open(temp_file_path(), flags, open_callback2.callback()); | 
| 777   EXPECT_EQ(OK, open_callback2.GetResult(rv)); | 776   EXPECT_EQ(OK, open_callback2.GetResult(rv)); | 
| 778   stream2.reset(); | 777   stream2.reset(); | 
| 779 | 778 | 
| 780   pool->Shutdown(); | 779   pool->Shutdown(); | 
| 781 | 780 | 
| 782   // open_callback won't be called. | 781   // open_callback won't be called. | 
| 783   base::RunLoop().RunUntilIdle(); | 782   base::RunLoop().RunUntilIdle(); | 
| 784   EXPECT_FALSE(open_callback.have_result()); | 783   EXPECT_FALSE(open_callback.have_result()); | 
| 785   base::ThreadRestrictions::SetIOAllowed(prev); | 784   base::ThreadRestrictions::SetIOAllowed(prev); | 
| 786 } | 785 } | 
| 787 | 786 | 
| 788 // Verify that Write() errors are mapped correctly. | 787 // Verify that Write() errors are mapped correctly. | 
| 789 TEST_F(FileStreamTest, WriteError) { | 788 TEST_F(FileStreamTest, WriteError) { | 
| 790   // Try opening file as read-only and then writing to it using FileStream. | 789   // Try opening file as read-only and then writing to it using FileStream. | 
| 791   uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 790   uint32 flags = | 
| 792                  base::File::FLAG_ASYNC; | 791       base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC; | 
| 793 | 792 | 
| 794   base::File file(temp_file_path(), flags); | 793   base::File file(temp_file_path(), flags); | 
| 795   ASSERT_TRUE(file.IsValid()); | 794   ASSERT_TRUE(file.IsValid()); | 
| 796 | 795 | 
| 797   scoped_ptr<FileStream> stream( | 796   scoped_ptr<FileStream> stream( | 
| 798   new FileStream(file.Pass(), base::MessageLoopProxy::current())); | 797       new FileStream(file.Pass(), base::MessageLoopProxy::current())); | 
| 799 | 798 | 
| 800   scoped_refptr<IOBuffer> buf = new IOBuffer(1); | 799   scoped_refptr<IOBuffer> buf = new IOBuffer(1); | 
| 801   buf->data()[0] = 0; | 800   buf->data()[0] = 0; | 
| 802 | 801 | 
| 803   TestCompletionCallback callback; | 802   TestCompletionCallback callback; | 
| 804   int rv = stream->Write(buf.get(), 1, callback.callback()); | 803   int rv = stream->Write(buf.get(), 1, callback.callback()); | 
| 805   if (rv == ERR_IO_PENDING) | 804   if (rv == ERR_IO_PENDING) | 
| 806     rv = callback.WaitForResult(); | 805     rv = callback.WaitForResult(); | 
| 807   EXPECT_LT(rv, 0); | 806   EXPECT_LT(rv, 0); | 
| 808 | 807 | 
| 809   stream.reset(); | 808   stream.reset(); | 
| 810   base::RunLoop().RunUntilIdle(); | 809   base::RunLoop().RunUntilIdle(); | 
| 811 } | 810 } | 
| 812 | 811 | 
| 813 // Verify that Read() errors are mapped correctly. | 812 // Verify that Read() errors are mapped correctly. | 
| 814 TEST_F(FileStreamTest, ReadError) { | 813 TEST_F(FileStreamTest, ReadError) { | 
| 815   // Try opening file for write and then reading from it using FileStream. | 814   // Try opening file for write and then reading from it using FileStream. | 
| 816   uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | | 815   uint32 flags = | 
| 817                  base::File::FLAG_ASYNC; | 816       base::File::FLAG_OPEN | base::File::FLAG_WRITE | base::File::FLAG_ASYNC; | 
| 818 | 817 | 
| 819   base::File file(temp_file_path(), flags); | 818   base::File file(temp_file_path(), flags); | 
| 820   ASSERT_TRUE(file.IsValid()); | 819   ASSERT_TRUE(file.IsValid()); | 
| 821 | 820 | 
| 822   scoped_ptr<FileStream> stream( | 821   scoped_ptr<FileStream> stream( | 
| 823   new FileStream(file.Pass(), base::MessageLoopProxy::current())); | 822       new FileStream(file.Pass(), base::MessageLoopProxy::current())); | 
| 824 | 823 | 
| 825   scoped_refptr<IOBuffer> buf = new IOBuffer(1); | 824   scoped_refptr<IOBuffer> buf = new IOBuffer(1); | 
| 826   TestCompletionCallback callback; | 825   TestCompletionCallback callback; | 
| 827   int rv = stream->Read(buf.get(), 1, callback.callback()); | 826   int rv = stream->Read(buf.get(), 1, callback.callback()); | 
| 828   if (rv == ERR_IO_PENDING) | 827   if (rv == ERR_IO_PENDING) | 
| 829     rv = callback.WaitForResult(); | 828     rv = callback.WaitForResult(); | 
| 830   EXPECT_LT(rv, 0); | 829   EXPECT_LT(rv, 0); | 
| 831 | 830 | 
| 832   stream.reset(); | 831   stream.reset(); | 
| 833   base::RunLoop().RunUntilIdle(); | 832   base::RunLoop().RunUntilIdle(); | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
| 846   // Insert the image into MediaStore. MediaStore will do some conversions, and | 845   // Insert the image into MediaStore. MediaStore will do some conversions, and | 
| 847   // return the content URI. | 846   // return the content URI. | 
| 848   base::FilePath path = base::InsertImageIntoMediaStore(image_file); | 847   base::FilePath path = base::InsertImageIntoMediaStore(image_file); | 
| 849   EXPECT_TRUE(path.IsContentUri()); | 848   EXPECT_TRUE(path.IsContentUri()); | 
| 850   EXPECT_TRUE(base::PathExists(path)); | 849   EXPECT_TRUE(base::PathExists(path)); | 
| 851   int64 file_size; | 850   int64 file_size; | 
| 852   EXPECT_TRUE(base::GetFileSize(path, &file_size)); | 851   EXPECT_TRUE(base::GetFileSize(path, &file_size)); | 
| 853   EXPECT_LT(0, file_size); | 852   EXPECT_LT(0, file_size); | 
| 854 | 853 | 
| 855   FileStream stream(base::MessageLoopProxy::current()); | 854   FileStream stream(base::MessageLoopProxy::current()); | 
| 856   int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 855   int flags = | 
| 857               base::File::FLAG_ASYNC; | 856       base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_ASYNC; | 
| 858   TestCompletionCallback callback; | 857   TestCompletionCallback callback; | 
| 859   int rv = stream.Open(path, flags, callback.callback()); | 858   int rv = stream.Open(path, flags, callback.callback()); | 
| 860   EXPECT_EQ(ERR_IO_PENDING, rv); | 859   EXPECT_EQ(ERR_IO_PENDING, rv); | 
| 861   EXPECT_EQ(OK, callback.WaitForResult()); | 860   EXPECT_EQ(OK, callback.WaitForResult()); | 
| 862 | 861 | 
| 863   int total_bytes_read = 0; | 862   int total_bytes_read = 0; | 
| 864 | 863 | 
| 865   std::string data_read; | 864   std::string data_read; | 
| 866   for (;;) { | 865   for (;;) { | 
| 867     scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 866     scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); | 
| 868     rv = stream.Read(buf.get(), buf->size(), callback.callback()); | 867     rv = stream.Read(buf.get(), buf->size(), callback.callback()); | 
| 869     if (rv == ERR_IO_PENDING) | 868     if (rv == ERR_IO_PENDING) | 
| 870       rv = callback.WaitForResult(); | 869       rv = callback.WaitForResult(); | 
| 871     EXPECT_LE(0, rv); | 870     EXPECT_LE(0, rv); | 
| 872     if (rv <= 0) | 871     if (rv <= 0) | 
| 873       break; | 872       break; | 
| 874     total_bytes_read += rv; | 873     total_bytes_read += rv; | 
| 875     data_read.append(buf->data(), rv); | 874     data_read.append(buf->data(), rv); | 
| 876   } | 875   } | 
| 877   EXPECT_EQ(file_size, total_bytes_read); | 876   EXPECT_EQ(file_size, total_bytes_read); | 
| 878 } | 877 } | 
| 879 #endif | 878 #endif | 
| 880 | 879 | 
| 881 }  // namespace | 880 }  // namespace | 
| 882 | 881 | 
| 883 }  // namespace net | 882 }  // namespace net | 
| OLD | NEW | 
|---|