Skip to content

Commit aa25e60

Browse files
committed
[nrf fromtree] tests: coap_client: Add test when separate response is lost
Test a scenario where Ack is received but the actual response is not coming. Signed-off-by: Seppo Takalo <[email protected]> (cherry picked from commit 120aabb)
1 parent 0168b1d commit aa25e60

File tree

1 file changed

+46
-1
lines changed
  • tests/net/lib/coap_client/src

1 file changed

+46
-1
lines changed

tests/net/lib/coap_client/src/main.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ DEFINE_FFF_GLOBALS;
2121
#define MORE_THAN_LONG_EXCHANGE_LIFETIME_MS 4 * LONG_ACK_TIMEOUT_MS
2222
#define MORE_THAN_ACK_TIMEOUT_MS \
2323
(CONFIG_COAP_INIT_ACK_TIMEOUT_MS + CONFIG_COAP_INIT_ACK_TIMEOUT_MS / 2)
24-
24+
#define COAP_SEPARATE_TIMEOUT (6000 * 3) /* Needs a safety marging, tests run faster than -rt */
2525
#define VALID_MESSAGE_ID BIT(31)
2626
#define TOKEN_OFFSET 4
2727

@@ -33,6 +33,9 @@ static uint8_t last_token[2][COAP_TOKEN_MAX_LEN];
3333
static const uint8_t empty_token[COAP_TOKEN_MAX_LEN] = {0};
3434

3535
static struct coap_client client;
36+
static struct coap_client client2 = {
37+
.fd = 1,
38+
};
3639

3740
static char *short_payload = "testing";
3841
static char *long_payload = LOREM_IPSUM_SHORT;
@@ -260,6 +263,18 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_empty_ack(int sock, void *buf,
260263
return sizeof(ack_data);
261264
}
262265

266+
static ssize_t z_impl_zsock_recvfrom_custom_fake_only_ack(int sock, void *buf, size_t max_len,
267+
int flags, struct sockaddr *src_addr,
268+
socklen_t *addrlen)
269+
{
270+
int ret;
271+
272+
ret = z_impl_zsock_recvfrom_custom_fake_empty_ack(sock, buf, max_len, flags, src_addr,
273+
addrlen);
274+
clear_socket_events(sock, ZSOCK_POLLIN);
275+
return ret;
276+
}
277+
263278
static ssize_t z_impl_zsock_recvfrom_custom_fake_unmatching(int sock, void *buf, size_t max_len,
264279
int flags, struct sockaddr *src_addr,
265280
socklen_t *addrlen)
@@ -659,6 +674,36 @@ ZTEST(coap_client, test_separate_response)
659674
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
660675
}
661676

677+
ZTEST(coap_client, test_separate_response_lost)
678+
{
679+
int ret = 0;
680+
struct k_sem sem;
681+
struct sockaddr address = {0};
682+
struct coap_client_request client_request = {
683+
.method = COAP_METHOD_GET,
684+
.confirmable = true,
685+
.path = test_path,
686+
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
687+
.cb = coap_callback,
688+
.payload = short_payload,
689+
.len = strlen(short_payload),
690+
.user_data = &sem,
691+
};
692+
693+
zassert_ok(k_sem_init(&sem, 0, 1));
694+
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_only_ack;
695+
set_socket_events(client.fd, ZSOCK_POLLOUT);
696+
697+
k_sleep(K_MSEC(1));
698+
699+
LOG_INF("Send request");
700+
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
701+
zassert_true(ret >= 0, "Sending request failed, %d", ret);
702+
703+
zassert_ok(k_sem_take(&sem, K_MSEC(COAP_SEPARATE_TIMEOUT)));
704+
zassert_equal(last_response_code, -ETIMEDOUT, "");
705+
}
706+
662707
ZTEST(coap_client, test_multiple_requests)
663708
{
664709
int ret = 0;

0 commit comments

Comments
 (0)