Chromium Code Reviews| Index: net/server/http_connection_unittest.cc |
| diff --git a/net/server/http_connection_unittest.cc b/net/server/http_connection_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3828bf229e71e8bbec57961de4a7921db3d35fb9 |
| --- /dev/null |
| +++ b/net/server/http_connection_unittest.cc |
| @@ -0,0 +1,255 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/server/http_connection.h" |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
|
mmenke
2014/08/08 18:35:44
nit: Put entire file in anonymous namespace.
byungchul
2014/08/12 21:36:54
Done.
|
| + |
| +class HttpConnectionTest : public testing::Test { |
| + 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.
|
| + // Visibility change. |
| + static const int kInitialBufSize = |
| + HttpConnection::ReadIOBuffer::kInitialBufSize; |
| + static const int kCapacityIncreaseFactor = |
| + HttpConnection::ReadIOBuffer::kCapacityIncreaseFactor; |
| + static const int kDefaultCapacityLimit = |
| + HttpConnection::ReadIOBuffer::kDefaultCapacityLimit; |
| + static const int kDefaultTotalSizeLimit = |
| + 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.
|
| + |
| + 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.
|
| + scoped_refptr<HttpConnection::ReadIOBuffer> read_buffer) { |
| + return read_buffer->base_; |
| + } |
| + |
| + static const std::queue<std::string>& GetPendingData( |
| + scoped_refptr<HttpConnection::PendingWriteIOBuffer> write_buffer) { |
| + 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.
|
| + } |
| + |
| + static void CheckCapacity(scoped_refptr<HttpConnection::ReadIOBuffer> buffer, |
| + int expected_capacity) { |
| + EXPECT_EQ(expected_capacity, buffer->GetCapacity()); |
| + EXPECT_EQ(expected_capacity, buffer->RemainingCapacity()); |
| + EXPECT_EQ(expected_capacity, GetBase(buffer)->capacity()); |
| + EXPECT_EQ(expected_capacity, GetBase(buffer)->RemainingCapacity()); |
| + } |
| + |
| + static std::string GetTestString(int size) { |
| + std::string test_string; |
| + for (int i = 0; i < size; ++i) { |
| + test_string.push_back('A' + (i % 26)); |
| + } |
| + return test_string; |
| + } |
| +}; |
| + |
| +TEST_F(HttpConnectionTest, ReadIOBuffer_SetCapacity) { |
| + scoped_refptr<HttpConnection::ReadIOBuffer> buffer( |
| + new HttpConnection::ReadIOBuffer); |
| + CheckCapacity(buffer, kInitialBufSize); |
| + EXPECT_EQ(0, buffer->GetSize()); |
| + |
| + const int kNewCapacity = kInitialBufSize + 128; |
| + buffer->SetCapacity(kNewCapacity); |
| + CheckCapacity(buffer, kNewCapacity); |
| + EXPECT_EQ(0, buffer->GetSize()); |
| +} |
| + |
| +TEST_F(HttpConnectionTest, ReadIOBuffer_IncreaseCapacity) { |
| + scoped_refptr<HttpConnection::ReadIOBuffer> buffer( |
| + new HttpConnection::ReadIOBuffer); |
| + EXPECT_TRUE(buffer->IncreaseCapacity()); |
| + CheckCapacity(buffer, kInitialBufSize * kCapacityIncreaseFactor); |
| + EXPECT_EQ(0, buffer->GetSize()); |
| + |
| + // Increase capacity until it fails. |
| + while (buffer->IncreaseCapacity()); |
| + EXPECT_FALSE(buffer->IncreaseCapacity()); |
| + EXPECT_EQ(kDefaultCapacityLimit + 0, buffer->capacity_limit()); |
| + EXPECT_EQ(kDefaultCapacityLimit + 0, buffer->GetCapacity()); |
| + |
| + // Enlarge capacity limit. |
| + buffer->set_capacity_limit(buffer->capacity_limit() * 2); |
| + EXPECT_TRUE(buffer->IncreaseCapacity()); |
| + EXPECT_EQ(kDefaultCapacityLimit * kCapacityIncreaseFactor, |
| + buffer->GetCapacity()); |
| + |
| + // Shrink capacity limit. It doesn't change capacity itself. |
| + buffer->set_capacity_limit(kDefaultCapacityLimit / 2); |
| + EXPECT_FALSE(buffer->IncreaseCapacity()); |
| + EXPECT_EQ(kDefaultCapacityLimit * kCapacityIncreaseFactor, |
| + buffer->GetCapacity()); |
| +} |
| + |
| +TEST_F(HttpConnectionTest, ReadIOBuffer_DidRead_DidConsume) { |
| + scoped_refptr<HttpConnection::ReadIOBuffer> buffer( |
| + new HttpConnection::ReadIOBuffer); |
| + const char* start_of_buffer = buffer->StartOfBuffer(); |
| + EXPECT_EQ(start_of_buffer, buffer->data()); |
| + |
| + // Read data. |
| + const int kReadLength = 128; |
| + const std::string kReadData(GetTestString(kReadLength)); |
| + memcpy(buffer->data(), kReadData.data(), kReadLength); |
| + buffer->DidRead(kReadLength); |
| + // No change in total capacity. |
| + EXPECT_EQ(kInitialBufSize + 0, buffer->GetCapacity()); |
| + // Change in unused capacity because of read data. |
| + EXPECT_EQ(kInitialBufSize - kReadLength, buffer->RemainingCapacity()); |
| + EXPECT_EQ(kReadLength, buffer->GetSize()); |
| + // No change in start pointers of read data. |
| + EXPECT_EQ(start_of_buffer, buffer->StartOfBuffer()); |
| + // Change in start pointer of unused buffer. |
| + EXPECT_EQ(start_of_buffer + kReadLength, buffer->data()); |
| + // Test read data. |
| + EXPECT_EQ(kReadData, std::string(buffer->StartOfBuffer(), buffer->GetSize())); |
| + |
| + // Consume data partially. |
| + const int kConsumedLength = 32; |
| + ASSERT_LT(kConsumedLength, kReadLength); |
| + buffer->DidConsume(kConsumedLength); |
| + // Capacity reduced because read data was too small comparing to capacity. |
| + EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor, buffer->GetCapacity()); |
| + // Change in unused capacity because of read data. |
| + EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor |
| + - kReadLength + kConsumedLength, |
| + buffer->RemainingCapacity()); |
| + // Change in read size. |
| + EXPECT_EQ(kReadLength - kConsumedLength, buffer->GetSize()); |
| + // Start data could be changed even when capacity is reduced. |
| + start_of_buffer = buffer->StartOfBuffer(); |
| + // Change in start pointer of unused buffer. |
| + EXPECT_EQ(start_of_buffer + kReadLength - kConsumedLength, buffer->data()); |
| + // Change in read data. |
| + EXPECT_EQ(kReadData.substr(kConsumedLength), |
| + std::string(buffer->StartOfBuffer(), buffer->GetSize())); |
| + |
| + // Read more data. |
| + const int kReadLength2 = 64; |
| + buffer->DidRead(kReadLength2); |
| + // No change in total capacity. |
| + EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor, buffer->GetCapacity()); |
| + // Change in unused capacity because of read data. |
| + EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor |
| + - kReadLength + kConsumedLength - kReadLength2, |
| + buffer->RemainingCapacity()); |
| + // Change in read size |
| + EXPECT_EQ(kReadLength - kConsumedLength + kReadLength2, buffer->GetSize()); |
| + // No change in start pointer of read part. |
| + EXPECT_EQ(start_of_buffer, buffer->StartOfBuffer()); |
| + // Change in start pointer of unused buffer. |
| + EXPECT_EQ(start_of_buffer + kReadLength - kConsumedLength + kReadLength2, |
| + buffer->data()); |
| + |
| + // Consume data fully. |
| + buffer->DidConsume(kReadLength - kConsumedLength + kReadLength2); |
| + // Capacity reduced again because read data was too small. |
| + EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor / kCapacityIncreaseFactor, |
| + buffer->GetCapacity()); |
| + EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor / kCapacityIncreaseFactor, |
| + buffer->RemainingCapacity()); |
| + // All reverts to initial because no data is left. |
| + EXPECT_EQ(0, buffer->GetSize()); |
| + // Start data could be changed even when capacity is reduced. |
| + start_of_buffer = buffer->StartOfBuffer(); |
| + EXPECT_EQ(start_of_buffer, buffer->data()); |
| +} |
| + |
| +TEST_F(HttpConnectionTest, PendingWriteIOBuffer_Append_DidConsume) { |
| + scoped_refptr<HttpConnection::PendingWriteIOBuffer> buffer( |
| + new HttpConnection::PendingWriteIOBuffer()); |
| + EXPECT_TRUE(buffer->IsEmpty()); |
| + EXPECT_EQ(0, buffer->GetSizeToWrite()); |
| + EXPECT_EQ(0, buffer->total_size()); |
| + EXPECT_TRUE(GetPendingData(buffer).empty()); |
| + |
| + const std::string kData("data to write"); |
| + EXPECT_TRUE(buffer->Append(kData)); |
| + EXPECT_FALSE(buffer->IsEmpty()); |
| + EXPECT_EQ(static_cast<int>(kData.size()), buffer->GetSizeToWrite()); |
| + EXPECT_EQ(static_cast<int>(kData.size()), buffer->total_size()); |
| + EXPECT_EQ(1U, GetPendingData(buffer).size()); |
| + |
| + const std::string kData2("more data to write"); |
| + EXPECT_TRUE(buffer->Append(kData2)); |
| + EXPECT_FALSE(buffer->IsEmpty()); |
| + // No change in size to write. |
| + EXPECT_EQ(static_cast<int>(kData.size()), buffer->GetSizeToWrite()); |
| + // Change in total size. |
| + EXPECT_EQ(static_cast<int>(kData.size() + kData2.size()), |
| + buffer->total_size()); |
| + EXPECT_EQ(2U, GetPendingData(buffer).size()); |
| + |
| + // Consume data partially. |
| + const int kConsumedLength = kData.length() - 1; |
| + buffer->DidConsume(kConsumedLength); |
| + EXPECT_FALSE(buffer->IsEmpty()); |
| + // Change in size to write. |
| + EXPECT_EQ(static_cast<int>(kData.size()) - kConsumedLength, |
| + buffer->GetSizeToWrite()); |
| + // Change in total size. |
| + EXPECT_EQ(static_cast<int>(kData.size() + kData2.size()) - kConsumedLength, |
| + buffer->total_size()); |
| + EXPECT_EQ(2U, GetPendingData(buffer).size()); |
| + |
| + // Consume first data fully. |
| + buffer->DidConsume(kData.size() - kConsumedLength); |
| + EXPECT_FALSE(buffer->IsEmpty()); |
| + // Now, size to write is size of data added second. |
| + EXPECT_EQ(static_cast<int>(kData2.size()), buffer->GetSizeToWrite()); |
| + // Change in total size. |
| + EXPECT_EQ(static_cast<int>(kData2.size()), buffer->total_size()); |
| + EXPECT_EQ(1U, GetPendingData(buffer).size()); |
| + |
| + // Consume second data fully. |
| + buffer->DidConsume(kData2.size()); |
| + EXPECT_TRUE(buffer->IsEmpty()); |
| + EXPECT_EQ(0, buffer->GetSizeToWrite()); |
| + EXPECT_EQ(0, buffer->total_size()); |
| + EXPECT_TRUE(GetPendingData(buffer).empty()); |
| +} |
| + |
| +TEST_F(HttpConnectionTest, PendingWriteIOBuffer_TotalSizeLimit) { |
| + scoped_refptr<HttpConnection::PendingWriteIOBuffer> buffer( |
| + new HttpConnection::PendingWriteIOBuffer()); |
| + EXPECT_EQ(kDefaultTotalSizeLimit + 0, buffer->total_size_limit()); |
| + |
| + // Set total size limit very small. |
| + buffer->set_total_size_limit(10); |
| + |
| + const int kDataLength = 4; |
| + const std::string kData(kDataLength, 'd'); |
| + EXPECT_TRUE(buffer->Append(kData)); |
| + EXPECT_EQ(kDataLength, buffer->total_size()); |
| + EXPECT_TRUE(buffer->Append(kData)); |
| + EXPECT_EQ(kDataLength * 2, buffer->total_size()); |
| + |
| + // Cannot append more data because it exceeds the limit. |
| + EXPECT_FALSE(buffer->Append(kData)); |
| + EXPECT_EQ(kDataLength * 2, buffer->total_size()); |
| + |
| + // Consume data partially. |
| + const int kConsumedLength = 2; |
| + buffer->DidConsume(kConsumedLength); |
| + EXPECT_EQ(kDataLength * 2 - kConsumedLength, buffer->total_size()); |
| + |
| + // Can add more data. |
| + EXPECT_TRUE(buffer->Append(kData)); |
| + EXPECT_EQ(kDataLength * 3 - kConsumedLength, buffer->total_size()); |
| + |
| + // Cannot append more data because it exceeds the limit. |
| + EXPECT_FALSE(buffer->Append(kData)); |
| + EXPECT_EQ(kDataLength * 3 - kConsumedLength, buffer->total_size()); |
| + |
| + // Enlarge limit. |
| + buffer->set_total_size_limit(20); |
| + // Can add more data. |
| + EXPECT_TRUE(buffer->Append(kData)); |
| + EXPECT_EQ(kDataLength * 4 - kConsumedLength, buffer->total_size()); |
| +} |
| + |
| +} // namespace net |