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 TEST(WebSocketEncoderHandshakeTest
,
11 CreateServerWithoutClientMaxWindowBitsParameter
) {
12 std::string response_extensions
;
13 scoped_ptr
<WebSocketEncoder
> server(WebSocketEncoder::CreateServer(
14 "permessage-deflate", &response_extensions
));
15 // The response must not include client_max_window_bits if the client didn't
16 // declare that it accepts the parameter.
17 EXPECT_EQ("permessage-deflate; server_max_window_bits=15",
21 TEST(WebSocketEncoderHandshakeTest
,
22 CreateServerWithServerNoContextTakeoverParameter
) {
23 std::string response_extensions
;
24 scoped_ptr
<WebSocketEncoder
> server(WebSocketEncoder::CreateServer(
25 "permessage-deflate; server_no_context_takeover", &response_extensions
));
27 "permessage-deflate; server_max_window_bits=15"
28 "; server_no_context_takeover",
32 class WebSocketEncoderTest
: public testing::Test
{
34 WebSocketEncoderTest() {}
36 void SetUp() override
{
37 std::string response_extensions
;
38 server_
.reset(WebSocketEncoder::CreateServer("", &response_extensions
));
39 EXPECT_EQ(std::string(), response_extensions
);
40 client_
.reset(WebSocketEncoder::CreateClient(""));
44 scoped_ptr
<WebSocketEncoder
> server_
;
45 scoped_ptr
<WebSocketEncoder
> client_
;
48 class WebSocketEncoderCompressionTest
: public WebSocketEncoderTest
{
50 WebSocketEncoderCompressionTest() : WebSocketEncoderTest() {}
52 void SetUp() override
{
53 std::string response_extensions
;
54 server_
.reset(WebSocketEncoder::CreateServer(
55 "permessage-deflate; client_max_window_bits", &response_extensions
));
57 "permessage-deflate; server_max_window_bits=15; "
58 "client_max_window_bits=15",
60 client_
.reset(WebSocketEncoder::CreateClient(response_extensions
));
64 TEST_F(WebSocketEncoderTest
, ClientToServer
) {
65 std::string
frame("ClientToServer");
71 client_
->EncodeFrame(frame
, mask
, &encoded
);
72 EXPECT_EQ(WebSocket::FRAME_OK
,
73 server_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
74 EXPECT_EQ(frame
, decoded
);
75 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
77 std::string partial
= encoded
.substr(0, encoded
.length() - 2);
78 EXPECT_EQ(WebSocket::FRAME_INCOMPLETE
,
79 server_
->DecodeFrame(partial
, &bytes_consumed
, &decoded
));
81 std::string extra
= encoded
+ "more stuff";
82 EXPECT_EQ(WebSocket::FRAME_OK
,
83 server_
->DecodeFrame(extra
, &bytes_consumed
, &decoded
));
84 EXPECT_EQ(frame
, decoded
);
85 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
88 WebSocket::FRAME_ERROR
,
89 server_
->DecodeFrame(std::string("abcde"), &bytes_consumed
, &decoded
));
92 TEST_F(WebSocketEncoderTest
, ServerToClient
) {
93 std::string
frame("ServerToClient");
99 server_
->EncodeFrame(frame
, mask
, &encoded
);
100 EXPECT_EQ(WebSocket::FRAME_OK
,
101 client_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
102 EXPECT_EQ(frame
, decoded
);
103 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
105 std::string partial
= encoded
.substr(0, encoded
.length() - 2);
106 EXPECT_EQ(WebSocket::FRAME_INCOMPLETE
,
107 client_
->DecodeFrame(partial
, &bytes_consumed
, &decoded
));
109 std::string extra
= encoded
+ "more stuff";
110 EXPECT_EQ(WebSocket::FRAME_OK
,
111 client_
->DecodeFrame(extra
, &bytes_consumed
, &decoded
));
112 EXPECT_EQ(frame
, decoded
);
113 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
116 WebSocket::FRAME_ERROR
,
117 client_
->DecodeFrame(std::string("abcde"), &bytes_consumed
, &decoded
));
120 TEST_F(WebSocketEncoderCompressionTest
, ClientToServer
) {
121 std::string
frame("CompressionCompressionCompressionCompression");
127 client_
->EncodeFrame(frame
, mask
, &encoded
);
128 EXPECT_LT(encoded
.length(), frame
.length());
129 EXPECT_EQ(WebSocket::FRAME_OK
,
130 server_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
131 EXPECT_EQ(frame
, decoded
);
132 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
135 TEST_F(WebSocketEncoderCompressionTest
, ServerToClient
) {
136 std::string
frame("CompressionCompressionCompressionCompression");
142 server_
->EncodeFrame(frame
, mask
, &encoded
);
143 EXPECT_LT(encoded
.length(), frame
.length());
144 EXPECT_EQ(WebSocket::FRAME_OK
,
145 client_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
146 EXPECT_EQ(frame
, decoded
);
147 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);
150 TEST_F(WebSocketEncoderCompressionTest
, LongFrame
) {
151 int length
= 1000000;
153 temp
.reserve(length
);
154 for (int i
= 0; i
< length
; ++i
)
155 temp
+= (char)('a' + (i
% 26));
158 frame
.reserve(length
);
159 for (int i
= 0; i
< length
; ++i
) {
161 frame
+= temp
.data()[(j
* j
) % length
];
169 server_
->EncodeFrame(frame
, mask
, &encoded
);
170 EXPECT_LT(encoded
.length(), frame
.length());
171 EXPECT_EQ(WebSocket::FRAME_OK
,
172 client_
->DecodeFrame(encoded
, &bytes_consumed
, &decoded
));
173 EXPECT_EQ(frame
, decoded
);
174 EXPECT_EQ((int)encoded
.length(), bytes_consumed
);