takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_TCPServer.cpp Source File

test_TCPServer.cpp

00001 /*
00002  * Copyright (c) 2018, Arm Limited and affiliates
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "gtest/gtest.h"
00019 #include "features/netsocket/TCPSocket.h"
00020 #include "features/netsocket/TCPServer.h"
00021 #include "NetworkStack_stub.h"
00022 
00023 // Control the rtos EventFlags stub. See EventFlags_stub.cpp
00024 extern std::list<uint32_t> eventFlagsStubNextRetval;
00025 
00026 class TestTCPServer : public testing::Test {
00027 public:
00028     unsigned int dataSize = 10;
00029     char dataBuf[10];
00030 protected:
00031     TCPSocket *socket;
00032     TCPServer *server;
00033     NetworkStackstub stack;
00034 
00035     virtual void SetUp()
00036     {
00037         server = new TCPServer();
00038         socket = new TCPSocket();
00039     }
00040 
00041     virtual void TearDown()
00042     {
00043         stack.return_values.clear();
00044         eventFlagsStubNextRetval.clear();
00045         delete socket;
00046         delete server;
00047     }
00048 };
00049 
00050 TEST_F(TestTCPServer, constructor)
00051 {
00052     EXPECT_TRUE(server);
00053 }
00054 
00055 TEST_F(TestTCPServer, constructor_parameters)
00056 {
00057     TCPServer serverParam = TCPServer(dynamic_cast<NetworkStack*>(&stack));
00058     const SocketAddress a("127.0.0.1", 1024);
00059     EXPECT_EQ(serverParam.connect(a), NSAPI_ERROR_OK );
00060 }
00061 
00062 TEST_F(TestTCPServer, accept)
00063 {
00064     const SocketAddress a("127.0.0.1", 1024);
00065     EXPECT_EQ(socket->open(static_cast<NetworkStack *>(&stack)), NSAPI_ERROR_OK );
00066     EXPECT_EQ(socket->connect(a), NSAPI_ERROR_OK );
00067     nsapi_error_t error;
00068     EXPECT_EQ(server->open(static_cast<NetworkStack *>(&stack)), NSAPI_ERROR_OK );
00069     EXPECT_EQ(server->bind(a), NSAPI_ERROR_OK );
00070     server->listen(1);
00071     SocketAddress client_addr;
00072     EXPECT_EQ(server->accept(socket, &client_addr), NSAPI_ERROR_OK );
00073 }
00074 
00075 TEST_F(TestTCPServer, accept_no_socket)
00076 {
00077     SocketAddress client_addr;
00078     EXPECT_EQ(server->accept(socket, &client_addr), NSAPI_ERROR_NO_SOCKET );
00079 }
00080 
00081 TEST_F(TestTCPServer, accept_error)
00082 {
00083     SocketAddress client_addr;
00084     EXPECT_EQ(server->open(static_cast<NetworkStack *>(&stack)), NSAPI_ERROR_OK );
00085     stack.return_value = NSAPI_ERROR_AUTH_FAILURE ;
00086     EXPECT_EQ(server->accept(socket, &client_addr), NSAPI_ERROR_AUTH_FAILURE );
00087 }
00088 
00089 TEST_F(TestTCPServer, accept_error_would_block)
00090 {
00091     SocketAddress client_addr;
00092     EXPECT_EQ(server->open(static_cast<NetworkStack *>(&stack)), NSAPI_ERROR_OK );
00093     stack.return_value = NSAPI_ERROR_WOULD_BLOCK ;
00094     eventFlagsStubNextRetval.push_back(0);
00095     eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop
00096 
00097     EXPECT_EQ(server->accept(socket, &client_addr), NSAPI_ERROR_WOULD_BLOCK );
00098 }