samples: net: websocket: Verify the received data properly
Make sure that we do not calculate terminating \n when comparing the received data to sent data because the \n is not part of the lorem_ipsum buffer. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
parent
b957581391
commit
748eb99646
1 changed files with 42 additions and 30 deletions
|
@ -32,7 +32,7 @@ LOG_MODULE_REGISTER(net_websocket_client_sample, LOG_LEVEL_DBG);
|
|||
/* Generated by http://www.lipsum.com/
|
||||
* 2 paragraphs, 178 words, 1160 bytes of Lorem Ipsum
|
||||
*/
|
||||
const char lorem_ipsum[] =
|
||||
static const char lorem_ipsum[] =
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
|
||||
"Vestibulum ultricies sapien tellus, ac viverra dolor bibendum "
|
||||
"lacinia. Vestibulum et nisl tristique tellus finibus gravida "
|
||||
|
@ -183,21 +183,22 @@ static ssize_t sendall_with_bsd_api(int sock, const void *buf, size_t len)
|
|||
return send(sock, buf, len, 0);
|
||||
}
|
||||
|
||||
static void recv_data_with_ws_api(int sock, size_t amount, u8_t *buf,
|
||||
size_t buf_len, const char *proto)
|
||||
static void recv_data_wso_api(int sock, size_t amount, u8_t *buf,
|
||||
size_t buf_len, const char *proto)
|
||||
{
|
||||
u64_t remaining = ULLONG_MAX;
|
||||
int total_read;
|
||||
u32_t message_type;
|
||||
int remaining;
|
||||
int ret, read_pos;
|
||||
|
||||
remaining = amount - 1;
|
||||
read_pos = 0;
|
||||
total_read = 0;
|
||||
|
||||
while (remaining > 0) {
|
||||
ret = websocket_recv_msg(sock, buf + read_pos,
|
||||
buf_len - read_pos,
|
||||
&message_type,
|
||||
NULL,
|
||||
&remaining,
|
||||
K_NO_WAIT);
|
||||
if (ret <= 0) {
|
||||
if (ret == -EAGAIN) {
|
||||
|
@ -211,25 +212,31 @@ static void recv_data_with_ws_api(int sock, size_t amount, u8_t *buf,
|
|||
}
|
||||
|
||||
read_pos += ret;
|
||||
remaining -= ret;
|
||||
}
|
||||
total_read += ret;
|
||||
};
|
||||
|
||||
if (remaining <= 0 &&
|
||||
if (remaining != 0 || total_read != amount ||
|
||||
/* Do not check the final \n at the end of the msg */
|
||||
memcmp(lorem_ipsum, buf, amount - 1) != 0) {
|
||||
LOG_ERR("%s data recv failure %zd bytes", proto, amount - 1);
|
||||
LOG_ERR("%s data recv failure %zd/%d bytes (remaining %lld)",
|
||||
proto, amount, total_read, remaining);
|
||||
LOG_HEXDUMP_DBG(buf, total_read, "received ws buf");
|
||||
LOG_HEXDUMP_DBG(lorem_ipsum, total_read, "sent ws buf");
|
||||
} else {
|
||||
LOG_DBG("%s recv %d bytes", proto, total_read);
|
||||
}
|
||||
}
|
||||
|
||||
static void recv_data_with_bsd_api(int sock, size_t amount, u8_t *buf,
|
||||
size_t buf_len, const char *proto)
|
||||
static void recv_data_bsd_api(int sock, size_t amount, u8_t *buf,
|
||||
size_t buf_len, const char *proto)
|
||||
{
|
||||
int remaining = amount;
|
||||
int remaining;
|
||||
int ret, read_pos;
|
||||
|
||||
remaining = amount - 1;
|
||||
remaining = amount;
|
||||
read_pos = 0;
|
||||
|
||||
while (remaining >= 0) {
|
||||
while (remaining > 0) {
|
||||
ret = recv(sock, buf + read_pos, buf_len - read_pos, 0);
|
||||
if (ret <= 0) {
|
||||
if (errno == EAGAIN || errno == ETIMEDOUT) {
|
||||
|
@ -246,14 +253,20 @@ static void recv_data_with_bsd_api(int sock, size_t amount, u8_t *buf,
|
|||
remaining -= ret;
|
||||
}
|
||||
|
||||
if (remaining <= 0 &&
|
||||
if (remaining != 0 ||
|
||||
/* Do not check the final \n at the end of the msg */
|
||||
memcmp(lorem_ipsum, buf, amount - 1) != 0) {
|
||||
LOG_ERR("%s data recv failure %zd bytes", proto, amount - 1);
|
||||
LOG_ERR("%s data recv failure %zd/%d bytes (remaining %d)",
|
||||
proto, amount, read_pos, remaining);
|
||||
LOG_HEXDUMP_DBG(buf, read_pos, "received bsd buf");
|
||||
LOG_HEXDUMP_DBG(lorem_ipsum, read_pos, "sent bsd buf");
|
||||
} else {
|
||||
LOG_DBG("%s recv %d bytes", proto, read_pos);
|
||||
}
|
||||
}
|
||||
|
||||
static bool send_and_wait_data(int sock, size_t amount, const char *proto,
|
||||
u8_t *buf, size_t buf_len)
|
||||
static bool send_and_wait_msg(int sock, size_t amount, const char *proto,
|
||||
u8_t *buf, size_t buf_len)
|
||||
{
|
||||
static int count;
|
||||
int ret;
|
||||
|
@ -271,12 +284,13 @@ static bool send_and_wait_data(int sock, size_t amount, const char *proto,
|
|||
|
||||
/* Send every 2nd message using dedicated websocket API and generic
|
||||
* BSD socket API. Real applications would not work like this but here
|
||||
* we want to test both APIs.
|
||||
* we want to test both APIs. We also need to send the \n so add it
|
||||
* here to amount variable.
|
||||
*/
|
||||
if (count % 2) {
|
||||
ret = sendall_with_ws_api(sock, buf, amount);
|
||||
ret = sendall_with_ws_api(sock, buf, amount + 1);
|
||||
} else {
|
||||
ret = sendall_with_bsd_api(sock, buf, amount);
|
||||
ret = sendall_with_bsd_api(sock, buf, amount + 1);
|
||||
}
|
||||
|
||||
if (ret <= 0) {
|
||||
|
@ -293,9 +307,9 @@ static bool send_and_wait_data(int sock, size_t amount, const char *proto,
|
|||
}
|
||||
|
||||
if (count % 2) {
|
||||
recv_data_with_ws_api(sock, amount, buf, buf_len, proto);
|
||||
recv_data_wso_api(sock, amount + 1, buf, buf_len, proto);
|
||||
} else {
|
||||
recv_data_with_bsd_api(sock, amount, buf, buf_len, proto);
|
||||
recv_data_bsd_api(sock, amount + 1, buf, buf_len, proto);
|
||||
}
|
||||
|
||||
count++;
|
||||
|
@ -397,16 +411,14 @@ void main(void)
|
|||
amount = how_much_to_send(ipsum_len);
|
||||
|
||||
if (websock4 >= 0 &&
|
||||
!send_and_wait_data(websock4, amount, "IPv4",
|
||||
recv_buf_ipv4,
|
||||
sizeof(recv_buf_ipv4))) {
|
||||
!send_and_wait_msg(websock4, amount, "IPv4",
|
||||
recv_buf_ipv4, sizeof(recv_buf_ipv4))) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (websock6 >= 0 &&
|
||||
!send_and_wait_data(websock6, amount, "IPv6",
|
||||
recv_buf_ipv6,
|
||||
sizeof(recv_buf_ipv6))) {
|
||||
!send_and_wait_msg(websock6, amount, "IPv6",
|
||||
recv_buf_ipv6, sizeof(recv_buf_ipv6))) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue