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