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 "base/memory/scoped_ptr.h" | |
Adam Rice
2013/08/30 13:20:13
scoped_ptr.h and gmock.h are not used.
yhirano
2013/09/02 02:01:12
Done.
| |
11 #include "net/base/io_buffer.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace net { | |
16 | |
Adam Rice
2013/08/30 13:20:13
Nitpick: it would be better to add an anonymous na
yhirano
2013/09/02 02:01:12
Done.
| |
17 TEST(WebSocketDeflaterTest, Construct) { | |
18 WebSocketDeflater deflater(8, WebSocketDeflater::TAKE_OVER_CONTEXT); | |
19 | |
20 scoped_refptr<IOBufferWithSize> actual = deflater.Finish(); | |
21 ASSERT_TRUE(actual); | |
22 EXPECT_EQ(std::string("\x02\00", 2), | |
23 std::string(actual->data(), actual->size())); | |
Adam Rice
2013/08/30 13:20:13
This line is repeated so many times that I would p
yhirano
2013/09/02 02:01:12
Done.
| |
24 } | |
25 | |
26 TEST(WebSocketDeflaterTest, DeflateHelloTakeOverContext) { | |
27 WebSocketDeflater deflater(15, WebSocketDeflater::TAKE_OVER_CONTEXT); | |
28 scoped_refptr<IOBufferWithSize> actual1, actual2; | |
29 | |
30 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); | |
31 actual1 = deflater.Finish(); | |
32 ASSERT_TRUE(actual1); | |
33 EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), | |
34 std::string(actual1->data(), actual1->size())); | |
35 | |
36 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); | |
37 actual2 = deflater.Finish(); | |
38 ASSERT_TRUE(actual2); | |
39 EXPECT_EQ(std::string("\xf2\x00\x11\x00\x00", 5), | |
40 std::string(actual2->data(), actual2->size())); | |
41 } | |
42 | |
43 TEST(WebSocketDeflaterTest, DeflateHelloDoNotTakeOverContext) { | |
44 WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); | |
45 scoped_refptr<IOBufferWithSize> actual1, actual2; | |
46 | |
47 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); | |
48 actual1 = deflater.Finish(); | |
49 ASSERT_TRUE(actual1); | |
50 EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), | |
51 std::string(actual1->data(), actual1->size())); | |
52 | |
53 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); | |
54 actual2 = deflater.Finish(); | |
55 ASSERT_TRUE(actual2); | |
56 EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), | |
57 std::string(actual2->data(), actual2->size())); | |
58 } | |
59 | |
60 TEST(WebSocketDeflaterTest, MultipleAddBytesCalls) { | |
61 WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); | |
62 std::string input(32, 'a'); | |
63 scoped_refptr<IOBufferWithSize> actual; | |
64 | |
65 for (size_t i = 0; i < input.size(); ++i) { | |
66 ASSERT_TRUE(deflater.AddBytes(&input[i], 1)); | |
67 } | |
68 actual = deflater.Finish(); | |
69 ASSERT_TRUE(actual); | |
70 EXPECT_EQ(std::string("\x4a\x4c\xc4\x0f\x00\x00", 6), | |
71 std::string(actual->data(), actual->size())); | |
72 } | |
73 | |
74 TEST(WebSocketDeflaterTest, WindowBits8) { | |
75 WebSocketDeflater deflater8(8, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); | |
76 // Set the head and tail of |input| so that back-reference | |
77 // can be used if the window size is sufficiently-large. | |
78 std::string input = | |
79 std::string(64, 'b') + std::string(1024, 'a') + std::string(64, 'b'); | |
Adam Rice
2013/08/30 13:20:13
64 'b's is too easy to compress, so the back refer
yhirano
2013/09/02 02:01:12
Done.
| |
80 scoped_refptr<IOBufferWithSize> actual8; | |
81 | |
82 ASSERT_TRUE(deflater8.AddBytes(input.data(), input.size())); | |
83 actual8 = deflater8.Finish(); | |
84 ASSERT_TRUE(actual8); | |
85 EXPECT_EQ(std::string("JJ\xa2\x0c$\x8e\x82Q0\nF,H\xa2\x10\x00\x00", 17), | |
86 std::string(actual8->data(), actual8->size())); | |
87 } | |
88 | |
89 TEST(WebSocketDeflaterTest, WindowBits15) { | |
Adam Rice
2013/08/30 13:20:13
You only actually need 11 or 12 window bits to get
yhirano
2013/09/02 02:01:12
Done.
| |
90 WebSocketDeflater deflater15(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); | |
91 // Set the head and tail of |input| so that back-reference | |
92 // can be used if the window size is sufficiently-large. | |
93 std::string input = | |
94 std::string(64, 'b') + std::string(1024, 'a') + std::string(64, 'b'); | |
95 scoped_refptr<IOBufferWithSize> actual15; | |
96 | |
97 ASSERT_TRUE(deflater15.AddBytes(input.data(), input.size())); | |
98 actual15 = deflater15.Finish(); | |
99 ASSERT_TRUE(actual15); | |
100 EXPECT_EQ(std::string("JJ\xa2\x0c$\x8e\x82Q0\nF,\xa0\xb0\xf8H\x02\x00", 18), | |
101 std::string(actual15->data(), actual15->size())); | |
102 } | |
103 | |
104 } // namespace net | |
OLD | NEW |