Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013, Matt Godbolt |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | #include "MockServerImpl.h" |
| 27 | #include "seasocks/Connection.h" |
| 28 | #include "seasocks/IgnoringLogger.h" |
| 29 | |
| 30 | #include <gmock/gmock.h> |
| 31 | |
| 32 | #include <iostream> |
| 33 | #include <sstream> |
| 34 | #include <string.h> |
| 35 | #include <string> |
| 36 | |
| 37 | using namespace seasocks; |
| 38 | |
| 39 | class TestHandler: public WebSocket::Handler { |
| 40 | public: |
| 41 | int _stage; |
| 42 | TestHandler() : |
| 43 | _stage(0) { |
| 44 | } |
| 45 | ~TestHandler() { |
| 46 | if (_stage != 2) { |
| 47 | ADD_FAILURE() << "Invalid state"; |
| 48 | } |
| 49 | } |
| 50 | virtual void onConnect(WebSocket*) { |
| 51 | } |
| 52 | virtual void onData(WebSocket*, const char* data) { |
| 53 | if (_stage == 0) { |
| 54 | ASSERT_STREQ(data, "a"); |
| 55 | } else if (_stage == 1) { |
| 56 | ASSERT_STREQ(data, "b"); |
| 57 | } else { |
| 58 | FAIL() << "unexpected state"; |
| 59 | } |
| 60 | ++_stage; |
| 61 | } |
| 62 | virtual void onDisconnect(WebSocket*) { |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | TEST(ConnectionTests, shouldBreakHixieMessagesApartInSameBuffer) { |
| 67 | sockaddr_in addr = { AF_INET, 0x1234, { 0x01020304 } }; |
| 68 | std::shared_ptr<Logger> logger(new IgnoringLogger); |
| 69 | testing::NiceMock<MockServerImpl> mockServer; |
| 70 | Connection connection(logger, mockServer, -1, addr); |
| 71 | connection.setHandler( |
| 72 | std::shared_ptr<WebSocket::Handler>(new TestHandler)); |
| 73 | uint8_t foo[] = { 0x00, 'a', 0xff, 0x00, 'b', 0xff }; |
| 74 | connection.getInputBuffer().assign(&foo[0], &foo[sizeof(foo)]); |
| 75 | connection.handleHixieWebSocket(); |
| 76 | SUCCEED(); |
| 77 | } |
| 78 | |
| 79 | TEST(ConnectionTests, shouldAcceptMultipleConnectionTypes) { |
| 80 | sockaddr_in addr = { AF_INET, 0x1234, { 0x01020304 } }; |
| 81 | std::shared_ptr<Logger> logger(new IgnoringLogger); |
| 82 | testing::NiceMock<MockServerImpl> mockServer; |
| 83 | Connection connection(logger, mockServer, -1, addr); |
| 84 | const uint8_t message[] = "GET /ws-test HTTP/1.1\r\nConnection: keep-alive, Upgrade\r\nUpgrade: websocket\r\n\r\n"; |
| 85 | connection.getInputBuffer().assign(&message[0], &message[sizeof(message)]); |
| 86 | EXPECT_CALL(mockServer, getWebSocketHandler(testing::StrEq("/ws-test"))) |
| 87 | .WillOnce(testing::Return(std::shared_ptr<WebSocket::Handler>())); |
| 88 | connection.handleNewData(); |
| 89 | } |