| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "base/platform_file.h" | 7 #include "base/platform_file.h" |
| 8 #include "net/base/file_stream.h" | 8 #include "net/base/file_stream.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/base/test_completion_callback.h" | 10 #include "net/base/test_completion_callback.h" |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 EXPECT_EQ(net::OK, rv); | 177 EXPECT_EQ(net::OK, rv); |
| 178 | 178 |
| 179 int64 total_bytes_avail = stream.Available(); | 179 int64 total_bytes_avail = stream.Available(); |
| 180 EXPECT_EQ(file_size, total_bytes_avail); | 180 EXPECT_EQ(file_size, total_bytes_avail); |
| 181 | 181 |
| 182 TestCompletionCallback callback; | 182 TestCompletionCallback callback; |
| 183 | 183 |
| 184 char buf[4]; | 184 char buf[4]; |
| 185 rv = stream.Read(buf, arraysize(buf), &callback); | 185 rv = stream.Read(buf, arraysize(buf), &callback); |
| 186 stream.Close(); | 186 stream.Close(); |
| 187 if (rv == net::ERR_IO_PENDING) | 187 if (rv < 0) { |
| 188 rv = callback.WaitForResult(); | 188 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 189 ASSERT_LE(0, rv); | 189 // The callback should not be called if the request is cancelled. |
| 190 EXPECT_EQ(std::string(kTestData, rv), std::string(buf, rv)); | 190 MessageLoop::current()->RunAllPending(); |
| 191 EXPECT_FALSE(callback.have_result()); |
| 192 } else { |
| 193 EXPECT_EQ(std::string(kTestData, rv), std::string(buf, rv)); |
| 194 } |
| 191 } | 195 } |
| 192 | 196 |
| 193 TEST_F(FileStreamTest, BasicRead_FromOffset) { | 197 TEST_F(FileStreamTest, BasicRead_FromOffset) { |
| 194 int64 file_size; | 198 int64 file_size; |
| 195 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); | 199 bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 196 EXPECT_TRUE(ok); | 200 EXPECT_TRUE(ok); |
| 197 | 201 |
| 198 net::FileStream stream; | 202 net::FileStream stream; |
| 199 int flags = base::PLATFORM_FILE_OPEN | | 203 int flags = base::PLATFORM_FILE_OPEN | |
| 200 base::PLATFORM_FILE_READ; | 204 base::PLATFORM_FILE_READ; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 EXPECT_TRUE(ok); | 357 EXPECT_TRUE(ok); |
| 354 EXPECT_EQ(0, file_size); | 358 EXPECT_EQ(0, file_size); |
| 355 | 359 |
| 356 TestCompletionCallback callback; | 360 TestCompletionCallback callback; |
| 357 int total_bytes_written = 0; | 361 int total_bytes_written = 0; |
| 358 | 362 |
| 359 rv = stream.Write(kTestData + total_bytes_written, | 363 rv = stream.Write(kTestData + total_bytes_written, |
| 360 kTestDataSize - total_bytes_written, | 364 kTestDataSize - total_bytes_written, |
| 361 &callback); | 365 &callback); |
| 362 stream.Close(); | 366 stream.Close(); |
| 363 if (rv == net::ERR_IO_PENDING) | 367 if (rv < 0) { |
| 364 rv = callback.WaitForResult(); | 368 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 365 ASSERT_LT(0, rv); | 369 // The callback should not be called if the request is cancelled. |
| 366 ok = file_util::GetFileSize(temp_file_path(), &file_size); | 370 MessageLoop::current()->RunAllPending(); |
| 367 EXPECT_TRUE(ok); | 371 EXPECT_FALSE(callback.have_result()); |
| 368 EXPECT_EQ(file_size, rv); | 372 } else { |
| 373 ok = file_util::GetFileSize(temp_file_path(), &file_size); |
| 374 EXPECT_TRUE(ok); |
| 375 EXPECT_EQ(file_size, rv); |
| 376 } |
| 369 } | 377 } |
| 370 | 378 |
| 371 TEST_F(FileStreamTest, BasicWrite_FromOffset) { | 379 TEST_F(FileStreamTest, BasicWrite_FromOffset) { |
| 372 net::FileStream stream; | 380 net::FileStream stream; |
| 373 int flags = base::PLATFORM_FILE_OPEN | | 381 int flags = base::PLATFORM_FILE_OPEN | |
| 374 base::PLATFORM_FILE_WRITE; | 382 base::PLATFORM_FILE_WRITE; |
| 375 int rv = stream.Open(temp_file_path(), flags); | 383 int rv = stream.Open(temp_file_path(), flags); |
| 376 EXPECT_EQ(net::OK, rv); | 384 EXPECT_EQ(net::OK, rv); |
| 377 | 385 |
| 378 int64 file_size; | 386 int64 file_size; |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 876 write_stream.Close(); | 884 write_stream.Close(); |
| 877 | 885 |
| 878 // Read in the contents and make sure we get back what we expected. | 886 // Read in the contents and make sure we get back what we expected. |
| 879 std::string read_contents; | 887 std::string read_contents; |
| 880 file_util::ReadFileToString(temp_file_path(), &read_contents); | 888 file_util::ReadFileToString(temp_file_path(), &read_contents); |
| 881 | 889 |
| 882 EXPECT_EQ("01230123", read_contents); | 890 EXPECT_EQ("01230123", read_contents); |
| 883 } | 891 } |
| 884 | 892 |
| 885 } // namespace | 893 } // namespace |
| OLD | NEW |