Skip to content

Commit 8ec9f6b

Browse files
committed
Correct test 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 3c793a7 commit 8ec9f6b

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

TESTS/netsocket/socket_sigio/main.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@ bool find_substring(const char *first, const char *last, const char *s_first, co
6767
void get_data(TCPSocket* sock){
6868
bool result = false;
6969
// Server will respond with HTTP GET's success code
70-
const int ret = sock->recv(buffer, sizeof(buffer) - 1);
71-
if(ret <= 0)
72-
return;
73-
74-
buffer[ret] = '\0';
70+
int len = 0;
71+
int ret;
72+
while((ret = sock->recv(buffer+len, sizeof(buffer) - 1 - len)) > 0) {
73+
len += ret;
74+
}
75+
buffer[len] = '\0';
7576

7677
// Find 200 OK HTTP status in reply
77-
bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
78+
bool found_200_ok = find_substring(buffer, buffer + len, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
7879
// Find "Hello World!" string in reply
79-
bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
80+
bool found_hello = find_substring(buffer, buffer + len, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
8081

8182
TEST_ASSERT_TRUE(found_200_ok);
8283
TEST_ASSERT_TRUE(found_hello);
@@ -85,7 +86,7 @@ void get_data(TCPSocket* sock){
8586

8687
TEST_ASSERT_EQUAL(result, true);
8788

88-
printf("HTTP: Received %d chars from server\r\n", ret);
89+
printf("HTTP: Received %d chars from server\r\n", len);
8990
printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
9091
printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
9192
printf("HTTP: Received message:\r\n");

0 commit comments

Comments
 (0)