Skip to content

Commit bbcba31

Browse files
Cruz Monrreal IICruz Monrreal II
authored andcommitted
Merge pull request ARMmbed#9904 from michalpasztamobica/greentea_packet_size_fix
Handle oversized packets in tcp and udp socket tests.
2 parents 1549c5c + 266d4c4 commit bbcba31

File tree

8 files changed

+74
-69
lines changed

8 files changed

+74
-69
lines changed

TESTS/netsocket/tcp/tcpsocket_echotest.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ void TCPSOCKET_ECHOTEST()
6969
int x = 0;
7070
for (int pkt_s = pkt_sizes[x]; x < PKTS; pkt_s = pkt_sizes[x++]) {
7171
fill_tx_buffer_ascii(tcp_global::tx_buffer, BUFF_SIZE);
72-
7372
sent = sock.send(tcp_global::tx_buffer, pkt_s);
7473
if (sent < 0) {
7574
printf("[Round#%02d] network error %d\n", x, sent);
7675
TEST_FAIL();
77-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
78-
return;
76+
break;
77+
} else if (sent != pkt_s) {
78+
printf("[%02d] sock.send return size %d does not match the expectation %d\n", x, sent, pkt_s);
79+
TEST_FAIL();
80+
break;
7981
}
8082

8183
int bytes2recv = sent;

TESTS/netsocket/tcp/tcpsocket_echotest_burst.cpp

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,33 @@ void TCPSOCKET_ECHOTEST_BURST()
4747
fill_tx_buffer_ascii(tcp_global::tx_buffer, BURST_SIZE);
4848

4949
int recvd;
50-
int bt_left;
5150
int sent;
5251
for (int i = 0; i < BURST_CNT; i++) {
53-
bt_left = BURST_SIZE;
54-
while (bt_left > 0) {
55-
sent = sock.send(&(tcp_global::tx_buffer[BURST_SIZE - bt_left]), bt_left);
56-
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
57-
if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
58-
TEST_FAIL();
59-
goto END;
60-
}
61-
continue;
62-
} else if (sent < 0) {
63-
printf("[%02d] network error %d\n", i, sent);
64-
TEST_FAIL();
65-
goto END;
66-
}
67-
bt_left -= sent;
52+
sent = sock.send(tcp_global::tx_buffer, BURST_SIZE);
53+
if (sent < 0) {
54+
printf("[%02d] network error %d\n", i, sent);
55+
TEST_FAIL();
56+
break;
57+
} else if (sent != BURST_SIZE) {
58+
printf("[%02d] sock.send return size %d does not match the expectation %d\n", i, sent, BURST_SIZE);
59+
TEST_FAIL();
60+
break;
6861
}
6962

70-
bt_left = BURST_SIZE;
71-
while (bt_left > 0) {
72-
recvd = sock.recv(&(tcp_global::rx_buffer[BURST_SIZE - bt_left]), BURST_SIZE);
63+
int bytes2recv = sent;
64+
while (bytes2recv) {
65+
recvd = sock.recv(&(tcp_global::rx_buffer[sent - bytes2recv]), bytes2recv);
7366
if (recvd < 0) {
74-
printf("[%02d] network error %d\n", i, recvd);
75-
break;
76-
} else if (recvd > bt_left) {
77-
TEST_FAIL_MESSAGE("sock.recv returned more bytes than requested");
67+
printf("[Round#%02d] network error %d\n", i, recvd);
68+
TEST_FAIL();
69+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
70+
return;
7871
}
79-
bt_left -= recvd;
80-
}
81-
82-
if (bt_left != 0) {
83-
TEST_FAIL_MESSAGE("bt_left != 0");
84-
goto END;
72+
bytes2recv -= recvd;
8573
}
8674

8775
TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
8876
}
89-
END:
9077
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
9178
}
9279

TESTS/netsocket/tls/tlssocket_echotest.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ void TLSSOCKET_ECHOTEST()
7777
if (sent < 0) {
7878
printf("[Round#%02d] network error %d\n", x, sent);
7979
TEST_FAIL();
80-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
81-
delete sock;
82-
return;
80+
break;
81+
} else if (sent != pkt_s) {
82+
printf("[%02d] sock.send return size %d does not match the expectation %d\n", x, sent, pkt_s);
83+
TEST_FAIL();
84+
break;
8385
}
8486

