Skip to content

Commit 96c569e

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 96c569e

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

TESTS/netsocket/socket_sigio/main.cpp

Lines changed: 13 additions & 8 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);
@@ -83,7 +88,7 @@ void get_data(TCPSocket* sock){
8388

8489
TEST_ASSERT_EQUAL(result, true);
8590

86-
printf("HTTP: Received %d chars from server\r\n", ret);
91+
printf("HTTP: Received %d chars from server\r\n", len);
8792
printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
8893
printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
8994
printf("HTTP: Received message:\r\n");

TESTS/netsocket/tcp_hello_world/main.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,28 @@ 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);
8795

8896
if (found_200_ok && found_hello) result = true;
8997

90-
printf("HTTP: Received %d chars from server\r\n", ret);
98+
printf("HTTP: Received %d chars from server\r\n", len);
9199
printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
92100
printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
93101
printf("HTTP: Received message:\r\n");

0 commit comments

Comments
 (0)