| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "net/websockets/websocket_channel.h" | 5 #include "net/websockets/websocket_channel.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <iostream> | 10 #include <iostream> |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 MOCK_METHOD0(OnStartOpeningHandshakeCalled, void()); // NOLINT | 185 MOCK_METHOD0(OnStartOpeningHandshakeCalled, void()); // NOLINT |
| 186 MOCK_METHOD0(OnFinishOpeningHandshakeCalled, void()); // NOLINT | 186 MOCK_METHOD0(OnFinishOpeningHandshakeCalled, void()); // NOLINT |
| 187 MOCK_METHOD4( | 187 MOCK_METHOD4( |
| 188 OnSSLCertificateErrorCalled, | 188 OnSSLCertificateErrorCalled, |
| 189 void(SSLErrorCallbacks*, const GURL&, const SSLInfo&, bool)); // NOLINT | 189 void(SSLErrorCallbacks*, const GURL&, const SSLInfo&, bool)); // NOLINT |
| 190 }; | 190 }; |
| 191 | 191 |
| 192 // This fake EventInterface is for tests which need a WebSocketEventInterface | 192 // This fake EventInterface is for tests which need a WebSocketEventInterface |
| 193 // implementation but are not verifying how it is used. | 193 // implementation but are not verifying how it is used. |
| 194 class FakeWebSocketEventInterface : public WebSocketEventInterface { | 194 class FakeWebSocketEventInterface : public WebSocketEventInterface { |
| 195 virtual ChannelState OnAddChannelResponse( | 195 ChannelState OnAddChannelResponse(bool fail, |
| 196 bool fail, | 196 const std::string& selected_protocol, |
| 197 const std::string& selected_protocol, | 197 const std::string& extensions) override { |
| 198 const std::string& extensions) override { | |
| 199 return fail ? CHANNEL_DELETED : CHANNEL_ALIVE; | 198 return fail ? CHANNEL_DELETED : CHANNEL_ALIVE; |
| 200 } | 199 } |
| 201 virtual ChannelState OnDataFrame(bool fin, | 200 ChannelState OnDataFrame(bool fin, |
| 202 WebSocketMessageType type, | 201 WebSocketMessageType type, |
| 203 const std::vector<char>& data) override { | 202 const std::vector<char>& data) override { |
| 204 return CHANNEL_ALIVE; | 203 return CHANNEL_ALIVE; |
| 205 } | 204 } |
| 206 virtual ChannelState OnFlowControl(int64 quota) override { | 205 ChannelState OnFlowControl(int64 quota) override { return CHANNEL_ALIVE; } |
| 207 return CHANNEL_ALIVE; | 206 ChannelState OnClosingHandshake() override { return CHANNEL_ALIVE; } |
| 208 } | 207 ChannelState OnFailChannel(const std::string& message) override { |
| 209 virtual ChannelState OnClosingHandshake() override { return CHANNEL_ALIVE; } | |
| 210 virtual ChannelState OnFailChannel(const std::string& message) override { | |
| 211 return CHANNEL_DELETED; | 208 return CHANNEL_DELETED; |
| 212 } | 209 } |
| 213 virtual ChannelState OnDropChannel(bool was_clean, | 210 ChannelState OnDropChannel(bool was_clean, |
| 214 uint16 code, | 211 uint16 code, |
| 215 const std::string& reason) override { | 212 const std::string& reason) override { |
| 216 return CHANNEL_DELETED; | 213 return CHANNEL_DELETED; |
| 217 } | 214 } |
| 218 virtual ChannelState OnStartOpeningHandshake( | 215 ChannelState OnStartOpeningHandshake( |
| 219 scoped_ptr<WebSocketHandshakeRequestInfo> request) override { | 216 scoped_ptr<WebSocketHandshakeRequestInfo> request) override { |
| 220 return CHANNEL_ALIVE; | 217 return CHANNEL_ALIVE; |
| 221 } | 218 } |
| 222 virtual ChannelState OnFinishOpeningHandshake( | 219 ChannelState OnFinishOpeningHandshake( |
| 223 scoped_ptr<WebSocketHandshakeResponseInfo> response) override { | 220 scoped_ptr<WebSocketHandshakeResponseInfo> response) override { |
| 224 return CHANNEL_ALIVE; | 221 return CHANNEL_ALIVE; |
| 225 } | 222 } |
| 226 virtual ChannelState OnSSLCertificateError( | 223 ChannelState OnSSLCertificateError( |
| 227 scoped_ptr<SSLErrorCallbacks> ssl_error_callbacks, | 224 scoped_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
| 228 const GURL& url, | 225 const GURL& url, |
| 229 const SSLInfo& ssl_info, | 226 const SSLInfo& ssl_info, |
| 230 bool fatal) override { | 227 bool fatal) override { |
| 231 return CHANNEL_ALIVE; | 228 return CHANNEL_ALIVE; |
| 232 } | 229 } |
| 233 }; | 230 }; |
| 234 | 231 |
| 235 // This fake WebSocketStream is for tests that require a WebSocketStream but are | 232 // This fake WebSocketStream is for tests that require a WebSocketStream but are |
| 236 // not testing the way it is used. It has minimal functionality to return | 233 // not testing the way it is used. It has minimal functionality to return |
| 237 // the |protocol| and |extensions| that it was constructed with. | 234 // the |protocol| and |extensions| that it was constructed with. |
| 238 class FakeWebSocketStream : public WebSocketStream { | 235 class FakeWebSocketStream : public WebSocketStream { |
| 239 public: | 236 public: |
| 240 // Constructs with empty protocol and extensions. | 237 // Constructs with empty protocol and extensions. |
| 241 FakeWebSocketStream() {} | 238 FakeWebSocketStream() {} |
| 242 | 239 |
| 243 // Constructs with specified protocol and extensions. | 240 // Constructs with specified protocol and extensions. |
| 244 FakeWebSocketStream(const std::string& protocol, | 241 FakeWebSocketStream(const std::string& protocol, |
| 245 const std::string& extensions) | 242 const std::string& extensions) |
| 246 : protocol_(protocol), extensions_(extensions) {} | 243 : protocol_(protocol), extensions_(extensions) {} |
| 247 | 244 |
| 248 virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames, | 245 int ReadFrames(ScopedVector<WebSocketFrame>* frames, |
| 249 const CompletionCallback& callback) override { | 246 const CompletionCallback& callback) override { |
| 250 return ERR_IO_PENDING; | 247 return ERR_IO_PENDING; |
| 251 } | 248 } |
| 252 | 249 |
| 253 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames, | 250 int WriteFrames(ScopedVector<WebSocketFrame>* frames, |
| 254 const CompletionCallback& callback) override { | 251 const CompletionCallback& callback) override { |
| 255 return ERR_IO_PENDING; | 252 return ERR_IO_PENDING; |
| 256 } | 253 } |
| 257 | 254 |
| 258 virtual void Close() override {} | 255 void Close() override {} |
| 259 | 256 |
| 260 // Returns the string passed to the constructor. | 257 // Returns the string passed to the constructor. |
| 261 virtual std::string GetSubProtocol() const override { return protocol_; } | 258 std::string GetSubProtocol() const override { return protocol_; } |
| 262 | 259 |
| 263 // Returns the string passed to the constructor. | 260 // Returns the string passed to the constructor. |
| 264 virtual std::string GetExtensions() const override { return extensions_; } | 261 std::string GetExtensions() const override { return extensions_; } |
| 265 | 262 |
| 266 private: | 263 private: |
| 267 // The string to return from GetSubProtocol(). | 264 // The string to return from GetSubProtocol(). |
| 268 std::string protocol_; | 265 std::string protocol_; |
| 269 | 266 |
| 270 // The string to return from GetExtensions(). | 267 // The string to return from GetExtensions(). |
| 271 std::string extensions_; | 268 std::string extensions_; |
| 272 }; | 269 }; |
| 273 | 270 |
| 274 // To make the static initialisers easier to read, we use enums rather than | 271 // To make the static initialisers easier to read, we use enums rather than |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 // A FakeWebSocketStream whose ReadFrames() function returns data. | 446 // A FakeWebSocketStream whose ReadFrames() function returns data. |
| 450 class ReadableFakeWebSocketStream : public FakeWebSocketStream { | 447 class ReadableFakeWebSocketStream : public FakeWebSocketStream { |
| 451 public: | 448 public: |
| 452 enum IsSync { SYNC, ASYNC }; | 449 enum IsSync { SYNC, ASYNC }; |
| 453 | 450 |
| 454 // After constructing the object, call PrepareReadFrames() once for each | 451 // After constructing the object, call PrepareReadFrames() once for each |
| 455 // time you wish it to return from the test. | 452 // time you wish it to return from the test. |
| 456 ReadableFakeWebSocketStream() : index_(0), read_frames_pending_(false) {} | 453 ReadableFakeWebSocketStream() : index_(0), read_frames_pending_(false) {} |
| 457 | 454 |
| 458 // Check that all the prepared responses have been consumed. | 455 // Check that all the prepared responses have been consumed. |
| 459 virtual ~ReadableFakeWebSocketStream() { | 456 ~ReadableFakeWebSocketStream() override { |
| 460 CHECK(index_ >= responses_.size()); | 457 CHECK(index_ >= responses_.size()); |
| 461 CHECK(!read_frames_pending_); | 458 CHECK(!read_frames_pending_); |
| 462 } | 459 } |
| 463 | 460 |
| 464 // Prepares a fake response. Fake responses will be returned from ReadFrames() | 461 // Prepares a fake response. Fake responses will be returned from ReadFrames() |
| 465 // in the same order they were prepared with PrepareReadFrames() and | 462 // in the same order they were prepared with PrepareReadFrames() and |
| 466 // PrepareReadFramesError(). If |async| is ASYNC, then ReadFrames() will | 463 // PrepareReadFramesError(). If |async| is ASYNC, then ReadFrames() will |
| 467 // return ERR_IO_PENDING and the callback will be scheduled to run on the | 464 // return ERR_IO_PENDING and the callback will be scheduled to run on the |
| 468 // message loop. This requires the test case to run the message loop. If | 465 // message loop. This requires the test case to run the message loop. If |
| 469 // |async| is SYNC, the response will be returned synchronously. |error| is | 466 // |async| is SYNC, the response will be returned synchronously. |error| is |
| (...skipping 15 matching lines...) Expand all Loading... |
| 485 ScopedVector<WebSocketFrame> frames) { | 482 ScopedVector<WebSocketFrame> frames) { |
| 486 responses_.push_back(new Response(async, error, frames.Pass())); | 483 responses_.push_back(new Response(async, error, frames.Pass())); |
| 487 } | 484 } |
| 488 | 485 |
| 489 // Prepares a fake error response (ie. there is no data). | 486 // Prepares a fake error response (ie. there is no data). |
| 490 void PrepareReadFramesError(IsSync async, int error) { | 487 void PrepareReadFramesError(IsSync async, int error) { |
| 491 responses_.push_back( | 488 responses_.push_back( |
| 492 new Response(async, error, ScopedVector<WebSocketFrame>())); | 489 new Response(async, error, ScopedVector<WebSocketFrame>())); |
| 493 } | 490 } |
| 494 | 491 |
| 495 virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames, | 492 int ReadFrames(ScopedVector<WebSocketFrame>* frames, |
| 496 const CompletionCallback& callback) override { | 493 const CompletionCallback& callback) override { |
| 497 CHECK(!read_frames_pending_); | 494 CHECK(!read_frames_pending_); |
| 498 if (index_ >= responses_.size()) | 495 if (index_ >= responses_.size()) |
| 499 return ERR_IO_PENDING; | 496 return ERR_IO_PENDING; |
| 500 if (responses_[index_]->async == ASYNC) { | 497 if (responses_[index_]->async == ASYNC) { |
| 501 read_frames_pending_ = true; | 498 read_frames_pending_ = true; |
| 502 base::MessageLoop::current()->PostTask( | 499 base::MessageLoop::current()->PostTask( |
| 503 FROM_HERE, | 500 FROM_HERE, |
| 504 base::Bind(&ReadableFakeWebSocketStream::DoCallback, | 501 base::Bind(&ReadableFakeWebSocketStream::DoCallback, |
| 505 base::Unretained(this), | 502 base::Unretained(this), |
| 506 frames, | 503 frames, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 // to "real" async responses. Once all the prepared responses have been | 539 // to "real" async responses. Once all the prepared responses have been |
| 543 // returned, ReadFrames() returns ERR_IO_PENDING but read_frames_pending_ is | 540 // returned, ReadFrames() returns ERR_IO_PENDING but read_frames_pending_ is |
| 544 // not set to true. | 541 // not set to true. |
| 545 bool read_frames_pending_; | 542 bool read_frames_pending_; |
| 546 }; | 543 }; |
| 547 | 544 |
| 548 // A FakeWebSocketStream where writes always complete successfully and | 545 // A FakeWebSocketStream where writes always complete successfully and |
| 549 // synchronously. | 546 // synchronously. |
| 550 class WriteableFakeWebSocketStream : public FakeWebSocketStream { | 547 class WriteableFakeWebSocketStream : public FakeWebSocketStream { |
| 551 public: | 548 public: |
| 552 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames, | 549 int WriteFrames(ScopedVector<WebSocketFrame>* frames, |
| 553 const CompletionCallback& callback) override { | 550 const CompletionCallback& callback) override { |
| 554 return OK; | 551 return OK; |
| 555 } | 552 } |
| 556 }; | 553 }; |
| 557 | 554 |
| 558 // A FakeWebSocketStream where writes always fail. | 555 // A FakeWebSocketStream where writes always fail. |
| 559 class UnWriteableFakeWebSocketStream : public FakeWebSocketStream { | 556 class UnWriteableFakeWebSocketStream : public FakeWebSocketStream { |
| 560 public: | 557 public: |
| 561 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames, | 558 int WriteFrames(ScopedVector<WebSocketFrame>* frames, |
| 562 const CompletionCallback& callback) override { | 559 const CompletionCallback& callback) override { |
| 563 return ERR_CONNECTION_RESET; | 560 return ERR_CONNECTION_RESET; |
| 564 } | 561 } |
| 565 }; | 562 }; |
| 566 | 563 |
| 567 // A FakeWebSocketStream which echoes any frames written back. Clears the | 564 // A FakeWebSocketStream which echoes any frames written back. Clears the |
| 568 // "masked" header bit, but makes no other checks for validity. Tests using this | 565 // "masked" header bit, but makes no other checks for validity. Tests using this |
| 569 // must run the MessageLoop to receive the callback(s). If a message with opcode | 566 // must run the MessageLoop to receive the callback(s). If a message with opcode |
| 570 // Close is echoed, then an ERR_CONNECTION_CLOSED is returned in the next | 567 // Close is echoed, then an ERR_CONNECTION_CLOSED is returned in the next |
| 571 // callback. The test must do something to cause WriteFrames() to be called, | 568 // callback. The test must do something to cause WriteFrames() to be called, |
| 572 // otherwise the ReadFrames() callback will never be called. | 569 // otherwise the ReadFrames() callback will never be called. |
| 573 class EchoeyFakeWebSocketStream : public FakeWebSocketStream { | 570 class EchoeyFakeWebSocketStream : public FakeWebSocketStream { |
| 574 public: | 571 public: |
| 575 EchoeyFakeWebSocketStream() : read_frames_(NULL), done_(false) {} | 572 EchoeyFakeWebSocketStream() : read_frames_(NULL), done_(false) {} |
| 576 | 573 |
| 577 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames, | 574 int WriteFrames(ScopedVector<WebSocketFrame>* frames, |
| 578 const CompletionCallback& callback) override { | 575 const CompletionCallback& callback) override { |
| 579 // Users of WebSocketStream will not expect the ReadFrames() callback to be | 576 // Users of WebSocketStream will not expect the ReadFrames() callback to be |
| 580 // called from within WriteFrames(), so post it to the message loop instead. | 577 // called from within WriteFrames(), so post it to the message loop instead. |
| 581 stored_frames_.insert(stored_frames_.end(), frames->begin(), frames->end()); | 578 stored_frames_.insert(stored_frames_.end(), frames->begin(), frames->end()); |
| 582 frames->weak_clear(); | 579 frames->weak_clear(); |
| 583 PostCallback(); | 580 PostCallback(); |
| 584 return OK; | 581 return OK; |
| 585 } | 582 } |
| 586 | 583 |
| 587 virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames, | 584 int ReadFrames(ScopedVector<WebSocketFrame>* frames, |
| 588 const CompletionCallback& callback) override { | 585 const CompletionCallback& callback) override { |
| 589 read_callback_ = callback; | 586 read_callback_ = callback; |
| 590 read_frames_ = frames; | 587 read_frames_ = frames; |
| 591 if (done_) | 588 if (done_) |
| 592 PostCallback(); | 589 PostCallback(); |
| 593 return ERR_IO_PENDING; | 590 return ERR_IO_PENDING; |
| 594 } | 591 } |
| 595 | 592 |
| 596 private: | 593 private: |
| 597 void PostCallback() { | 594 void PostCallback() { |
| 598 base::MessageLoop::current()->PostTask( | 595 base::MessageLoop::current()->PostTask( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 // This differs from UnWriteableFakeWebSocketStream in that it is asynchronous | 637 // This differs from UnWriteableFakeWebSocketStream in that it is asynchronous |
| 641 // and triggers ReadFrames to return a reset as well. Tests using this need to | 638 // and triggers ReadFrames to return a reset as well. Tests using this need to |
| 642 // run the message loop. There are two tricky parts here: | 639 // run the message loop. There are two tricky parts here: |
| 643 // 1. Calling the write callback may call Close(), after which the read callback | 640 // 1. Calling the write callback may call Close(), after which the read callback |
| 644 // should not be called. | 641 // should not be called. |
| 645 // 2. Calling either callback may delete the stream altogether. | 642 // 2. Calling either callback may delete the stream altogether. |
| 646 class ResetOnWriteFakeWebSocketStream : public FakeWebSocketStream { | 643 class ResetOnWriteFakeWebSocketStream : public FakeWebSocketStream { |
| 647 public: | 644 public: |
| 648 ResetOnWriteFakeWebSocketStream() : closed_(false), weak_ptr_factory_(this) {} | 645 ResetOnWriteFakeWebSocketStream() : closed_(false), weak_ptr_factory_(this) {} |
| 649 | 646 |
| 650 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames, | 647 int WriteFrames(ScopedVector<WebSocketFrame>* frames, |
| 651 const CompletionCallback& callback) override { | 648 const CompletionCallback& callback) override { |
| 652 base::MessageLoop::current()->PostTask( | 649 base::MessageLoop::current()->PostTask( |
| 653 FROM_HERE, | 650 FROM_HERE, |
| 654 base::Bind(&ResetOnWriteFakeWebSocketStream::CallCallbackUnlessClosed, | 651 base::Bind(&ResetOnWriteFakeWebSocketStream::CallCallbackUnlessClosed, |
| 655 weak_ptr_factory_.GetWeakPtr(), | 652 weak_ptr_factory_.GetWeakPtr(), |
| 656 callback, | 653 callback, |
| 657 ERR_CONNECTION_RESET)); | 654 ERR_CONNECTION_RESET)); |
| 658 base::MessageLoop::current()->PostTask( | 655 base::MessageLoop::current()->PostTask( |
| 659 FROM_HERE, | 656 FROM_HERE, |
| 660 base::Bind(&ResetOnWriteFakeWebSocketStream::CallCallbackUnlessClosed, | 657 base::Bind(&ResetOnWriteFakeWebSocketStream::CallCallbackUnlessClosed, |
| 661 weak_ptr_factory_.GetWeakPtr(), | 658 weak_ptr_factory_.GetWeakPtr(), |
| 662 read_callback_, | 659 read_callback_, |
| 663 ERR_CONNECTION_RESET)); | 660 ERR_CONNECTION_RESET)); |
| 664 return ERR_IO_PENDING; | 661 return ERR_IO_PENDING; |
| 665 } | 662 } |
| 666 | 663 |
| 667 virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames, | 664 int ReadFrames(ScopedVector<WebSocketFrame>* frames, |
| 668 const CompletionCallback& callback) override { | 665 const CompletionCallback& callback) override { |
| 669 read_callback_ = callback; | 666 read_callback_ = callback; |
| 670 return ERR_IO_PENDING; | 667 return ERR_IO_PENDING; |
| 671 } | 668 } |
| 672 | 669 |
| 673 virtual void Close() override { closed_ = true; } | 670 void Close() override { closed_ = true; } |
| 674 | 671 |
| 675 private: | 672 private: |
| 676 void CallCallbackUnlessClosed(const CompletionCallback& callback, int value) { | 673 void CallCallbackUnlessClosed(const CompletionCallback& callback, int value) { |
| 677 if (!closed_) | 674 if (!closed_) |
| 678 callback.Run(value); | 675 callback.Run(value); |
| 679 } | 676 } |
| 680 | 677 |
| 681 CompletionCallback read_callback_; | 678 CompletionCallback read_callback_; |
| 682 bool closed_; | 679 bool closed_; |
| 683 // An IO error can result in the socket being deleted, so we use weak pointers | 680 // An IO error can result in the socket being deleted, so we use weak pointers |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 // Converts a std::string to a std::vector<char>. For test purposes, it is | 726 // Converts a std::string to a std::vector<char>. For test purposes, it is |
| 730 // convenient to be able to specify data as a string, but the | 727 // convenient to be able to specify data as a string, but the |
| 731 // WebSocketEventInterface requires the vector<char> type. | 728 // WebSocketEventInterface requires the vector<char> type. |
| 732 std::vector<char> AsVector(const std::string& s) { | 729 std::vector<char> AsVector(const std::string& s) { |
| 733 return std::vector<char>(s.begin(), s.end()); | 730 return std::vector<char>(s.begin(), s.end()); |
| 734 } | 731 } |
| 735 | 732 |
| 736 class FakeSSLErrorCallbacks | 733 class FakeSSLErrorCallbacks |
| 737 : public WebSocketEventInterface::SSLErrorCallbacks { | 734 : public WebSocketEventInterface::SSLErrorCallbacks { |
| 738 public: | 735 public: |
| 739 virtual void CancelSSLRequest(int error, const SSLInfo* ssl_info) override {} | 736 void CancelSSLRequest(int error, const SSLInfo* ssl_info) override {} |
| 740 virtual void ContinueSSLRequest() override {} | 737 void ContinueSSLRequest() override {} |
| 741 }; | 738 }; |
| 742 | 739 |
| 743 // Base class for all test fixtures. | 740 // Base class for all test fixtures. |
| 744 class WebSocketChannelTest : public ::testing::Test { | 741 class WebSocketChannelTest : public ::testing::Test { |
| 745 protected: | 742 protected: |
| 746 WebSocketChannelTest() : stream_(new FakeWebSocketStream) {} | 743 WebSocketChannelTest() : stream_(new FakeWebSocketStream) {} |
| 747 | 744 |
| 748 // Creates a new WebSocketChannel and connects it, using the settings stored | 745 // Creates a new WebSocketChannel and connects it, using the settings stored |
| 749 // in |connect_data_|. | 746 // in |connect_data_|. |
| 750 void CreateChannelAndConnect() { | 747 void CreateChannelAndConnect() { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 : deleting_(EVENT_ON_ADD_CHANNEL_RESPONSE | EVENT_ON_DATA_FRAME | | 837 : deleting_(EVENT_ON_ADD_CHANNEL_RESPONSE | EVENT_ON_DATA_FRAME | |
| 841 EVENT_ON_FLOW_CONTROL | | 838 EVENT_ON_FLOW_CONTROL | |
| 842 EVENT_ON_CLOSING_HANDSHAKE | | 839 EVENT_ON_CLOSING_HANDSHAKE | |
| 843 EVENT_ON_FAIL_CHANNEL | | 840 EVENT_ON_FAIL_CHANNEL | |
| 844 EVENT_ON_DROP_CHANNEL | | 841 EVENT_ON_DROP_CHANNEL | |
| 845 EVENT_ON_START_OPENING_HANDSHAKE | | 842 EVENT_ON_START_OPENING_HANDSHAKE | |
| 846 EVENT_ON_FINISH_OPENING_HANDSHAKE | | 843 EVENT_ON_FINISH_OPENING_HANDSHAKE | |
| 847 EVENT_ON_SSL_CERTIFICATE_ERROR) {} | 844 EVENT_ON_SSL_CERTIFICATE_ERROR) {} |
| 848 // Create a ChannelDeletingFakeWebSocketEventInterface. Defined out-of-line to | 845 // Create a ChannelDeletingFakeWebSocketEventInterface. Defined out-of-line to |
| 849 // avoid circular dependency. | 846 // avoid circular dependency. |
| 850 virtual scoped_ptr<WebSocketEventInterface> CreateEventInterface() override; | 847 scoped_ptr<WebSocketEventInterface> CreateEventInterface() override; |
| 851 | 848 |
| 852 // Tests can set deleting_ to a bitmap of EventInterfaceCall members that they | 849 // Tests can set deleting_ to a bitmap of EventInterfaceCall members that they |
| 853 // want to cause Channel deletion. The default is for all calls to cause | 850 // want to cause Channel deletion. The default is for all calls to cause |
| 854 // deletion. | 851 // deletion. |
| 855 int deleting_; | 852 int deleting_; |
| 856 }; | 853 }; |
| 857 | 854 |
| 858 // A FakeWebSocketEventInterface that deletes the WebSocketChannel on failure to | 855 // A FakeWebSocketEventInterface that deletes the WebSocketChannel on failure to |
| 859 // connect. | 856 // connect. |
| 860 class ChannelDeletingFakeWebSocketEventInterface | 857 class ChannelDeletingFakeWebSocketEventInterface |
| 861 : public FakeWebSocketEventInterface { | 858 : public FakeWebSocketEventInterface { |
| 862 public: | 859 public: |
| 863 ChannelDeletingFakeWebSocketEventInterface( | 860 ChannelDeletingFakeWebSocketEventInterface( |
| 864 WebSocketChannelDeletingTest* fixture) | 861 WebSocketChannelDeletingTest* fixture) |
| 865 : fixture_(fixture) {} | 862 : fixture_(fixture) {} |
| 866 | 863 |
| 867 virtual ChannelState OnAddChannelResponse( | 864 ChannelState OnAddChannelResponse(bool fail, |
| 868 bool fail, | 865 const std::string& selected_protocol, |
| 869 const std::string& selected_protocol, | 866 const std::string& extensions) override { |
| 870 const std::string& extensions) override { | |
| 871 return fixture_->DeleteIfDeleting(EVENT_ON_ADD_CHANNEL_RESPONSE); | 867 return fixture_->DeleteIfDeleting(EVENT_ON_ADD_CHANNEL_RESPONSE); |
| 872 } | 868 } |
| 873 | 869 |
| 874 virtual ChannelState OnDataFrame(bool fin, | 870 ChannelState OnDataFrame(bool fin, |
| 875 WebSocketMessageType type, | 871 WebSocketMessageType type, |
| 876 const std::vector<char>& data) override { | 872 const std::vector<char>& data) override { |
| 877 return fixture_->DeleteIfDeleting(EVENT_ON_DATA_FRAME); | 873 return fixture_->DeleteIfDeleting(EVENT_ON_DATA_FRAME); |
| 878 } | 874 } |
| 879 | 875 |
| 880 virtual ChannelState OnFlowControl(int64 quota) override { | 876 ChannelState OnFlowControl(int64 quota) override { |
| 881 return fixture_->DeleteIfDeleting(EVENT_ON_FLOW_CONTROL); | 877 return fixture_->DeleteIfDeleting(EVENT_ON_FLOW_CONTROL); |
| 882 } | 878 } |
| 883 | 879 |
| 884 virtual ChannelState OnClosingHandshake() override { | 880 ChannelState OnClosingHandshake() override { |
| 885 return fixture_->DeleteIfDeleting(EVENT_ON_CLOSING_HANDSHAKE); | 881 return fixture_->DeleteIfDeleting(EVENT_ON_CLOSING_HANDSHAKE); |
| 886 } | 882 } |
| 887 | 883 |
| 888 virtual ChannelState OnFailChannel(const std::string& message) override { | 884 ChannelState OnFailChannel(const std::string& message) override { |
| 889 return fixture_->DeleteIfDeleting(EVENT_ON_FAIL_CHANNEL); | 885 return fixture_->DeleteIfDeleting(EVENT_ON_FAIL_CHANNEL); |
| 890 } | 886 } |
| 891 | 887 |
| 892 virtual ChannelState OnDropChannel(bool was_clean, | 888 ChannelState OnDropChannel(bool was_clean, |
| 893 uint16 code, | 889 uint16 code, |
| 894 const std::string& reason) override { | 890 const std::string& reason) override { |
| 895 return fixture_->DeleteIfDeleting(EVENT_ON_DROP_CHANNEL); | 891 return fixture_->DeleteIfDeleting(EVENT_ON_DROP_CHANNEL); |
| 896 } | 892 } |
| 897 | 893 |
| 898 virtual ChannelState OnStartOpeningHandshake( | 894 ChannelState OnStartOpeningHandshake( |
| 899 scoped_ptr<WebSocketHandshakeRequestInfo> request) override { | 895 scoped_ptr<WebSocketHandshakeRequestInfo> request) override { |
| 900 return fixture_->DeleteIfDeleting(EVENT_ON_START_OPENING_HANDSHAKE); | 896 return fixture_->DeleteIfDeleting(EVENT_ON_START_OPENING_HANDSHAKE); |
| 901 } | 897 } |
| 902 virtual ChannelState OnFinishOpeningHandshake( | 898 ChannelState OnFinishOpeningHandshake( |
| 903 scoped_ptr<WebSocketHandshakeResponseInfo> response) override { | 899 scoped_ptr<WebSocketHandshakeResponseInfo> response) override { |
| 904 return fixture_->DeleteIfDeleting(EVENT_ON_FINISH_OPENING_HANDSHAKE); | 900 return fixture_->DeleteIfDeleting(EVENT_ON_FINISH_OPENING_HANDSHAKE); |
| 905 } | 901 } |
| 906 virtual ChannelState OnSSLCertificateError( | 902 ChannelState OnSSLCertificateError( |
| 907 scoped_ptr<SSLErrorCallbacks> ssl_error_callbacks, | 903 scoped_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
| 908 const GURL& url, | 904 const GURL& url, |
| 909 const SSLInfo& ssl_info, | 905 const SSLInfo& ssl_info, |
| 910 bool fatal) override { | 906 bool fatal) override { |
| 911 return fixture_->DeleteIfDeleting(EVENT_ON_SSL_CERTIFICATE_ERROR); | 907 return fixture_->DeleteIfDeleting(EVENT_ON_SSL_CERTIFICATE_ERROR); |
| 912 } | 908 } |
| 913 | 909 |
| 914 private: | 910 private: |
| 915 // A pointer to the test fixture. Owned by the test harness; this object will | 911 // A pointer to the test fixture. Owned by the test harness; this object will |
| 916 // be deleted before it is. | 912 // be deleted before it is. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 939 } | 935 } |
| 940 | 936 |
| 941 virtual ~WebSocketChannelEventInterfaceTest() { | 937 virtual ~WebSocketChannelEventInterfaceTest() { |
| 942 DefaultValue<ChannelState>::Clear(); | 938 DefaultValue<ChannelState>::Clear(); |
| 943 } | 939 } |
| 944 | 940 |
| 945 // Tests using this fixture must set expectations on the event_interface_ mock | 941 // Tests using this fixture must set expectations on the event_interface_ mock |
| 946 // object before calling CreateChannelAndConnect() or | 942 // object before calling CreateChannelAndConnect() or |
| 947 // CreateChannelAndConnectSuccessfully(). This will only work once per test | 943 // CreateChannelAndConnectSuccessfully(). This will only work once per test |
| 948 // case, but once should be enough. | 944 // case, but once should be enough. |
| 949 virtual scoped_ptr<WebSocketEventInterface> CreateEventInterface() override { | 945 scoped_ptr<WebSocketEventInterface> CreateEventInterface() override { |
| 950 return scoped_ptr<WebSocketEventInterface>(event_interface_.release()); | 946 return scoped_ptr<WebSocketEventInterface>(event_interface_.release()); |
| 951 } | 947 } |
| 952 | 948 |
| 953 scoped_ptr<MockWebSocketEventInterface> event_interface_; | 949 scoped_ptr<MockWebSocketEventInterface> event_interface_; |
| 954 }; | 950 }; |
| 955 | 951 |
| 956 // Base class for tests which verify that WebSocketStream methods are called | 952 // Base class for tests which verify that WebSocketStream methods are called |
| 957 // appropriately by using a MockWebSocketStream. | 953 // appropriately by using a MockWebSocketStream. |
| 958 class WebSocketChannelStreamTest : public WebSocketChannelTest { | 954 class WebSocketChannelStreamTest : public WebSocketChannelTest { |
| 959 protected: | 955 protected: |
| 960 WebSocketChannelStreamTest() | 956 WebSocketChannelStreamTest() |
| 961 : mock_stream_(new StrictMock<MockWebSocketStream>) {} | 957 : mock_stream_(new StrictMock<MockWebSocketStream>) {} |
| 962 | 958 |
| 963 virtual void CreateChannelAndConnectSuccessfully() override { | 959 void CreateChannelAndConnectSuccessfully() override { |
| 964 set_stream(mock_stream_.Pass()); | 960 set_stream(mock_stream_.Pass()); |
| 965 WebSocketChannelTest::CreateChannelAndConnectSuccessfully(); | 961 WebSocketChannelTest::CreateChannelAndConnectSuccessfully(); |
| 966 } | 962 } |
| 967 | 963 |
| 968 scoped_ptr<MockWebSocketStream> mock_stream_; | 964 scoped_ptr<MockWebSocketStream> mock_stream_; |
| 969 }; | 965 }; |
| 970 | 966 |
| 971 // Fixture for tests which test UTF-8 validation of sent Text frames via the | 967 // Fixture for tests which test UTF-8 validation of sent Text frames via the |
| 972 // EventInterface. | 968 // EventInterface. |
| 973 class WebSocketChannelSendUtf8Test | 969 class WebSocketChannelSendUtf8Test |
| (...skipping 2333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3307 EXPECT_CALL(*mock_stream_, Close()); | 3303 EXPECT_CALL(*mock_stream_, Close()); |
| 3308 | 3304 |
| 3309 CreateChannelAndConnectSuccessfully(); | 3305 CreateChannelAndConnectSuccessfully(); |
| 3310 } | 3306 } |
| 3311 | 3307 |
| 3312 // Set the closing handshake timeout to a very tiny value before connecting. | 3308 // Set the closing handshake timeout to a very tiny value before connecting. |
| 3313 class WebSocketChannelStreamTimeoutTest : public WebSocketChannelStreamTest { | 3309 class WebSocketChannelStreamTimeoutTest : public WebSocketChannelStreamTest { |
| 3314 protected: | 3310 protected: |
| 3315 WebSocketChannelStreamTimeoutTest() {} | 3311 WebSocketChannelStreamTimeoutTest() {} |
| 3316 | 3312 |
| 3317 virtual void CreateChannelAndConnectSuccessfully() override { | 3313 void CreateChannelAndConnectSuccessfully() override { |
| 3318 set_stream(mock_stream_.Pass()); | 3314 set_stream(mock_stream_.Pass()); |
| 3319 CreateChannelAndConnect(); | 3315 CreateChannelAndConnect(); |
| 3320 channel_->SendFlowControl(kPlentyOfQuota); | 3316 channel_->SendFlowControl(kPlentyOfQuota); |
| 3321 channel_->SetClosingHandshakeTimeoutForTesting( | 3317 channel_->SetClosingHandshakeTimeoutForTesting( |
| 3322 TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis)); | 3318 TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis)); |
| 3323 connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass()); | 3319 connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass()); |
| 3324 } | 3320 } |
| 3325 }; | 3321 }; |
| 3326 | 3322 |
| 3327 // In this case the server initiates the closing handshake with a Close | 3323 // In this case the server initiates the closing handshake with a Close |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3425 channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK"); | 3421 channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK"); |
| 3426 ASSERT_TRUE(read_frames); | 3422 ASSERT_TRUE(read_frames); |
| 3427 // Provide the "Close" message from the server. | 3423 // Provide the "Close" message from the server. |
| 3428 *read_frames = CreateFrameVector(frames); | 3424 *read_frames = CreateFrameVector(frames); |
| 3429 read_callback.Run(OK); | 3425 read_callback.Run(OK); |
| 3430 completion.WaitForResult(); | 3426 completion.WaitForResult(); |
| 3431 } | 3427 } |
| 3432 | 3428 |
| 3433 } // namespace | 3429 } // namespace |
| 3434 } // namespace net | 3430 } // namespace net |
| OLD | NEW |