| 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" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "wtf/PtrUtil.h" | 23 #include "wtf/PtrUtil.h" |
| 24 #include "wtf/Vector.h" | 24 #include "wtf/Vector.h" |
| 25 #include "wtf/text/WTFString.h" | 25 #include "wtf/text/WTFString.h" |
| 26 #include <memory> | 26 #include <memory> |
| 27 #include <stdint.h> | 27 #include <stdint.h> |
| 28 | 28 |
| 29 using testing::_; | 29 using testing::_; |
| 30 using testing::InSequence; | 30 using testing::InSequence; |
| 31 using testing::PrintToString; | 31 using testing::PrintToString; |
| 32 using testing::AnyNumber; | 32 using testing::AnyNumber; |
| 33 | 33 using testing::SaveArg; |
| 34 | 34 |
| 35 namespace blink { | 35 namespace blink { |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 typedef testing::StrictMock< testing::MockFunction<void(int)>> Checkpoint; | 39 typedef testing::StrictMock< testing::MockFunction<void(int)>> Checkpoint; |
| 40 | 40 |
| 41 class MockWebSocketChannelClient : public GarbageCollectedFinalized<MockWebSocke
tChannelClient>, public WebSocketChannelClient { | 41 class MockWebSocketChannelClient : public GarbageCollectedFinalized<MockWebSocke
tChannelClient>, public WebSocketChannelClient { |
| 42 USING_GARBAGE_COLLECTED_MIXIN(MockWebSocketChannelClient); | 42 USING_GARBAGE_COLLECTED_MIXIN(MockWebSocketChannelClient); |
| 43 public: | 43 public: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 73 public: | 73 public: |
| 74 static MockWebSocketHandle* create() | 74 static MockWebSocketHandle* create() |
| 75 { | 75 { |
| 76 return new testing::StrictMock<MockWebSocketHandle>(); | 76 return new testing::StrictMock<MockWebSocketHandle>(); |
| 77 } | 77 } |
| 78 | 78 |
| 79 MockWebSocketHandle() { } | 79 MockWebSocketHandle() { } |
| 80 | 80 |
| 81 ~MockWebSocketHandle() override { } | 81 ~MockWebSocketHandle() override { } |
| 82 | 82 |
| 83 MOCK_METHOD5(connect, void(const WebURL&, const WebVector<WebString>&, const
WebSecurityOrigin&, const WebString&, WebSocketHandleClient*)); | 83 MOCK_METHOD6(connect, void(const WebURL&, const WebVector<WebString>&, const
WebSecurityOrigin&, const WebURL&, const WebString&, WebSocketHandleClient*)); |
| 84 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)); |
| 85 MOCK_METHOD1(flowControl, void(int64_t)); | 85 MOCK_METHOD1(flowControl, void(int64_t)); |
| 86 MOCK_METHOD2(close, void(unsigned short, const WebString&)); | 86 MOCK_METHOD2(close, void(unsigned short, const WebString&)); |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 class DocumentWebSocketChannelTest : public ::testing::Test { | 89 class DocumentWebSocketChannelTest : public ::testing::Test { |
| 90 public: | 90 public: |
| 91 DocumentWebSocketChannelTest() | 91 DocumentWebSocketChannelTest() |
| 92 : m_pageHolder(DummyPageHolder::create()) | 92 : m_pageHolder(DummyPageHolder::create()) |
| 93 , m_channelClient(MockWebSocketChannelClient::create()) | 93 , m_channelClient(MockWebSocketChannelClient::create()) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 125 |
| 126 void didConsumeBufferedAmount(unsigned long a) | 126 void didConsumeBufferedAmount(unsigned long a) |
| 127 { | 127 { |
| 128 m_sumOfConsumedBufferedAmount += a; | 128 m_sumOfConsumedBufferedAmount += a; |
| 129 } | 129 } |
| 130 | 130 |
| 131 void connect() | 131 void connect() |
| 132 { | 132 { |
| 133 { | 133 { |
| 134 InSequence s; | 134 InSequence s; |
| 135 EXPECT_CALL(*handle(), connect(WebURL(KURL(KURL(), "ws://localhost/"
)), _, _, _, handleClient())); | 135 EXPECT_CALL(*handle(), connect(WebURL(KURL(KURL(), "ws://localhost/"
)), _, _, _, _, handleClient())); |
| 136 EXPECT_CALL(*handle(), flowControl(65536)); | 136 EXPECT_CALL(*handle(), flowControl(65536)); |
| 137 EXPECT_CALL(*channelClient(), didConnect(String("a"), String("b"))); | 137 EXPECT_CALL(*channelClient(), didConnect(String("a"), String("b"))); |
| 138 } | 138 } |
| 139 EXPECT_TRUE(channel()->connect(KURL(KURL(), "ws://localhost/"), "x")); | 139 EXPECT_TRUE(channel()->connect(KURL(KURL(), "ws://localhost/"), "x")); |
| 140 handleClient()->didConnect(handle(), WebString("a"), WebString("b")); | 140 handleClient()->didConnect(handle(), WebString("a"), WebString("b")); |
| 141 ::testing::Mock::VerifyAndClearExpectations(this); | 141 ::testing::Mock::VerifyAndClearExpectations(this); |
| 142 } | 142 } |
| 143 | 143 |
| 144 std::unique_ptr<DummyPageHolder> m_pageHolder; | 144 std::unique_ptr<DummyPageHolder> m_pageHolder; |
| 145 Persistent<MockWebSocketChannelClient> m_channelClient; | 145 Persistent<MockWebSocketChannelClient> m_channelClient; |
| 146 MockWebSocketHandle* m_handle; | 146 MockWebSocketHandle* m_handle; |
| 147 Persistent<DocumentWebSocketChannel> m_channel; | 147 Persistent<DocumentWebSocketChannel> m_channel; |
| 148 unsigned long m_sumOfConsumedBufferedAmount; | 148 unsigned long m_sumOfConsumedBufferedAmount; |
| 149 }; | 149 }; |
| 150 | 150 |
| 151 MATCHER_P2(MemEq, p, len, | 151 MATCHER_P2(MemEq, p, len, |
| 152 std::string("pointing to memory") | 152 std::string("pointing to memory") |
| 153 + (negation ? " not" : "") | 153 + (negation ? " not" : "") |
| 154 + " equal to \"" | 154 + " equal to \"" |
| 155 + std::string(p, len) + "\" (length=" + PrintToString(len) + ")" | 155 + std::string(p, len) + "\" (length=" + PrintToString(len) + ")" |
| 156 ) | 156 ) |
| 157 { | 157 { |
| 158 return memcmp(arg, p, len) == 0; | 158 return memcmp(arg, p, len) == 0; |
| 159 } | 159 } |
| 160 | 160 |
| 161 MATCHER_P(WebURLEq, urlString, |
| 162 std::string(negation ? "doesn't equal" : "equals") |
| 163 + " to \"" + urlString + "\"" |
| 164 ) |
| 165 { |
| 166 WebURL url(KURL(KURL(), urlString)); |
| 167 *result_listener << "where the url is \"" << arg.string().utf8() << "\""; |
| 168 return arg == url; |
| 169 } |
| 170 |
| 161 TEST_F(DocumentWebSocketChannelTest, connectSuccess) | 171 TEST_F(DocumentWebSocketChannelTest, connectSuccess) |
| 162 { | 172 { |
| 173 WebVector<WebString> protocols; |
| 174 WebSecurityOrigin origin; |
| 175 |
| 163 Checkpoint checkpoint; | 176 Checkpoint checkpoint; |
| 164 { | 177 { |
| 165 InSequence s; | 178 InSequence s; |
| 166 EXPECT_CALL(*handle(), connect(WebURL(KURL(KURL(), "ws://localhost/")),
_, _, _, handleClient())); | 179 EXPECT_CALL(*handle(), connect(WebURLEq("ws://localhost/"), _, _, WebURL
Eq("http://example.com/"), _, handleClient())).WillOnce(DoAll( |
| 180 SaveArg<1>(&protocols), |
| 181 SaveArg<2>(&origin))); |
| 167 EXPECT_CALL(*handle(), flowControl(65536)); | 182 EXPECT_CALL(*handle(), flowControl(65536)); |
| 168 EXPECT_CALL(checkpoint, Call(1)); | 183 EXPECT_CALL(checkpoint, Call(1)); |
| 169 EXPECT_CALL(*channelClient(), didConnect(String("a"), String("b"))); | 184 EXPECT_CALL(*channelClient(), didConnect(String("a"), String("b"))); |
| 170 } | 185 } |
| 171 | 186 |
| 187 KURL pageUrl(KURL(), "http://example.com/"); |
| 188 m_pageHolder->frame().securityContext()->setSecurityOrigin(SecurityOrigin::c
reate(pageUrl)); |
| 189 Document& document = m_pageHolder->document(); |
| 190 document.setURL(pageUrl); |
| 191 // Make sure that firstPartyForCookies() is set to the given value. |
| 192 EXPECT_STREQ("http://example.com/", document.firstPartyForCookies().getStrin
g().utf8().data()); |
| 193 |
| 172 EXPECT_TRUE(channel()->connect(KURL(KURL(), "ws://localhost/"), "x")); | 194 EXPECT_TRUE(channel()->connect(KURL(KURL(), "ws://localhost/"), "x")); |
| 195 |
| 196 EXPECT_EQ(1U, protocols.size()); |
| 197 EXPECT_STREQ("x", protocols[0].utf8().data()); |
| 198 |
| 199 EXPECT_STREQ("http://example.com", origin.toString().utf8().data()); |
| 200 |
| 173 checkpoint.Call(1); | 201 checkpoint.Call(1); |
| 174 handleClient()->didConnect(handle(), WebString("a"), WebString("b")); | 202 handleClient()->didConnect(handle(), WebString("a"), WebString("b")); |
| 175 } | 203 } |
| 176 | 204 |
| 177 TEST_F(DocumentWebSocketChannelTest, sendText) | 205 TEST_F(DocumentWebSocketChannelTest, sendText) |
| 178 { | 206 { |
| 179 connect(); | 207 connect(); |
| 180 { | 208 { |
| 181 InSequence s; | 209 InSequence s; |
| 182 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)); |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 679 EXPECT_CALL(*channelClient(), didError()); | 707 EXPECT_CALL(*channelClient(), didError()); |
| 680 EXPECT_CALL(*channelClient(), didClose(WebSocketChannelClient::ClosingHa
ndshakeIncomplete, WebSocketChannel::CloseEventCodeAbnormalClosure, String())); | 708 EXPECT_CALL(*channelClient(), didClose(WebSocketChannelClient::ClosingHa
ndshakeIncomplete, WebSocketChannel::CloseEventCodeAbnormalClosure, String())); |
| 681 } | 709 } |
| 682 | 710 |
| 683 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)); |
| 684 } | 712 } |
| 685 | 713 |
| 686 } // namespace | 714 } // namespace |
| 687 | 715 |
| 688 } // namespace blink | 716 } // namespace blink |
| OLD | NEW |