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