Skip to content

Commit

Permalink
Merge pull request ARMmbed#9904 from michalpasztamobica/greentea_pack…
Browse files Browse the repository at this point in the history
…et_size_fix

Handle oversized packets in tcp and udp socket tests.
  • Loading branch information
Cruz Monrreal II authored and Cruz Monrreal II committed Mar 28, 2019
2 parents 1549c5c + 266d4c4 commit bbcba31
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 69 deletions.
8 changes: 5 additions & 3 deletions TESTS/netsocket/tcp/tcpsocket_echotest.cpp
Expand Up @@ -69,13 +69,15 @@ void TCPSOCKET_ECHOTEST()
int x = 0;
for (int pkt_s = pkt_sizes[x]; x < PKTS; pkt_s = pkt_sizes[x++]) {
fill_tx_buffer_ascii(tcp_global::tx_buffer, BUFF_SIZE);

sent = sock.send(tcp_global::tx_buffer, pkt_s);
if (sent < 0) {
printf("[Round#%02d] network error %d\n", x, sent);
TEST_FAIL();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
return;
break;
} else if (sent != pkt_s) {
printf("[%02d] sock.send return size %d does not match the expectation %d\n", x, sent, pkt_s);
TEST_FAIL();
break;
}

int bytes2recv = sent;
Expand Down
47 changes: 17 additions & 30 deletions TESTS/netsocket/tcp/tcpsocket_echotest_burst.cpp
Expand Up @@ -47,46 +47,33 @@ void TCPSOCKET_ECHOTEST_BURST()
fill_tx_buffer_ascii(tcp_global::tx_buffer, BURST_SIZE);

int recvd;
int bt_left;
int sent;
for (int i = 0; i < BURST_CNT; i++) {
bt_left = BURST_SIZE;
while (bt_left > 0) {
sent = sock.send(&(tcp_global::tx_buffer[BURST_SIZE - bt_left]), bt_left);
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
TEST_FAIL();
goto END;
}
continue;
} else if (sent < 0) {
printf("[%02d] network error %d\n", i, sent);
TEST_FAIL();
goto END;
}
bt_left -= sent;
sent = sock.send(tcp_global::tx_buffer, BURST_SIZE);
if (sent < 0) {
printf("[%02d] network error %d\n", i, sent);
TEST_FAIL();
break;
} else if (sent != BURST_SIZE) {
printf("[%02d] sock.send return size %d does not match the expectation %d\n", i, sent, BURST_SIZE);
TEST_FAIL();
break;
}

bt_left = BURST_SIZE;
while (bt_left > 0) {
recvd = sock.recv(&(tcp_global::rx_buffer[BURST_SIZE - bt_left]), BURST_SIZE);
int bytes2recv = sent;
while (bytes2recv) {
recvd = sock.recv(&(tcp_global::rx_buffer[sent - bytes2recv]), bytes2recv);
if (recvd < 0) {
printf("[%02d] network error %d\n", i, recvd);
break;
} else if (recvd > bt_left) {
TEST_FAIL_MESSAGE("sock.recv returned more bytes than requested");
printf("[Round#%02d] network error %d\n", i, recvd);
TEST_FAIL();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
return;
}
bt_left -= recvd;
}

if (bt_left != 0) {
TEST_FAIL_MESSAGE("bt_left != 0");
goto END;
bytes2recv -= recvd;
}

TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
}
END:
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
}

Expand Down
9 changes: 5 additions & 4 deletions TESTS/netsocket/tls/tlssocket_echotest.cpp
Expand Up @@ -77,9 +77,11 @@ void TLSSOCKET_ECHOTEST()
if (sent < 0) {
printf("[Round#%02d] network error %d\n", x, sent);
TEST_FAIL();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
delete sock;
return;
break;
} else if (sent != pkt_s) {
printf("[%02d] sock.send return size %d does not match the expectation %d\n", x, sent, pkt_s);
TEST_FAIL();
break;
}

