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

Side by Side Diff: net/server/http_connection_unittest.cc

Issue 296053012: Replace StreamListenSocket with StreamSocket in HttpServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. Created 6 years, 4 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/server/http_connection.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace net {
10 namespace {
11
12 class HttpConnectionTest : public testing::Test {
mmenke 2014/08/14 16:36:02 This test fixture doesn't really get us anything,
byungchul 2014/08/14 18:44:03 Done.
13 public:
14 static void CheckCapacity(scoped_refptr<HttpConnection::ReadIOBuffer> buffer,
mmenke 2014/08/14 16:36:02 include ref_counted
byungchul 2014/08/14 18:44:02 Done.
15 int expected_capacity) {
16 EXPECT_EQ(expected_capacity, buffer->GetCapacity());
17 EXPECT_EQ(expected_capacity, buffer->RemainingCapacity());
18 }
19
20 static std::string GetTestString(int size) {
mmenke 2014/08/14 16:36:02 include <string>
byungchul 2014/08/14 18:44:02 Done.
21 std::string test_string;
22 for (int i = 0; i < size; ++i) {
23 test_string.push_back('A' + (i % 26));
24 }
25 return test_string;
26 }
27 };
28
29 TEST_F(HttpConnectionTest, ReadIOBuffer_SetCapacity) {
30 scoped_refptr<HttpConnection::ReadIOBuffer> buffer(
31 new HttpConnection::ReadIOBuffer);
32 CheckCapacity(buffer, HttpConnection::ReadIOBuffer::kInitialBufSize);
33 EXPECT_EQ(0, buffer->GetSize());
34
35 const int kNewCapacity = HttpConnection::ReadIOBuffer::kInitialBufSize + 128;
36 buffer->SetCapacity(kNewCapacity);
37 CheckCapacity(buffer, kNewCapacity);
38 EXPECT_EQ(0, buffer->GetSize());
mmenke 2014/08/14 16:36:02 Should we write kInitialBufSize bytes to the buffe
byungchul 2014/08/14 18:44:03 Done.
39 }
40
41 TEST_F(HttpConnectionTest, ReadIOBuffer_IncreaseCapacity) {
42 scoped_refptr<HttpConnection::ReadIOBuffer> buffer(
43 new HttpConnection::ReadIOBuffer);
44 EXPECT_TRUE(buffer->IncreaseCapacity());
45 CheckCapacity(buffer,
46 HttpConnection::ReadIOBuffer::kInitialBufSize
47 * HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor);
48 EXPECT_EQ(0, buffer->GetSize());
49
50 // Increase capacity until it fails.
51 while (buffer->IncreaseCapacity());
52 EXPECT_FALSE(buffer->IncreaseCapacity());
53 EXPECT_EQ(HttpConnection::ReadIOBuffer::kDefaultMaxBufferSize + 0,
54 buffer->max_buffer_size());
55 EXPECT_EQ(HttpConnection::ReadIOBuffer::kDefaultMaxBufferSize + 0,
56 buffer->GetCapacity());
57
58 // Enlarge capacity limit.
59 buffer->set_max_buffer_size(buffer->max_buffer_size() * 2);
60 EXPECT_TRUE(buffer->IncreaseCapacity());
61 EXPECT_EQ(HttpConnection::ReadIOBuffer::kDefaultMaxBufferSize
62 * HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor,
63 buffer->GetCapacity());
64
65 // Shrink capacity limit. It doesn't change capacity itself.
66 buffer->set_max_buffer_size(
67 HttpConnection::ReadIOBuffer::kDefaultMaxBufferSize / 2);
68 EXPECT_FALSE(buffer->IncreaseCapacity());
69 EXPECT_EQ(HttpConnection::ReadIOBuffer::kDefaultMaxBufferSize
70 * HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor,
71 buffer->GetCapacity());
mmenke 2014/08/14 16:36:02 Same goes for this - should we make sure that we k
byungchul 2014/08/14 18:44:02 Done.
72 }
73
74 TEST_F(HttpConnectionTest, ReadIOBuffer_DidRead_DidConsume) {
75 scoped_refptr<HttpConnection::ReadIOBuffer> buffer(
76 new HttpConnection::ReadIOBuffer);
77 const char* start_of_buffer = buffer->StartOfBuffer();
78 EXPECT_EQ(start_of_buffer, buffer->data());
79
80 // Read data.
81 const int kReadLength = 128;
82 const std::string kReadData(GetTestString(kReadLength));
83 memcpy(buffer->data(), kReadData.data(), kReadLength);
84 buffer->DidRead(kReadLength);
85 // No change in total capacity.
86 EXPECT_EQ(HttpConnection::ReadIOBuffer::kInitialBufSize + 0,
87 buffer->GetCapacity());
88 // Change in unused capacity because of read data.
89 EXPECT_EQ(HttpConnection::ReadIOBuffer::kInitialBufSize - kReadLength,
90 buffer->RemainingCapacity());
91 EXPECT_EQ(kReadLength, buffer->GetSize());
92 // No change in start pointers of read data.
93 EXPECT_EQ(start_of_buffer, buffer->StartOfBuffer());
94 // Change in start pointer of unused buffer.
95 EXPECT_EQ(start_of_buffer + kReadLength, buffer->data());
96 // Test read data.
97 EXPECT_EQ(kReadData, std::string(buffer->StartOfBuffer(), buffer->GetSize()));
98
99 // Consume data partially.
100 const int kConsumedLength = 32;
101 ASSERT_LT(kConsumedLength, kReadLength);
102 buffer->DidConsume(kConsumedLength);
103 // Capacity reduced because read data was too small comparing to capacity.
104 EXPECT_EQ(HttpConnection::ReadIOBuffer::kInitialBufSize
105 / HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor,
106 buffer->GetCapacity());
107 // Change in unused capacity because of read data.
108 EXPECT_EQ(HttpConnection::ReadIOBuffer::kInitialBufSize
109 / HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor
110 - kReadLength + kConsumedLength,
mmenke 2014/08/14 16:36:02 Put binary operators on previous line (Goes for th
byungchul 2014/08/14 18:44:03 Done. I knew the style but it was more readable fo
111 buffer->RemainingCapacity());
112 // Change in read size.
113 EXPECT_EQ(kReadLength - kConsumedLength, buffer->GetSize());
114 // Start data could be changed even when capacity is reduced.
115 start_of_buffer = buffer->StartOfBuffer();
116 // Change in start pointer of unused buffer.
117 EXPECT_EQ(start_of_buffer + kReadLength - kConsumedLength, buffer->data());
118 // Change in read data.
119 EXPECT_EQ(kReadData.substr(kConsumedLength),
120 std::string(buffer->StartOfBuffer(), buffer->GetSize()));
121
122 // Read more data.
123 const int kReadLength2 = 64;
124 buffer->DidRead(kReadLength2);
125 // No change in total capacity.
126 EXPECT_EQ(HttpConnection::ReadIOBuffer::kInitialBufSize
127 / HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor,
128 buffer->GetCapacity());
129 // Change in unused capacity because of read data.
130 EXPECT_EQ(HttpConnection::ReadIOBuffer::kInitialBufSize
131 / HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor
132 - kReadLength + kConsumedLength - kReadLength2,
133 buffer->RemainingCapacity());
134 // Change in read size
135 EXPECT_EQ(kReadLength - kConsumedLength + kReadLength2, buffer->GetSize());
136 // No change in start pointer of read part.
137 EXPECT_EQ(start_of_buffer, buffer->StartOfBuffer());
138 // Change in start pointer of unused buffer.
139 EXPECT_EQ(start_of_buffer + kReadLength - kConsumedLength + kReadLength2,
140 buffer->data());
141
142 // Consume data fully.
143 buffer->DidConsume(kReadLength - kConsumedLength + kReadLength2);
144 // Capacity reduced again because read data was too small.
145 EXPECT_EQ(HttpConnection::ReadIOBuffer::kInitialBufSize
146 / HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor
147 / HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor,
148 buffer->GetCapacity());
149 EXPECT_EQ(HttpConnection::ReadIOBuffer::kInitialBufSize
150 / HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor
151 / HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor,
152 buffer->RemainingCapacity());
153 // All reverts to initial because no data is left.
154 EXPECT_EQ(0, buffer->GetSize());
155 // Start data could be changed even when capacity is reduced.
156 start_of_buffer = buffer->StartOfBuffer();
157 EXPECT_EQ(start_of_buffer, buffer->data());
158 }
159
160 TEST_F(HttpConnectionTest, PendingWriteIOBuffer_Append_DidConsume) {
161 scoped_refptr<HttpConnection::PendingWriteIOBuffer> buffer(
162 new HttpConnection::PendingWriteIOBuffer());
163 EXPECT_TRUE(buffer->IsEmpty());
164 EXPECT_EQ(0, buffer->GetSizeToWrite());
165 EXPECT_EQ(0, buffer->total_size());
166
167 const std::string kData("data to write");
168 EXPECT_TRUE(buffer->Append(kData));
169 EXPECT_FALSE(buffer->IsEmpty());
170 EXPECT_EQ(static_cast<int>(kData.size()), buffer->GetSizeToWrite());
171 EXPECT_EQ(static_cast<int>(kData.size()), buffer->total_size());
172
173 const std::string kData2("more data to write");
174 EXPECT_TRUE(buffer->Append(kData2));
175 EXPECT_FALSE(buffer->IsEmpty());
176 // No change in size to write.
177 EXPECT_EQ(static_cast<int>(kData.size()), buffer->GetSizeToWrite());
178 // Change in total size.
179 EXPECT_EQ(static_cast<int>(kData.size() + kData2.size()),
180 buffer->total_size());
181
182 // Consume data partially.
183 const int kConsumedLength = kData.length() - 1;
184 buffer->DidConsume(kConsumedLength);
185 EXPECT_FALSE(buffer->IsEmpty());
186 // Change in size to write.
187 EXPECT_EQ(static_cast<int>(kData.size()) - kConsumedLength,
188 buffer->GetSizeToWrite());
189 // Change in total size.
190 EXPECT_EQ(static_cast<int>(kData.size() + kData2.size()) - kConsumedLength,
191 buffer->total_size());
mmenke 2014/08/14 16:36:02 Should have a partial read, with an EXPECT_EQ for
byungchul 2014/08/14 18:44:03 Did you mean partial consumption on ReadIOBuffer?
192
193 // Consume first data fully.
194 buffer->DidConsume(kData.size() - kConsumedLength);
195 EXPECT_FALSE(buffer->IsEmpty());
196 // Now, size to write is size of data added second.
197 EXPECT_EQ(static_cast<int>(kData2.size()), buffer->GetSizeToWrite());
198 // Change in total size.
199 EXPECT_EQ(static_cast<int>(kData2.size()), buffer->total_size());
200
201 // Consume second data fully.
202 buffer->DidConsume(kData2.size());
203 EXPECT_TRUE(buffer->IsEmpty());
204 EXPECT_EQ(0, buffer->GetSizeToWrite());
205 EXPECT_EQ(0, buffer->total_size());
206 }
207
208 TEST_F(HttpConnectionTest, PendingWriteIOBuffer_TotalSizeLimit) {
209 scoped_refptr<HttpConnection::PendingWriteIOBuffer> buffer(
210 new HttpConnection::PendingWriteIOBuffer());
211 EXPECT_EQ(HttpConnection::PendingWriteIOBuffer::kDefaultMaxBufferSize + 0,
212 buffer->max_buffer_size());
213
214 // Set total size limit very small.
215 buffer->set_max_buffer_size(10);
216
217 const int kDataLength = 4;
218 const std::string kData(kDataLength, 'd');
219 EXPECT_TRUE(buffer->Append(kData));
220 EXPECT_EQ(kDataLength, buffer->total_size());
221 EXPECT_TRUE(buffer->Append(kData));
222 EXPECT_EQ(kDataLength * 2, buffer->total_size());
223
224 // Cannot append more data because it exceeds the limit.
225 EXPECT_FALSE(buffer->Append(kData));
226 EXPECT_EQ(kDataLength * 2, buffer->total_size());
227
228 // Consume data partially.
229 const int kConsumedLength = 2;
230 buffer->DidConsume(kConsumedLength);
231 EXPECT_EQ(kDataLength * 2 - kConsumedLength, buffer->total_size());
232
233 // Can add more data.
234 EXPECT_TRUE(buffer->Append(kData));
235 EXPECT_EQ(kDataLength * 3 - kConsumedLength, buffer->total_size());
236
237 // Cannot append more data because it exceeds the limit.
238 EXPECT_FALSE(buffer->Append(kData));
239 EXPECT_EQ(kDataLength * 3 - kConsumedLength, buffer->total_size());
240
241 // Enlarge limit.
242 buffer->set_max_buffer_size(20);
243 // Can add more data.
244 EXPECT_TRUE(buffer->Append(kData));
245 EXPECT_EQ(kDataLength * 4 - kConsumedLength, buffer->total_size());
246 }
247
248 } // namespace
249 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698