Skip to content

Commit d740d69

Browse files
michalpasztamobicaSeppo Takalo
authored andcommitted
unittests: InternetSocket class coverage improved.
Added more tests, improved the existing ones. setblocking tests were not checking anything, so they were removed and these functions are called in TCPSocket tests instead.
1 parent e609e2f commit d740d69

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,13 @@
1919
#include "features/netsocket/InternetSocket.h"
2020
#include "NetworkStack_stub.h"
2121

22+
extern std::list<uint32_t> eventFlagsStubNextRetval;
23+
24+
// InternetSocket is an abstract class, so we have to test it via its child.
2225
class stubInternetSocket : public InternetSocket {
2326
protected:
2427
nsapi_error_t return_value = 0;
2528
public:
26-
virtual void event()
27-
{
28-
if (_callback) {
29-
_callback.call();
30-
}
31-
}
32-
3329
virtual nsapi_error_t connect(const SocketAddress &address)
3430
{
3531
return return_value;
@@ -60,6 +56,15 @@ class stubInternetSocket : public InternetSocket {
6056
{
6157
return return_value;
6258
}
59+
60+
// Testing functions
61+
void add_reader (void) { _readers++;}
62+
void rem_reader (void) { _readers--;}
63+
void add_writer (void) { _writers++;}
64+
void rem_writer (void) { _writers--;}
65+
void add_pending (void) { _pending++;}
66+
void rem_pending (void) { _pending--;}
67+
6368
protected:
6469
virtual nsapi_protocol_t get_proto()
6570
{
@@ -94,7 +99,6 @@ TEST_F(TestInternetSocket, constructor)
9499
EXPECT_TRUE(socket);
95100
}
96101

97-
98102
TEST_F(TestInternetSocket, open_null_stack)
99103
{
100104
EXPECT_EQ(socket->open(NULL), NSAPI_ERROR_PARAMETER);
@@ -126,34 +130,81 @@ TEST_F(TestInternetSocket, close)
126130
EXPECT_EQ(socket->close(), NSAPI_ERROR_OK);
127131
}
128132

133+
TEST_F(TestInternetSocket, close_no_open)
134+
{
135+
stack.return_value = NSAPI_ERROR_OK;
136+
EXPECT_EQ(socket->close(), NSAPI_ERROR_OK);
137+
}
138+
139+
TEST_F(TestInternetSocket, close_during_read)
140+
{
141+
stack.return_value = NSAPI_ERROR_OK;
142+
socket->open((NetworkStack *)&stack);
143+
// when c++11 is available use something like the code below to test the blocking behavior
144+
// socket->add_reader();
145+
// std::async(c[](){std::this_thread::sleep_for(1ms); socket->rem_reader()});
146+
EXPECT_EQ(socket->close(), NSAPI_ERROR_OK);
147+
}
148+
129149
TEST_F(TestInternetSocket, modify_multicast_group)
130150
{
131151
SocketAddress a("127.0.0.1", 1024);
132152
stack.return_value = NSAPI_ERROR_OK;
133153
socket->open((NetworkStack *)&stack);
134-
154+
// when c++11 is available use something like the code below to test the blocking behavior
155+
// socket->add_reader();
156+
// std::async(c[](){std::this_thread::sleep_for(1ms); socket->rem_reader()});
135157
EXPECT_EQ(socket->join_multicast_group(a), NSAPI_ERROR_UNSUPPORTED);
136158
EXPECT_EQ(socket->leave_multicast_group(a), NSAPI_ERROR_UNSUPPORTED);
137159
}
138160

139-
TEST_F(TestInternetSocket, set_blocking)
161+
// set_blocking and set_timeout are tested within TCPSocket.
162+
163+
TEST_F(TestInternetSocket, bind_no_socket)
164+
{
165+
EXPECT_EQ(socket->bind(1), NSAPI_ERROR_NO_SOCKET);
166+
}
167+
168+
TEST_F(TestInternetSocket, bind)
169+
{
170+
socket->open((NetworkStack *)&stack);
171+
EXPECT_EQ(socket->bind("127.0.0.1", 80), NSAPI_ERROR_OK);
172+
}
173+
174+
TEST_F(TestInternetSocket, bind_nullstring)
140175
{
141-
socket->set_blocking(false);
142-
socket->set_blocking(true);
176+
socket->open((NetworkStack *)&stack);
177+
EXPECT_EQ(socket->bind(NULL, 80), NSAPI_ERROR_OK);
143178
}
144179

180+
// setsockopt and getsockopt are really just calling the underlying stack functions
181+
145182
TEST_F(TestInternetSocket, setsockopt_no_stack)
146183
{
147-
socket->close();
148184
EXPECT_EQ(socket->setsockopt(0, 0, 0, 0), NSAPI_ERROR_NO_SOCKET);
149185
}
150186

187+
TEST_F(TestInternetSocket, setsockopt)
188+
{
189+
socket->open((NetworkStack *)&stack);
190+
EXPECT_EQ(socket->setsockopt(0, 0, 0, 0), NSAPI_ERROR_UNSUPPORTED);
191+
}
192+
193+
TEST_F(TestInternetSocket, getsockopt_no_stack)
194+
{
195+
EXPECT_EQ(socket->getsockopt(0, 0, 0, 0), NSAPI_ERROR_NO_SOCKET);
196+
}
197+
198+
TEST_F(TestInternetSocket, getsockopt)
199+
{
200+
socket->open((NetworkStack *)&stack);
201+
EXPECT_EQ(socket->getsockopt(0, 0, 0, 0), NSAPI_ERROR_UNSUPPORTED);
202+
}
203+
151204
TEST_F(TestInternetSocket, sigio)
152205
{
153206
callback_is_called = false;
154-
// I'm calling sigio() through the DEPRECATED method, just to get coverage for both.
155-
// Not sure if this is wise at all, we should not aim for 100%
156207
socket->sigio(mbed::callback(my_callback));
157-
socket->event();
208+
socket->close(); // Trigger event;
158209
EXPECT_EQ(callback_is_called, true);
159210
}

UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ TEST_F(TestTCPSocket, connect_with_timeout)
100100
stack.return_value = NSAPI_ERROR_IN_PROGRESS;
101101
const SocketAddress a("127.0.0.1", 1024);
102102
eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop
103-
socket->set_timeout(1);
103+
socket->set_blocking(true);
104104
EXPECT_EQ(socket->connect(a), NSAPI_ERROR_IN_PROGRESS);
105105
}
106106

@@ -170,7 +170,7 @@ TEST_F(TestTCPSocket, send_error_no_timeout)
170170
{
171171
socket->open((NetworkStack *)&stack);
172172
stack.return_value = NSAPI_ERROR_WOULD_BLOCK;
173-
socket->set_timeout(0);
173+
socket->set_blocking(false);
174174
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
175175
}
176176

0 commit comments

Comments
 (0)