OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/upload_data_stream.h" | 5 #include "net/base/upload_data_stream.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/scoped_ptr.h" |
| 13 #include "base/time.h" |
| 14 #include "net/base/net_errors.h" |
12 #include "net/base/upload_data.h" | 15 #include "net/base/upload_data.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
15 | 18 |
16 namespace net { | 19 namespace net { |
17 | 20 |
18 namespace { | 21 namespace { |
19 | 22 |
20 const char kTestData[] = "0123456789"; | 23 const char kTestData[] = "0123456789"; |
21 const int kTestDataSize = arraysize(kTestData) - 1; | 24 const int kTestDataSize = arraysize(kTestData) - 1; |
22 | 25 |
23 } // namespace | 26 } // namespace |
24 | 27 |
25 class UploadDataStreamTest : public PlatformTest { | 28 class UploadDataStreamTest : public PlatformTest { |
26 public: | 29 public: |
27 UploadDataStreamTest() : upload_data_(new UploadData) { } | 30 UploadDataStreamTest() : upload_data_(new UploadData) { } |
28 | 31 |
| 32 void FileChangedHelper(const FilePath& file_path, |
| 33 const base::Time& time, |
| 34 bool error_expected); |
| 35 |
29 scoped_refptr<UploadData> upload_data_; | 36 scoped_refptr<UploadData> upload_data_; |
30 }; | 37 }; |
31 | 38 |
32 TEST_F(UploadDataStreamTest, EmptyUploadData) { | 39 TEST_F(UploadDataStreamTest, EmptyUploadData) { |
33 upload_data_->AppendBytes("", 0); | 40 upload_data_->AppendBytes("", 0); |
34 UploadDataStream stream(upload_data_); | 41 scoped_ptr<UploadDataStream> stream( |
35 EXPECT_TRUE(stream.eof()); | 42 UploadDataStream::Create(upload_data_, NULL)); |
| 43 ASSERT_TRUE(stream.get()); |
| 44 EXPECT_TRUE(stream->eof()); |
36 } | 45 } |
37 | 46 |
38 TEST_F(UploadDataStreamTest, ConsumeAll) { | 47 TEST_F(UploadDataStreamTest, ConsumeAll) { |
39 upload_data_->AppendBytes(kTestData, kTestDataSize); | 48 upload_data_->AppendBytes(kTestData, kTestDataSize); |
40 UploadDataStream stream(upload_data_); | 49 scoped_ptr<UploadDataStream> stream( |
41 while (!stream.eof()) { | 50 UploadDataStream::Create(upload_data_, NULL)); |
42 stream.DidConsume(stream.buf_len()); | 51 ASSERT_TRUE(stream.get()); |
| 52 while (!stream->eof()) { |
| 53 stream->DidConsume(stream->buf_len()); |
43 } | 54 } |
44 } | 55 } |
45 | 56 |
46 TEST_F(UploadDataStreamTest, FileSmallerThanLength) { | 57 TEST_F(UploadDataStreamTest, FileSmallerThanLength) { |
47 FilePath temp_file_path; | 58 FilePath temp_file_path; |
48 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); | 59 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
49 ASSERT_EQ(kTestDataSize, file_util::WriteFile(temp_file_path, | 60 ASSERT_EQ(kTestDataSize, file_util::WriteFile(temp_file_path, |
50 kTestData, kTestDataSize)); | 61 kTestData, kTestDataSize)); |
51 const uint64 kFakeSize = kTestDataSize*2; | 62 const uint64 kFakeSize = kTestDataSize*2; |
52 | 63 |
53 std::vector<UploadData::Element> elements; | 64 std::vector<UploadData::Element> elements; |
54 UploadData::Element element; | 65 UploadData::Element element; |
55 element.SetToFilePath(temp_file_path); | 66 element.SetToFilePath(temp_file_path); |
56 element.SetContentLength(kFakeSize); | 67 element.SetContentLength(kFakeSize); |
57 elements.push_back(element); | 68 elements.push_back(element); |
58 upload_data_->set_elements(elements); | 69 upload_data_->set_elements(elements); |
59 EXPECT_EQ(kFakeSize, upload_data_->GetContentLength()); | 70 EXPECT_EQ(kFakeSize, upload_data_->GetContentLength()); |
60 | 71 |
61 UploadDataStream stream(upload_data_); | 72 scoped_ptr<UploadDataStream> stream( |
62 EXPECT_FALSE(stream.eof()); | 73 UploadDataStream::Create(upload_data_, NULL)); |
| 74 ASSERT_TRUE(stream.get()); |
| 75 EXPECT_FALSE(stream->eof()); |
63 uint64 read_counter = 0; | 76 uint64 read_counter = 0; |
64 while (!stream.eof()) { | 77 while (!stream->eof()) { |
65 read_counter += stream.buf_len(); | 78 read_counter += stream->buf_len(); |
66 stream.DidConsume(stream.buf_len()); | 79 stream->DidConsume(stream->buf_len()); |
67 } | 80 } |
68 EXPECT_LT(read_counter, stream.size()); | 81 EXPECT_LT(read_counter, stream->size()); |
| 82 |
| 83 file_util::Delete(temp_file_path, false); |
| 84 } |
| 85 |
| 86 void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path, |
| 87 const base::Time& time, |
| 88 bool error_expected) { |
| 89 std::vector<UploadData::Element> elements; |
| 90 UploadData::Element element; |
| 91 element.SetToFilePathRange(file_path, 1, 2, time); |
| 92 elements.push_back(element); |
| 93 upload_data_->set_elements(elements); |
| 94 |
| 95 int error_code; |
| 96 scoped_ptr<UploadDataStream> stream( |
| 97 UploadDataStream::Create(upload_data_, &error_code)); |
| 98 if (error_expected) |
| 99 ASSERT_TRUE(!stream.get() && error_code == net::ERR_UPLOAD_FILE_CHANGED); |
| 100 else |
| 101 ASSERT_TRUE(stream.get() && error_code == net::OK); |
| 102 } |
| 103 |
| 104 TEST_F(UploadDataStreamTest, FileChanged) { |
| 105 FilePath temp_file_path; |
| 106 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
| 107 ASSERT_EQ(kTestDataSize, file_util::WriteFile(temp_file_path, |
| 108 kTestData, kTestDataSize)); |
| 109 |
| 110 file_util::FileInfo file_info; |
| 111 ASSERT_TRUE(file_util::GetFileInfo(temp_file_path, &file_info)); |
| 112 |
| 113 // Test file not changed. |
| 114 FileChangedHelper(temp_file_path, file_info.last_modified, false); |
| 115 |
| 116 // Test file changed. |
| 117 FileChangedHelper(temp_file_path, |
| 118 file_info.last_modified - base::TimeDelta::FromSeconds(1), |
| 119 true); |
69 | 120 |
70 file_util::Delete(temp_file_path, false); | 121 file_util::Delete(temp_file_path, false); |
71 } | 122 } |
72 | 123 |
73 } // namespace net | 124 } // namespace net |
74 | 125 |
OLD | NEW |