17
17
18
18
#include " gtest/gtest.h"
19
19
#include " features/netsocket/TCPSocket.h"
20
+ #include " NetworkStack_stub.h"
21
+
22
+ // Control the rtos EventFlags stub. See EventFlags_stub.cpp
23
+ extern std::list<uint32_t > eventFlagsStubNextRetval;
24
+
25
+ // To test protected functions
26
+ class TCPSocketFriend : public TCPSocket {
27
+ friend class TestTCPSocket ;
28
+ FRIEND_TEST (TestTCPSocket, get_proto);
29
+ };
20
30
21
31
class TestTCPSocket : public testing ::Test {
32
+ public:
33
+ unsigned int dataSize = 10 ;
34
+ char dataBuf[10 ];
22
35
protected:
23
36
TCPSocket *socket;
37
+ NetworkStackstub stack;
24
38
25
39
virtual void SetUp ()
26
40
{
@@ -29,11 +43,227 @@ class TestTCPSocket : public testing::Test {
29
43
30
44
virtual void TearDown ()
31
45
{
46
+ stack.return_values .clear ();
47
+ eventFlagsStubNextRetval.clear ();
32
48
delete socket;
33
49
}
34
50
};
35
51
52
+ TEST_F (TestTCPSocket, get_proto)
53
+ {
54
+ TCPSocketFriend tcpFriend;
55
+ EXPECT_EQ (tcpFriend.get_proto (), NSAPI_TCP);
56
+ }
57
+
36
58
TEST_F (TestTCPSocket, constructor)
37
59
{
38
60
EXPECT_TRUE (socket);
39
61
}
62
+
63
+ TEST_F (TestTCPSocket, constructor_parameters)
64
+ {
65
+ TCPSocket socketParam = TCPSocket (dynamic_cast <NetworkStack*>(&stack));
66
+ const SocketAddress a (" 127.0.0.1" , 1024 );
67
+ EXPECT_EQ (socketParam.connect (a), NSAPI_ERROR_OK);
68
+ }
69
+
70
+ /* connect */
71
+
72
+ TEST_F (TestTCPSocket, connect)
73
+ {
74
+ socket->open ((NetworkStack *)&stack);
75
+
76
+ stack.return_value = NSAPI_ERROR_OK;
77
+ const SocketAddress a (" 127.0.0.1" , 1024 );
78
+ EXPECT_EQ (socket->connect (a), NSAPI_ERROR_OK);
79
+ }
80
+
81
+ TEST_F (TestTCPSocket, connect_no_open)
82
+ {
83
+ stack.return_value = NSAPI_ERROR_OK;
84
+ const SocketAddress a (" 127.0.0.1" , 1024 );
85
+ EXPECT_EQ (socket->connect (a), NSAPI_ERROR_NO_SOCKET);
86
+ }
87
+
88
+ TEST_F (TestTCPSocket, connect_error_in_progress_no_timeout)
89
+ {
90
+ socket->open ((NetworkStack *)&stack);
91
+ stack.return_value = NSAPI_ERROR_IN_PROGRESS;
92
+ const SocketAddress a (" 127.0.0.1" , 1024 );
93
+ eventFlagsStubNextRetval.push_back (osFlagsError); // Break the wait loop
94
+ EXPECT_EQ (socket->connect (a), NSAPI_ERROR_IN_PROGRESS);
95
+ }
96
+
97
+ TEST_F (TestTCPSocket, connect_with_timeout)
98
+ {
99
+ socket->open ((NetworkStack *)&stack);
100
+ stack.return_value = NSAPI_ERROR_IN_PROGRESS;
101
+ const SocketAddress a (" 127.0.0.1" , 1024 );
102
+ eventFlagsStubNextRetval.push_back (osFlagsError); // Break the wait loop
103
+ socket->set_timeout (1 );
104
+ EXPECT_EQ (socket->connect (a), NSAPI_ERROR_IN_PROGRESS);
105
+ }
106
+
107
+ TEST_F (TestTCPSocket, connect_error_is_connected)
108
+ {
109
+ socket->open ((NetworkStack *)&stack);
110
+ stack.return_values .push_back (NSAPI_ERROR_ALREADY);
111
+ stack.return_values .push_back (NSAPI_ERROR_IS_CONNECTED);
112
+ const SocketAddress a (" 127.0.0.1" , 1024 );
113
+ socket->set_timeout (1 );
114
+ EXPECT_EQ (socket->connect (a), NSAPI_ERROR_OK);
115
+ }
116
+
117
+ TEST_F (TestTCPSocket, connect_by_name_and_port)
118
+ {
119
+ socket->open ((NetworkStack *)&stack);
120
+ stack.return_value = NSAPI_ERROR_OK;
121
+ EXPECT_EQ (socket->connect (" testhost" , 80 ), NSAPI_ERROR_OK);
122
+ }
123
+
124
+ TEST_F (TestTCPSocket, connect_by_name_and_port_dns_fail)
125
+ {
126
+ socket->open ((NetworkStack *)&stack);
127
+ stack.return_value = NSAPI_ERROR_DNS_FAILURE;
128
+ EXPECT_EQ (socket->connect (" testhost" , 80 ), NSAPI_ERROR_DNS_FAILURE);
129
+ }
130
+
131
+ /* send */
132
+
133
+ TEST_F (TestTCPSocket, send_no_open)
134
+ {
135
+ stack.return_value = NSAPI_ERROR_OK;
136
+ EXPECT_EQ (socket->send (dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET);
137
+ }
138
+
139
+ TEST_F (TestTCPSocket, send_in_one_chunk)
140
+ {
141
+ socket->open ((NetworkStack *)&stack);
142
+ stack.return_value = dataSize;
143
+ EXPECT_EQ (socket->send (dataBuf, dataSize), dataSize);
144
+ }
145
+
146
+ TEST_F (TestTCPSocket, send_in_two_chunks)
147
+ {
148
+ socket->open ((NetworkStack *)&stack);
149
+ stack.return_values .push_back (4 );
150
+ stack.return_values .push_back (dataSize - 4 );
151
+ EXPECT_EQ (socket->send (dataBuf, dataSize), dataSize);
152
+ }
153
+
154
+ TEST_F (TestTCPSocket, send_error_would_block)
155
+ {
156
+ socket->open ((NetworkStack *)&stack);
157
+ stack.return_value = NSAPI_ERROR_WOULD_BLOCK;
158
+ eventFlagsStubNextRetval.push_back (osFlagsError); // Break the wait loop
159
+ EXPECT_EQ (socket->send (dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
160
+ }
161
+
162
+ TEST_F (TestTCPSocket, send_error_other)
163
+ {
164
+ socket->open ((NetworkStack *)&stack);
165
+ stack.return_value = NSAPI_ERROR_NO_MEMORY;
166
+ EXPECT_EQ (socket->send (dataBuf, dataSize), NSAPI_ERROR_NO_MEMORY);
167
+ }
168
+
169
+ TEST_F (TestTCPSocket, send_error_no_timeout)
170
+ {
171
+ socket->open ((NetworkStack *)&stack);
172
+ stack.return_value = NSAPI_ERROR_WOULD_BLOCK;
173
+ socket->set_timeout (0 );
174
+ EXPECT_EQ (socket->send (dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
175
+ }
176
+
177
+ TEST_F (TestTCPSocket, send_to)
178
+ {
179
+ socket->open ((NetworkStack *)&stack);
180
+ stack.return_value = 10 ;
181
+ const SocketAddress a (" 127.0.0.1" , 1024 );
182
+ EXPECT_EQ (socket->sendto (a, dataBuf, dataSize), dataSize);
183
+ }
184
+
185
+ /* recv */
186
+
187
+ TEST_F (TestTCPSocket, recv_no_open)
188
+ {
189
+ stack.return_value = NSAPI_ERROR_OK;
190
+ EXPECT_EQ (socket->recv (dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET);
191
+ }
192
+
193
+ TEST_F (TestTCPSocket, recv_all_data)
194
+ {
195
+ socket->open ((NetworkStack *)&stack);
196
+ stack.return_value = dataSize;
197
+ EXPECT_EQ (socket->recv (dataBuf, dataSize), dataSize);
198
+ }
199
+
200
+ TEST_F (TestTCPSocket, recv_less_than_expected)
201
+ {
202
+ socket->open ((NetworkStack *)&stack);
203
+ unsigned int lessThanDataSize = dataSize -1 ;
204
+ stack.return_values .push_back (lessThanDataSize);
205
+ EXPECT_EQ (socket->recv (dataBuf, dataSize), lessThanDataSize);
206
+ }
207
+
208
+ TEST_F (TestTCPSocket, recv_would_block)
209
+ {
210
+ socket->open ((NetworkStack *)&stack);
211
+ stack.return_value = NSAPI_ERROR_WOULD_BLOCK;
212
+ eventFlagsStubNextRetval.push_back (0 );
213
+ eventFlagsStubNextRetval.push_back (osFlagsError); // Break the wait loop
214
+ EXPECT_EQ (socket->recv (dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
215
+ }
216
+
217
+ TEST_F (TestTCPSocket, recv_from)
218
+ {
219
+ stack.return_value = NSAPI_ERROR_OK;
220
+ SocketAddress a (" 127.0.0.1" , 1024 );
221
+ EXPECT_EQ (socket->recvfrom (&a, dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET);
222
+ }
223
+
224
+ /* listen */
225
+
226
+ TEST_F (TestTCPSocket, listen_no_open)
227
+ {
228
+ stack.return_value = NSAPI_ERROR_OK;
229
+ EXPECT_EQ (socket->listen (1 ), NSAPI_ERROR_NO_SOCKET);
230
+ }
231
+
232
+ TEST_F (TestTCPSocket, listen)
233
+ {
234
+ stack.return_value = NSAPI_ERROR_OK;
235
+ socket->open ((NetworkStack *)&stack);
236
+ EXPECT_EQ (socket->listen (1 ), NSAPI_ERROR_OK);
237
+ }
238
+
239
+ /* accept - will accept ever be used on TCPSocket, or is it just meant for TCPServer?
240
+ * accept currently always returns NULL - is this intended? Memory leak
241
+ * Perhaps we should make it return UNSUPPORTED ? */
242
+
243
+ TEST_F (TestTCPSocket, accept_no_open)
244
+ {
245
+ nsapi_error_t error;
246
+ stack.return_value = NSAPI_ERROR_OK;
247
+ EXPECT_EQ (socket->accept (&error), static_cast <TCPSocket*>(NULL ));
248
+ EXPECT_EQ (error, NSAPI_ERROR_NO_SOCKET);
249
+ }
250
+
251
+ TEST_F (TestTCPSocket, accept)
252
+ {
253
+ nsapi_error_t error;
254
+ stack.return_value = NSAPI_ERROR_OK;
255
+ socket->open ((NetworkStack *)&stack);
256
+ EXPECT_EQ (socket->accept (&error), static_cast <TCPSocket*>(NULL ));
257
+ EXPECT_EQ (error, NSAPI_ERROR_OK);
258
+ }
259
+
260
+ TEST_F (TestTCPSocket, accept_would_block)
261
+ {
262
+ nsapi_error_t error;
263
+ socket->open ((NetworkStack *)&stack);
264
+ stack.return_value = NSAPI_ERROR_WOULD_BLOCK;
265
+ eventFlagsStubNextRetval.push_back (0 );
266
+ eventFlagsStubNextRetval.push_back (osFlagsError); // Break the wait loop
267
+ EXPECT_EQ (socket->accept (&error), static_cast <TCPSocket*>(NULL ));
268
+ EXPECT_EQ (error, NSAPI_ERROR_WOULD_BLOCK);
269
+ }
0 commit comments