summaryrefslogtreecommitdiffstats
path: root/net/websockets/websocket_handshake_draft75_unittest.cc
blob: aff75addb08749913949c6746850f1c3b6091171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>
#include <vector>

#include "base/scoped_ptr.h"
#include "net/websockets/websocket_handshake_draft75.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/platform_test.h"

namespace net {

TEST(WebSocketHandshakeDraft75Test, Connect) {
  const std::string kExpectedClientHandshakeMessage =
      "GET /demo HTTP/1.1\r\n"
      "Upgrade: WebSocket\r\n"
      "Connection: Upgrade\r\n"
      "Host: example.com\r\n"
      "Origin: http://example.com\r\n"
      "WebSocket-Protocol: sample\r\n"
      "\r\n";

  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("ws://example.com/demo"),
                                    "http://example.com",
                                    "ws://example.com/demo",
                                    "sample"));
  EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode());
  EXPECT_EQ(kExpectedClientHandshakeMessage,
            handshake->CreateClientHandshakeMessage());

  const char kResponse[] = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
      "Upgrade: WebSocket\r\n"
      "Connection: Upgrade\r\n"
      "WebSocket-Origin: http://example.com\r\n"
      "WebSocket-Location: ws://example.com/demo\r\n"
      "WebSocket-Protocol: sample\r\n"
      "\r\n";

  EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode());
  // too short
  EXPECT_EQ(-1, handshake->ReadServerHandshake(kResponse, 16));
  EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode());
  // only status line
  EXPECT_EQ(-1, handshake->ReadServerHandshake(
      kResponse,
      WebSocketHandshakeDraft75::kServerHandshakeHeaderLength));
  EXPECT_EQ(WebSocketHandshake::MODE_NORMAL, handshake->mode());
  // by upgrade header
  EXPECT_EQ(-1, handshake->ReadServerHandshake(
      kResponse,
      WebSocketHandshakeDraft75::kServerHandshakeHeaderLength +
      WebSocketHandshakeDraft75::kUpgradeHeaderLength));
  EXPECT_EQ(WebSocketHandshake::MODE_NORMAL, handshake->mode());
  // by connection header
  EXPECT_EQ(-1, handshake->ReadServerHandshake(
      kResponse,
      WebSocketHandshakeDraft75::kServerHandshakeHeaderLength +
      WebSocketHandshakeDraft75::kUpgradeHeaderLength +
      WebSocketHandshakeDraft75::kConnectionHeaderLength));
  EXPECT_EQ(WebSocketHandshake::MODE_NORMAL, handshake->mode());

  EXPECT_EQ(-1, handshake->ReadServerHandshake(
      kResponse, sizeof(kResponse) - 2));
  EXPECT_EQ(WebSocketHandshake::MODE_NORMAL, handshake->mode());

  int handshake_length = strlen(kResponse);
  EXPECT_EQ(handshake_length, handshake->ReadServerHandshake(
      kResponse, sizeof(kResponse) - 1));  // -1 for terminating \0
  EXPECT_EQ(WebSocketHandshake::MODE_CONNECTED, handshake->mode());
}

TEST(WebSocketHandshakeDraft75Test, ServerSentData) {
  const std::string kExpectedClientHandshakeMessage =
      "GET /demo HTTP/1.1\r\n"
      "Upgrade: WebSocket\r\n"
      "Connection: Upgrade\r\n"
      "Host: example.com\r\n"
      "Origin: http://example.com\r\n"
      "WebSocket-Protocol: sample\r\n"
      "\r\n";
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("ws://example.com/demo"),
                                    "http://example.com",
                                    "ws://example.com/demo",
                                    "sample"));
  EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode());
  EXPECT_EQ(kExpectedClientHandshakeMessage,
            handshake->CreateClientHandshakeMessage());

  const char kResponse[] ="HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
      "Upgrade: WebSocket\r\n"
      "Connection: Upgrade\r\n"
      "WebSocket-Origin: http://example.com\r\n"
      "WebSocket-Location: ws://example.com/demo\r\n"
      "WebSocket-Protocol: sample\r\n"
      "\r\n"
      "\0Hello\xff";

  int handshake_length = strlen(kResponse);
  EXPECT_EQ(handshake_length, handshake->ReadServerHandshake(
      kResponse, sizeof(kResponse) - 1));  // -1 for terminating \0
  EXPECT_EQ(WebSocketHandshake::MODE_CONNECTED, handshake->mode());
}

TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_Simple) {
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("ws://example.com/demo"),
                                    "http://example.com",
                                    "ws://example.com/demo",
                                    "sample"));
  EXPECT_EQ("GET /demo HTTP/1.1\r\n"
            "Upgrade: WebSocket\r\n"
            "Connection: Upgrade\r\n"
            "Host: example.com\r\n"
            "Origin: http://example.com\r\n"
            "WebSocket-Protocol: sample\r\n"
            "\r\n",
            handshake->CreateClientHandshakeMessage());
}

TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_PathAndQuery) {
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("ws://example.com/Test?q=xxx&p=%20"),
                                    "http://example.com",
                                    "ws://example.com/demo",
                                    "sample"));
  // Path and query should be preserved as-is.
  EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
              testing::HasSubstr("GET /Test?q=xxx&p=%20 HTTP/1.1\r\n"));
}

TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_Host) {
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("ws://Example.Com/demo"),
                                    "http://Example.Com",
                                    "ws://Example.Com/demo",
                                    "sample"));
  // Host should be lowercased
  EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
              testing::HasSubstr("Host: example.com\r\n"));
  EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
              testing::HasSubstr("Origin: http://example.com\r\n"));
}

TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_TrimPort80) {
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("ws://example.com:80/demo"),
                                    "http://example.com",
                                    "ws://example.com/demo",
                                    "sample"));
  // :80 should be trimmed as it's the default port for ws://.
  EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
              testing::HasSubstr("Host: example.com\r\n"));
}

TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_TrimPort443) {
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("wss://example.com:443/demo"),
                                    "http://example.com",
                                    "wss://example.com/demo",
                                    "sample"));
  // :443 should be trimmed as it's the default port for wss://.
  EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
              testing::HasSubstr("Host: example.com\r\n"));
}

TEST(WebSocketHandshakeDraft75Test,
     CreateClientHandshakeMessage_NonDefaultPortForWs) {
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("ws://example.com:8080/demo"),
                                    "http://example.com",
                                    "wss://example.com/demo",
                                    "sample"));
  // :8080 should be preserved as it's not the default port for ws://.
  EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
              testing::HasSubstr("Host: example.com:8080\r\n"));
}

TEST(WebSocketHandshakeDraft75Test,
     CreateClientHandshakeMessage_NonDefaultPortForWss) {
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("wss://example.com:4443/demo"),
                                    "http://example.com",
                                    "wss://example.com/demo",
                                    "sample"));
  // :4443 should be preserved as it's not the default port for wss://.
  EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
              testing::HasSubstr("Host: example.com:4443\r\n"));
}

TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_WsBut443) {
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("ws://example.com:443/demo"),
                                    "http://example.com",
                                    "ws://example.com/demo",
                                    "sample"));
  // :443 should be preserved as it's not the default port for ws://.
  EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
              testing::HasSubstr("Host: example.com:443\r\n"));
}

TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_WssBut80) {
  scoped_ptr<WebSocketHandshakeDraft75> handshake(
      new WebSocketHandshakeDraft75(GURL("wss://example.com:80/demo"),
                                    "http://example.com",
                                    "wss://example.com/demo",
                                    "sample"));
  // :80 should be preserved as it's not the default port for wss://.
  EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
              testing::HasSubstr("Host: example.com:80\r\n"));
}

}  // namespace net