|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Arm Limited and affiliates |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "gtest/gtest.h" |
| 19 | +#include "features/netsocket/TCPSocket.h" |
| 20 | +#include "features/netsocket/TCPServer.h" |
| 21 | +#include "NetworkStack_stub.h" |
| 22 | + |
| 23 | +// Control the rtos EventFlags stub. See EventFlags_stub.cpp |
| 24 | +extern std::list<uint32_t> eventFlagsStubNextRetval; |
| 25 | + |
| 26 | +class TestTCPServer : public testing::Test { |
| 27 | +public: |
| 28 | + unsigned int dataSize = 10; |
| 29 | + char dataBuf[10]; |
| 30 | +protected: |
| 31 | + TCPSocket *socket; |
| 32 | + TCPServer *server; |
| 33 | + NetworkStackstub stack; |
| 34 | + |
| 35 | + virtual void SetUp() |
| 36 | + { |
| 37 | + server = new TCPServer(); |
| 38 | + socket = new TCPSocket(); |
| 39 | + } |
| 40 | + |
| 41 | + virtual void TearDown() |
| 42 | + { |
| 43 | + stack.return_values.clear(); |
| 44 | + eventFlagsStubNextRetval.clear(); |
| 45 | + delete socket; |
| 46 | + delete server; |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +TEST_F(TestTCPServer, constructor) |
| 51 | +{ |
| 52 | + EXPECT_TRUE(server); |
| 53 | +} |
| 54 | + |
| 55 | +TEST_F(TestTCPServer, constructor_parameters) |
| 56 | +{ |
| 57 | + TCPServer serverParam = TCPServer(dynamic_cast<NetworkStack*>(&stack)); |
| 58 | + const SocketAddress a("127.0.0.1", 1024); |
| 59 | + EXPECT_EQ(serverParam.connect(a), NSAPI_ERROR_OK); |
| 60 | +} |
| 61 | + |
| 62 | +TEST_F(TestTCPServer, accept) |
| 63 | +{ |
| 64 | + const SocketAddress a("127.0.0.1", 1024); |
| 65 | + EXPECT_EQ(socket->open(static_cast<NetworkStack *>(&stack)), NSAPI_ERROR_OK); |
| 66 | + EXPECT_EQ(socket->connect(a), NSAPI_ERROR_OK); |
| 67 | + nsapi_error_t error; |
| 68 | + EXPECT_EQ(server->open(static_cast<NetworkStack *>(&stack)), NSAPI_ERROR_OK); |
| 69 | + EXPECT_EQ(server->bind(a), NSAPI_ERROR_OK); |
| 70 | + server->listen(1); |
| 71 | + SocketAddress client_addr; |
| 72 | + EXPECT_EQ(server->accept(socket, &client_addr), NSAPI_ERROR_OK); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(TestTCPServer, accept_no_socket) |
| 76 | +{ |
| 77 | + SocketAddress client_addr; |
| 78 | + EXPECT_EQ(server->accept(socket, &client_addr), NSAPI_ERROR_NO_SOCKET); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_F(TestTCPServer, accept_error) |
| 82 | +{ |
| 83 | + SocketAddress client_addr; |
| 84 | + EXPECT_EQ(server->open(static_cast<NetworkStack *>(&stack)), NSAPI_ERROR_OK); |
| 85 | + stack.return_value = NSAPI_ERROR_AUTH_FAILURE; |
| 86 | + EXPECT_EQ(server->accept(socket, &client_addr), NSAPI_ERROR_AUTH_FAILURE); |
| 87 | +} |
| 88 | + |
| 89 | +TEST_F(TestTCPServer, accept_error_would_block) |
| 90 | +{ |
| 91 | + SocketAddress client_addr; |
| 92 | + EXPECT_EQ(server->open(static_cast<NetworkStack *>(&stack)), NSAPI_ERROR_OK); |
| 93 | + stack.return_value = NSAPI_ERROR_WOULD_BLOCK; |
| 94 | + eventFlagsStubNextRetval.push_back(0); |
| 95 | + eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop |
| 96 | + |
| 97 | + EXPECT_EQ(server->accept(socket, &client_addr), NSAPI_ERROR_WOULD_BLOCK); |
| 98 | +} |
0 commit comments