Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / net / server / web_socket_encoder_unittest.cc
blobb52939a22e46be1efbc01e49ccceaa0d9e1b728f
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 class WebSocketEncoderTest : public testing::Test {
11 public:
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(""));
21 protected:
22 scoped_ptr<WebSocketEncoder> server_;
23 scoped_ptr<WebSocketEncoder> client_;
26 class WebSocketEncoderCompressionTest : public WebSocketEncoderTest {
27 public:
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));
34 EXPECT_EQ(
35 "permessage-deflate; server_max_window_bits=15; "
36 "client_max_window_bits=15",
37 response_extensions);
38 client_.reset(WebSocketEncoder::CreateClient(response_extensions));
42 TEST_F(WebSocketEncoderTest, ClientToServer) {
43 std::string frame("ClientToServer");
44 int mask = 123456;
45 std::string encoded;
46 int bytes_consumed;
47 std::string decoded;
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);
65 EXPECT_EQ(
66 WebSocket::FRAME_ERROR,
67 server_->DecodeFrame(std::string("abcde"), &bytes_consumed, &decoded));
70 TEST_F(WebSocketEncoderTest, ServerToClient) {
71 std::string frame("ServerToClient");
72 int mask = 0;
73 std::string encoded;
74 int bytes_consumed;
75 std::string decoded;
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);
93 EXPECT_EQ(
94 WebSocket::FRAME_ERROR,
95 client_->DecodeFrame(std::string("abcde"), &bytes_consumed, &decoded));
98 TEST_F(WebSocketEncoderCompressionTest, ClientToServer) {
99 std::string frame("CompressionCompressionCompressionCompression");
100 int mask = 654321;
101 std::string encoded;
102 int bytes_consumed;
103 std::string decoded;
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");
115 int mask = 0;
116 std::string encoded;
117 int bytes_consumed;
118 std::string decoded;
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;
130 std::string temp;
131 temp.reserve(length);
132 for (int i = 0; i < length; ++i)
133 temp += (char)('a' + (i % 26));
135 std::string frame;
136 frame.reserve(length);
137 for (int i = 0; i < length; ++i) {
138 int64 j = i;
139 frame += temp.data()[(j * j) % length];
142 int mask = 0;
143 std::string encoded;
144 int bytes_consumed;
145 std::string decoded;
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);
155 } // namespace net