Index: net/websockets/websocket_deflater_test.cc |
diff --git a/net/websockets/websocket_deflater_test.cc b/net/websockets/websocket_deflater_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6c9e3fcef2a535794fa1b846427f8ed0d67e6701 |
--- /dev/null |
+++ b/net/websockets/websocket_deflater_test.cc |
@@ -0,0 +1,104 @@ |
+// Copyright (c) 2013 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/websockets/websocket_deflater.h" |
+ |
+#include <string> |
+ |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "net/base/io_buffer.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace net { |
+ |
+TEST(WebSocketDeflaterTest, Construct) { |
+ WebSocketDeflater deflater(8, WebSocketDeflater::TAKE_OVER_CONTEXT); |
+ |
+ scoped_refptr<IOBufferWithSize> actual = deflater.Finish(); |
+ ASSERT_TRUE(actual); |
+ EXPECT_EQ(std::string("\x02\00", 2), |
+ std::string(actual->data(), actual->size())); |
+} |
+ |
+TEST(WebSocketDeflaterTest, DeflateHelloTakeOverContext) { |
+ WebSocketDeflater deflater(15, WebSocketDeflater::TAKE_OVER_CONTEXT); |
+ scoped_refptr<IOBufferWithSize> actual1, actual2; |
+ |
+ ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
+ actual1 = deflater.Finish(); |
+ ASSERT_TRUE(actual1); |
+ EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), |
+ std::string(actual1->data(), actual1->size())); |
+ |
+ ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
+ actual2 = deflater.Finish(); |
+ ASSERT_TRUE(actual2); |
+ EXPECT_EQ(std::string("\xf2\x00\x11\x00\x00", 5), |
+ std::string(actual2->data(), actual2->size())); |
+} |
+ |
+TEST(WebSocketDeflaterTest, DeflateHelloDoNotTakeOverContext) { |
+ WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
+ scoped_refptr<IOBufferWithSize> actual1, actual2; |
+ |
+ ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
+ actual1 = deflater.Finish(); |
+ ASSERT_TRUE(actual1); |
+ EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), |
+ std::string(actual1->data(), actual1->size())); |
+ |
+ ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
+ actual2 = deflater.Finish(); |
+ ASSERT_TRUE(actual2); |
+ EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), |
+ std::string(actual2->data(), actual2->size())); |
+} |
+ |
+TEST(WebSocketDeflaterTest, MultipleAddBytesCalls) { |
+ WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
+ std::string input(32, 'a'); |
+ scoped_refptr<IOBufferWithSize> actual; |
+ |
+ for (size_t i = 0; i < input.size(); ++i) { |
+ ASSERT_TRUE(deflater.AddBytes(&input[i], 1)); |
+ } |
+ actual = deflater.Finish(); |
+ ASSERT_TRUE(actual); |
+ EXPECT_EQ(std::string("\x4a\x4c\xc4\x0f\x00\x00", 6), |
+ std::string(actual->data(), actual->size())); |
+} |
+ |
+TEST(WebSocketDeflaterTest, WindowBits8) { |
+ WebSocketDeflater deflater8(8, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
+ // Set the head and tail of |input| so that back-reference |
+ // can be used if the window size is sufficiently-large. |
+ std::string input = |
+ std::string(64, 'b') + std::string(1024, 'a') + std::string(64, 'b'); |
+ scoped_refptr<IOBufferWithSize> actual8; |
+ |
+ ASSERT_TRUE(deflater8.AddBytes(input.data(), input.size())); |
+ actual8 = deflater8.Finish(); |
+ ASSERT_TRUE(actual8); |
+ EXPECT_EQ(std::string("JJ\xa2\x0c$\x8e\x82Q0\nF,H\xa2\x10\x00\x00", 17), |
+ std::string(actual8->data(), actual8->size())); |
+} |
+ |
+TEST(WebSocketDeflaterTest, WindowBits15) { |
+ WebSocketDeflater deflater15(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
+ // Set the head and tail of |input| so that back-reference |
+ // can be used if the window size is sufficiently-large. |
+ std::string input = |
+ std::string(64, 'b') + std::string(1024, 'a') + std::string(64, 'b'); |
+ scoped_refptr<IOBufferWithSize> actual15; |
+ |
+ ASSERT_TRUE(deflater15.AddBytes(input.data(), input.size())); |
+ actual15 = deflater15.Finish(); |
+ ASSERT_TRUE(actual15); |
+ EXPECT_EQ(std::string("JJ\xa2\x0c$\x8e\x82Q0\nF,\xa0\xb0\xf8H\x02\x00", 18), |
+ std::string(actual15->data(), actual15->size())); |
+} |
+ |
+} // namespace net |