Skip to content

Commit e951add

Browse files
committed
[nrf fromtree] tests: coap_client: Add testcase for receiving RST
When server responds with CoAP RESET, we should inform the client and stop the request. Signed-off-by: Seppo Takalo <[email protected]> (cherry picked from commit 107dc9b)
1 parent 3b7fa86 commit e951add

File tree

1 file changed

+44
-0
lines changed
  • tests/net/lib/coap_client/src

1 file changed

+44
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,25 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_empty_ack(int sock, void *buf,
279279
return sizeof(ack_data);
280280
}
281281

282+
static ssize_t z_impl_zsock_recvfrom_custom_fake_rst(int sock, void *buf, size_t max_len, int flags,
283+
struct sockaddr *src_addr, socklen_t *addrlen)
284+
{
285+
uint16_t last_message_id = 0;
286+
287+
static uint8_t rst_data[] = {0x70, 0x00, 0x00, 0x00, 0x00, 0x00,
288+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
289+
290+
last_message_id = get_next_pending_message_id();
291+
292+
rst_data[2] = (uint8_t)(last_message_id >> 8);
293+
rst_data[3] = (uint8_t)last_message_id;
294+
295+
memcpy(buf, rst_data, sizeof(rst_data));
296+
clear_socket_events(sock, ZSOCK_POLLIN);
297+
298+
return sizeof(rst_data);
299+
}
300+
282301
static ssize_t z_impl_zsock_recvfrom_custom_fake_only_ack(int sock, void *buf, size_t max_len,
283302
int flags, struct sockaddr *src_addr,
284303
socklen_t *addrlen)
@@ -1098,3 +1117,28 @@ ZTEST(coap_client, test_observe)
10981117

10991118
zassert_not_ok(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
11001119
}
1120+
1121+
ZTEST(coap_client, test_request_rst)
1122+
{
1123+
struct k_sem sem;
1124+
struct sockaddr address = {0};
1125+
struct coap_client_request client_request = {
1126+
.method = COAP_METHOD_GET,
1127+
.confirmable = true,
1128+
.path = test_path,
1129+
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
1130+
.cb = coap_callback,
1131+
.payload = short_payload,
1132+
.len = strlen(short_payload),
1133+
.user_data = &sem,
1134+
};
1135+
1136+
zassert_ok(k_sem_init(&sem, 0, 1));
1137+
k_sleep(K_MSEC(1));
1138+
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_rst;
1139+
1140+
zassert_ok(coap_client_req(&client, 0, &address, &client_request, NULL));
1141+
1142+
zassert_ok(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
1143+
zassert_equal(last_response_code, -ECONNRESET, "");
1144+
}

0 commit comments

Comments
 (0)