Skip to content

Commit 6b0639d

Browse files
authored
Merge pull request #9226 from mirelachirica/feature-cellular-refactor_nonip_ut
Cellular: Unit tests for Non-IP socket
2 parents 2af12ae + 6533e19 commit 6b0639d

File tree

14 files changed

+559
-81
lines changed

14 files changed

+559
-81
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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/cellular/CellularNonIPSocket.h"
20+
#include "CellularContext_stub.h"
21+
22+
using namespace mbed;
23+
24+
// Control the rtos EventFlags stub. See EventFlags_stub.cpp
25+
extern std::list<uint32_t> eventFlagsStubNextRetval;
26+
27+
static bool callback_is_called;
28+
static void my_callback()
29+
{
30+
callback_is_called = true;
31+
}
32+
33+
class TestCellularNonIPSocket : public testing::Test {
34+
protected:
35+
CellularNonIPSocket *socket;
36+
ControlPlane_netif_stub *cp_netif;
37+
CellularContext_stub cellular_context;
38+
nsapi_size_t dataSize;
39+
char dataBuf[10];
40+
41+
virtual void SetUp()
42+
{
43+
socket = new CellularNonIPSocket();
44+
cp_netif = NULL;
45+
dataSize = 10;
46+
}
47+
48+
virtual void TearDown()
49+
{
50+
delete socket;
51+
}
52+
};
53+
54+
TEST_F(TestCellularNonIPSocket, open_null_cp_netif)
55+
{
56+
EXPECT_EQ(socket->open(static_cast<ControlPlane_netif *>(NULL)), NSAPI_ERROR_PARAMETER);
57+
}
58+
59+
TEST_F(TestCellularNonIPSocket, open_null_context)
60+
{
61+
EXPECT_EQ(socket->open(static_cast<CellularContext *>(NULL)), NSAPI_ERROR_PARAMETER);
62+
}
63+
64+
TEST_F(TestCellularNonIPSocket, open_context)
65+
{
66+
EXPECT_EQ(socket->open((CellularContext *)&cellular_context), NSAPI_ERROR_OK);
67+
}
68+
69+
TEST_F(TestCellularNonIPSocket, open_cp_netif)
70+
{
71+
cp_netif = cellular_context.get_cp_netif();
72+
EXPECT_EQ(socket->open((ControlPlane_netif *)cp_netif), NSAPI_ERROR_OK);
73+
}
74+
75+
TEST_F(TestCellularNonIPSocket, open_twice)
76+
{
77+
EXPECT_EQ(socket->open((CellularContext *)&cellular_context), NSAPI_ERROR_OK);
78+
EXPECT_EQ(socket->open((CellularContext *)&cellular_context), NSAPI_ERROR_PARAMETER);
79+
}
80+
81+
TEST_F(TestCellularNonIPSocket, close)
82+
{
83+
socket->open((CellularContext *)&cellular_context);
84+
EXPECT_EQ(socket->close(), NSAPI_ERROR_OK);
85+
}
86+
87+
TEST_F(TestCellularNonIPSocket, close_no_open)
88+
{
89+
EXPECT_EQ(socket->close(), NSAPI_ERROR_NO_SOCKET);
90+
}
91+
92+
TEST_F(TestCellularNonIPSocket, sigio)
93+
{
94+
callback_is_called = false;
95+
socket->open((CellularContext *)&cellular_context);
96+
socket->sigio(mbed::callback(my_callback));
97+
socket->close(); // Trigger event;
98+
EXPECT_EQ(callback_is_called, true);
99+
}
100+
101+
/* send */
102+
103+
TEST_F(TestCellularNonIPSocket, send_no_open)
104+
{
105+
EXPECT_EQ(socket->send((char *)dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET);
106+
}
107+
108+
TEST_F(TestCellularNonIPSocket, send_error_would_block)
109+
{
110+
socket->open((CellularContext *)&cellular_context);
111+
cp_netif = cellular_context.get_cp_netif();
112+
cp_netif->return_value = NSAPI_ERROR_WOULD_BLOCK;
113+
eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop
114+
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
115+
}
116+
117+
TEST_F(TestCellularNonIPSocket, send_error_other)
118+
{
119+
socket->open((CellularContext *)&cellular_context);
120+
cp_netif = cellular_context.get_cp_netif();
121+
cp_netif->return_value = NSAPI_ERROR_NO_MEMORY;
122+
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_NO_MEMORY);
123+
}
124+
125+
TEST_F(TestCellularNonIPSocket, send_error_no_timeout)
126+
{
127+
socket->open((CellularContext *)&cellular_context);
128+
cp_netif = cellular_context.get_cp_netif();
129+
cp_netif->return_value = NSAPI_ERROR_WOULD_BLOCK;
130+
socket->set_blocking(false);
131+
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
132+
}
133+
134+
TEST_F(TestCellularNonIPSocket, send)
135+
{
136+
socket->open((CellularContext *)&cellular_context);
137+
cp_netif = cellular_context.get_cp_netif();
138+
cp_netif->return_value = dataSize;
139+
EXPECT_EQ(socket->send(dataBuf, dataSize), dataSize);
140+
}
141+
142+
TEST_F(TestCellularNonIPSocket, recv)
143+
{
144+
EXPECT_EQ(socket->recv(&dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET);
145+
146+
socket->open((CellularContext *)&cellular_context);
147+
cp_netif = cellular_context.get_cp_netif();
148+
149+
cp_netif->return_value = 100;
150+
EXPECT_EQ(socket->recv(&dataBuf, dataSize), 100);
151+
152+
cp_netif->return_value = NSAPI_ERROR_WOULD_BLOCK;
153+
eventFlagsStubNextRetval.push_back(0);
154+
eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop
155+
EXPECT_EQ(socket->recv(&dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
156+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
# Add test specific include paths
7+
set(unittest-includes ${unittest-includes}
8+
../features/netsocket/cellular
9+
)
10+
11+
set(unittest-sources
12+
../features/netsocket/cellular/CellularNonIPSocket.cpp
13+
)
14+
15+
set(unittest-test-sources
16+
features/netsocket/cellular/CellularNonIPSocket/test_CellularNonIPSocket.cpp
17+
stubs/NetworkInterface_stub.cpp
18+
stubs/NetworkStack_stub.cpp
19+
stubs/EventFlags_stub.cpp
20+
stubs/Mutex_stub.cpp
21+
)

UNITTESTS/stubs/AT_CellularContext_stub.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using namespace mbed;
2121

2222
AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
2323
AT_CellularBase(at), _is_blocking(true), _is_connected(false),
24-
_current_op(OP_INVALID), _device(device), _nw(0), _fh(0), _cp_req(cp_req), _nonip_req(nonip_req), _cp_in_use(false), _cp_netif(NULL)
24+
_current_op(OP_INVALID), _device(device), _nw(0), _fh(0), _cp_req(cp_req), _nonip_req(nonip_req), _cp_in_use(false)
2525
{
2626
_stack = NULL;
2727
_pdp_type = DEFAULT_PDP_TYPE;
@@ -36,6 +36,7 @@ AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, co
3636
_cid = -1;
3737
_new_context_set = false;
3838
_next = NULL;
39+
_cp_netif = NULL;
3940
}
4041

4142
AT_CellularContext::~AT_CellularContext()

0 commit comments

Comments
 (0)