Skip to content

Commit

Permalink
tests: net: coap: add test for age wrap around
Browse files Browse the repository at this point in the history
- Added a test to the CoAP testsuite for age wrap around
- Added test for `coap_age_is_newer`

Signed-off-by: Sibert Declercq <[email protected]>
  • Loading branch information
sibertdeclercq authored and henrikbrixandersen committed Mar 5, 2024
1 parent 808f79d commit 8fc88a0
Showing 1 changed file with 85 additions and 7 deletions.
92 changes: 85 additions & 7 deletions tests/net/lib/coap/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,25 @@ bool _coap_match_path_uri(const char * const *path,
const char *uri, uint16_t len);

/* Some forward declarations */
static void server_notify_callback(struct coap_resource *resource,
struct coap_observer *observer);
static void server_resource_1_callback(struct coap_resource *resource,
struct coap_observer *observer);

static void server_resource_2_callback(struct coap_resource *resource,
struct coap_observer *observer);

static int server_resource_1_get(struct coap_resource *resource,
struct coap_packet *request,
struct sockaddr *addr, socklen_t addr_len);

static const char * const server_resource_1_path[] = { "s", "1", NULL };
static struct coap_resource server_resources[] = {
static const char *const server_resource_2_path[] = { "s", "2", NULL };
static struct coap_resource server_resources[] = {
{ .path = server_resource_1_path,
.get = server_resource_1_get,
.notify = server_notify_callback },
.notify = server_resource_1_callback },
{ .path = server_resource_2_path,
.get = server_resource_1_get, /* Get can be shared with the first resource */
.notify = server_resource_2_callback },
{ },
};

Expand All @@ -63,6 +70,12 @@ static struct sockaddr_in6 dummy_addr = {

static uint8_t data_buf[2][COAP_BUF_SIZE];

#define COAP_ROLLOVER_AGE (1 << 23)
#define COAP_MAX_AGE 0xffffff
#define COAP_FIRST_AGE 2

extern bool coap_age_is_newer(int v1, int v2);

ZTEST(coap, test_build_empty_pdu)
{
uint8_t result_pdu[] = { 0x40, 0x01, 0x0, 0x0 };
Expand Down Expand Up @@ -822,8 +835,8 @@ static bool ipaddr_cmp(const struct sockaddr *a, const struct sockaddr *b)
return false;
}

static void server_notify_callback(struct coap_resource *resource,
struct coap_observer *observer)
static void server_resource_1_callback(struct coap_resource *resource,
struct coap_observer *observer)
{
bool r;

Expand All @@ -832,6 +845,14 @@ static void server_notify_callback(struct coap_resource *resource,

coap_remove_observer(resource, observer);
}
static void server_resource_2_callback(struct coap_resource *resource,
struct coap_observer *observer)
{
bool r;

r = ipaddr_cmp(&observer->addr, (const struct sockaddr *)&dummy_addr);
zassert_true(r, "The address of the observer doesn't match");
}

static int server_resource_1_get(struct coap_resource *resource,
struct coap_packet *request,
Expand Down Expand Up @@ -891,7 +912,7 @@ ZTEST(coap, test_observer_server)
0x45, 0x01, 0x12, 0x34,
't', 'o', 'k', 'e', 'n',
0x60, /* enable observe option */
0x51, 's', 0x01, '2', /* path */
0x51, 's', 0x01, '3', /* path */
};
struct coap_packet req;
struct coap_option options[4] = {};
Expand Down Expand Up @@ -1771,4 +1792,61 @@ ZTEST(coap, test_transmission_parameters)
zassert_equal(pending->params.max_retransmission, 2, "Wrong max retransmission value");
}

ZTEST(coap, test_notify_age)
{
uint8_t valid_request_pdu[] = {
0x45, 0x01, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x60, /* enable observe option */
0x51, 's', 0x01, '2', /* path */
};

struct coap_packet req;
struct coap_option options[4] = {};
uint8_t *data = data_buf[0];
uint8_t opt_num = ARRAY_SIZE(options) - 1;
struct coap_resource *resource = &server_resources[1];
int r;
struct coap_observer *observer;
int last_age;

memcpy(data, valid_request_pdu, sizeof(valid_request_pdu));

r = coap_packet_parse(&req, data, sizeof(valid_request_pdu), options, opt_num);
zassert_equal(r, 0, "Could not initialize packet");

r = coap_handle_request(&req, server_resources, options, opt_num,
(struct sockaddr *)&dummy_addr, sizeof(dummy_addr));
zassert_equal(r, 0, "Could not handle packet");

/* Forward time a bit, as not to run this 8 million time */
resource->age = COAP_OBSERVE_MAX_AGE - 10;

last_age = resource->age;

for (int i = 0; i < 15; i++) {
r = coap_resource_notify(resource);
zassert_true(coap_age_is_newer(last_age, resource->age),
"Resource age expected to be newer");
last_age = resource->age;
}

observer =
CONTAINER_OF(sys_slist_peek_head(&resource->observers), struct coap_observer, list);
coap_remove_observer(resource, observer);
}

ZTEST(coap, test_age_is_newer)
{
for (int i = COAP_FIRST_AGE; i < COAP_MAX_AGE; ++i) {
zassert_true(coap_age_is_newer(i, i + 1),
"Resource age expected to be marked as newer");
}

zassert_true(coap_age_is_newer(COAP_MAX_AGE, COAP_FIRST_AGE),
"First age should be marked as newer");
zassert_true(coap_age_is_newer(COAP_FIRST_AGE, COAP_ROLLOVER_AGE),
"Rollover age should be marked as newer");
zassert_true(coap_age_is_newer(COAP_ROLLOVER_AGE, COAP_MAX_AGE),
"Max age should be marked as newer");
}

ZTEST_SUITE(coap, NULL, NULL, NULL, NULL, NULL);

0 comments on commit 8fc88a0

Please sign in to comment.