1 // Copyright 2014 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.
5 #include "net/server/web_socket_encoder.h"
6 #include "testing/gtest/include/gtest/gtest.h"
10 class WebSocketEncoderTest
: public testing::Test
{
12 WebSocketEncoderTest() {}
14 void SetUp() override
{
15 std::string response_extensions
;
16 server_
.reset(WebSocketEncoder::CreateServer("", &response_extensions
));
17 EXPECT_EQ(std::string(), response_extensions
);
18 client_
.reset(WebSocketEncoder::CreateClient(""));
22 scoped_ptr
<WebSocketEncoder
> server_
;
23 scoped_ptr
<WebSocketEncoder
> client_
;
26 class WebSocketEncoderCompressionTest
: public WebSocketEncoderTest
{
28 WebSocketEncoderCompressionTest() : WebSocketEncoderTest() {}
30 void SetUp() override
{
31 std::string response_extensions
;
32 server_
.reset(WebSocketEncoder::CreateServer(
33 "permessage-deflate; client_max_window_bits", &response_extensions
));
35 "permessage-deflate; server_max_window_bits=15; "
36 "client_max_window_bits=15",
38 client_
.reset(WebSocketEncoder::CreateClient(response_extensions
));
42 TEST_F(WebSocketEncoderTest
, ClientToServer
) {
43 std::string
frame("ClientToServer");
49 client_
->EncodeFrame(frame
, mask
, &encoded
);
50 EXPECT_EQ(WebSocket::FRAME_OK
,
51 server_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
52 EXPECT_EQ(frame
, decoded
);
53 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
55 std::string partial
= encoded
.substr(0, encoded
.length() - 2);
56 EXPECT_EQ(WebSocket::FRAME_INCOMPLETE
,
57 server_
->DecodeFrame(partial
, &bytes_consumed
, &decoded
));
59 std::string extra
= encoded
+ "more stuff";
60 EXPECT_EQ(WebSocket::FRAME_OK
,
61 server_
->DecodeFrame(extra
, &bytes_consumed
, &decoded
));
62 EXPECT_EQ(frame
, decoded
);
63 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
66 WebSocket::FRAME_ERROR
,
67 server_
->DecodeFrame(std::string("abcde"), &bytes_consumed
, &decoded
));
70 TEST_F(WebSocketEncoderTest
, ServerToClient
) {
71 std::string
frame("ServerToClient");
77 server_
->EncodeFrame(frame
, mask
, &encoded
);
78 EXPECT_EQ(WebSocket::FRAME_OK
,
79 client_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
80 EXPECT_EQ(frame
, decoded
);
81 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
83 std::string partial
= encoded
.substr(0, encoded
.length() - 2);
84 EXPECT_EQ(WebSocket::FRAME_INCOMPLETE
,
85 client_
->DecodeFrame(partial
, &bytes_consumed
, &decoded
));
87 std::string extra
= encoded
+ "more stuff";
88 EXPECT_EQ(WebSocket::FRAME_OK
,
89 client_
->DecodeFrame(extra
, &bytes_consumed
, &decoded
));
90 EXPECT_EQ(frame
, decoded
);
91 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
94 WebSocket::FRAME_ERROR
,
95 client_
->DecodeFrame(std::string("abcde"), &bytes_consumed
, &decoded
));
98 TEST_F(WebSocketEncoderCompressionTest
, ClientToServer
) {
99 std::string
frame("CompressionCompressionCompressionCompression");
105 client_
->EncodeFrame(frame
, mask
, &encoded
);
106 EXPECT_LT(encoded
.length(), frame
.length());
107 EXPECT_EQ(WebSocket::FRAME_OK
,
108 server_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
109 EXPECT_EQ(frame
, decoded
);
110 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
113 TEST_F(WebSocketEncoderCompressionTest
, ServerToClient
) {
114 std::string
frame("CompressionCompressionCompressionCompression");
120 server_
->EncodeFrame(frame
, mask
, &encoded
);
121 EXPECT_LT(encoded
.length(), frame
.length());
122 EXPECT_EQ(WebSocket::FRAME_OK
,
123 client_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
124 EXPECT_EQ(frame
, decoded
);
125 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
128 TEST_F(WebSocketEncoderCompressionTest
, LongFrame
) {
129 int length
= 1000000;
131 temp
.reserve(length
);
132 for (int i
= 0; i
< length
; ++i
)
133 temp
+= (char)('a' + (i
% 26));
136 frame
.reserve(length
);
137 for (int i
= 0; i
< length
; ++i
) {
139 frame
+= temp
.data()[(j
* j
) % length
];
147 server_
->EncodeFrame(frame
, mask
, &encoded
);
148 EXPECT_LT(encoded
.length(), frame
.length());
149 EXPECT_EQ(WebSocket::FRAME_OK
,
150 client_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
151 EXPECT_EQ(frame
, decoded
);
152 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);