tests: coap_client: Refactor tests
Refactor tests to be a bit shorter, so its easier to read and copy-paste for a new testcase All idioms like "ret = somecall(); zasser.." are replaced with just "zassert_ok(some_call());" Commonly used structures are global and initialized once. To avoid cross-test side-effects, suite_after-function is added to cleanup all requests. Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
This commit is contained in:
parent
34a6d5a5dc
commit
d64748cc52
1 changed files with 119 additions and 475 deletions
|
@ -27,20 +27,48 @@ DEFINE_FFF_GLOBALS;
|
|||
#define VALID_MESSAGE_ID BIT(31)
|
||||
#define TOKEN_OFFSET 4
|
||||
|
||||
void coap_callback(int16_t code, size_t offset, const uint8_t *payload, size_t len, bool last_block,
|
||||
void *user_data);
|
||||
|
||||
static int16_t last_response_code;
|
||||
static const char *test_path = "test";
|
||||
static const char test_path[] = "test";
|
||||
|
||||
static uint32_t messages_needing_response[2];
|
||||
static uint8_t last_token[2][COAP_TOKEN_MAX_LEN];
|
||||
static const uint8_t empty_token[COAP_TOKEN_MAX_LEN] = {0};
|
||||
K_SEM_DEFINE(sem1, 0, 1);
|
||||
K_SEM_DEFINE(sem2, 0, 1);
|
||||
|
||||
static struct coap_client client;
|
||||
static struct coap_client client2 = {
|
||||
.fd = 1,
|
||||
};
|
||||
|
||||
static char *short_payload = "testing";
|
||||
static char *long_payload = LOREM_IPSUM_SHORT;
|
||||
static const char short_payload[] = "testing";
|
||||
static const char long_payload[] = LOREM_IPSUM_SHORT;
|
||||
static struct coap_client_request short_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = sizeof(short_payload) - 1,
|
||||
.user_data = &sem1,
|
||||
};
|
||||
static struct coap_client_request long_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = long_payload,
|
||||
.len = sizeof(long_payload) - 1,
|
||||
.user_data = &sem2,
|
||||
};
|
||||
static struct sockaddr dst_address;
|
||||
|
||||
|
||||
|
||||
static uint16_t get_next_pending_message_id(void)
|
||||
{
|
||||
|
@ -423,6 +451,16 @@ static ssize_t z_impl_zsock_recvfrom_custom_fake_observe(int sock, void *buf, si
|
|||
return ret;
|
||||
}
|
||||
|
||||
void coap_callback(int16_t code, size_t offset, const uint8_t *payload, size_t len, bool last_block,
|
||||
void *user_data)
|
||||
{
|
||||
LOG_INF("CoAP response callback, %d", code);
|
||||
last_response_code = code;
|
||||
if (user_data) {
|
||||
k_sem_give((struct k_sem *) user_data);
|
||||
}
|
||||
}
|
||||
|
||||
extern void net_coap_init(void);
|
||||
|
||||
static void *suite_setup(void)
|
||||
|
@ -463,90 +501,38 @@ static void test_setup(void *data)
|
|||
|
||||
memset(&client.requests, 0, sizeof(client.requests));
|
||||
memset(last_token, 0, sizeof(last_token));
|
||||
|
||||
last_response_code = 0;
|
||||
k_sem_reset(&sem1);
|
||||
k_sem_reset(&sem2);
|
||||
|
||||
k_mutex_unlock(&client.lock);
|
||||
}
|
||||
|
||||
void coap_callback(int16_t code, size_t offset, const uint8_t *payload, size_t len, bool last_block,
|
||||
void *user_data)
|
||||
static void test_after(void *data)
|
||||
{
|
||||
LOG_INF("CoAP response callback, %d", code);
|
||||
last_response_code = code;
|
||||
if (user_data) {
|
||||
k_sem_give((struct k_sem *) user_data);
|
||||
}
|
||||
coap_client_cancel_requests(&client);
|
||||
coap_client_cancel_requests(&client2);
|
||||
}
|
||||
|
||||
ZTEST_SUITE(coap_client, NULL, suite_setup, test_setup, NULL, NULL);
|
||||
ZTEST_SUITE(coap_client, NULL, suite_setup, test_setup, test_after, NULL);
|
||||
|
||||
ZTEST(coap_client, test_get_request)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
client_request.payload = short_payload;
|
||||
client_request.len = strlen(short_payload);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, NULL));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
LOG_INF("Test done");
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_request_block)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
};
|
||||
|
||||
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_block;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_equal(ret, -EAGAIN, "");
|
||||
zassert_equal(coap_client_req(&client, 0, &dst_address, &short_request, NULL), -EAGAIN, "");
|
||||
}
|
||||
|
||||
|
||||
ZTEST(coap_client, test_resend_request)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
};
|
||||
|
||||
int (*sendto_fakes[])(int, void *, size_t, int, const struct sockaddr *, socklen_t) = {
|
||||
z_impl_zsock_sendto_custom_fake_no_reply,
|
||||
z_impl_zsock_sendto_custom_fake_block,
|
||||
|
@ -556,86 +542,43 @@ ZTEST(coap_client, test_resend_request)
|
|||
SET_CUSTOM_FAKE_SEQ(z_impl_zsock_sendto, sendto_fakes, ARRAY_SIZE(sendto_fakes));
|
||||
set_socket_events(client.fd, ZSOCK_POLLOUT);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, NULL));
|
||||
k_sleep(K_MSEC(MORE_THAN_ACK_TIMEOUT_MS));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
zassert_equal(z_impl_zsock_sendto_fake.call_count, 3);
|
||||
LOG_INF("Test done");
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_echo_option)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
client_request.payload = short_payload;
|
||||
client_request.len = strlen(short_payload);
|
||||
|
||||
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_echo;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, NULL));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
LOG_INF("Test done");
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_echo_option_next_req)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
client_request.payload = short_payload;
|
||||
client_request.len = strlen(short_payload);
|
||||
struct coap_client_request req = short_request;
|
||||
|
||||
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_echo_next_req;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, NULL));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
|
||||
char *payload = "echo testing";
|
||||
|
||||
client_request.method = COAP_METHOD_POST;
|
||||
client_request.payload = payload;
|
||||
client_request.len = strlen(payload);
|
||||
req.method = COAP_METHOD_POST;
|
||||
req.payload = payload;
|
||||
req.len = strlen(payload);
|
||||
|
||||
LOG_INF("Send next request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req, NULL));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
|
@ -643,51 +586,15 @@ ZTEST(coap_client, test_echo_option_next_req)
|
|||
|
||||
ZTEST(coap_client, test_get_no_path)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = NULL,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
struct coap_client_request req = short_request;
|
||||
|
||||
client_request.payload = short_payload;
|
||||
client_request.len = strlen(short_payload);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
|
||||
zassert_equal(ret, -EINVAL, "Get request without path");
|
||||
req.path = NULL;
|
||||
zassert_equal(coap_client_req(&client, 0, &dst_address, &req, NULL), -EINVAL, "");
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_send_large_data)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
client_request.payload = long_payload;
|
||||
client_request.len = strlen(long_payload);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &long_request, NULL));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
|
@ -695,35 +602,16 @@ ZTEST(coap_client, test_send_large_data)
|
|||
|
||||
ZTEST(coap_client, test_no_response)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
struct coap_transmission_parameters params = {
|
||||
.ack_timeout = LONG_ACK_TIMEOUT_MS,
|
||||
.coap_backoff_percent = 200,
|
||||
.max_retransmission = 0
|
||||
};
|
||||
|
||||
client_request.payload = short_payload;
|
||||
client_request.len = strlen(short_payload);
|
||||
|
||||
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_no_reply;
|
||||
set_socket_events(client.fd, ZSOCK_POLLOUT);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, ¶ms);
|
||||
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, ¶ms));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_LONG_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, -ETIMEDOUT, "Unexpected response");
|
||||
|
@ -731,28 +619,9 @@ ZTEST(coap_client, test_no_response)
|
|||
|
||||
ZTEST(coap_client, test_separate_response)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
client_request.payload = short_payload;
|
||||
client_request.len = strlen(short_payload);
|
||||
|
||||
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_empty_ack;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, NULL));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
|
@ -760,50 +629,24 @@ ZTEST(coap_client, test_separate_response)
|
|||
|
||||
ZTEST(coap_client, test_separate_response_lost)
|
||||
{
|
||||
int ret = 0;
|
||||
struct k_sem sem;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem,
|
||||
};
|
||||
struct coap_client_request req = short_request;
|
||||
|
||||
req.user_data = &sem1;
|
||||
|
||||
zassert_ok(k_sem_init(&sem, 0, 1));
|
||||
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_only_ack;
|
||||
set_socket_events(client.fd, ZSOCK_POLLOUT);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req, NULL));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
|
||||
zassert_ok(k_sem_take(&sem, K_MSEC(COAP_SEPARATE_TIMEOUT)));
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(COAP_SEPARATE_TIMEOUT)));
|
||||
zassert_equal(last_response_code, -ETIMEDOUT, "");
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_separate_response_ack_fail)
|
||||
{
|
||||
int ret = 0;
|
||||
struct k_sem sem;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem,
|
||||
};
|
||||
zassert_ok(k_sem_init(&sem, 0, 1));
|
||||
struct coap_client_request req = short_request;
|
||||
|
||||
req.user_data = &sem1;
|
||||
|
||||
int (*sendto_fakes[])(int, void *, size_t, int, const struct sockaddr *, socklen_t) = {
|
||||
z_impl_zsock_sendto_custom_fake,
|
||||
|
@ -813,95 +656,47 @@ ZTEST(coap_client, test_separate_response_ack_fail)
|
|||
SET_CUSTOM_FAKE_SEQ(z_impl_zsock_sendto, sendto_fakes, ARRAY_SIZE(sendto_fakes));
|
||||
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_empty_ack;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req, NULL));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
|
||||
zassert_ok(k_sem_take(&sem, K_MSEC(COAP_SEPARATE_TIMEOUT)));
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(COAP_SEPARATE_TIMEOUT)));
|
||||
zassert_equal(last_response_code, -ENETDOWN, "");
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_multiple_requests)
|
||||
{
|
||||
int ret = 0;
|
||||
int retry = MORE_THAN_EXCHANGE_LIFETIME_MS;
|
||||
struct k_sem sem1, sem2;
|
||||
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request req1 = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem1
|
||||
};
|
||||
struct coap_client_request req2 = req1;
|
||||
struct coap_client_request req1 = short_request;
|
||||
struct coap_client_request req2 = short_request;
|
||||
|
||||
req1.user_data = &sem1;
|
||||
req2.user_data = &sem2;
|
||||
|
||||
k_sem_init(&sem1, 0, 1);
|
||||
k_sem_init(&sem2, 0, 1);
|
||||
|
||||
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_no_reply;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &req1, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
|
||||
ret = coap_client_req(&client, 0, &address, &req2, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req1, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req2, NULL));
|
||||
|
||||
set_socket_events(client.fd, ZSOCK_POLLIN);
|
||||
while (last_response_code == 0 && retry > 0) {
|
||||
retry--;
|
||||
k_sleep(K_MSEC(1));
|
||||
}
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
|
||||
last_response_code = 0;
|
||||
set_socket_events(client.fd, ZSOCK_POLLIN);
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem2, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_unmatching_tokens)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
struct coap_transmission_parameters params = {
|
||||
.ack_timeout = LONG_ACK_TIMEOUT_MS,
|
||||
.coap_backoff_percent = 200,
|
||||
.max_retransmission = 0
|
||||
};
|
||||
|
||||
client_request.payload = short_payload;
|
||||
client_request.len = strlen(short_payload);
|
||||
|
||||
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_unmatching;
|
||||
set_socket_events(client.fd, ZSOCK_POLLIN | ZSOCK_POLLOUT);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, ¶ms);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, ¶ms));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_LONG_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, -ETIMEDOUT, "Unexpected response");
|
||||
|
@ -909,45 +704,14 @@ ZTEST(coap_client, test_unmatching_tokens)
|
|||
|
||||
ZTEST(coap_client, test_multiple_clients)
|
||||
{
|
||||
int ret;
|
||||
int retry = MORE_THAN_EXCHANGE_LIFETIME_MS;
|
||||
struct k_sem sem1, sem2;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request req1 = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem1
|
||||
};
|
||||
struct coap_client_request req2 = req1;
|
||||
struct coap_client_request req1 = short_request;
|
||||
struct coap_client_request req2 = long_request;
|
||||
|
||||
req1.user_data = &sem1;
|
||||
req2.user_data = &sem2;
|
||||
req2.payload = long_payload;
|
||||
req2.len = strlen(long_payload);
|
||||
|
||||
zassert_ok(k_sem_init(&sem1, 0, 1));
|
||||
zassert_ok(k_sem_init(&sem2, 0, 1));
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Sending requests");
|
||||
ret = coap_client_req(&client, client.fd, &address, &req1, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
|
||||
ret = coap_client_req(&client2, client2.fd, &address, &req2, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
|
||||
while (last_response_code == 0 && retry > 0) {
|
||||
retry--;
|
||||
k_sleep(K_MSEC(1));
|
||||
}
|
||||
set_socket_events(client2.fd, ZSOCK_POLLIN);
|
||||
|
||||
k_sleep(K_SECONDS(1));
|
||||
zassert_ok(coap_client_req(&client, client.fd, &dst_address, &req1, NULL));
|
||||
zassert_ok(coap_client_req(&client2, client2.fd, &dst_address, &req2, NULL));
|
||||
|
||||
/* ensure we got both responses */
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
|
@ -958,26 +722,10 @@ ZTEST(coap_client, test_multiple_clients)
|
|||
|
||||
ZTEST(coap_client, test_poll_err)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
};
|
||||
|
||||
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_no_reply;
|
||||
set_socket_events(client.fd, ZSOCK_POLLERR);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, NULL));
|
||||
|
||||
k_sleep(K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS));
|
||||
zassert_equal(last_response_code, -EIO, "Unexpected response");
|
||||
|
@ -985,29 +733,10 @@ ZTEST(coap_client, test_poll_err)
|
|||
|
||||
ZTEST(coap_client, test_poll_err_after_response)
|
||||
{
|
||||
int ret = 0;
|
||||
struct k_sem sem1;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem1
|
||||
};
|
||||
|
||||
zassert_ok(k_sem_init(&sem1, 0, 1));
|
||||
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_no_reply;
|
||||
set_socket_events(client.fd, ZSOCK_POLLIN);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, NULL));
|
||||
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
|
@ -1018,35 +747,17 @@ ZTEST(coap_client, test_poll_err_after_response)
|
|||
|
||||
ZTEST(coap_client, test_poll_err_on_another_sock)
|
||||
{
|
||||
int ret = 0;
|
||||
struct k_sem sem1, sem2;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem1
|
||||
};
|
||||
struct coap_client_request request2 = client_request;
|
||||
struct coap_client_request req1 = short_request;
|
||||
struct coap_client_request req2 = short_request;
|
||||
|
||||
request2.user_data = &sem2;
|
||||
|
||||
zassert_ok(k_sem_init(&sem1, 0, 1));
|
||||
zassert_ok(k_sem_init(&sem2, 0, 1));
|
||||
req1.user_data = &sem1;
|
||||
req2.user_data = &sem2;
|
||||
|
||||
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_no_reply;
|
||||
set_socket_events(client.fd, ZSOCK_POLLERR);
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
ret = coap_client_req(&client2, client2.fd, &address, &request2, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
ret = coap_client_req(&client, client.fd, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client2, client2.fd, &dst_address, &req2, NULL));
|
||||
zassert_ok(coap_client_req(&client, client.fd, &dst_address, &req1, NULL));
|
||||
|
||||
set_socket_events(client2.fd, ZSOCK_POLLIN);
|
||||
|
||||
|
@ -1058,45 +769,25 @@ ZTEST(coap_client, test_poll_err_on_another_sock)
|
|||
|
||||
ZTEST(coap_client, test_duplicate_response)
|
||||
{
|
||||
int ret = 0;
|
||||
struct k_sem sem;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem,
|
||||
};
|
||||
|
||||
zassert_ok(k_sem_init(&sem, 0, 2));
|
||||
z_impl_zsock_recvfrom_fake.custom_fake =
|
||||
z_impl_zsock_recvfrom_custom_fake_duplicate_response;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
LOG_INF("Send request");
|
||||
ret = coap_client_req(&client, 0, &address, &client_request, NULL);
|
||||
zassert_true(ret >= 0, "Sending request failed, %d", ret);
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, NULL));
|
||||
|
||||
zassert_ok(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_equal(last_response_code, COAP_RESPONSE_CODE_OK, "Unexpected response");
|
||||
|
||||
zassert_equal(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)), -EAGAIN, "");
|
||||
zassert_equal(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)), -EAGAIN, "");
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_observe)
|
||||
{
|
||||
struct k_sem sem;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_option options = {
|
||||
.code = COAP_OPTION_OBSERVE,
|
||||
.value[0] = 0,
|
||||
.len = 1,
|
||||
};
|
||||
struct coap_client_request client_request = {
|
||||
struct coap_client_request req = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
|
@ -1106,79 +797,47 @@ ZTEST(coap_client, test_observe)
|
|||
.len = strlen(short_payload),
|
||||
.options = &options,
|
||||
.num_options = 1,
|
||||
.user_data = &sem,
|
||||
.user_data = &sem1,
|
||||
};
|
||||
|
||||
zassert_ok(k_sem_init(&sem, 0, 1));
|
||||
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_observe;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
zassert_ok(coap_client_req(&client, 0, &address, &client_request, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req, NULL));
|
||||
|
||||
zassert_ok(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
|
||||
coap_client_cancel_requests(&client);
|
||||
zassert_ok(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_equal(last_response_code, -ECANCELED, "");
|
||||
|
||||
zassert_not_ok(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_not_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_request_rst)
|
||||
{
|
||||
struct k_sem sem;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request client_request = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem,
|
||||
};
|
||||
|
||||
zassert_ok(k_sem_init(&sem, 0, 1));
|
||||
k_sleep(K_MSEC(1));
|
||||
z_impl_zsock_recvfrom_fake.custom_fake = z_impl_zsock_recvfrom_custom_fake_rst;
|
||||
|
||||
zassert_ok(coap_client_req(&client, 0, &address, &client_request, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &short_request, NULL));
|
||||
|
||||
zassert_ok(k_sem_take(&sem, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_equal(last_response_code, -ECONNRESET, "");
|
||||
}
|
||||
|
||||
ZTEST(coap_client, test_cancel)
|
||||
{
|
||||
struct k_sem sem1, sem2;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request req1 = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem1
|
||||
};
|
||||
struct coap_client_request req2 = req1;
|
||||
struct coap_client_request req1 = short_request;
|
||||
struct coap_client_request req2 = short_request;
|
||||
|
||||
req1.user_data = &sem1;
|
||||
req2.user_data = &sem2;
|
||||
|
||||
k_sem_init(&sem1, 0, 1);
|
||||
k_sem_init(&sem2, 0, 1);
|
||||
|
||||
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_no_reply;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
zassert_ok(coap_client_req(&client, 0, &address, &req1, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &address, &req2, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req1, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req2, NULL));
|
||||
|
||||
k_sleep(K_SECONDS(1));
|
||||
|
||||
|
@ -1196,32 +855,17 @@ ZTEST(coap_client, test_cancel)
|
|||
|
||||
ZTEST(coap_client, test_cancel_match)
|
||||
{
|
||||
struct k_sem sem1, sem2;
|
||||
struct sockaddr address = {0};
|
||||
struct coap_client_request req1 = {
|
||||
.method = COAP_METHOD_GET,
|
||||
.confirmable = true,
|
||||
.path = test_path,
|
||||
.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN,
|
||||
.cb = coap_callback,
|
||||
.payload = short_payload,
|
||||
.len = strlen(short_payload),
|
||||
.user_data = &sem1
|
||||
};
|
||||
struct coap_client_request req2 = req1;
|
||||
struct coap_client_request req1 = short_request;
|
||||
struct coap_client_request req2 = short_request;
|
||||
|
||||
req1.user_data = &sem1;
|
||||
req2.user_data = &sem2;
|
||||
req2.path = "another";
|
||||
|
||||
k_sem_init(&sem1, 0, 1);
|
||||
k_sem_init(&sem2, 0, 1);
|
||||
|
||||
z_impl_zsock_sendto_fake.custom_fake = z_impl_zsock_sendto_custom_fake_no_reply;
|
||||
|
||||
k_sleep(K_MSEC(1));
|
||||
|
||||
zassert_ok(coap_client_req(&client, 0, &address, &req1, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &address, &req2, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req1, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req2, NULL));
|
||||
|
||||
k_sleep(K_SECONDS(1));
|
||||
|
||||
|
@ -1233,7 +877,7 @@ ZTEST(coap_client, test_cancel_match)
|
|||
zassert_not_ok(k_sem_take(&sem2, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_equal(last_response_code, -ECANCELED, "");
|
||||
|
||||
zassert_ok(coap_client_req(&client, 0, &address, &req1, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req1, NULL));
|
||||
|
||||
/* should not match */
|
||||
coap_client_cancel_request(&client, &(struct coap_client_request) {
|
||||
|
@ -1250,8 +894,8 @@ ZTEST(coap_client, test_cancel_match)
|
|||
zassert_ok(k_sem_take(&sem1, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
zassert_ok(k_sem_take(&sem2, K_MSEC(MORE_THAN_EXCHANGE_LIFETIME_MS)));
|
||||
|
||||
zassert_ok(coap_client_req(&client, 0, &address, &req1, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &address, &req2, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req1, NULL));
|
||||
zassert_ok(coap_client_req(&client, 0, &dst_address, &req2, NULL));
|
||||
|
||||
/* match both (wildcard)*/
|
||||
coap_client_cancel_request(&client, &(struct coap_client_request) {0});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue