OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/websockets/WebSocketChannel.h" | 5 #include "modules/websockets/WebSocketChannel.h" |
6 | 6 |
7 #include "core/dom/DOMArrayBuffer.h" | 7 #include "core/dom/DOMArrayBuffer.h" |
8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
9 #include "core/fileapi/Blob.h" | 9 #include "core/fileapi/Blob.h" |
10 #include "core/testing/DummyPageHolder.h" | 10 #include "core/testing/DummyPageHolder.h" |
11 #include "modules/websockets/DocumentWebSocketChannel.h" | 11 #include "modules/websockets/DocumentWebSocketChannel.h" |
12 #include "modules/websockets/WebSocketChannelClient.h" | 12 #include "modules/websockets/WebSocketChannelClient.h" |
| 13 #include "modules/websockets/WebSocketHandle.h" |
| 14 #include "modules/websockets/WebSocketHandleClient.h" |
13 #include "platform/heap/Handle.h" | 15 #include "platform/heap/Handle.h" |
14 #include "platform/weborigin/KURL.h" | 16 #include "platform/weborigin/KURL.h" |
15 #include "public/platform/WebSecurityOrigin.h" | |
16 #include "public/platform/WebString.h" | |
17 #include "public/platform/WebURL.h" | |
18 #include "public/platform/WebVector.h" | |
19 #include "public/platform/modules/websockets/WebSocketHandle.h" | |
20 #include "public/platform/modules/websockets/WebSocketHandleClient.h" | |
21 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "wtf/PtrUtil.h" | 19 #include "wtf/PtrUtil.h" |
24 #include "wtf/Vector.h" | 20 #include "wtf/Vector.h" |
25 #include "wtf/text/WTFString.h" | 21 #include "wtf/text/WTFString.h" |
26 #include <memory> | 22 #include <memory> |
27 #include <stdint.h> | 23 #include <stdint.h> |
28 | 24 |
29 using testing::_; | 25 using testing::_; |
30 using testing::InSequence; | 26 using testing::InSequence; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 public: | 69 public: |
74 static MockWebSocketHandle* create() | 70 static MockWebSocketHandle* create() |
75 { | 71 { |
76 return new testing::StrictMock<MockWebSocketHandle>(); | 72 return new testing::StrictMock<MockWebSocketHandle>(); |
77 } | 73 } |
78 | 74 |
79 MockWebSocketHandle() { } | 75 MockWebSocketHandle() { } |
80 | 76 |
81 ~MockWebSocketHandle() override { } | 77 ~MockWebSocketHandle() override { } |
82 | 78 |
83 MOCK_METHOD6(connect, void(const WebURL&, const WebVector<WebString>&, const
WebSecurityOrigin&, const WebURL&, const WebString&, WebSocketHandleClient*)); | 79 MOCK_METHOD6(connect, void(const KURL&, const Vector<String>&, SecurityOrigi
n*, const KURL&, const String&, WebSocketHandleClient*)); |
84 MOCK_METHOD4(send, void(bool, WebSocketHandle::MessageType, const char*, siz
e_t)); | 80 MOCK_METHOD4(send, void(bool, WebSocketHandle::MessageType, const char*, siz
e_t)); |
85 MOCK_METHOD1(flowControl, void(int64_t)); | 81 MOCK_METHOD1(flowControl, void(int64_t)); |
86 MOCK_METHOD2(close, void(unsigned short, const WebString&)); | 82 MOCK_METHOD2(close, void(unsigned short, const String&)); |
87 }; | 83 }; |
88 | 84 |
89 class DocumentWebSocketChannelTest : public ::testing::Test { | 85 class DocumentWebSocketChannelTest : public ::testing::Test { |
90 public: | 86 public: |
91 DocumentWebSocketChannelTest() | 87 DocumentWebSocketChannelTest() |
92 : m_pageHolder(DummyPageHolder::create()) | 88 : m_pageHolder(DummyPageHolder::create()) |
93 , m_channelClient(MockWebSocketChannelClient::create()) | 89 , m_channelClient(MockWebSocketChannelClient::create()) |
94 , m_handle(MockWebSocketHandle::create()) | 90 , m_handle(MockWebSocketHandle::create()) |
95 , m_channel(DocumentWebSocketChannel::create(&m_pageHolder->document(),
m_channelClient.get(), SourceLocation::capture(), handle())) | 91 , m_channel(DocumentWebSocketChannel::create(&m_pageHolder->document(),
m_channelClient.get(), SourceLocation::capture(), handle())) |
96 , m_sumOfConsumedBufferedAmount(0) | 92 , m_sumOfConsumedBufferedAmount(0) |
(...skipping 28 matching lines...) Expand all Loading... |
125 | 121 |
126 void didConsumeBufferedAmount(unsigned long a) | 122 void didConsumeBufferedAmount(unsigned long a) |
127 { | 123 { |
128 m_sumOfConsumedBufferedAmount += a; | 124 m_sumOfConsumedBufferedAmount += a; |
129 } | 125 } |
130 | 126 |
131 void connect() | 127 void connect() |
132 { | 128 { |
133 { | 129 { |
134 InSequence s; | 130 InSequence s; |
135 EXPECT_CALL(*handle(), connect(WebURL(KURL(KURL(), "ws://localhost/"
)), _, _, _, _, handleClient())); | 131 EXPECT_CALL(*handle(), connect(KURL(KURL(), "ws://localhost/"), _, _
, _, _, handleClient())); |
136 EXPECT_CALL(*handle(), flowControl(65536)); | 132 EXPECT_CALL(*handle(), flowControl(65536)); |
137 EXPECT_CALL(*channelClient(), didConnect(String("a"), String("b"))); | 133 EXPECT_CALL(*channelClient(), didConnect(String("a"), String("b"))); |
138 } | 134 } |
139 EXPECT_TRUE(channel()->connect(KURL(KURL(), "ws://localhost/"), "x")); | 135 EXPECT_TRUE(channel()->connect(KURL(KURL(), "ws://localhost/"), "x")); |
140 handleClient()->didConnect(handle(), WebString("a"), WebString("b")); | 136 handleClient()->didConnect(handle(), String("a"), String("b")); |
141 ::testing::Mock::VerifyAndClearExpectations(this); | 137 ::testing::Mock::VerifyAndClearExpectations(this); |
142 } | 138 } |
143 | 139 |
144 std::unique_ptr<DummyPageHolder> m_pageHolder; | 140 std::unique_ptr<DummyPageHolder> m_pageHolder; |
145 Persistent<MockWebSocketChannelClient> m_channelClient; | 141 Persistent<MockWebSocketChannelClient> m_channelClient; |
146 MockWebSocketHandle* m_handle; | 142 MockWebSocketHandle* m_handle; |
147 Persistent<DocumentWebSocketChannel> m_channel; | 143 Persistent<DocumentWebSocketChannel> m_channel; |
148 unsigned long m_sumOfConsumedBufferedAmount; | 144 unsigned long m_sumOfConsumedBufferedAmount; |
149 }; | 145 }; |
150 | 146 |
151 MATCHER_P2(MemEq, p, len, | 147 MATCHER_P2(MemEq, p, len, |
152 std::string("pointing to memory") | 148 std::string("pointing to memory") |
153 + (negation ? " not" : "") | 149 + (negation ? " not" : "") |
154 + " equal to \"" | 150 + " equal to \"" |
155 + std::string(p, len) + "\" (length=" + PrintToString(len) + ")" | 151 + std::string(p, len) + "\" (length=" + PrintToString(len) + ")" |
156 ) | 152 ) |
157 { | 153 { |
158 return memcmp(arg, p, len) == 0; | 154 return memcmp(arg, p, len) == 0; |
159 } | 155 } |
160 | 156 |
161 MATCHER_P(WebURLEq, urlString, | 157 MATCHER_P(KURLEq, urlString, |
162 std::string(negation ? "doesn't equal" : "equals") | 158 std::string(negation ? "doesn't equal" : "equals") |
163 + " to \"" + urlString + "\"" | 159 + " to \"" + urlString + "\"" |
164 ) | 160 ) |
165 { | 161 { |
166 WebURL url(KURL(KURL(), urlString)); | 162 KURL url(KURL(), urlString); |
167 *result_listener << "where the url is \"" << arg.string().utf8() << "\""; | 163 *result_listener << "where the url is \"" << arg.getString().utf8().data() <
< "\""; |
168 return arg == url; | 164 return arg == url; |
169 } | 165 } |
170 | 166 |
171 TEST_F(DocumentWebSocketChannelTest, connectSuccess) | 167 TEST_F(DocumentWebSocketChannelTest, connectSuccess) |
172 { | 168 { |
173 WebVector<WebString> protocols; | 169 Vector<String> protocols; |
174 WebSecurityOrigin origin; | 170 RefPtr<SecurityOrigin> origin; |
175 | 171 |
176 Checkpoint checkpoint; | 172 Checkpoint checkpoint; |
177 { | 173 { |
178 InSequence s; | 174 InSequence s; |
179 EXPECT_CALL(*handle(), connect(WebURLEq("ws://localhost/"), _, _, WebURL
Eq("http://example.com/"), _, handleClient())).WillOnce(DoAll( | 175 EXPECT_CALL(*handle(), connect(KURLEq("ws://localhost/"), _, _, KURLEq("
http://example.com/"), _, handleClient())).WillOnce(DoAll( |
180 SaveArg<1>(&protocols), | 176 SaveArg<1>(&protocols), |
181 SaveArg<2>(&origin))); | 177 SaveArg<2>(&origin))); |
182 EXPECT_CALL(*handle(), flowControl(65536)); | 178 EXPECT_CALL(*handle(), flowControl(65536)); |
183 EXPECT_CALL(checkpoint, Call(1)); | 179 EXPECT_CALL(checkpoint, Call(1)); |
184 EXPECT_CALL(*channelClient(), didConnect(String("a"), String("b"))); | 180 EXPECT_CALL(*channelClient(), didConnect(String("a"), String("b"))); |
185 } | 181 } |
186 | 182 |
187 KURL pageUrl(KURL(), "http://example.com/"); | 183 KURL pageUrl(KURL(), "http://example.com/"); |
188 m_pageHolder->frame().securityContext()->setSecurityOrigin(SecurityOrigin::c
reate(pageUrl)); | 184 m_pageHolder->frame().securityContext()->setSecurityOrigin(SecurityOrigin::c
reate(pageUrl)); |
189 Document& document = m_pageHolder->document(); | 185 Document& document = m_pageHolder->document(); |
190 document.setURL(pageUrl); | 186 document.setURL(pageUrl); |
191 // Make sure that firstPartyForCookies() is set to the given value. | 187 // Make sure that firstPartyForCookies() is set to the given value. |
192 EXPECT_STREQ("http://example.com/", document.firstPartyForCookies().getStrin
g().utf8().data()); | 188 EXPECT_STREQ("http://example.com/", document.firstPartyForCookies().getStrin
g().utf8().data()); |
193 | 189 |
194 EXPECT_TRUE(channel()->connect(KURL(KURL(), "ws://localhost/"), "x")); | 190 EXPECT_TRUE(channel()->connect(KURL(KURL(), "ws://localhost/"), "x")); |
195 | 191 |
196 EXPECT_EQ(1U, protocols.size()); | 192 EXPECT_EQ(1U, protocols.size()); |
197 EXPECT_STREQ("x", protocols[0].utf8().data()); | 193 EXPECT_STREQ("x", protocols[0].utf8().data()); |
198 | 194 |
199 EXPECT_STREQ("http://example.com", origin.toString().utf8().data()); | 195 EXPECT_STREQ("http://example.com", origin->toString().utf8().data()); |
200 | 196 |
201 checkpoint.Call(1); | 197 checkpoint.Call(1); |
202 handleClient()->didConnect(handle(), WebString("a"), WebString("b")); | 198 handleClient()->didConnect(handle(), String("a"), String("b")); |
203 } | 199 } |
204 | 200 |
205 TEST_F(DocumentWebSocketChannelTest, sendText) | 201 TEST_F(DocumentWebSocketChannelTest, sendText) |
206 { | 202 { |
207 connect(); | 203 connect(); |
208 { | 204 { |
209 InSequence s; | 205 InSequence s; |
210 EXPECT_CALL(*handle(), send(true, WebSocketHandle::MessageTypeText, MemE
q("foo", 3), 3)); | 206 EXPECT_CALL(*handle(), send(true, WebSocketHandle::MessageTypeText, MemE
q("foo", 3), 3)); |
211 EXPECT_CALL(*handle(), send(true, WebSocketHandle::MessageTypeText, MemE
q("bar", 3), 3)); | 207 EXPECT_CALL(*handle(), send(true, WebSocketHandle::MessageTypeText, MemE
q("bar", 3), 3)); |
212 EXPECT_CALL(*handle(), send(true, WebSocketHandle::MessageTypeText, MemE
q("baz", 3), 3)); | 208 EXPECT_CALL(*handle(), send(true, WebSocketHandle::MessageTypeText, MemE
q("baz", 3), 3)); |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 TEST_F(DocumentWebSocketChannelTest, closeFromBrowser) | 632 TEST_F(DocumentWebSocketChannelTest, closeFromBrowser) |
637 { | 633 { |
638 connect(); | 634 connect(); |
639 Checkpoint checkpoint; | 635 Checkpoint checkpoint; |
640 { | 636 { |
641 InSequence s; | 637 InSequence s; |
642 | 638 |
643 EXPECT_CALL(*channelClient(), didStartClosingHandshake()); | 639 EXPECT_CALL(*channelClient(), didStartClosingHandshake()); |
644 EXPECT_CALL(checkpoint, Call(1)); | 640 EXPECT_CALL(checkpoint, Call(1)); |
645 | 641 |
646 EXPECT_CALL(*handle(), close(WebSocketChannel::CloseEventCodeNormalClosu
re, WebString("close reason"))); | 642 EXPECT_CALL(*handle(), close(WebSocketChannel::CloseEventCodeNormalClosu
re, String("close reason"))); |
647 EXPECT_CALL(checkpoint, Call(2)); | 643 EXPECT_CALL(checkpoint, Call(2)); |
648 | 644 |
649 EXPECT_CALL(*channelClient(), didClose(WebSocketChannelClient::ClosingHa
ndshakeComplete, WebSocketChannel::CloseEventCodeNormalClosure, String("close re
ason"))); | 645 EXPECT_CALL(*channelClient(), didClose(WebSocketChannelClient::ClosingHa
ndshakeComplete, WebSocketChannel::CloseEventCodeNormalClosure, String("close re
ason"))); |
650 EXPECT_CALL(checkpoint, Call(3)); | 646 EXPECT_CALL(checkpoint, Call(3)); |
651 } | 647 } |
652 | 648 |
653 handleClient()->didStartClosingHandshake(handle()); | 649 handleClient()->didStartClosingHandshake(handle()); |
654 checkpoint.Call(1); | 650 checkpoint.Call(1); |
655 | 651 |
656 channel()->close(WebSocketChannel::CloseEventCodeNormalClosure, String("clos
e reason")); | 652 channel()->close(WebSocketChannel::CloseEventCodeNormalClosure, String("clos
e reason")); |
657 checkpoint.Call(2); | 653 checkpoint.Call(2); |
658 | 654 |
659 handleClient()->didClose(handle(), true, WebSocketChannel::CloseEventCodeNor
malClosure, String("close reason")); | 655 handleClient()->didClose(handle(), true, WebSocketChannel::CloseEventCodeNor
malClosure, String("close reason")); |
660 checkpoint.Call(3); | 656 checkpoint.Call(3); |
661 | 657 |
662 channel()->disconnect(); | 658 channel()->disconnect(); |
663 } | 659 } |
664 | 660 |
665 TEST_F(DocumentWebSocketChannelTest, closeFromWebSocket) | 661 TEST_F(DocumentWebSocketChannelTest, closeFromWebSocket) |
666 { | 662 { |
667 connect(); | 663 connect(); |
668 Checkpoint checkpoint; | 664 Checkpoint checkpoint; |
669 { | 665 { |
670 InSequence s; | 666 InSequence s; |
671 | 667 |
672 EXPECT_CALL(*handle(), close(WebSocketChannel::CloseEventCodeNormalClosu
re, WebString("close reason"))); | 668 EXPECT_CALL(*handle(), close(WebSocketChannel::CloseEventCodeNormalClosu
re, String("close reason"))); |
673 EXPECT_CALL(checkpoint, Call(1)); | 669 EXPECT_CALL(checkpoint, Call(1)); |
674 | 670 |
675 EXPECT_CALL(*channelClient(), didClose(WebSocketChannelClient::ClosingHa
ndshakeComplete, WebSocketChannel::CloseEventCodeNormalClosure, String("close re
ason"))); | 671 EXPECT_CALL(*channelClient(), didClose(WebSocketChannelClient::ClosingHa
ndshakeComplete, WebSocketChannel::CloseEventCodeNormalClosure, String("close re
ason"))); |
676 EXPECT_CALL(checkpoint, Call(2)); | 672 EXPECT_CALL(checkpoint, Call(2)); |
677 } | 673 } |
678 | 674 |
679 channel()->close(WebSocketChannel::CloseEventCodeNormalClosure, String("clos
e reason")); | 675 channel()->close(WebSocketChannel::CloseEventCodeNormalClosure, String("clos
e reason")); |
680 checkpoint.Call(1); | 676 checkpoint.Call(1); |
681 | 677 |
682 handleClient()->didClose(handle(), true, WebSocketChannel::CloseEventCodeNor
malClosure, String("close reason")); | 678 handleClient()->didClose(handle(), true, WebSocketChannel::CloseEventCodeNor
malClosure, String("close reason")); |
(...skipping 24 matching lines...) Expand all Loading... |
707 EXPECT_CALL(*channelClient(), didError()); | 703 EXPECT_CALL(*channelClient(), didError()); |
708 EXPECT_CALL(*channelClient(), didClose(WebSocketChannelClient::ClosingHa
ndshakeIncomplete, WebSocketChannel::CloseEventCodeAbnormalClosure, String())); | 704 EXPECT_CALL(*channelClient(), didClose(WebSocketChannelClient::ClosingHa
ndshakeIncomplete, WebSocketChannel::CloseEventCodeAbnormalClosure, String())); |
709 } | 705 } |
710 | 706 |
711 channel()->fail("fail message from WebSocket", ErrorMessageLevel, SourceLoca
tion::create(String(), 0, 0, nullptr)); | 707 channel()->fail("fail message from WebSocket", ErrorMessageLevel, SourceLoca
tion::create(String(), 0, 0, nullptr)); |
712 } | 708 } |
713 | 709 |
714 } // namespace | 710 } // namespace |
715 | 711 |
716 } // namespace blink | 712 } // namespace blink |
OLD | NEW |