OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/websockets/websocket_deflater.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "net/base/io_buffer.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 namespace { |
| 16 |
| 17 std::string ToString(IOBufferWithSize* buffer) { |
| 18 return std::string(buffer->data(), buffer->size()); |
| 19 } |
| 20 |
| 21 TEST(WebSocketDeflaterTest, Construct) { |
| 22 WebSocketDeflater deflater(8, WebSocketDeflater::TAKE_OVER_CONTEXT); |
| 23 |
| 24 scoped_refptr<IOBufferWithSize> actual = deflater.Finish(); |
| 25 ASSERT_TRUE(actual); |
| 26 EXPECT_EQ(std::string("\x02\00", 2), ToString(actual.get())); |
| 27 } |
| 28 |
| 29 TEST(WebSocketDeflaterTest, DeflateHelloTakeOverContext) { |
| 30 WebSocketDeflater deflater(15, WebSocketDeflater::TAKE_OVER_CONTEXT); |
| 31 scoped_refptr<IOBufferWithSize> actual1, actual2; |
| 32 |
| 33 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
| 34 actual1 = deflater.Finish(); |
| 35 ASSERT_TRUE(actual1); |
| 36 EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), |
| 37 ToString(actual1.get())); |
| 38 |
| 39 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
| 40 actual2 = deflater.Finish(); |
| 41 ASSERT_TRUE(actual2); |
| 42 EXPECT_EQ(std::string("\xf2\x00\x11\x00\x00", 5), ToString(actual2.get())); |
| 43 } |
| 44 |
| 45 TEST(WebSocketDeflaterTest, DeflateHelloDoNotTakeOverContext) { |
| 46 WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
| 47 scoped_refptr<IOBufferWithSize> actual1, actual2; |
| 48 |
| 49 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
| 50 actual1 = deflater.Finish(); |
| 51 ASSERT_TRUE(actual1); |
| 52 EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), |
| 53 ToString(actual1.get())); |
| 54 |
| 55 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); |
| 56 actual2 = deflater.Finish(); |
| 57 ASSERT_TRUE(actual2); |
| 58 EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), |
| 59 ToString(actual2.get())); |
| 60 } |
| 61 |
| 62 TEST(WebSocketDeflaterTest, MultipleAddBytesCalls) { |
| 63 WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
| 64 std::string input(32, 'a'); |
| 65 scoped_refptr<IOBufferWithSize> actual; |
| 66 |
| 67 for (size_t i = 0; i < input.size(); ++i) { |
| 68 ASSERT_TRUE(deflater.AddBytes(&input[i], 1)); |
| 69 } |
| 70 actual = deflater.Finish(); |
| 71 ASSERT_TRUE(actual); |
| 72 EXPECT_EQ(std::string("\x4a\x4c\xc4\x0f\x00\x00", 6), ToString(actual.get())); |
| 73 } |
| 74 |
| 75 TEST(WebSocketDeflaterTest, WindowBits8) { |
| 76 WebSocketDeflater deflater(8, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
| 77 // Set the head and tail of |input| so that back-reference |
| 78 // can be used if the window size is sufficiently-large. |
| 79 const std::string word = "Chromium"; |
| 80 std::string input = word + std::string(256, 'a') + word; |
| 81 scoped_refptr<IOBufferWithSize> actual; |
| 82 |
| 83 ASSERT_TRUE(deflater.AddBytes(input.data(), input.size())); |
| 84 actual = deflater.Finish(); |
| 85 ASSERT_TRUE(actual); |
| 86 EXPECT_EQ(std::string("r\xce(\xca\xcf\xcd,\xcdM\x1c\xe1\xc0\x39\xa3" |
| 87 "(?7\xb3\x34\x17\x00", 21), |
| 88 ToString(actual.get())); |
| 89 } |
| 90 |
| 91 TEST(WebSocketDeflaterTest, WindowBits10) { |
| 92 WebSocketDeflater deflater(10, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); |
| 93 // Set the head and tail of |input| so that back-reference |
| 94 // can be used if the window size is sufficiently-large. |
| 95 const std::string word = "Chromium"; |
| 96 std::string input = word + std::string(256, 'a') + word; |
| 97 scoped_refptr<IOBufferWithSize> actual; |
| 98 |
| 99 ASSERT_TRUE(deflater.AddBytes(input.data(), input.size())); |
| 100 actual = deflater.Finish(); |
| 101 ASSERT_TRUE(actual); |
| 102 EXPECT_EQ( |
| 103 std::string("r\xce(\xca\xcf\xcd,\xcdM\x1c\xe1\xc0\x19\x1a\x0e\0\0", 17), |
| 104 ToString(actual.get())); |
| 105 } |
| 106 |
| 107 } // namespace |
| 108 |
| 109 } // namespace net |
OLD | NEW |