fixed buffer management in case of packet fragmentation; improved test pattern with pseudo random to avoid pattern simulation

Fork of NSAPITests by Licio Mapelli

Committer:
mapellil
Date:
Wed Oct 26 09:52:29 2016 +0000
Revision:
13:950327f445a3
Parent:
10:0d938ec9100c
Fixed buffer wraparound in case of packet fragmentation; improved test pattern with pseudo random to avoid pattern simulation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 10:0d938ec9100c 1 #include "NetworkStack.h"
Brian Daniels 2:41bf867fedd2 2 #include "TCPSocket.h"
Brian Daniels 3:8b595ee6219d 3 #include "UDPSocket.h"
Brian Daniels 2:41bf867fedd2 4 #include <stdio.h>
Brian Daniels 2:41bf867fedd2 5 #include "string.h"
Brian Daniels 2:41bf867fedd2 6
Christopher Haster 10:0d938ec9100c 7 #define NSAPI_MAX_DATA_SIZE 2048
Christopher Haster 10:0d938ec9100c 8 #define NSAPI_TEST_HOST "mbed.org"
Christopher Haster 10:0d938ec9100c 9 #define NSAPI_TEST_IP "8.8.4.4"
Brian Daniels 2:41bf867fedd2 10
mapellil 13:950327f445a3 11 uint8_t tx_test_buffer[NSAPI_MAX_DATA_SIZE];
mapellil 13:950327f445a3 12 uint8_t rx_test_buffer[NSAPI_MAX_DATA_SIZE];
Brian Daniels 2:41bf867fedd2 13
Christopher Haster 10:0d938ec9100c 14 int nsapi_networkstack_get_ip_address_test(NetworkStack *stack)
Christopher Haster 10:0d938ec9100c 15 {
Christopher Haster 10:0d938ec9100c 16 if (!stack->get_ip_address()[0]) {
Christopher Haster 10:0d938ec9100c 17 printf("error: 'get_ip_address()' did not return an IP address\r\n");
Christopher Haster 10:0d938ec9100c 18 return -1;
Christopher Haster 10:0d938ec9100c 19 }
Brian Daniels 2:41bf867fedd2 20
Christopher Haster 10:0d938ec9100c 21 return 0;
Brian Daniels 2:41bf867fedd2 22 }
Brian Daniels 2:41bf867fedd2 23
Christopher Haster 10:0d938ec9100c 24 int nsapi_networkstack_gethostbyname_test(NetworkStack *stack, SocketAddress *address, const char *test_host)
Brian Daniels 2:41bf867fedd2 25 {
Christopher Haster 10:0d938ec9100c 26 int ret = stack->gethostbyname(address, NSAPI_TEST_HOST);
Brian Daniels 2:41bf867fedd2 27
Christopher Haster 10:0d938ec9100c 28 if (ret) {
Christopher Haster 10:0d938ec9100c 29 printf("error: 'gethostbyname(\"%s\")' failed with code %d\r\n", NSAPI_TEST_HOST, ret);
Christopher Haster 10:0d938ec9100c 30 return -1;
Christopher Haster 10:0d938ec9100c 31 } else if (!address) {
Christopher Haster 10:0d938ec9100c 32 printf("error: 'gethostbyname(\"%s\")' returned null IP address\r\n", NSAPI_TEST_HOST);
Christopher Haster 10:0d938ec9100c 33 return -2;
Christopher Haster 10:0d938ec9100c 34 }
Christopher Haster 10:0d938ec9100c 35
Christopher Haster 10:0d938ec9100c 36 ret = stack->gethostbyname(address, NSAPI_TEST_IP);
Brian Daniels 2:41bf867fedd2 37
Christopher Haster 10:0d938ec9100c 38 if (ret) {
Christopher Haster 10:0d938ec9100c 39 printf("error: 'gethostbyname(\"%s\")' failed with code %d\r\n", NSAPI_TEST_IP, ret);
Christopher Haster 10:0d938ec9100c 40 return -1;
Christopher Haster 10:0d938ec9100c 41 } else if (!address) {
Christopher Haster 10:0d938ec9100c 42 printf("error: 'gethostbyname(\"%s\")' returned null IP address\r\n", NSAPI_TEST_IP);
Christopher Haster 10:0d938ec9100c 43 return -2;
Christopher Haster 10:0d938ec9100c 44 }
Christopher Haster 10:0d938ec9100c 45
Christopher Haster 10:0d938ec9100c 46 ret = stack->gethostbyname(address, test_host);
Christopher Haster 10:0d938ec9100c 47
Christopher Haster 10:0d938ec9100c 48 if (ret) {
Christopher Haster 10:0d938ec9100c 49 printf("error: 'gethostbyname(\"%s\")' failed with code %d\r\n", test_host, ret);
Christopher Haster 10:0d938ec9100c 50 return -1;
Christopher Haster 10:0d938ec9100c 51 } else if (!address) {
Christopher Haster 10:0d938ec9100c 52 printf("error: 'gethostbyname(\"%s\")' returned null IP address\r\n", test_host);
Christopher Haster 10:0d938ec9100c 53 return -2;
Christopher Haster 10:0d938ec9100c 54 }
Christopher Haster 10:0d938ec9100c 55
Brian Daniels 2:41bf867fedd2 56 return 0;
Christopher Haster 10:0d938ec9100c 57 }
Christopher Haster 10:0d938ec9100c 58
Christopher Haster 10:0d938ec9100c 59 static void nsapi_buffer_init(uint8_t *buffer, unsigned size)
Christopher Haster 10:0d938ec9100c 60 {
Christopher Haster 10:0d938ec9100c 61 for (unsigned i = 0; i < size; i++) {
mapellil 13:950327f445a3 62 buffer[i] = rand() % 256;
Christopher Haster 10:0d938ec9100c 63 }
Brian Daniels 2:41bf867fedd2 64 }
Brian Daniels 2:41bf867fedd2 65
Christopher Haster 10:0d938ec9100c 66 static bool nsapi_buffer_check(const uint8_t *buffer, unsigned size)
Brian Daniels 3:8b595ee6219d 67 {
Christopher Haster 10:0d938ec9100c 68 for (unsigned i = 0; i < size; i++) {
mapellil 13:950327f445a3 69 if (buffer[i] != tx_test_buffer[i]) {
Christopher Haster 10:0d938ec9100c 70 return false;
Christopher Haster 10:0d938ec9100c 71 }
Christopher Haster 10:0d938ec9100c 72 }
Brian Daniels 3:8b595ee6219d 73
Christopher Haster 10:0d938ec9100c 74 return true;
Christopher Haster 10:0d938ec9100c 75 }
Christopher Haster 10:0d938ec9100c 76
Christopher Haster 10:0d938ec9100c 77 static void nsapi_tcp_flush(TCPSocket *socket)
Christopher Haster 10:0d938ec9100c 78 {
Christopher Haster 10:0d938ec9100c 79 socket->set_timeout(1000);
mapellil 13:950327f445a3 80 socket->recv(rx_test_buffer, sizeof rx_test_buffer);
Christopher Haster 10:0d938ec9100c 81 socket->set_timeout(-1);
Brian Daniels 3:8b595ee6219d 82 }
Brian Daniels 3:8b595ee6219d 83
Christopher Haster 10:0d938ec9100c 84 int nsapi_tcp_open_test(TCPSocket *udp, NetworkStack *stack)
Brian Daniels 3:8b595ee6219d 85 {
Christopher Haster 10:0d938ec9100c 86 int ret = udp->open(stack);
Christopher Haster 10:0d938ec9100c 87
Christopher Haster 10:0d938ec9100c 88 if (ret) {
Christopher Haster 10:0d938ec9100c 89 printf("error: 'open(%p)' failed with code %d\r\n", stack, ret);
Christopher Haster 10:0d938ec9100c 90 return -1;
Christopher Haster 10:0d938ec9100c 91 } else {
Christopher Haster 10:0d938ec9100c 92 return 0;
Christopher Haster 10:0d938ec9100c 93 }
Christopher Haster 10:0d938ec9100c 94 }
Christopher Haster 10:0d938ec9100c 95
Christopher Haster 10:0d938ec9100c 96 int nsapi_tcp_connect_test(TCPSocket *tcp, SocketAddress *addr)
Christopher Haster 10:0d938ec9100c 97 {
Christopher Haster 10:0d938ec9100c 98 int ret = tcp->connect(*addr);
Christopher Haster 10:0d938ec9100c 99
Christopher Haster 10:0d938ec9100c 100 if (ret) {
Christopher Haster 10:0d938ec9100c 101 printf("error: 'connect(SocketAddress(%s, %d))' failed with code %d\r\n",
Christopher Haster 10:0d938ec9100c 102 addr->get_ip_address(), addr->get_port(), ret);
Christopher Haster 10:0d938ec9100c 103 return -1;
Christopher Haster 10:0d938ec9100c 104 } else {
Christopher Haster 10:0d938ec9100c 105 return 0;
Christopher Haster 10:0d938ec9100c 106 }
Brian Daniels 3:8b595ee6219d 107 }
Brian Daniels 3:8b595ee6219d 108
Christopher Haster 10:0d938ec9100c 109 static int nsapi_tcp_blocking_test_helper(TCPSocket *tcp, unsigned size)
Christopher Haster 10:0d938ec9100c 110 {
Christopher Haster 10:0d938ec9100c 111 unsigned total;
Christopher Haster 10:0d938ec9100c 112 nsapi_tcp_flush(tcp);
mapellil 13:950327f445a3 113 nsapi_buffer_init(tx_test_buffer, size);
Brian Daniels 3:8b595ee6219d 114
Christopher Haster 10:0d938ec9100c 115 for (total = 0; total < size;) {
mapellil 13:950327f445a3 116 int ret = tcp->send(tx_test_buffer+total, size-total);
Christopher Haster 10:0d938ec9100c 117
Christopher Haster 10:0d938ec9100c 118 if (ret < 0) {
Christopher Haster 10:0d938ec9100c 119 printf("error: 'send(buffer, %d)' failed during test with code %d\r\n", size, ret);
Christopher Haster 10:0d938ec9100c 120 return -1;
Christopher Haster 10:0d938ec9100c 121 }
Christopher Haster 10:0d938ec9100c 122
Christopher Haster 10:0d938ec9100c 123 total += ret;
Christopher Haster 10:0d938ec9100c 124 }
mapellil 13:950327f445a3 125
mapellil 13:950327f445a3 126 memset(rx_test_buffer, 0, size);
Christopher Haster 10:0d938ec9100c 127 for (total = 0; total < size;) {
mapellil 13:950327f445a3 128 int ret = tcp->recv(rx_test_buffer+total, (sizeof rx_test_buffer)-total);
Christopher Haster 10:0d938ec9100c 129
Christopher Haster 10:0d938ec9100c 130 if (ret < 0) {
mapellil 13:950327f445a3 131 printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret);
Christopher Haster 10:0d938ec9100c 132 return -2;
Christopher Haster 10:0d938ec9100c 133 }
Brian Daniels 3:8b595ee6219d 134
Christopher Haster 10:0d938ec9100c 135 total += ret;
Christopher Haster 10:0d938ec9100c 136 }
Christopher Haster 10:0d938ec9100c 137
mapellil 13:950327f445a3 138 if (total != size || !nsapi_buffer_check(rx_test_buffer, size)) {
mapellil 13:950327f445a3 139 printf("error: 'recv(buffer, %d)' recieved incorrect data with length %d\r\n", sizeof rx_test_buffer, total);
Christopher Haster 10:0d938ec9100c 140 return -3;
Christopher Haster 10:0d938ec9100c 141 }
Christopher Haster 10:0d938ec9100c 142
Christopher Haster 10:0d938ec9100c 143 return 0;
Brian Daniels 3:8b595ee6219d 144 }
Brian Daniels 3:8b595ee6219d 145
Christopher Haster 10:0d938ec9100c 146 int nsapi_tcp_blocking_test(TCPSocket *tcp)
Brian Daniels 3:8b595ee6219d 147 {
Christopher Haster 10:0d938ec9100c 148 for (unsigned size = 64; size <= NSAPI_MAX_DATA_SIZE; size *= 2) {
Christopher Haster 10:0d938ec9100c 149 printf("%s: size %d\r\n", __func__, size);
Brian Daniels 3:8b595ee6219d 150
Christopher Haster 10:0d938ec9100c 151 int ret = nsapi_tcp_blocking_test_helper(tcp, size);
Christopher Haster 10:0d938ec9100c 152 if (ret) {
Christopher Haster 10:0d938ec9100c 153 return ret;
Christopher Haster 10:0d938ec9100c 154 }
Christopher Haster 10:0d938ec9100c 155 }
Brian Daniels 3:8b595ee6219d 156
Christopher Haster 10:0d938ec9100c 157 return 0;
Brian Daniels 7:444bc156318f 158 }
Brian Daniels 3:8b595ee6219d 159
Christopher Haster 10:0d938ec9100c 160 static int nsapi_tcp_non_blocking_test_helper(TCPSocket *tcp, unsigned size)
Brian Daniels 7:444bc156318f 161 {
Christopher Haster 10:0d938ec9100c 162 unsigned total;
Christopher Haster 10:0d938ec9100c 163 nsapi_tcp_flush(tcp);
mapellil 13:950327f445a3 164 nsapi_buffer_init(tx_test_buffer, size);
Brian Daniels 3:8b595ee6219d 165
Christopher Haster 10:0d938ec9100c 166 // First check to make sure `recv` will not block and return 0 for bytes received.
Christopher Haster 10:0d938ec9100c 167 tcp->set_blocking(false);
mapellil 13:950327f445a3 168 int ret = tcp->recv(rx_test_buffer, sizeof rx_test_buffer);
Brian Daniels 2:41bf867fedd2 169
Christopher Haster 10:0d938ec9100c 170 if (ret != NSAPI_ERROR_WOULD_BLOCK) {
Christopher Haster 10:0d938ec9100c 171 if (ret < 0) {
mapellil 13:950327f445a3 172 printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret);
Christopher Haster 10:0d938ec9100c 173 return -4;
Christopher Haster 10:0d938ec9100c 174 } else {
mapellil 13:950327f445a3 175 printf("error: 'recv(buffer, %d)' returned %d when no data was expected\r\n", sizeof rx_test_buffer, ret);
Christopher Haster 10:0d938ec9100c 176 return -5;
Christopher Haster 10:0d938ec9100c 177 }
Brian Daniels 3:8b595ee6219d 178 }
Christopher Haster 10:0d938ec9100c 179
Christopher Haster 10:0d938ec9100c 180 for (total = 0; total < size;) {
mapellil 13:950327f445a3 181 int ret = tcp->send(tx_test_buffer+total, size-total);
Brian Daniels 3:8b595ee6219d 182
Christopher Haster 10:0d938ec9100c 183 if (ret < 0) {
Christopher Haster 10:0d938ec9100c 184 printf("error: 'send(buffer, %d)' failed during test with code %d\r\n", size, ret);
Christopher Haster 10:0d938ec9100c 185 return -1;
Christopher Haster 10:0d938ec9100c 186 }
Christopher Haster 10:0d938ec9100c 187
Christopher Haster 10:0d938ec9100c 188 total += ret;
Christopher Haster 10:0d938ec9100c 189 }
Christopher Haster 10:0d938ec9100c 190
mapellil 13:950327f445a3 191 memset (rx_test_buffer, 0, size);
Christopher Haster 10:0d938ec9100c 192 for (total = 0; total < size;) {
mapellil 13:950327f445a3 193 ret = tcp->recv(rx_test_buffer+total, (sizeof rx_test_buffer)-total);
Brian Daniels 7:444bc156318f 194
Christopher Haster 10:0d938ec9100c 195 if (ret == NSAPI_ERROR_WOULD_BLOCK) {
Christopher Haster 10:0d938ec9100c 196 continue;
Christopher Haster 10:0d938ec9100c 197 } else if (ret < 0) {
mapellil 13:950327f445a3 198 printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret);
Christopher Haster 10:0d938ec9100c 199 return -2;
Christopher Haster 10:0d938ec9100c 200 } else {
Christopher Haster 10:0d938ec9100c 201 total += ret;
Christopher Haster 10:0d938ec9100c 202 }
Brian Daniels 7:444bc156318f 203 }
Brian Daniels 7:444bc156318f 204
mapellil 13:950327f445a3 205 if (total != size || !nsapi_buffer_check(rx_test_buffer, size)) {
mapellil 13:950327f445a3 206 printf("error: 'recv(buffer, %d)' recieved incorrect data with length %d\r\n", sizeof rx_test_buffer, total);
Christopher Haster 10:0d938ec9100c 207 return -3;
Christopher Haster 10:0d938ec9100c 208 }
Christopher Haster 10:0d938ec9100c 209
Brian Daniels 7:444bc156318f 210 return 0;
Brian Daniels 7:444bc156318f 211 }
Brian Daniels 7:444bc156318f 212
Christopher Haster 10:0d938ec9100c 213 int nsapi_tcp_non_blocking_test(TCPSocket *tcp)
Brian Daniels 3:8b595ee6219d 214 {
Christopher Haster 10:0d938ec9100c 215 for (unsigned size = 64; size <= NSAPI_MAX_DATA_SIZE; size *= 2) {
Christopher Haster 10:0d938ec9100c 216 printf("%s: size %d\r\n", __func__, size);
Brian Daniels 3:8b595ee6219d 217
Christopher Haster 10:0d938ec9100c 218 int ret = nsapi_tcp_non_blocking_test_helper(tcp, size);
Christopher Haster 10:0d938ec9100c 219 if (ret) {
Christopher Haster 10:0d938ec9100c 220 return ret;
Christopher Haster 10:0d938ec9100c 221 }
Christopher Haster 10:0d938ec9100c 222 }
Brian Daniels 7:444bc156318f 223
Christopher Haster 10:0d938ec9100c 224 return 0;
Christopher Haster 10:0d938ec9100c 225 }
Brian Daniels 3:8b595ee6219d 226
Christopher Haster 10:0d938ec9100c 227 int nsapi_tcp_close_test(TCPSocket *tcp)
Christopher Haster 10:0d938ec9100c 228 {
Christopher Haster 10:0d938ec9100c 229 int ret = tcp->close();
Brian Daniels 2:41bf867fedd2 230
Christopher Haster 10:0d938ec9100c 231 if (ret) {
Christopher Haster 10:0d938ec9100c 232 printf("error 'close()' failed with code %d\r\n", ret);
Christopher Haster 10:0d938ec9100c 233 return -1;
Christopher Haster 10:0d938ec9100c 234 } else {
Christopher Haster 10:0d938ec9100c 235 return 0;
Christopher Haster 10:0d938ec9100c 236 }
Christopher Haster 10:0d938ec9100c 237 }
Brian Daniels 5:03b0570b4c29 238
Christopher Haster 10:0d938ec9100c 239 static void nsapi_udp_flush(UDPSocket *udp)
Christopher Haster 10:0d938ec9100c 240 {
Christopher Haster 10:0d938ec9100c 241 udp->set_timeout(1000);
mapellil 13:950327f445a3 242 udp->recvfrom(0, rx_test_buffer, sizeof rx_test_buffer);
Christopher Haster 10:0d938ec9100c 243 udp->set_timeout(-1);
Christopher Haster 10:0d938ec9100c 244 }
Christopher Haster 10:0d938ec9100c 245
Christopher Haster 10:0d938ec9100c 246 int nsapi_udp_open_test(UDPSocket *udp, NetworkStack *stack)
Christopher Haster 10:0d938ec9100c 247 {
Christopher Haster 10:0d938ec9100c 248 int ret = udp->open(stack);
Christopher Haster 10:0d938ec9100c 249
Christopher Haster 10:0d938ec9100c 250 if (ret) {
Christopher Haster 10:0d938ec9100c 251 printf("error: 'open(%p)' failed with code %d\r\n", stack, ret);
Christopher Haster 10:0d938ec9100c 252 return -1;
Christopher Haster 10:0d938ec9100c 253 } else {
Christopher Haster 10:0d938ec9100c 254 return 0;
Brian Daniels 3:8b595ee6219d 255 }
Brian Daniels 2:41bf867fedd2 256 }
Christopher Haster 1:796ba8b082b1 257
Christopher Haster 10:0d938ec9100c 258 static int nsapi_udp_blocking_test_helper(UDPSocket *udp, SocketAddress *addr, unsigned size)
Brian Daniels 7:444bc156318f 259 {
Christopher Haster 10:0d938ec9100c 260 unsigned total;
Christopher Haster 10:0d938ec9100c 261 nsapi_udp_flush(udp);
mapellil 13:950327f445a3 262 nsapi_buffer_init(tx_test_buffer, size);
Christopher Haster 10:0d938ec9100c 263
Christopher Haster 10:0d938ec9100c 264 for (total = 0; total < size;) {
mapellil 13:950327f445a3 265 int ret = udp->sendto(*addr, tx_test_buffer+total, size-total);
Brian Daniels 7:444bc156318f 266
Christopher Haster 10:0d938ec9100c 267 if (ret < 0) {
Christopher Haster 10:0d938ec9100c 268 printf("error: 'sendto(SocketAddress(%s, %d), buffer, %d)' failed during test with code %d\r\n",
Christopher Haster 10:0d938ec9100c 269 addr->get_ip_address(), addr->get_port(), size, ret);
Christopher Haster 10:0d938ec9100c 270 return -1;
Christopher Haster 10:0d938ec9100c 271 }
Christopher Haster 10:0d938ec9100c 272
Christopher Haster 10:0d938ec9100c 273 total += ret;
Christopher Haster 10:0d938ec9100c 274 }
mapellil 13:950327f445a3 275
mapellil 13:950327f445a3 276 memset(rx_test_buffer, 0, size);
Christopher Haster 10:0d938ec9100c 277 for (total = 0; total < size;) {
mapellil 13:950327f445a3 278 int ret = udp->recvfrom(0, rx_test_buffer+total, (sizeof rx_test_buffer)-total);
Brian Daniels 7:444bc156318f 279
Christopher Haster 10:0d938ec9100c 280 if (ret < 0) {
mapellil 13:950327f445a3 281 printf("error: 'recvfrom(0, buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret);
Christopher Haster 10:0d938ec9100c 282 return -2;
Christopher Haster 10:0d938ec9100c 283 }
Christopher Haster 10:0d938ec9100c 284
Christopher Haster 10:0d938ec9100c 285 total += ret;
Brian Daniels 7:444bc156318f 286 }
Christopher Haster 10:0d938ec9100c 287
mapellil 13:950327f445a3 288 if (total != size || !nsapi_buffer_check(rx_test_buffer, size)) {
mapellil 13:950327f445a3 289 printf("error: 'recvfrom(0, buffer, %d)' recieved incorrect data with length %d\r\n", sizeof rx_test_buffer, total);
Christopher Haster 10:0d938ec9100c 290 return -3;
Christopher Haster 10:0d938ec9100c 291 }
Brian Daniels 7:444bc156318f 292
Brian Daniels 7:444bc156318f 293 return 0;
Christopher Haster 10:0d938ec9100c 294 }
Christopher Haster 10:0d938ec9100c 295
Christopher Haster 10:0d938ec9100c 296 int nsapi_udp_blocking_test(UDPSocket *udp, SocketAddress *addr)
Christopher Haster 10:0d938ec9100c 297 {
Christopher Haster 10:0d938ec9100c 298 for (unsigned size = 64; size <= NSAPI_MAX_DATA_SIZE; size *= 2) {
Christopher Haster 10:0d938ec9100c 299 printf("%s: size %d\r\n", __func__, size);
Christopher Haster 10:0d938ec9100c 300
Christopher Haster 10:0d938ec9100c 301 int ret = nsapi_udp_blocking_test_helper(udp, addr, size);
Christopher Haster 10:0d938ec9100c 302 if (ret) {
Christopher Haster 10:0d938ec9100c 303 return ret;
Christopher Haster 10:0d938ec9100c 304 }
Christopher Haster 10:0d938ec9100c 305 }
Christopher Haster 10:0d938ec9100c 306
Christopher Haster 10:0d938ec9100c 307 return 0;
Brian Daniels 7:444bc156318f 308 }
Brian Daniels 7:444bc156318f 309
Christopher Haster 10:0d938ec9100c 310 int nsapi_udp_non_blocking_test_helper(UDPSocket *udp, SocketAddress *addr, unsigned size)
Christopher Haster 1:796ba8b082b1 311 {
Christopher Haster 10:0d938ec9100c 312 unsigned total;
Christopher Haster 10:0d938ec9100c 313 nsapi_udp_flush(udp);
mapellil 13:950327f445a3 314 nsapi_buffer_init(tx_test_buffer, size);
Christopher Haster 10:0d938ec9100c 315
Christopher Haster 10:0d938ec9100c 316 // First check to make sure `recv` will not block and return 0 for bytes received.
Christopher Haster 10:0d938ec9100c 317 udp->set_blocking(false);
mapellil 13:950327f445a3 318 int ret = udp->recvfrom(0, rx_test_buffer, sizeof rx_test_buffer);
Brian Daniels 3:8b595ee6219d 319
Christopher Haster 10:0d938ec9100c 320 if (ret != NSAPI_ERROR_WOULD_BLOCK) {
Christopher Haster 10:0d938ec9100c 321 if (ret < 0) {
mapellil 13:950327f445a3 322 printf("error: 'recvfrom(0, buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret);
Christopher Haster 10:0d938ec9100c 323 return -4;
Christopher Haster 10:0d938ec9100c 324 } else {
mapellil 13:950327f445a3 325 printf("error: 'recvfrom(0, buffer, %d)' returned %d when no data was expected\r\n", sizeof rx_test_buffer, ret);
Christopher Haster 10:0d938ec9100c 326 return -5;
Christopher Haster 10:0d938ec9100c 327 }
Christopher Haster 10:0d938ec9100c 328 }
Brian Daniels 3:8b595ee6219d 329
Christopher Haster 10:0d938ec9100c 330 for (total = 0; total < size;) {
mapellil 13:950327f445a3 331 int ret = udp->sendto(*addr, tx_test_buffer+total, size-total);
Brian Daniels 3:8b595ee6219d 332
Christopher Haster 10:0d938ec9100c 333 if (ret < 0) {
Christopher Haster 10:0d938ec9100c 334 printf("error: 'sendto(SocketAddress(%s, %d), buffer, %d)' failed during test with code %d\r\n",
Christopher Haster 10:0d938ec9100c 335 addr->get_ip_address(), addr->get_port(), size, ret);
Christopher Haster 10:0d938ec9100c 336 return -1;
Christopher Haster 10:0d938ec9100c 337 }
Christopher Haster 10:0d938ec9100c 338
Christopher Haster 10:0d938ec9100c 339 total += ret;
Christopher Haster 10:0d938ec9100c 340 }
Christopher Haster 10:0d938ec9100c 341
mapellil 13:950327f445a3 342 memset(rx_test_buffer, 0, size);
Christopher Haster 10:0d938ec9100c 343 for (total = 0; total < size;) {
mapellil 13:950327f445a3 344 ret = udp->recvfrom(0, rx_test_buffer+total, (sizeof rx_test_buffer)-total);
Brian Daniels 3:8b595ee6219d 345
Christopher Haster 10:0d938ec9100c 346 if (ret == NSAPI_ERROR_WOULD_BLOCK) {
Christopher Haster 10:0d938ec9100c 347 continue;
Christopher Haster 10:0d938ec9100c 348 } else if (ret < 0) {
mapellil 13:950327f445a3 349 printf("error: 'recv(buffer, %d)' failed during test with code %d\r\n", sizeof rx_test_buffer, ret);
Christopher Haster 10:0d938ec9100c 350 return -2;
Christopher Haster 10:0d938ec9100c 351 } else {
Christopher Haster 10:0d938ec9100c 352 total += ret;
Christopher Haster 10:0d938ec9100c 353 }
Christopher Haster 10:0d938ec9100c 354 }
Brian Daniels 3:8b595ee6219d 355
mapellil 13:950327f445a3 356 if (total != size || !nsapi_buffer_check(rx_test_buffer, size)) {
mapellil 13:950327f445a3 357 printf("error: 'recv(buffer, %d)' recieved incorrect data with length %d\r\n", sizeof rx_test_buffer, total);
Christopher Haster 10:0d938ec9100c 358 return -3;
Christopher Haster 10:0d938ec9100c 359 }
Christopher Haster 10:0d938ec9100c 360
Christopher Haster 10:0d938ec9100c 361 return 0;
Brian Daniels 3:8b595ee6219d 362 }
Brian Daniels 3:8b595ee6219d 363
Christopher Haster 10:0d938ec9100c 364 int nsapi_udp_non_blocking_test(UDPSocket *udp, SocketAddress *addr)
Christopher Haster 10:0d938ec9100c 365 {
Christopher Haster 10:0d938ec9100c 366 for (unsigned size = 64; size <= NSAPI_MAX_DATA_SIZE; size *= 2) {
Christopher Haster 10:0d938ec9100c 367 printf("%s: size %d\r\n", __func__, size);
Christopher Haster 10:0d938ec9100c 368
Christopher Haster 10:0d938ec9100c 369 int ret = nsapi_udp_non_blocking_test_helper(udp, addr, size);
Christopher Haster 10:0d938ec9100c 370 if (ret) {
Christopher Haster 10:0d938ec9100c 371 return ret;
Christopher Haster 10:0d938ec9100c 372 }
Christopher Haster 10:0d938ec9100c 373 }
Christopher Haster 10:0d938ec9100c 374
Christopher Haster 10:0d938ec9100c 375 return 0;
Christopher Haster 10:0d938ec9100c 376 }
Christopher Haster 10:0d938ec9100c 377
Christopher Haster 10:0d938ec9100c 378 int nsapi_udp_close_test(UDPSocket *udp)
Christopher Haster 10:0d938ec9100c 379 {
Christopher Haster 10:0d938ec9100c 380 int ret = udp->close();
Christopher Haster 10:0d938ec9100c 381
Christopher Haster 10:0d938ec9100c 382 if (ret) {
Christopher Haster 10:0d938ec9100c 383 printf("error 'close()' failed with code %d\r\n", ret);
Christopher Haster 10:0d938ec9100c 384 return -1;
Christopher Haster 10:0d938ec9100c 385 } else {
Christopher Haster 10:0d938ec9100c 386 return 0;
Christopher Haster 10:0d938ec9100c 387 }
Brian Daniels 3:8b595ee6219d 388 }
Brian Daniels 3:8b595ee6219d 389
Christopher Haster 10:0d938ec9100c 390 int nsapi_tests(const char *name, NetworkStack *stack, const char *test_host, uint16_t test_port)
Brian Daniels 3:8b595ee6219d 391 {
Christopher Haster 10:0d938ec9100c 392 SocketAddress address(0, test_port);
Christopher Haster 10:0d938ec9100c 393 TCPSocket tcp;
Christopher Haster 10:0d938ec9100c 394 UDPSocket udp;
Christopher Haster 10:0d938ec9100c 395
Christopher Haster 10:0d938ec9100c 396 int result = 0;
Christopher Haster 10:0d938ec9100c 397
Christopher Haster 10:0d938ec9100c 398 #define NSAPI_MARK_TESTS(tests) \
Christopher Haster 10:0d938ec9100c 399 printf("\r\n\r\nRunning %s Tests\r\n\r\n", tests)
Brian Daniels 7:444bc156318f 400
Christopher Haster 10:0d938ec9100c 401 #define NSAPI_RUN_TEST(test, ...) \
Christopher Haster 10:0d938ec9100c 402 do { \
Christopher Haster 10:0d938ec9100c 403 printf("---------------------\r\n"); \
Christopher Haster 10:0d938ec9100c 404 printf("%s: running...\r\n", #test); \
Christopher Haster 10:0d938ec9100c 405 int test##_result = test(__VA_ARGS__); \
Christopher Haster 10:0d938ec9100c 406 if (!test##_result) { \
Christopher Haster 10:0d938ec9100c 407 printf("%s: PASS\r\n", #test); \
Christopher Haster 10:0d938ec9100c 408 } else { \
Christopher Haster 10:0d938ec9100c 409 printf("%s: FAIL (%d)\r\n", #test, test##_result); \
Christopher Haster 10:0d938ec9100c 410 } \
Christopher Haster 10:0d938ec9100c 411 result |= test##_result; \
Christopher Haster 10:0d938ec9100c 412 } while (0)
Brian Daniels 3:8b595ee6219d 413
Christopher Haster 10:0d938ec9100c 414 NSAPI_MARK_TESTS("NetworkStack");
Christopher Haster 10:0d938ec9100c 415 NSAPI_RUN_TEST(nsapi_networkstack_get_ip_address_test, stack);
Christopher Haster 10:0d938ec9100c 416 NSAPI_RUN_TEST(nsapi_networkstack_gethostbyname_test, stack, &address, test_host);
Christopher Haster 1:796ba8b082b1 417
Christopher Haster 10:0d938ec9100c 418 NSAPI_MARK_TESTS("UDPSocket");
Christopher Haster 10:0d938ec9100c 419 NSAPI_RUN_TEST(nsapi_udp_open_test, &udp, stack);
Christopher Haster 10:0d938ec9100c 420 NSAPI_RUN_TEST(nsapi_udp_blocking_test, &udp, &address);
Christopher Haster 10:0d938ec9100c 421 NSAPI_RUN_TEST(nsapi_udp_non_blocking_test, &udp, &address);
Christopher Haster 10:0d938ec9100c 422 NSAPI_RUN_TEST(nsapi_udp_close_test, &udp);
Christopher Haster 1:796ba8b082b1 423
Christopher Haster 10:0d938ec9100c 424 NSAPI_MARK_TESTS("TCPSocket");
Christopher Haster 10:0d938ec9100c 425 NSAPI_RUN_TEST(nsapi_tcp_open_test, &tcp, stack);
Christopher Haster 10:0d938ec9100c 426 NSAPI_RUN_TEST(nsapi_tcp_connect_test, &tcp, &address);
Christopher Haster 10:0d938ec9100c 427 NSAPI_RUN_TEST(nsapi_tcp_blocking_test, &tcp);
Christopher Haster 10:0d938ec9100c 428 NSAPI_RUN_TEST(nsapi_tcp_non_blocking_test, &tcp);
Christopher Haster 10:0d938ec9100c 429 NSAPI_RUN_TEST(nsapi_tcp_close_test, &tcp);
Brian Daniels 3:8b595ee6219d 430
Christopher Haster 10:0d938ec9100c 431 if (result == 0) {
Christopher Haster 10:0d938ec9100c 432 printf("\r\n\r\n--- ALL TESTS PASSING ---\r\n");
Christopher Haster 10:0d938ec9100c 433 } else {
Christopher Haster 10:0d938ec9100c 434 printf("\r\n\r\n--- TEST FAILURES OCCURRED ---\r\n");
Christopher Haster 10:0d938ec9100c 435 }
Brian Daniels 5:03b0570b4c29 436
Christopher Haster 10:0d938ec9100c 437 return result;
Christopher Haster 1:796ba8b082b1 438 }
Christopher Haster 1:796ba8b082b1 439