OLD | NEW |
---|---|
(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 { | |
mmenke
2014/08/08 18:35:44
nit: Put entire file in anonymous namespace.
byungchul
2014/08/12 21:36:54
Done.
| |
10 | |
11 class HttpConnectionTest : public testing::Test { | |
12 protected: | |
mmenke
2014/08/08 18:35:43
nit: Just make it public, don't get anything out
byungchul
2014/08/12 21:36:54
Done.
| |
13 // Visibility change. | |
14 static const int kInitialBufSize = | |
15 HttpConnection::ReadIOBuffer::kInitialBufSize; | |
16 static const int kCapacityIncreaseFactor = | |
17 HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor; | |
18 static const int kDefaultCapacityLimit = | |
19 HttpConnection::ReadIOBuffer::kDefaultCapacityLimit; | |
20 static const int kDefaultTotalSizeLimit = | |
21 HttpConnection::PendingWriteIOBuffer::kDefaultTotalSizeLimit; | |
mmenke
2014/08/08 18:35:44
Suggest just making these public in the base class
byungchul
2014/08/12 21:36:55
Done.
| |
22 | |
23 static scoped_refptr<GrowableIOBuffer> GetBase( | |
mmenke
2014/08/08 18:35:44
I don't think these tests should dig into the impl
byungchul
2014/08/12 21:36:54
Done.
| |
24 scoped_refptr<HttpConnection::ReadIOBuffer> read_buffer) { | |
25 return read_buffer->base_; | |
26 } | |
27 | |
28 static const std::queue<std::string>& GetPendingData( | |
29 scoped_refptr<HttpConnection::PendingWriteIOBuffer> write_buffer) { | |
30 return write_buffer->pending_data_; | |
mmenke
2014/08/08 18:35:43
Again, think these tests should only test the exte
byungchul
2014/08/12 21:36:54
Done.
| |
31 } | |
32 | |
33 static void CheckCapacity(scoped_refptr<HttpConnection::ReadIOBuffer> buffer, | |
34 int expected_capacity) { | |
35 EXPECT_EQ(expected_capacity, buffer->GetCapacity()); | |
36 EXPECT_EQ(expected_capacity, buffer->RemainingCapacity()); | |
37 EXPECT_EQ(expected_capacity, GetBase(buffer)->capacity()); | |
38 EXPECT_EQ(expected_capacity, GetBase(buffer)->RemainingCapacity()); | |
39 } | |
40 | |
41 static std::string GetTestString(int size) { | |
42 std::string test_string; | |
43 for (int i = 0; i < size; ++i) { | |
44 test_string.push_back('A' + (i % 26)); | |
45 } | |
46 return test_string; | |
47 } | |
48 }; | |
49 | |
50 TEST_F(HttpConnectionTest, ReadIOBuffer_SetCapacity) { | |
51 scoped_refptr<HttpConnection::ReadIOBuffer> buffer( | |
52 new HttpConnection::ReadIOBuffer); | |
53 CheckCapacity(buffer, kInitialBufSize); | |
54 EXPECT_EQ(0, buffer->GetSize()); | |
55 | |
56 const int kNewCapacity = kInitialBufSize + 128; | |
57 buffer->SetCapacity(kNewCapacity); | |
58 CheckCapacity(buffer, kNewCapacity); | |
59 EXPECT_EQ(0, buffer->GetSize()); | |
60 } | |
61 | |
62 TEST_F(HttpConnectionTest, ReadIOBuffer_IncreaseCapacity) { | |
63 scoped_refptr<HttpConnection::ReadIOBuffer> buffer( | |
64 new HttpConnection::ReadIOBuffer); | |
65 EXPECT_TRUE(buffer->IncreaseCapacity()); | |
66 CheckCapacity(buffer, kInitialBufSize * kCapacityIncreaseFactor); | |
67 EXPECT_EQ(0, buffer->GetSize()); | |
68 | |
69 // Increase capacity until it fails. | |
70 while (buffer->IncreaseCapacity()); | |
71 EXPECT_FALSE(buffer->IncreaseCapacity()); | |
72 EXPECT_EQ(kDefaultCapacityLimit + 0, buffer->capacity_limit()); | |
73 EXPECT_EQ(kDefaultCapacityLimit + 0, buffer->GetCapacity()); | |
74 | |
75 // Enlarge capacity limit. | |
76 buffer->set_capacity_limit(buffer->capacity_limit() * 2); | |
77 EXPECT_TRUE(buffer->IncreaseCapacity()); | |
78 EXPECT_EQ(kDefaultCapacityLimit * kCapacityIncreaseFactor, | |
79 buffer->GetCapacity()); | |
80 | |
81 // Shrink capacity limit. It doesn't change capacity itself. | |
82 buffer->set_capacity_limit(kDefaultCapacityLimit / 2); | |
83 EXPECT_FALSE(buffer->IncreaseCapacity()); | |
84 EXPECT_EQ(kDefaultCapacityLimit * kCapacityIncreaseFactor, | |
85 buffer->GetCapacity()); | |
86 } | |
87 | |
88 TEST_F(HttpConnectionTest, ReadIOBuffer_DidRead_DidConsume) { | |
89 scoped_refptr<HttpConnection::ReadIOBuffer> buffer( | |
90 new HttpConnection::ReadIOBuffer); | |
91 const char* start_of_buffer = buffer->StartOfBuffer(); | |
92 EXPECT_EQ(start_of_buffer, buffer->data()); | |
93 | |
94 // Read data. | |
95 const int kReadLength = 128; | |
96 const std::string kReadData(GetTestString(kReadLength)); | |
97 memcpy(buffer->data(), kReadData.data(), kReadLength); | |
98 buffer->DidRead(kReadLength); | |
99 // No change in total capacity. | |
100 EXPECT_EQ(kInitialBufSize + 0, buffer->GetCapacity()); | |
101 // Change in unused capacity because of read data. | |
102 EXPECT_EQ(kInitialBufSize - kReadLength, buffer->RemainingCapacity()); | |
103 EXPECT_EQ(kReadLength, buffer->GetSize()); | |
104 // No change in start pointers of read data. | |
105 EXPECT_EQ(start_of_buffer, buffer->StartOfBuffer()); | |
106 // Change in start pointer of unused buffer. | |
107 EXPECT_EQ(start_of_buffer + kReadLength, buffer->data()); | |
108 // Test read data. | |
109 EXPECT_EQ(kReadData, std::string(buffer->StartOfBuffer(), buffer->GetSize())); | |
110 | |
111 // Consume data partially. | |
112 const int kConsumedLength = 32; | |
113 ASSERT_LT(kConsumedLength, kReadLength); | |
114 buffer->DidConsume(kConsumedLength); | |
115 // Capacity reduced because read data was too small comparing to capacity. | |
116 EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor, buffer->GetCapacity()); | |
117 // Change in unused capacity because of read data. | |
118 EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor | |
119 - kReadLength + kConsumedLength, | |
120 buffer->RemainingCapacity()); | |
121 // Change in read size. | |
122 EXPECT_EQ(kReadLength - kConsumedLength, buffer->GetSize()); | |
123 // Start data could be changed even when capacity is reduced. | |
124 start_of_buffer = buffer->StartOfBuffer(); | |
125 // Change in start pointer of unused buffer. | |
126 EXPECT_EQ(start_of_buffer + kReadLength - kConsumedLength, buffer->data()); | |
127 // Change in read data. | |
128 EXPECT_EQ(kReadData.substr(kConsumedLength), | |
129 std::string(buffer->StartOfBuffer(), buffer->GetSize())); | |
130 | |
131 // Read more data. | |
132 const int kReadLength2 = 64; | |
133 buffer->DidRead(kReadLength2); | |
134 // No change in total capacity. | |
135 EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor, buffer->GetCapacity()); | |
136 // Change in unused capacity because of read data. | |
137 EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor | |
138 - kReadLength + kConsumedLength - kReadLength2, | |
139 buffer->RemainingCapacity()); | |
140 // Change in read size | |
141 EXPECT_EQ(kReadLength - kConsumedLength + kReadLength2, buffer->GetSize()); | |
142 // No change in start pointer of read part. | |
143 EXPECT_EQ(start_of_buffer, buffer->StartOfBuffer()); | |
144 // Change in start pointer of unused buffer. | |
145 EXPECT_EQ(start_of_buffer + kReadLength - kConsumedLength + kReadLength2, | |
146 buffer->data()); | |
147 | |
148 // Consume data fully. | |
149 buffer->DidConsume(kReadLength - kConsumedLength + kReadLength2); | |
150 // Capacity reduced again because read data was too small. | |
151 EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor / kCapacityIncreaseFactor, | |
152 buffer->GetCapacity()); | |
153 EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor / kCapacityIncreaseFactor, | |
154 buffer->RemainingCapacity()); | |
155 // All reverts to initial because no data is left. | |
156 EXPECT_EQ(0, buffer->GetSize()); | |
157 // Start data could be changed even when capacity is reduced. | |
158 start_of_buffer = buffer->StartOfBuffer(); | |
159 EXPECT_EQ(start_of_buffer, buffer->data()); | |
160 } | |
161 | |
162 TEST_F(HttpConnectionTest, PendingWriteIOBuffer_Append_DidConsume) { | |
163 scoped_refptr<HttpConnection::PendingWriteIOBuffer> buffer( | |
164 new HttpConnection::PendingWriteIOBuffer()); | |
165 EXPECT_TRUE(buffer->IsEmpty()); | |
166 EXPECT_EQ(0, buffer->GetSizeToWrite()); | |
167 EXPECT_EQ(0, buffer->total_size()); | |
168 EXPECT_TRUE(GetPendingData(buffer).empty()); | |
169 | |
170 const std::string kData("data to write"); | |
171 EXPECT_TRUE(buffer->Append(kData)); | |
172 EXPECT_FALSE(buffer->IsEmpty()); | |
173 EXPECT_EQ(static_cast<int>(kData.size()), buffer->GetSizeToWrite()); | |
174 EXPECT_EQ(static_cast<int>(kData.size()), buffer->total_size()); | |
175 EXPECT_EQ(1U, GetPendingData(buffer).size()); | |
176 | |
177 const std::string kData2("more data to write"); | |
178 EXPECT_TRUE(buffer->Append(kData2)); | |
179 EXPECT_FALSE(buffer->IsEmpty()); | |
180 // No change in size to write. | |
181 EXPECT_EQ(static_cast<int>(kData.size()), buffer->GetSizeToWrite()); | |
182 // Change in total size. | |
183 EXPECT_EQ(static_cast<int>(kData.size() + kData2.size()), | |
184 buffer->total_size()); | |
185 EXPECT_EQ(2U, GetPendingData(buffer).size()); | |
186 | |
187 // Consume data partially. | |
188 const int kConsumedLength = kData.length() - 1; | |
189 buffer->DidConsume(kConsumedLength); | |
190 EXPECT_FALSE(buffer->IsEmpty()); | |
191 // Change in size to write. | |
192 EXPECT_EQ(static_cast<int>(kData.size()) - kConsumedLength, | |
193 buffer->GetSizeToWrite()); | |
194 // Change in total size. | |
195 EXPECT_EQ(static_cast<int>(kData.size() + kData2.size()) - kConsumedLength, | |
196 buffer->total_size()); | |
197 EXPECT_EQ(2U, GetPendingData(buffer).size()); | |
198 | |
199 // Consume first data fully. | |
200 buffer->DidConsume(kData.size() - kConsumedLength); | |
201 EXPECT_FALSE(buffer->IsEmpty()); | |
202 // Now, size to write is size of data added second. | |
203 EXPECT_EQ(static_cast<int>(kData2.size()), buffer->GetSizeToWrite()); | |
204 // Change in total size. | |
205 EXPECT_EQ(static_cast<int>(kData2.size()), buffer->total_size()); | |
206 EXPECT_EQ(1U, GetPendingData(buffer).size()); | |
207 | |
208 // Consume second data fully. | |
209 buffer->DidConsume(kData2.size()); | |
210 EXPECT_TRUE(buffer->IsEmpty()); | |
211 EXPECT_EQ(0, buffer->GetSizeToWrite()); | |
212 EXPECT_EQ(0, buffer->total_size()); | |
213 EXPECT_TRUE(GetPendingData(buffer).empty()); | |
214 } | |
215 | |
216 TEST_F(HttpConnectionTest, PendingWriteIOBuffer_TotalSizeLimit) { | |
217 scoped_refptr<HttpConnection::PendingWriteIOBuffer> buffer( | |
218 new HttpConnection::PendingWriteIOBuffer()); | |
219 EXPECT_EQ(kDefaultTotalSizeLimit + 0, buffer->total_size_limit()); | |
220 | |
221 // Set total size limit very small. | |
222 buffer->set_total_size_limit(10); | |
223 | |
224 const int kDataLength = 4; | |
225 const std::string kData(kDataLength, 'd'); | |
226 EXPECT_TRUE(buffer->Append(kData)); | |
227 EXPECT_EQ(kDataLength, buffer->total_size()); | |
228 EXPECT_TRUE(buffer->Append(kData)); | |
229 EXPECT_EQ(kDataLength * 2, buffer->total_size()); | |
230 | |
231 // Cannot append more data because it exceeds the limit. | |
232 EXPECT_FALSE(buffer->Append(kData)); | |
233 EXPECT_EQ(kDataLength * 2, buffer->total_size()); | |
234 | |
235 // Consume data partially. | |
236 const int kConsumedLength = 2; | |
237 buffer->DidConsume(kConsumedLength); | |
238 EXPECT_EQ(kDataLength * 2 - kConsumedLength, buffer->total_size()); | |
239 | |
240 // Can add more data. | |
241 EXPECT_TRUE(buffer->Append(kData)); | |
242 EXPECT_EQ(kDataLength * 3 - kConsumedLength, buffer->total_size()); | |
243 | |
244 // Cannot append more data because it exceeds the limit. | |
245 EXPECT_FALSE(buffer->Append(kData)); | |
246 EXPECT_EQ(kDataLength * 3 - kConsumedLength, buffer->total_size()); | |
247 | |
248 // Enlarge limit. | |
249 buffer->set_total_size_limit(20); | |
250 // Can add more data. | |
251 EXPECT_TRUE(buffer->Append(kData)); | |
252 EXPECT_EQ(kDataLength * 4 - kConsumedLength, buffer->total_size()); | |
253 } | |
254 | |
255 } // namespace net | |
OLD | NEW |