Skip to content

Commit 6f34221

Browse files
committed
Correct tests tcp_hello_world & socket_sigio
A call to `TCPSocket::recv(void *data, nsapi_size_t size)` returns, following the mbed documentation, the number of received bytes on success, and a negative error code on failure. So in case of success, the return value depends on both the value of parameter `size` but also on the amount of data already available. This means, that the value returned can be lower than or equal to the `size` of the `data` buffer passed as argument to the call. Therefore, in the cases of `test_tcp_hello_world()` & `find_substring()` (i.e. test `socket_sigio`), the calls to `TCPSocket::recv()` might return from one byte up to `sizeof(buffer) - 1` (i.e. 511) bytes for each single call, while the tests expect to receive the whole response string with a single call. This commit applies a fix to this situation by implementing a receive loop which exits once there is no data anymore available to be read from the socket.
1 parent 56dfd02 commit 6f34221

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

TESTS/netsocket/socket_sigio/main.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,21 @@ bool find_substring(const char *first, const char *last, const char *s_first, co
6565
void get_data(TCPSocket* sock){
6666
bool result = false;
6767
// Server will respond with HTTP GET's success code
68-
const int ret = sock->recv(buffer, sizeof(buffer) - 1);
69-
if(ret <= 0)
70-
return;
71-
72-
buffer[ret] = '\0';
68+
int len = 0;
69+
int ret;
70+
while((ret = sock->recv(buffer+len, sizeof(buffer) - 1- len)) > 0) {
71+
len += ret;
72+
sock->set_timeout(0);
73+
}
74+
if(len >= 0)
75+
buffer[len] = '\0';
76+
else
77+
buffer[0] = '\0';
7378

7479
// Find 200 OK HTTP status in reply
75-
bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
80+
bool found_200_ok = find_substring(buffer, buffer + len, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
7681
// Find "Hello World!" string in reply
77-
bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
82+
bool found_hello = find_substring(buffer, buffer + len, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
7883

7984
TEST_ASSERT_TRUE(found_200_ok);
8085
TEST_ASSERT_TRUE(found_hello);

TESTS/netsocket/tcp_hello_world/main.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,21 @@ void test_tcp_hello_world() {
7474
sock.send(buffer, strlen(buffer));
7575

7676
// Server will respond with HTTP GET's success code
77-
const int ret = sock.recv(buffer, sizeof(buffer) - 1);
78-
buffer[ret] = '\0';
77+
int len = 0;
78+
int ret;
79+
while((ret = sock.recv(buffer+len, sizeof(buffer) - 1- len)) > 0) {
80+
len += ret;
81+
sock.set_timeout(0);
82+
}
83+
if(len >= 0)
84+
buffer[len] = '\0';
85+
else
86+
buffer[0] = '\0';
7987

8088
// Find 200 OK HTTP status in reply
81-
bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
89+
bool found_200_ok = find_substring(buffer, buffer + len, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
8290
// Find "Hello World!" string in reply
83-
bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
91+
bool found_hello = find_substring(buffer, buffer + len, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
8492

8593
TEST_ASSERT_TRUE(found_200_ok);
8694
TEST_ASSERT_TRUE(found_hello);

0 commit comments

Comments
 (0)