Skip to content

Commit aa90f5b

Browse files
michalpasztamobicaSeppo Takalo
authored andcommitted
unittests: Add TCPServer unit tests
TCPServer class only really implements attach method.
1 parent 51dedfd commit aa90f5b

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
# Unit test suite name
7+
set(TEST_SUITE_NAME "features_netsocket_TCPServer")
8+
9+
set(unittest-sources
10+
../features/netsocket/SocketAddress.cpp
11+
../features/netsocket/NetworkStack.cpp
12+
../features/netsocket/InternetSocket.cpp
13+
../features/netsocket/TCPSocket.cpp
14+
../features/netsocket/TCPServer.cpp
15+
../features/frameworks/nanostack-libservice/source/libip4string/ip4tos.c
16+
../features/frameworks/nanostack-libservice/source/libip4string/stoip4.c
17+
)
18+
19+
set(unittest-test-sources
20+
stubs/Mutex_stub.cpp
21+
stubs/mbed_assert_stub.c
22+
stubs/equeue_stub.c
23+
stubs/EventQueue_stub.cpp
24+
stubs/mbed_shared_queues_stub.cpp
25+
stubs/nsapi_dns_stub.cpp
26+
stubs/EventFlags_stub.cpp
27+
features/netsocket/TCPServer/test_TCPServer.cpp
28+
)

0 commit comments

Comments
 (0)