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/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/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #include "net/base/upload_data.h" | 18 #include "net/base/upload_data.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const char kTestData[] = "0123456789"; | 26 const char kTestData[] = "0123456789"; |
27 const size_t kTestDataSize = arraysize(kTestData) - 1; | 27 const size_t kTestDataSize = arraysize(kTestData) - 1; |
28 const size_t kTestBufferSize = 1 << 14; // 16KB. | 28 const size_t kTestBufferSize = 1 << 14; // 16KB. |
29 | 29 |
| 30 // Reads data from the upload data stream, and returns the data as string. |
| 31 std::string ReadFromUploadDataStream(UploadDataStream* stream) { |
| 32 std::string data_read; |
| 33 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); |
| 34 while (!stream->IsEOF()) { |
| 35 const int bytes_read = stream->Read(buf, kTestBufferSize); |
| 36 data_read.append(buf->data(), bytes_read); |
| 37 } |
| 38 return data_read; |
| 39 } |
| 40 |
30 } // namespace | 41 } // namespace |
31 | 42 |
32 class UploadDataStreamTest : public PlatformTest { | 43 class UploadDataStreamTest : public PlatformTest { |
33 public: | 44 public: |
34 UploadDataStreamTest() : upload_data_(new UploadData) { } | 45 UploadDataStreamTest() : upload_data_(new UploadData) { } |
35 | 46 |
36 void FileChangedHelper(const FilePath& file_path, | 47 void FileChangedHelper(const FilePath& file_path, |
37 const base::Time& time, | 48 const base::Time& time, |
38 bool error_expected); | 49 bool error_expected); |
39 | 50 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 FileChangedHelper(temp_file_path, file_info.last_modified, false); | 148 FileChangedHelper(temp_file_path, file_info.last_modified, false); |
138 | 149 |
139 // Test file changed. | 150 // Test file changed. |
140 FileChangedHelper(temp_file_path, | 151 FileChangedHelper(temp_file_path, |
141 file_info.last_modified - base::TimeDelta::FromSeconds(1), | 152 file_info.last_modified - base::TimeDelta::FromSeconds(1), |
142 true); | 153 true); |
143 | 154 |
144 file_util::Delete(temp_file_path, false); | 155 file_util::Delete(temp_file_path, false); |
145 } | 156 } |
146 | 157 |
| 158 TEST_F(UploadDataStreamTest, UploadDataReused) { |
| 159 FilePath temp_file_path; |
| 160 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
| 161 ASSERT_EQ(static_cast<int>(kTestDataSize), |
| 162 file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); |
| 163 |
| 164 // Prepare |upload_data_| that contains a file. |
| 165 std::vector<UploadData::Element> elements; |
| 166 UploadData::Element element; |
| 167 element.SetToFilePath(temp_file_path); |
| 168 elements.push_back(element); |
| 169 upload_data_->SetElements(elements); |
| 170 EXPECT_EQ(kTestDataSize, upload_data_->GetContentLengthSync()); |
| 171 |
| 172 // Confirm that the file is read properly. |
| 173 { |
| 174 UploadDataStream stream(upload_data_); |
| 175 ASSERT_EQ(OK, stream.Init()); |
| 176 EXPECT_EQ(kTestData, ReadFromUploadDataStream(&stream)); |
| 177 } |
| 178 |
| 179 // Reuse |upload_data_| for another UploadDataStream, and confirm that the |
| 180 // file is read properly. |
| 181 { |
| 182 UploadDataStream stream(upload_data_); |
| 183 ASSERT_EQ(OK, stream.Init()); |
| 184 EXPECT_EQ(kTestData, ReadFromUploadDataStream(&stream)); |
| 185 } |
| 186 |
| 187 file_util::Delete(temp_file_path, false); |
| 188 } |
| 189 |
147 } // namespace net | 190 } // namespace net |
OLD | NEW |