Skip to content

Commit

Permalink
tests: Ztest tests for coap client
Browse files Browse the repository at this point in the history
Test handling multiple requests.

Signed-off-by: Jarno Lämsä <jarno.lamsa@nordicsemi.no>
  • Loading branch information
Jarno Lämsä authored and carlescufi committed Jun 28, 2023
1 parent 419fa3c commit a3612c7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
3 changes: 2 additions & 1 deletion tests/net/lib/coap_client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ add_compile_definitions(CONFIG_COAP_CLIENT_MESSAGE_HEADER_SIZE=48)
add_compile_definitions(CONFIG_COAP_CLIENT_STACK_SIZE=1024)
add_compile_definitions(CONFIG_COAP_CLIENT_THREAD_PRIORITY=10)
add_compile_definitions(CONFIG_COAP_LOG_LEVEL=4)
add_compile_definitions(CONFIG_NET_SOCKETS_POSIX_NAMES=y)
add_compile_definitions(CONFIG_COAP_INIT_ACK_TIMEOUT_MS=2000)
add_compile_definitions(CONFIG_COAP_CLIENT_MAX_REQUESTS=2)
add_compile_definitions(CONFIG_COAP_CLIENT_MAX_INSTANCES=2)
62 changes: 53 additions & 9 deletions tests/net/lib/coap_client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ DEFINE_FFF_GLOBALS;

static uint8_t last_response_code;
static const char *test_path = "test";
static uint16_t last_message_id;

static uint16_t messages_needing_response[2];

static struct coap_client client;

Expand All @@ -33,11 +34,21 @@ static char *long_payload = "Lorem ipsum dolor sit amet, consectetur adipiscing
static ssize_t z_impl_zsock_recvfrom_custom_fake(int sock, void *buf, size_t max_len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen)
{
uint16_t last_message_id = 0;

LOG_INF("Recvfrom");
static uint8_t ack_data[] = {
uint8_t ack_data[] = {
0x68, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

if (messages_needing_response[0] != 0) {
last_message_id = messages_needing_response[0];
messages_needing_response[0] = 0;
} else {
last_message_id = messages_needing_response[1];
messages_needing_response[1] = 0;
}

ack_data[2] = (uint8_t) (last_message_id >> 8);
ack_data[3] = (uint8_t) last_message_id;

Expand All @@ -50,11 +61,17 @@ static ssize_t z_impl_zsock_sendto_custom_fake(int sock, void *buf, size_t len,
int flags, const struct sockaddr *dest_addr,
socklen_t addrlen)
{
LOG_INF("Sendto");
last_message_id = 0;
uint16_t last_message_id = 0;

last_message_id |= ((uint8_t *) buf)[2] << 8;
last_message_id |= ((uint8_t *) buf)[3];

if (messages_needing_response[0] == 0) {
messages_needing_response[0] = last_message_id;
} else {
messages_needing_response[1] = last_message_id;
}

last_response_code = ((uint8_t *) buf)[1];

LOG_INF("Latest message ID: %d", last_message_id);
Expand All @@ -65,10 +82,20 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_response(int sock, void *buf, s
int flags, struct sockaddr *src_addr,
socklen_t *addrlen)
{
uint16_t last_message_id = 0;

static uint8_t ack_data[] = {
0x48, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

if (messages_needing_response[0] != 0) {
last_message_id = messages_needing_response[0];
messages_needing_response[0] = 0;
} else {
last_message_id = messages_needing_response[1];
messages_needing_response[1] = 0;
}

ack_data[2] = (uint8_t) (last_message_id >> 8);
ack_data[3] = (uint8_t) last_message_id;

Expand All @@ -81,10 +108,20 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_empty_ack(int sock, void *buf,
int flags, struct sockaddr *src_addr,
socklen_t *addrlen)
{
uint16_t last_message_id = 0;

static uint8_t ack_data[] = {
0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

if (messages_needing_response[0] != 0) {
last_message_id = messages_needing_response[0];
messages_needing_response[0] = 0;
} else {
last_message_id = messages_needing_response[1];
messages_needing_response[1] = 0;
}

ack_data[2] = (uint8_t) (last_message_id >> 8);
ack_data[3] = (uint8_t) last_message_id;

Expand All @@ -99,10 +136,20 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_unmatching(int sock, void *buf,
int flags, struct sockaddr *src_addr,
socklen_t *addrlen)
{
uint16_t last_message_id = 0;

static uint8_t ack_data[] = {
0x68, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
};

if (messages_needing_response[0] != 0) {
last_message_id = messages_needing_response[0];
messages_needing_response[0] = 0;
} else {
last_message_id = messages_needing_response[1];
messages_needing_response[1] = 0;
}

ack_data[2] = (uint8_t) (last_message_id >> 8);
ack_data[3] = (uint8_t) last_message_id;

Expand Down Expand Up @@ -242,7 +289,7 @@ ZTEST(coap_client, test_no_response)

LOG_INF("Send request");
clear_socket_events();
ret = coap_client_req(&client, 0, &address, &client_request, -1);
ret = coap_client_req(&client, 0, &address, &client_request, 0);

zassert_true(ret >= 0, "Sending request failed, %d", ret);
k_sleep(K_MSEC(1000));
Expand Down Expand Up @@ -307,15 +354,12 @@ ZTEST(coap_client, test_multiple_requests)
zassert_true(ret >= 0, "Sending request failed, %d", ret);

ret = coap_client_req(&client, 0, &address, &client_request, -1);
zassert_equal(ret, -EAGAIN, "Shouldn't be able to send 2 requests at same time");
zassert_true(ret >= 0, "Sending request failed, %d", ret);

k_sleep(K_MSEC(5));
k_sleep(K_MSEC(1000));
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");

ret = coap_client_req(&client, 0, &address, &client_request, -1);
zassert_true(ret >= 0, "Sending request failed, %d", ret);

k_sleep(K_MSEC(5));
k_sleep(K_MSEC(1000));
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
Expand Down

0 comments on commit a3612c7

Please sign in to comment.