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..772cd099ddfb20fce985a336bdf6cfc3ba624ad7 |
--- /dev/null |
+++ b/net/websockets/websocket_deflater_test.cc |
@@ -0,0 +1,99 @@ |
+// 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); |
+ std::string expected("\x02\x00", 2); |
+ |
+ scoped_refptr<IOBufferWithSize> actual = deflater.Finish(); |
+ ASSERT_TRUE(actual); |
+ EXPECT_EQ(expected, std::string(actual->data(), actual->size())); |
Adam Rice
2013/08/29 12:04:33
My preference would be to construct the expected s
yhirano
2013/08/30 06:20:35
Done.
|
+} |
+ |
+TEST(WebSocketDeflaterTest, DeflateHelloTakeOverContext) { |
+ WebSocketDeflater deflater(15, WebSocketDeflater::TAKE_OVER_CONTEXT); |
+ scoped_refptr<IOBufferWithSize> actual1, actual2; |
+ std::string expected1("\xf2\x48\xcd\xc9\xc9\x07\x00", 7); |
+ std::string expected2("\xf2\x00\x11\x00\x00", 5); |
+ |
+ ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
+ actual1 = deflater.Finish(); |
+ ASSERT_TRUE(actual1); |
+ EXPECT_EQ(expected1, std::string(actual1->data(), actual1->size())); |
+ |
+ ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
+ actual2 = deflater.Finish(); |
+ ASSERT_TRUE(actual2); |
+ EXPECT_EQ(expected2, std::string(actual2->data(), actual2->size())); |
+} |
+ |
+TEST(WebSocketDeflaterTest, DeflateHelloDoNotTakeOverContext) { |
+ WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
+ scoped_refptr<IOBufferWithSize> actual1, actual2; |
+ std::string expected1("\xf2\x48\xcd\xc9\xc9\x07\x00", 7); |
+ std::string expected2("\xf2\x48\xcd\xc9\xc9\x07\x00", 7); |
+ |
+ ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
+ actual1 = deflater.Finish(); |
+ ASSERT_TRUE(actual1); |
+ EXPECT_EQ(expected1, std::string(actual1->data(), actual1->size())); |
+ |
+ ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
+ actual2 = deflater.Finish(); |
+ ASSERT_TRUE(actual2); |
+ EXPECT_EQ(expected2, std::string(actual2->data(), actual2->size())); |
+} |
+ |
+TEST(WebSocketDeflaterTest, MultipleAddBytesCalls) { |
+ WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
+ std::vector<char> input(32, 'a'); |
+ scoped_refptr<IOBufferWithSize> actual; |
+ std::string expected("\x4a\x4c\xc4\x0f\x00\x00", 6); |
+ |
+ for (size_t i = 0; i < input.size(); ++i) { |
+ ASSERT_TRUE(deflater.AddBytes(&input[i], 1)); |
+ } |
+ actual = deflater.Finish(); |
+ ASSERT_TRUE(actual); |
+ EXPECT_EQ(expected, std::string(actual->data(), actual->size())); |
+} |
+ |
+TEST(WebSocketDeflaterTest, WindowBits8And15) { |
Adam Rice
2013/08/29 12:04:33
If it was me I would split this into two tests, "W
yhirano
2013/08/30 06:20:35
Done.
|
+ WebSocketDeflater deflater8(8, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
+ WebSocketDeflater deflater15(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
+ std::vector<char> input(1024 + 64 * 2, 'a'); |
+ // Modify the head and tail of |input| so that back-reference |
+ // can be used if the window size is sufficiently-large. |
+ for (size_t j = 0; j < 64; ++j) { |
+ input[j] = 'b'; |
+ input[input.size() - j - 1] = 'b'; |
+ } |
+ scoped_refptr<IOBufferWithSize> actual8; |
+ scoped_refptr<IOBufferWithSize> actual15; |
+ std::string expected8("JJ\xa2\x0c$\x8e\x82Q0\nF,H\xa2\x10\x00\x00", 17); |
+ std::string expected15("JJ\xa2\x0c$\x8e\x82Q0\nF,\xa0\xb0\xf8H\x02\x00", 18); |
+ |
+ ASSERT_TRUE(deflater8.AddBytes(input.data(), input.size())); |
+ ASSERT_TRUE(deflater15.AddBytes(input.data(), input.size())); |
+ actual8 = deflater8.Finish(); |
+ actual15 = deflater15.Finish(); |
+ ASSERT_TRUE(actual8); |
+ ASSERT_TRUE(actual15); |
+ EXPECT_EQ(expected8, std::string(actual8->data(), actual8->size())); |
+ EXPECT_EQ(expected15, std::string(actual15->data(), actual15->size())); |
+} |
+ |
+} // namespace net |