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..fff7d8f4fb9a1c7f86980f07c28851d3f45d9fcc |
--- /dev/null |
+++ b/net/server/http_connection_unittest.cc |
@@ -0,0 +1,241 @@ |
+// 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 { |
+ |
+class HttpConnectionTest : public testing::Test { |
+ protected: |
+ // 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; |
+ |
+ static scoped_refptr<GrowableIOBuffer> GetBase( |
+ 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_; |
+ } |
+ |
+ void CheckCapacity(scoped_refptr<HttpConnection::ReadIOBuffer> buffer, |
+ int expected_capacity) { |
+ EXPECT_EQ(expected_capacity, buffer->GetCapacity()); |
+ EXPECT_EQ(expected_capacity, buffer->GetUnusedCapacity()); |
+ EXPECT_EQ(expected_capacity, GetBase(buffer)->capacity()); |
+ EXPECT_EQ(expected_capacity, GetBase(buffer)->RemainingCapacity()); |
+ } |
+}; |
+ |
+TEST_F(HttpConnectionTest, ReadIOBuffer_SetCapacity) { |
+ scoped_refptr<HttpConnection::ReadIOBuffer> buffer( |
+ new HttpConnection::ReadIOBuffer); |
+ CheckCapacity(buffer, kInitialBufSize); |
+ EXPECT_EQ(0, buffer->GetUnconsumedSize()); |
+ |
+ const int kNewCapacity = kInitialBufSize + 128; |
+ buffer->SetCapacity(kNewCapacity); |
+ CheckCapacity(buffer, kNewCapacity); |
+ EXPECT_EQ(0, buffer->GetUnconsumedSize()); |
+} |
+ |
+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->GetUnconsumedSize()); |
+ |
+ // Increases 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); |
+ // Start pointers of all 3 parts are same. |
+ const char* initial_data = buffer->data(); |
+ EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer()); |
+ EXPECT_EQ(initial_data, buffer->GetUnusedIOBuffer()->data()); |
+ |
+ // Reads data. |
+ const int read_length = 128; |
+ buffer->DidRead(read_length); |
+ // No change in total capacity. |
+ EXPECT_EQ(kInitialBufSize + 0, buffer->GetCapacity()); |
+ // Change in unused capacity because of read data. |
+ EXPECT_EQ(kInitialBufSize - read_length, buffer->GetUnusedCapacity()); |
+ EXPECT_EQ(read_length, buffer->GetUnconsumedSize()); |
+ // No change in start pointers of consumed and unconsumed part. |
+ EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer()); |
+ EXPECT_EQ(initial_data, buffer->data()); |
+ // Change in start pointer of unused part. |
+ EXPECT_EQ(initial_data + read_length, buffer->GetUnusedIOBuffer()->data()); |
+ |
+ // Consumes data partially. |
+ const int consumed_length = 32; |
+ ASSERT_LT(consumed_length, read_length); |
+ buffer->DidConsume(consumed_length); |
+ // No change in capacity. |
+ EXPECT_EQ(kInitialBufSize + 0, buffer->GetCapacity()); |
+ EXPECT_EQ(kInitialBufSize - read_length, buffer->GetUnusedCapacity()); |
+ // Change in unconsumed size. |
+ EXPECT_EQ(read_length - consumed_length, buffer->GetUnconsumedSize()); |
+ // No change in start pointer of consumed part. |
+ EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer()); |
+ // Change in start pointer of unconsumed part. |
+ EXPECT_EQ(initial_data + consumed_length, buffer->data()); |
+ // No change in start pointer of unused part. |
+ EXPECT_EQ(initial_data + read_length, buffer->GetUnusedIOBuffer()->data()); |
+ |
+ // Reads another data. |
+ const int read_length_2 = 64; |
+ buffer->DidRead(read_length_2); |
+ // No change in total capacity. |
+ EXPECT_EQ(kInitialBufSize + 0, buffer->GetCapacity()); |
+ // Change in unused capacity because of read data. |
+ EXPECT_EQ(kInitialBufSize - read_length - read_length_2, |
+ buffer->GetUnusedCapacity()); |
+ EXPECT_EQ(read_length - consumed_length + read_length_2, |
+ buffer->GetUnconsumedSize()); |
+ // No change in start pointers of consumed and unconsumed part. |
+ EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer()); |
+ EXPECT_EQ(initial_data + consumed_length, buffer->data()); |
+ // Change in start pointer of unused part. |
+ EXPECT_EQ(initial_data + read_length + read_length_2, |
+ buffer->GetUnusedIOBuffer()->data()); |
+ |
+ // Consumes data fully. |
+ buffer->DidConsume(read_length - consumed_length + read_length_2); |
+ // Capacity reduced because read data was too small comparing to capacity. |
+ EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor, buffer->GetCapacity()); |
+ EXPECT_EQ(kInitialBufSize / kCapacityIncreaseFactor, |
+ buffer->GetUnusedCapacity()); |
+ // All reverts to initial because no data is left. |
+ EXPECT_EQ(0, buffer->GetUnconsumedSize()); |
+ // Start data could be changed even when capacity is reduced. |
+ initial_data = buffer->data(); |
+ EXPECT_EQ(initial_data, GetBase(buffer)->StartOfBuffer()); |
+ EXPECT_EQ(initial_data, buffer->GetUnusedIOBuffer()->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 data("data to write"); |
+ EXPECT_TRUE(buffer->Append(data)); |
+ EXPECT_FALSE(buffer->IsEmpty()); |
+ EXPECT_EQ(static_cast<int>(data.size()), buffer->GetSizeToWrite()); |
+ EXPECT_EQ(static_cast<int>(data.size()), buffer->total_size()); |
+ EXPECT_EQ(1U, GetPendingData(buffer).size()); |
+ |
+ const std::string data_2("another data to write"); |
+ EXPECT_TRUE(buffer->Append(data_2)); |
+ EXPECT_FALSE(buffer->IsEmpty()); |
+ // No change in size to write. |
+ EXPECT_EQ(static_cast<int>(data.size()), buffer->GetSizeToWrite()); |
+ // Change in total size. |
+ EXPECT_EQ(static_cast<int>(data.size() + data_2.size()), |
+ buffer->total_size()); |
+ EXPECT_EQ(2U, GetPendingData(buffer).size()); |
+ |
+ // Consumes data partially. |
+ const int consumed_length = data.length() - 1; |
+ buffer->DidConsume(consumed_length); |
+ EXPECT_FALSE(buffer->IsEmpty()); |
+ // Change in size to write. |
+ EXPECT_EQ(static_cast<int>(data.size()) - consumed_length, |
+ buffer->GetSizeToWrite()); |
+ // Change in total size. |
+ EXPECT_EQ(static_cast<int>(data.size() + data_2.size()) - consumed_length, |
+ buffer->total_size()); |
+ EXPECT_EQ(2U, GetPendingData(buffer).size()); |
+ |
+ // Consumes first data fully. |
+ buffer->DidConsume(data.size() - consumed_length); |
+ EXPECT_FALSE(buffer->IsEmpty()); |
+ // Now, size to write is size of data added second. |
+ EXPECT_EQ(static_cast<int>(data_2.size()), buffer->GetSizeToWrite()); |
+ // Change in total size. |
+ EXPECT_EQ(static_cast<int>(data_2.size()), buffer->total_size()); |
+ EXPECT_EQ(1U, GetPendingData(buffer).size()); |
+ |
+ // Consumes second data fully. |
+ buffer->DidConsume(data_2.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()); |
+ |
+ // Sets total size limit very small. |
+ buffer->set_total_size_limit(10); |
+ |
+ const int data_length = 4; |
+ const std::string data(data_length, 'd'); |
+ EXPECT_TRUE(buffer->Append(data)); |
+ EXPECT_EQ(data_length, buffer->total_size()); |
+ EXPECT_TRUE(buffer->Append(data)); |
+ EXPECT_EQ(data_length * 2, buffer->total_size()); |
+ |
+ // Cannot append more data because it exceeds the limit. |
+ EXPECT_FALSE(buffer->Append(data)); |
+ EXPECT_EQ(data_length * 2, buffer->total_size()); |
+ |
+ // Consumes data partially. |
+ const int consumed_length = 2; |
+ buffer->DidConsume(consumed_length); |
+ EXPECT_EQ(data_length * 2 - consumed_length, buffer->total_size()); |
+ |
+ // Can add more data. |
+ EXPECT_TRUE(buffer->Append(data)); |
+ EXPECT_EQ(data_length * 3 - consumed_length, buffer->total_size()); |
+ |
+ // Cannot append more data because it exceeds the limit. |
+ EXPECT_FALSE(buffer->Append(data)); |
+ EXPECT_EQ(data_length * 3 - consumed_length, buffer->total_size()); |
+ |
+ // Enlarge limit. |
+ buffer->set_total_size_limit(20); |
+ // Can add more data. |
+ EXPECT_TRUE(buffer->Append(data)); |
+ EXPECT_EQ(data_length * 4 - consumed_length, buffer->total_size()); |
+} |
+ |
+} // namespace net |