![](/media/cache/group/ST_logo_2020_blue_V_cmyk.jpg.50x50_q85.jpg)
fixed buffer management in case of packet fragmentation; improved test pattern with pseudo random to avoid pattern simulation
Fork of NSAPITests by
Revision 13:950327f445a3, committed 2016-10-26
- Comitter:
- mapellil
- Date:
- Wed Oct 26 09:52:29 2016 +0000
- Parent:
- 12:152ae238ddc1
- Commit message:
- Fixed buffer wraparound in case of packet fragmentation; improved test pattern with pseudo random to avoid pattern simulation.
Changed in this revision
NSAPITests.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/NSAPITests.cpp Mon Jun 06 08:42:21 2016 +0000 +++ b/NSAPITests.cpp Wed Oct 26 09:52:29 2016 +0000 @@ -8,7 +8,8 @@ #define NSAPI_TEST_HOST "mbed.org" #define NSAPI_TEST_IP "8.8.4.4" -uint8_t test_buffer[NSAPI_MAX_DATA_SIZE]; +uint8_t tx_test_buffer[NSAPI_MAX_DATA_SIZE]; +uint8_t rx_test_buffer[NSAPI_MAX_DATA_SIZE]; int nsapi_networkstack_get_ip_address_test(NetworkStack *stack) { @@ -58,14 +59,14 @@ static void nsapi_buffer_init(uint8_t *buffer, unsigned size) { for (unsigned i = 0; i < size; i++) { - buffer[i] = '0' + (i % 10); + buffer[i] = rand() % 256; } } static bool nsapi_buffer_check(const uint8_t *buffer, unsigned size) { for (unsigned i = 0; i < size; i++) { - if (buffer[i] != '0' + (i % 10)) { + if (buffer[i] != tx_test_buffer[i]) { return false; } } @@ -76,7 +77,7 @@ static void nsapi_tcp_flush(TCPSocket *socket) { socket->set_timeout(1000); - socket->recv(test_buffer, sizeof test_buffer); + socket->recv(rx_test_buffer, sizeof rx_test_buffer); socket->set_timeout(-1); } @@ -109,10 +110,10 @@ { unsigned total; nsapi_tcp_flush(tcp); - nsapi_buffer_init(test_buffer, size); + nsapi_buffer_init(tx_test_buffer, size); for (total = 0; total < size;) { - int ret = tcp->send(test_buffer, size); + int ret = tcp->send(tx_test_buffer+total, size-total); if (ret < 0) { printf("error: 'send(buffer, %d)' failed during test with code %d\r\n", size, ret); @@ -121,20 +122,21 @@ total += ret; } - + + memset(rx_test_buffer, 0, size); for (total = 0; total < size;) { - int ret = tcp->recv(test_buffer, sizeof test_buffer); + int ret = tcp->recv(rx_test_buffer+total, (sizeof rx_test_buffer)-total); if (ret < 0) { - printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof test_buffer, ret); + printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret); return -2; } total += ret; } - if (total != size || !nsapi_buffer_check(test_buffer, size)) { - printf("error: 'recv(buffer, %d)' recieved incorrect data with length %d\r\n", sizeof test_buffer, total); + if (total != size || !nsapi_buffer_check(rx_test_buffer, size)) { + printf("error: 'recv(buffer, %d)' recieved incorrect data with length %d\r\n", sizeof rx_test_buffer, total); return -3; } @@ -159,24 +161,24 @@ { unsigned total; nsapi_tcp_flush(tcp); - nsapi_buffer_init(test_buffer, size); + nsapi_buffer_init(tx_test_buffer, size); // First check to make sure `recv` will not block and return 0 for bytes received. tcp->set_blocking(false); - int ret = tcp->recv(test_buffer, sizeof test_buffer); + int ret = tcp->recv(rx_test_buffer, sizeof rx_test_buffer); if (ret != NSAPI_ERROR_WOULD_BLOCK) { if (ret < 0) { - printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof test_buffer, ret); + printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret); return -4; } else { - printf("error: 'recv(buffer, %d)' returned %d when no data was expected\r\n", sizeof test_buffer, ret); + printf("error: 'recv(buffer, %d)' returned %d when no data was expected\r\n", sizeof rx_test_buffer, ret); return -5; } } for (total = 0; total < size;) { - int ret = tcp->send(test_buffer, size); + int ret = tcp->send(tx_test_buffer+total, size-total); if (ret < 0) { printf("error: 'send(buffer, %d)' failed during test with code %d\r\n", size, ret); @@ -186,22 +188,22 @@ total += ret; } - + memset (rx_test_buffer, 0, size); for (total = 0; total < size;) { - ret = tcp->recv(test_buffer, sizeof test_buffer); + ret = tcp->recv(rx_test_buffer+total, (sizeof rx_test_buffer)-total); if (ret == NSAPI_ERROR_WOULD_BLOCK) { continue; } else if (ret < 0) { - printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof test_buffer, ret); + printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret); return -2; } else { total += ret; } } - if (total != size || !nsapi_buffer_check(test_buffer, size)) { - printf("error: 'recv(buffer, %d)' recieved incorrect data with length %d\r\n", sizeof test_buffer, total); + if (total != size || !nsapi_buffer_check(rx_test_buffer, size)) { + printf("error: 'recv(buffer, %d)' recieved incorrect data with length %d\r\n", sizeof rx_test_buffer, total); return -3; } @@ -237,7 +239,7 @@ static void nsapi_udp_flush(UDPSocket *udp) { udp->set_timeout(1000); - udp->recvfrom(0, test_buffer, sizeof test_buffer); + udp->recvfrom(0, rx_test_buffer, sizeof rx_test_buffer); udp->set_timeout(-1); } @@ -257,10 +259,10 @@ { unsigned total; nsapi_udp_flush(udp); - nsapi_buffer_init(test_buffer, size); + nsapi_buffer_init(tx_test_buffer, size); for (total = 0; total < size;) { - int ret = udp->sendto(*addr, test_buffer, size); + int ret = udp->sendto(*addr, tx_test_buffer+total, size-total); if (ret < 0) { printf("error: 'sendto(SocketAddress(%s, %d), buffer, %d)' failed during test with code %d\r\n", @@ -270,20 +272,21 @@ total += ret; } - + + memset(rx_test_buffer, 0, size); for (total = 0; total < size;) { - int ret = udp->recvfrom(0, test_buffer, sizeof test_buffer); + int ret = udp->recvfrom(0, rx_test_buffer+total, (sizeof rx_test_buffer)-total); if (ret < 0) { - printf("error: 'recvfrom(0, buffer, %d)' failed during test with code %d\r\n", sizeof test_buffer, ret); + printf("error: 'recvfrom(0, buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret); return -2; } total += ret; } - if (total != size || !nsapi_buffer_check(test_buffer, size)) { - printf("error: 'recvfrom(0, buffer, %d)' recieved incorrect data with length %d\r\n", sizeof test_buffer, total); + if (total != size || !nsapi_buffer_check(rx_test_buffer, size)) { + printf("error: 'recvfrom(0, buffer, %d)' recieved incorrect data with length %d\r\n", sizeof rx_test_buffer, total); return -3; } @@ -308,24 +311,24 @@ { unsigned total; nsapi_udp_flush(udp); - nsapi_buffer_init(test_buffer, size); + nsapi_buffer_init(tx_test_buffer, size); // First check to make sure `recv` will not block and return 0 for bytes received. udp->set_blocking(false); - int ret = udp->recvfrom(0, test_buffer, sizeof test_buffer); + int ret = udp->recvfrom(0, rx_test_buffer, sizeof rx_test_buffer); if (ret != NSAPI_ERROR_WOULD_BLOCK) { if (ret < 0) { - printf("error: 'recvfrom(0, buffer, %d)' failed during test with code %d\r\n", sizeof test_buffer, ret); + printf("error: 'recvfrom(0, buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret); return -4; } else { - printf("error: 'recvfrom(0, buffer, %d)' returned %d when no data was expected\r\n", sizeof test_buffer, ret); + printf("error: 'recvfrom(0, buffer, %d)' returned %d when no data was expected\r\n", sizeof rx_test_buffer, ret); return -5; } } for (total = 0; total < size;) { - int ret = udp->sendto(*addr, test_buffer, size); + int ret = udp->sendto(*addr, tx_test_buffer+total, size-total); if (ret < 0) { printf("error: 'sendto(SocketAddress(%s, %d), buffer, %d)' failed during test with code %d\r\n", @@ -336,21 +339,22 @@ total += ret; } + memset(rx_test_buffer, 0, size); for (total = 0; total < size;) { - ret = udp->recvfrom(0, test_buffer, sizeof test_buffer); + ret = udp->recvfrom(0, rx_test_buffer+total, (sizeof rx_test_buffer)-total); if (ret == NSAPI_ERROR_WOULD_BLOCK) { continue; } else if (ret < 0) { - printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof test_buffer, ret); + printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret); return -2; } else { total += ret; } } - if (total != size || !nsapi_buffer_check(test_buffer, size)) { - printf("error: 'recv(buffer, %d)' recieved incorrect data with length %d\r\n", sizeof test_buffer, total); + if (total != size || !nsapi_buffer_check(rx_test_buffer, size)) { + printf("error: 'recv(buffer, %d)' recieved incorrect data with length %d\r\n", sizeof rx_test_buffer, total); return -3; }