Skip to content

Commit 1fef160

Browse files
Veijo PesonenSeppo Takalo
authored andcommitted
Adds UDP Greentea test cases
udpsocket_echotest udpsocket_echotest_nonblock
1 parent a2f5ffa commit 1fef160

File tree

4 files changed

+169
-13
lines changed

4 files changed

+169
-13
lines changed

TESTS/netsocket/udp/main.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ static void _ifdown() {
5555
printf("MBED: ifdown\n");
5656
}
5757

58+
void drop_bad_packets(UDPSocket& sock) {
59+
nsapi_error_t err;
60+
sock.set_timeout(0);
61+
while (true) {
62+
err = sock.recvfrom(NULL, 0, 0);
63+
if (err == NSAPI_ERROR_WOULD_BLOCK) {
64+
break;
65+
}
66+
}
67+
}
68+
5869
void fill_tx_buffer_ascii(char *buff, size_t len)
5970
{
6071
for (size_t i = 0; i<len; ++i) {
@@ -76,8 +87,9 @@ void greentea_teardown(const size_t passed, const size_t failed, const failure_t
7687
return greentea_test_teardown_handler(passed, failed, failure);
7788
}
7889

79-
8090
Case cases[] = {
91+
Case("Echo", test_udpsocket_echotest),
92+
Case("Echo non-block", test_udpsocket_echotest_nonblock),
8193
Case("Echo burst", test_udpsocket_echotest_burst),
8294
Case("Echo burst non-block", test_udpsocket_echotest_burst_nonblock),
8395
Case("Reuse a socket", test_udpsocket_open_close_repeat),

TESTS/netsocket/udp/udp_tests.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
#define UDP_TESTS_H
2020

2121
NetworkInterface* get_interface();
22+
void drop_bad_packets(UDPSocket& sock);
2223
void fill_tx_buffer_ascii(char *buff, size_t len);
2324

2425
/*
2526
* Test cases
2627
*/
28+
void test_udpsocket_echotest();
29+
void test_udpsocket_echotest_nonblock();
2730
void test_udpsocket_echotest_burst();
2831
void test_udpsocket_echotest_burst_nonblock();
2932
void test_udpsocket_open_close_repeat();
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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 "UDPSocket.h"
21+
#include "greentea-client/test_env.h"
22+
#include "unity/unity.h"
23+
#include "utest.h"
24+
#include "udp_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+
static const int WAIT2RECV_TIMEOUT = 1000; //[ms]
33+
static const int RETRIES = 2;
34+
35+
UDPSocket sock;
36+
Semaphore tx_sem(0, 1);
37+
38+
static const int BUFF_SIZE = 1200;
39+
char rx_buffer[BUFF_SIZE] = {0};
40+
char tx_buffer[BUFF_SIZE] = {0};
41+
42+
static const int PKTS = 22;
43+
static const int pkt_sizes[PKTS] = {1,2,3,4,5,6,7,8,9,10, \
44+
100,200,300,400,500,600,700,800,900,1000,\
45+
1100,1200};
46+
}
47+
48+
static void _sigio_handler(osThreadId id) {
49+
osSignalSet(id, SIGNAL_SIGIO);
50+
}
51+
52+
void test_udpsocket_echotest()
53+
{
54+
SocketAddress udp_addr;
55+
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
56+
udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
57+
58+
UDPSocket sock;
59+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
60+
61+
int recvd;
62+
int sent;
63+
int s_idx = 0;
64+
for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; pkt_s = ++s_idx) {
65+
pkt_s = pkt_sizes[s_idx];
66+
67+
fill_tx_buffer_ascii(tx_buffer, BUFF_SIZE);
68+
69+
for (int retry_cnt = 0; retry_cnt <= 2; retry_cnt++) {
70+
sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
71+
if (sent != pkt_s) {
72+
printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
73+
continue;
74+
}
75+
recvd = sock.recvfrom(NULL, rx_buffer, pkt_s);
76+
if (recvd == pkt_s) {
77+
break;
78+
}
79+
}
80+
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, pkt_s));
81+
}
82+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
83+
}
84+
85+
void udpsocket_echotest_nonblock_receiver(void *receive_bytes)
86+
{
87+
int expt2recv = *(int*)receive_bytes;
88+
int recvd;
89+
for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) {
90+
recvd = sock.recvfrom(NULL, rx_buffer, expt2recv);
91+
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
92+
wait_ms(WAIT2RECV_TIMEOUT);
93+
continue;
94+
} else if (recvd == expt2recv) {
95+
break;
96+
}
97+
}
98+
99+
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, expt2recv));
100+
drop_bad_packets(sock);
101+
sock.set_blocking(false);
102+
103+
tx_sem.release();
104+
}
105+
106+
void test_udpsocket_echotest_nonblock()
107+
{
108+
SocketAddress udp_addr;
109+
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
110+
udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
111+
112+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
113+
sock.set_blocking(false);
114+
sock.sigio(callback(_sigio_handler, Thread::gettid()));
115+
116+
int sent;
117+
int s_idx = 0;
118+
Thread *thread;
119+
unsigned char *stack_mem = (unsigned char *)malloc(OS_STACK_SIZE);
120+
TEST_ASSERT_NOT_NULL(stack_mem);
121+
122+
for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) {
123+
pkt_s = pkt_sizes[s_idx];
124+
125+
thread = new Thread(osPriorityNormal,
126+
OS_STACK_SIZE,
127+
stack_mem,
128+
"receiver");
129+
TEST_ASSERT_EQUAL(osOK, thread->start(callback(udpsocket_echotest_nonblock_receiver, &pkt_s)));
130+
131+
for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) {
132+
fill_tx_buffer_ascii(tx_buffer, pkt_s);
133+
134+
sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
135+
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
136+
if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
137+
continue;
138+
}
139+
} else if (sent != pkt_s) {
140+
printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
141+
continue;
142+
}
143+
if (tx_sem.wait(WAIT2RECV_TIMEOUT*2) == 0) { // RX might wait up to WAIT2RECV_TIMEOUT before recvfrom
144+
continue;
145+
}
146+
break;
147+
}
148+
thread->join();
149+
delete thread;
150+
}
151+
free(stack_mem);
152+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
153+
}

TESTS/netsocket/udp/udpsocket_echotest_burst.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,6 @@ void free_tx_buffers() {
5656
}
5757
}
5858

59-
void drop_bad_packets(UDPSocket& sock) {
60-
nsapi_error_t err;
61-
sock.set_timeout(0);
62-
while (true) {
63-
err = sock.recvfrom(NULL, NULL, 0);
64-
if (err == NSAPI_ERROR_WOULD_BLOCK) {
65-
break;
66-
}
67-
}
68-
sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
69-
}
70-
7159
void test_udpsocket_echotest_burst()
7260
{
7361
SocketAddress udp_addr;

0 commit comments

Comments
 (0)