Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / server / web_socket_encoder_unittest.cc
blob7bca8764753257c350357da3d0fa19458aeff9cf
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"
8 namespace net {
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",
18 response_extensions);
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));
26 EXPECT_EQ(
27 "permessage-deflate; server_max_window_bits=15"
28 "; server_no_context_takeover",
29 response_extensions);
32 class WebSocketEncoderTest : public testing::Test {
33 public:
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(""));
43 protected:
44 scoped_ptr<WebSocketEncoder> server_;
45 scoped_ptr<WebSocketEncoder> client_;
48 class WebSocketEncoderCompressionTest : public WebSocketEncoderTest {
49 public:
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));
56 EXPECT_EQ(
57 "permessage-deflate; server_max_window_bits=15; "
58 "client_max_window_bits=15",
59 response_extensions);
60 client_.reset(WebSocketEncoder::CreateClient(response_extensions));
64 TEST_F(WebSocketEncoderTest, ClientToServer) {
65 std::string frame("ClientToServer");
66 int mask = 123456;
67 std::string encoded;
68 int bytes_consumed;
69 std::string decoded;
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);
87 EXPECT_EQ(
88 WebSocket::FRAME_ERROR,
89 server_->DecodeFrame(std::string("abcde"), &bytes_consumed, &decoded));
92 TEST_F(WebSocketEncoderTest, ServerToClient) {
93 std::string frame("ServerToClient");
94 int mask = 0;
95 std::string encoded;
96 int bytes_consumed;
97 std::string decoded;
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);
115 EXPECT_EQ(
116 WebSocket::FRAME_ERROR,
117 client_->DecodeFrame(std::string("abcde"), &bytes_consumed, &decoded));
120 TEST_F(WebSocketEncoderCompressionTest, ClientToServer) {
121 std::string frame("CompressionCompressionCompressionCompression");
122 int mask = 654321;
123 std::string encoded;
124 int bytes_consumed;
125 std::string decoded;
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");
137 int mask = 0;
138 std::string encoded;
139 int bytes_consumed;
140 std::string decoded;
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;
152 std::string temp;
153 temp.reserve(length);
154 for (int i = 0; i < length; ++i)
155 temp += (char)('a' + (i % 26));
157 std::string frame;
158 frame.reserve(length);
159 for (int i = 0; i < length; ++i) {
160 int64 j = i;
161 frame += temp.data()[(j * j) % length];
164 int mask = 0;
165 std::string encoded;
166 int bytes_consumed;
167 std::string decoded;
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);
177 } // namespace net