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

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

Issue 647883002: git cl format the final third of the net/base directory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/chunked_upload_data_stream.h" 5 #include "net/base/chunked_upload_data_stream.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
12 #include "net/base/test_completion_callback.h" 12 #include "net/base/test_completion_callback.h"
13 #include "net/base/upload_data_stream.h" 13 #include "net/base/upload_data_stream.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace net { 16 namespace net {
17 17
18 namespace { 18 namespace {
19 19
20 const char kTestData[] = "0123456789"; 20 const char kTestData[] = "0123456789";
21 const size_t kTestDataSize = arraysize(kTestData) - 1; 21 const size_t kTestDataSize = arraysize(kTestData) - 1;
22 const size_t kTestBufferSize = 1 << 14; // 16KB. 22 const size_t kTestBufferSize = 1 << 14; // 16KB.
23 23
24 } // namespace 24 } // namespace
25 25
26 // Reads data once from the upload data stream, and returns the data as string. 26 // Reads data once from the upload data stream, and returns the data as string.
27 // Expects the read to succeed synchronously. 27 // Expects the read to succeed synchronously.
28 std::string ReadSync(UploadDataStream* stream, int buffer_size) { 28 std::string ReadSync(UploadDataStream* stream, int buffer_size) {
29 scoped_refptr<IOBuffer> buf = new IOBuffer(buffer_size); 29 scoped_refptr<IOBuffer> buf = new IOBuffer(buffer_size);
30 int result = stream->Read(buf.get(), 30 int result =
31 buffer_size, 31 stream->Read(buf.get(), buffer_size, TestCompletionCallback().callback());
32 TestCompletionCallback().callback());
33 EXPECT_GE(result, 0); 32 EXPECT_GE(result, 0);
34 return std::string(buf->data(), result); 33 return std::string(buf->data(), result);
35 } 34 }
36 35
37 // Check the case data is added after the first read attempt. 36 // Check the case data is added after the first read attempt.
38 TEST(ChunkedUploadDataStreamTest, AppendOnce) { 37 TEST(ChunkedUploadDataStreamTest, AppendOnce) {
39 ChunkedUploadDataStream stream(0); 38 ChunkedUploadDataStream stream(0);
40 39
41 ASSERT_EQ(OK, stream.Init(TestCompletionCallback().callback())); 40 ASSERT_EQ(OK, stream.Init(TestCompletionCallback().callback()));
42 EXPECT_FALSE(stream.IsInMemory()); 41 EXPECT_FALSE(stream.IsInMemory());
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 EXPECT_EQ(0u, stream.size()); 103 EXPECT_EQ(0u, stream.size());
105 EXPECT_EQ(0u, stream.position()); 104 EXPECT_EQ(0u, stream.position());
106 EXPECT_FALSE(stream.IsEOF()); 105 EXPECT_FALSE(stream.IsEOF());
107 106
108 TestCompletionCallback callback; 107 TestCompletionCallback callback;
109 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); 108 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
110 for (size_t i = 0; i < kTestDataSize; ++i) { 109 for (size_t i = 0; i < kTestDataSize; ++i) {
111 EXPECT_EQ(0u, stream.size()); // Content-Length is 0 for chunked data. 110 EXPECT_EQ(0u, stream.size()); // Content-Length is 0 for chunked data.
112 EXPECT_EQ(i, stream.position()); 111 EXPECT_EQ(i, stream.position());
113 ASSERT_FALSE(stream.IsEOF()); 112 ASSERT_FALSE(stream.IsEOF());
114 int bytes_read = stream.Read(buf.get(), 113 int bytes_read =
115 kTestBufferSize, 114 stream.Read(buf.get(), kTestBufferSize, callback.callback());
116 callback.callback());
117 ASSERT_EQ(ERR_IO_PENDING, bytes_read); 115 ASSERT_EQ(ERR_IO_PENDING, bytes_read);
118 stream.AppendData(&kTestData[i], 1, i == kTestDataSize - 1); 116 stream.AppendData(&kTestData[i], 1, i == kTestDataSize - 1);
119 ASSERT_EQ(1, callback.WaitForResult()); 117 ASSERT_EQ(1, callback.WaitForResult());
120 EXPECT_EQ(kTestData[i], buf->data()[0]); 118 EXPECT_EQ(kTestData[i], buf->data()[0]);
121 } 119 }
122 120
123 EXPECT_EQ(0u, stream.size()); // Content-Length is 0 for chunked data. 121 EXPECT_EQ(0u, stream.size()); // Content-Length is 0 for chunked data.
124 EXPECT_EQ(kTestDataSize, stream.position()); 122 EXPECT_EQ(kTestDataSize, stream.position());
125 ASSERT_TRUE(stream.IsEOF()); 123 ASSERT_TRUE(stream.IsEOF());
126 } 124 }
127 125
128 TEST(ChunkedUploadDataStreamTest, MultipleAppendsBetweenReads) { 126 TEST(ChunkedUploadDataStreamTest, MultipleAppendsBetweenReads) {
129 ChunkedUploadDataStream stream(0); 127 ChunkedUploadDataStream stream(0);
130 128
131 ASSERT_EQ(OK, stream.Init(TestCompletionCallback().callback())); 129 ASSERT_EQ(OK, stream.Init(TestCompletionCallback().callback()));
132 EXPECT_FALSE(stream.IsInMemory()); 130 EXPECT_FALSE(stream.IsInMemory());
133 EXPECT_EQ(0u, stream.size()); // Content-Length is 0 for chunked data. 131 EXPECT_EQ(0u, stream.size()); // Content-Length is 0 for chunked data.
134 EXPECT_EQ(0u, stream.position()); 132 EXPECT_EQ(0u, stream.position());
135 EXPECT_FALSE(stream.IsEOF()); 133 EXPECT_FALSE(stream.IsEOF());
136 134
137 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize); 135 scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
138 for (size_t i = 0; i < kTestDataSize; ++i) { 136 for (size_t i = 0; i < kTestDataSize; ++i) {
139 EXPECT_EQ(i, stream.position()); 137 EXPECT_EQ(i, stream.position());
140 ASSERT_FALSE(stream.IsEOF()); 138 ASSERT_FALSE(stream.IsEOF());
141 stream.AppendData(&kTestData[i], 1, i == kTestDataSize - 1); 139 stream.AppendData(&kTestData[i], 1, i == kTestDataSize - 1);
142 int bytes_read = stream.Read(buf.get(), 140 int bytes_read = stream.Read(
143 kTestBufferSize, 141 buf.get(), kTestBufferSize, TestCompletionCallback().callback());
144 TestCompletionCallback().callback());
145 ASSERT_EQ(1, bytes_read); 142 ASSERT_EQ(1, bytes_read);
146 EXPECT_EQ(kTestData[i], buf->data()[0]); 143 EXPECT_EQ(kTestData[i], buf->data()[0]);
147 } 144 }
148 145
149 EXPECT_EQ(kTestDataSize, stream.position()); 146 EXPECT_EQ(kTestDataSize, stream.position());
150 ASSERT_TRUE(stream.IsEOF()); 147 ASSERT_TRUE(stream.IsEOF());
151 } 148 }
152 149
153 // Checks that multiple reads can be merged. 150 // Checks that multiple reads can be merged.
154 TEST(ChunkedUploadDataStreamTest, MultipleAppendsBeforeInit) { 151 TEST(ChunkedUploadDataStreamTest, MultipleAppendsBeforeInit) {
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 EXPECT_FALSE(callback.have_result()); 296 EXPECT_FALSE(callback.have_result());
300 297
301 std::string data = ReadSync(&stream, kTestBufferSize); 298 std::string data = ReadSync(&stream, kTestBufferSize);
302 EXPECT_EQ(kTestData, data); 299 EXPECT_EQ(kTestData, data);
303 EXPECT_EQ(kTestDataSize, stream.position()); 300 EXPECT_EQ(kTestDataSize, stream.position());
304 ASSERT_TRUE(stream.IsEOF()); 301 ASSERT_TRUE(stream.IsEOF());
305 EXPECT_FALSE(callback.have_result()); 302 EXPECT_FALSE(callback.have_result());
306 } 303 }
307 304
308 } // namespace net 305 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698