int bytes2recv = sent;
Expand All @@ -89,7 +91,6 @@ void TLSSOCKET_ECHOTEST()
printf("[Round#%02d] network error %d\n", x, recvd);
TEST_FAIL();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
delete sock;
return;
} else if (recvd > bytes2recv) {
TEST_FAIL_MESSAGE("sock.recv returned more bytes than requested");
Expand Down
47 changes: 17 additions & 30 deletions TESTS/netsocket/tls/tlssocket_echotest_burst.cpp
Expand Up @@ -49,46 +49,33 @@ void TLSSOCKET_ECHOTEST_BURST()
fill_tx_buffer_ascii(tls_global::tx_buffer, BURST_SIZE);

int recvd;
int bt_left;
int sent;
for (int i = 0; i < BURST_CNT; i++) {
bt_left = BURST_SIZE;
while (bt_left > 0) {
sent = sock->send(&(tls_global::tx_buffer[BURST_SIZE - bt_left]), bt_left);
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
TEST_FAIL();
goto END;
}
continue;
} else if (sent < 0) {
printf("[%02d] network error %d\n", i, sent);
TEST_FAIL();
goto END;
}
bt_left -= sent;
sent = sock->send(tls_global::tx_buffer, BURST_SIZE);
if (sent < 0) {
printf("[%02d] network error %d\n", i, sent);
TEST_FAIL();
break;
} else if (sent != BURST_SIZE) {
printf("[%02d] sock.send return size %d does not match the expectation %d\n", i, sent, BURST_SIZE);
TEST_FAIL();
break;
}

bt_left = BURST_SIZE;
while (bt_left > 0) {
recvd = sock->recv(&(tls_global::rx_buffer[BURST_SIZE - bt_left]), BURST_SIZE);
int bytes2recv = sent;
while (bytes2recv) {
recvd = sock->recv(&(tls_global::rx_buffer[sent - bytes2recv]), bytes2recv);
if (recvd < 0) {
printf("[%02d] network error %d\n", i, recvd);
break;
} else if (recvd > bt_left) {
TEST_FAIL_MESSAGE("sock.recv returned more bytes than requested");
printf("[Round#%02d] network error %d\n", i, recvd);
TEST_FAIL();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
return;
}
bt_left -= recvd;
}

if (bt_left != 0) {
TEST_FAIL_MESSAGE("bt_left != 0");
goto END;
bytes2recv -= recvd;
}

TEST_ASSERT_EQUAL(0, memcmp(tls_global::tx_buffer, tls_global::rx_buffer, BURST_SIZE));
}
END:
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
delete sock;
}
Expand Down
18 changes: 18 additions & 0 deletions TESTS/netsocket/udp/main.cpp
Expand Up @@ -77,6 +77,24 @@ nsapi_version_t get_ip_version()
return test.get_ip_version();
}

bool check_oversized_packets(nsapi_error_t error, int &size)
{
if (error == NSAPI_ERROR_PARAMETER) {
if (get_ip_version() == NSAPI_IPv4) {
if (size > udp_global::MAX_SEND_SIZE_IPV4) {
size = udp_global::MAX_SEND_SIZE_IPV4;
return true;
}
} else if (get_ip_version() == NSAPI_IPv6) {
if (size > udp_global::MAX_SEND_SIZE_IPV6) {
size = udp_global::MAX_SEND_SIZE_IPV6;
return true;
}
}
}
return false;
}

void fill_tx_buffer_ascii(char *buff, size_t len)
{
for (size_t i = 0; i < len; ++i) {
Expand Down
4 changes: 4 additions & 0 deletions TESTS/netsocket/udp/udp_tests.h
Expand Up @@ -21,6 +21,7 @@
NetworkInterface *get_interface();
void drop_bad_packets(UDPSocket &sock, int orig_timeout);
nsapi_version_t get_ip_version();
bool check_oversized_packets(nsapi_error_t error, int &size);
void fill_tx_buffer_ascii(char *buff, size_t len);

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
Expand All @@ -39,6 +40,9 @@ static const int TESTS_TIMEOUT = MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S;
#else
static const int TESTS_TIMEOUT = 480;
#endif

static const int MAX_SEND_SIZE_IPV4 = 536;
static const int MAX_SEND_SIZE_IPV6 = 1220;
}

/*
Expand Down
4 changes: 3 additions & 1 deletion TESTS/netsocket/udp/udpsocket_echotest.cpp
Expand Up @@ -79,7 +79,9 @@ void UDPSOCKET_ECHOTEST()
for (int retry_cnt = 0; retry_cnt <= 2; retry_cnt++) {
memset(rx_buffer, 0, BUFF_SIZE);
sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
if (sent > 0) {
if (check_oversized_packets(sent, pkt_s)) {
TEST_IGNORE_MESSAGE("This device does not handle oversized packets");
} else if (sent > 0) {
packets_sent++;
}
if (sent != pkt_s) {
Expand Down
6 changes: 5 additions & 1 deletion TESTS/netsocket/udp/udpsocket_echotest_burst.cpp
Expand Up @@ -91,7 +91,11 @@ void UDPSOCKET_ECHOTEST_BURST()
SocketAddress temp_addr;
for (int i = 0; i < BURST_CNT; i++) {
for (int x = 0; x < BURST_PKTS; x++) {
TEST_ASSERT_EQUAL(tx_buffers[x].len, sock.sendto(udp_addr, tx_buffers[x].payload, tx_buffers[x].len));
int sent = sock.sendto(udp_addr, tx_buffers[x].payload, tx_buffers[x].len);
if (check_oversized_packets(sent, tx_buffers[x].len)) {
TEST_IGNORE_MESSAGE("This device does not handle oversized packets");
}
TEST_ASSERT_EQUAL(tx_buffers[x].len, sent);
}

bt_total = 0;
Expand Down

0 comments on commit bbcba31

Please sign in to comment.