8587
int bytes2recv = sent;
@@ -89,7 +91,6 @@ void TLSSOCKET_ECHOTEST()
8991
printf("[Round#%02d] network error %d\n", x, recvd);
9092
TEST_FAIL();
9193
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
92-
delete sock;
9394
return;
9495
} else if (recvd > bytes2recv) {
9596
TEST_FAIL_MESSAGE("sock.recv returned more bytes than requested");

TESTS/netsocket/tls/tlssocket_echotest_burst.cpp

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,46 +49,33 @@ void TLSSOCKET_ECHOTEST_BURST()
4949
fill_tx_buffer_ascii(tls_global::tx_buffer, BURST_SIZE);
5050

5151
int recvd;
52-
int bt_left;
5352
int sent;
5453
for (int i = 0; i < BURST_CNT; i++) {
55-
bt_left = BURST_SIZE;
56-
while (bt_left > 0) {
57-
sent = sock->send(&(tls_global::tx_buffer[BURST_SIZE - bt_left]), bt_left);
58-
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
59-
if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
60-
TEST_FAIL();
61-
goto END;
62-
}
63-
continue;
64-
} else if (sent < 0) {
65-
printf("[%02d] network error %d\n", i, sent);
66-
TEST_FAIL();
67-
goto END;
68-
}
69-
bt_left -= sent;
54+
sent = sock->send(tls_global::tx_buffer, BURST_SIZE);
55+
if (sent < 0) {
56+
printf("[%02d] network error %d\n", i, sent);
57+
TEST_FAIL();
58+
break;
59+
} else if (sent != BURST_SIZE) {
60+
printf("[%02d] sock.send return size %d does not match the expectation %d\n", i, sent, BURST_SIZE);
61+
TEST_FAIL();
62+
break;
7063
}
7164

72-
bt_left = BURST_SIZE;
73-
while (bt_left > 0) {
74-
recvd = sock->recv(&(tls_global::rx_buffer[BURST_SIZE - bt_left]), BURST_SIZE);
65+
int bytes2recv = sent;
66+
while (bytes2recv) {
67+
recvd = sock->recv(&(tls_global::rx_buffer[sent - bytes2recv]), bytes2recv);
7568
if (recvd < 0) {
76-
printf("[%02d] network error %d\n", i, recvd);
77-
break;
78-
} else if (recvd > bt_left) {
79-
TEST_FAIL_MESSAGE("sock.recv returned more bytes than requested");
69+
printf("[Round#%02d] network error %d\n", i, recvd);
70+
TEST_FAIL();
71+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
72+
return;
8073
}
81-
bt_left -= recvd;
82-
}
83-
84-
if (bt_left != 0) {
85-
TEST_FAIL_MESSAGE("bt_left != 0");
86-
goto END;
74+
bytes2recv -= recvd;
8775
}
8876

8977
TEST_ASSERT_EQUAL(0, memcmp(tls_global::tx_buffer, tls_global::rx_buffer, BURST_SIZE));
9078
}
91-
END:
9279
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
9380
delete sock;
9481
}

TESTS/netsocket/udp/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ nsapi_version_t get_ip_version()
7777
return test.get_ip_version();
7878
}
7979

80+
bool check_oversized_packets(nsapi_error_t error, int &size)
81+
{
82+
if (error == NSAPI_ERROR_PARAMETER) {
83+
if (get_ip_version() == NSAPI_IPv4) {
84+
if (size > udp_global::MAX_SEND_SIZE_IPV4) {
85+
size = udp_global::MAX_SEND_SIZE_IPV4;
86+
return true;
87+
}
88+
} else if (get_ip_version() == NSAPI_IPv6) {
89+
if (size > udp_global::MAX_SEND_SIZE_IPV6) {
90+
size = udp_global::MAX_SEND_SIZE_IPV6;
91+
return true;
92+
}
93+
}
94+
}
95+
return false;
96+
}
97+
8098
void fill_tx_buffer_ascii(char *buff, size_t len)
8199
{
82100
for (size_t i = 0; i < len; ++i) {

TESTS/netsocket/udp/udp_tests.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
NetworkInterface *get_interface();
2222
void drop_bad_packets(UDPSocket &sock, int orig_timeout);
2323
nsapi_version_t get_ip_version();
24+
bool check_oversized_packets(nsapi_error_t error, int &size);
2425
void fill_tx_buffer_ascii(char *buff, size_t len);
2526

2627
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
@@ -39,6 +40,9 @@ static const int TESTS_TIMEOUT = MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S;
3940
#else
4041
static const int TESTS_TIMEOUT = 480;
4142
#endif
43+
44+
static const int MAX_SEND_SIZE_IPV4 = 536;
45+
static const int MAX_SEND_SIZE_IPV6 = 1220;
4246
}
4347

4448
/*

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ void UDPSOCKET_ECHOTEST()
7979
for (int retry_cnt = 0; retry_cnt <= 2; retry_cnt++) {
8080
memset(rx_buffer, 0, BUFF_SIZE);
8181
sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
82-
if (sent > 0) {
82+
if (check_oversized_packets(sent, pkt_s)) {
83+
TEST_IGNORE_MESSAGE("This device does not handle oversized packets");
84+
} else if (sent > 0) {
8385
packets_sent++;
8486
}
8587
if (sent != pkt_s) {

TESTS/netsocket/udp/udpsocket_echotest_burst.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ void UDPSOCKET_ECHOTEST_BURST()
9191
SocketAddress temp_addr;
9292
for (int i = 0; i < BURST_CNT; i++) {
9393
for (int x = 0; x < BURST_PKTS; x++) {
94-
TEST_ASSERT_EQUAL(tx_buffers[x].len, sock.sendto(udp_addr, tx_buffers[x].payload, tx_buffers[x].len));
94+
int sent = sock.sendto(udp_addr, tx_buffers[x].payload, tx_buffers[x].len);
95+
if (check_oversized_packets(sent, tx_buffers[x].len)) {
96+
TEST_IGNORE_MESSAGE("This device does not handle oversized packets");
97+
}
98+
TEST_ASSERT_EQUAL(tx_buffers[x].len, sent);
9599
}
96100

97101
bt_total = 0;

0 commit comments

Comments
 (0)