Skip to content

Commit df825cb

Browse files
Veijo PesonenSeppo Takalo
authored andcommitted
Adds TCP test case
tcpsocket_connect_invalid
1 parent 83fdd13 commit df825cb

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed

TESTS/netsocket/tcp/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void greentea_teardown(const size_t passed, const size_t failed, const failure_t
9191

9292

9393
Case cases[] = {
94+
Case("Invalid endpoint rejected", test_tcpsocket_connect_invalid),
9495
Case("Echo burst", test_tcpsocket_echotest_burst),
9596
Case("Echo burst non-block", test_tcpsocket_echotest_burst_nonblock),
9697
Case("Reuse a socket", test_tcpsocket_open_close_repeat),

TESTS/netsocket/tcp/tcp_tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void fill_tx_buffer_ascii(char *buff, size_t len);
2525
/*
2626
* Test cases
2727
*/
28+
void test_tcpsocket_connect_invalid();
2829
void test_tcpsocket_echotest_burst();
2930
void test_tcpsocket_echotest_burst_nonblock();
3031
void test_tcpsocket_endpoint_close();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* 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, WITHOUT
13+
* 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 "mbed.h"
19+
#include MBED_CONF_APP_HEADER_FILE
20+
#include "TCPSocket.h"
21+
#include "greentea-client/test_env.h"
22+
#include "unity/unity.h"
23+
#include "utest.h"
24+
#include "tcp_tests.h"
25+
26+
using namespace utest::v1;
27+
28+
void test_tcpsocket_connect_invalid()
29+
{
30+
TCPSocket sock;
31+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
32+
33+
TEST_ASSERT(sock.connect(NULL, 9) < 0);
34+
TEST_ASSERT(sock.connect("", 9) < 0);
35+
TEST_ASSERT(sock.connect("", 0) < 0);
36+
TEST_ASSERT(sock.connect(MBED_CONF_APP_ECHO_SERVER_ADDR, 0) < 0);
37+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(MBED_CONF_APP_ECHO_SERVER_ADDR, 9));
38+
39+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
40+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* 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, WITHOUT
13+
* 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 "mbed.h"
19+
#include MBED_CONF_APP_HEADER_FILE
20+
#include "TCPSocket.h"
21+
#include "greentea-client/test_env.h"
22+
#include "unity/unity.h"
23+
#include "utest.h"
24+
#include "tcp_tests.h"
25+
26+
using namespace utest::v1;
27+
28+
namespace
29+
{
30+
static const int SIGNAL_SIGIO = 0x1;
31+
static const int SIGIO_TIMEOUT = 5000; //[ms]
32+
33+
static const int BUFF_SIZE = 1200;
34+
char rx_buffer[BUFF_SIZE] = {0};
35+
char tx_buffer[BUFF_SIZE] = {0};
36+
37+
static const int PKTS = 22;
38+
static const int pkt_sizes[PKTS] = {1,2,3,4,5,6,7,8,9,10, \
39+
100,200,300,400,500,600,700,800,900,1000,\
40+
1100,1200};
41+
TCPSocket sock;
42+
Semaphore tx_sem(0, 1);
43+
}
44+
45+
static void _sigio_handler(osThreadId id) {
46+
osSignalSet(id, SIGNAL_SIGIO);
47+
}
48+
49+
void test_tcpsocket_echotest()
50+
{
51+
SocketAddress tcp_addr;
52+
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
53+
tcp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
54+
55+
TCPSocket sock;
56+
sock.open(get_interface());
57+
sock.connect(tcp_addr);
58+
59+
int recvd;
60+
int sent;
61+
int x = 0;
62+
for (int pkt_s = pkt_sizes[x]; x < PKTS; pkt_s = pkt_sizes[x++]) {
63+
fill_tx_buffer_ascii(tx_buffer, BUFF_SIZE);
64+
65+
sent = sock.send(tx_buffer, pkt_s);
66+
if (sent < 0) {
67+
printf("[Round#%02d] network error %d\n", x, sent);
68+
TEST_FAIL();
69+
}
70+
71+
int bytes2recv = sent;
72+
while (bytes2recv) {
73+
recvd = sock.recv(&(rx_buffer[sent-bytes2recv]), bytes2recv);
74+
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
75+
TEST_ASSERT_NOT_EQUAL(osEventTimeout, osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status);
76+
continue;
77+
} else if (recvd <= 0) {
78+
printf("[Round#%02d] network error %d\n", x, recvd);
79+
TEST_FAIL();
80+
}
81+
bytes2recv -= recvd;
82+
}
83+
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, sent));
84+
}
85+
sock.close();
86+
}
87+
88+
void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)
89+
{
90+
int bytes2recv = *(int*)receive_bytes;
91+
int recvd;
92+
while (bytes2recv) {
93+
recvd = sock.recv(&(rx_buffer[*(int*)receive_bytes-bytes2recv]), bytes2recv);
94+
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
95+
wait(1);
96+
continue;
97+
} else if (recvd < 0) {
98+
TEST_FAIL();
99+
}
100+
bytes2recv -= recvd;
101+
}
102+
103+
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, *(int*)receive_bytes));
104+
105+
static int round = 0;
106+
printf("[Recevr#%02d] bytes received: %d\n", round++, *(int*)receive_bytes);
107+
108+
tx_sem.release();
109+
110+
}
111+
112+
void test_tcpsocket_echotest_nonblock()
113+
{
114+
SocketAddress tcp_addr;
115+
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
116+
tcp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
117+
118+
sock.open(get_interface());
119+
sock.connect(tcp_addr);
120+
sock.set_blocking(false);
121+
sock.sigio(callback(_sigio_handler, Thread::gettid()));
122+
123+
int bytes2send;
124+
int sent;
125+
int s_idx = 0;
126+
Thread *thread;
127+
unsigned char *stack_mem = (unsigned char *)malloc(OS_STACK_SIZE);
128+
TEST_ASSERT_NOT_NULL(stack_mem);
129+
130+
for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) {
131+
pkt_s = pkt_sizes[s_idx];
132+
thread = new Thread(osPriorityNormal,
133+
OS_STACK_SIZE,
134+
stack_mem,
135+
"receiver");
136+
TEST_ASSERT_EQUAL(osOK, thread->start(callback(tcpsocket_echotest_nonblock_receiver, &pkt_s)));
137+
138+
fill_tx_buffer_ascii(tx_buffer, pkt_s);
139+
140+
bytes2send = pkt_s;
141+
while (bytes2send > 0) {
142+
sent = sock.send(&(tx_buffer[pkt_s-bytes2send]), bytes2send);
143+
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
144+
TEST_ASSERT_NOT_EQUAL(osEventTimeout, osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status);
145+
continue;
146+
} else if (sent <= 0) {
147+
printf("[Sender#%02d] network error %d\n", s_idx, sent);
148+
TEST_FAIL();
149+
}
150+
bytes2send -= sent;
151+
}
152+
printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s);
153+
tx_sem.wait();
154+
thread->join();
155+
delete thread;
156+
}
157+
free(stack_mem);
158+
sock.close();
159+
}

0 commit comments

Comments
 (0)