2018-01-12 11:41:37 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Ken Bannister. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
2018-05-04 19:02:02 +02:00
|
|
|
#include <stdbool.h>
|
2018-10-01 07:10:28 +02:00
|
|
|
#include <string.h>
|
2019-01-11 11:27:50 +01:00
|
|
|
#include <stdio.h>
|
2018-01-12 11:41:37 +01:00
|
|
|
|
|
|
|
#include "embUnit.h"
|
|
|
|
|
|
|
|
#include "net/nanocoap.h"
|
|
|
|
|
|
|
|
#include "unittests-constants.h"
|
|
|
|
#include "tests-nanocoap.h"
|
|
|
|
|
2018-10-01 07:10:28 +02:00
|
|
|
|
|
|
|
#define _BUF_SIZE (128U)
|
|
|
|
|
2018-01-12 11:41:37 +01:00
|
|
|
/*
|
2018-03-16 10:02:39 +01:00
|
|
|
* Validates encoded message ID byte order and put/get URI option.
|
2018-01-12 11:41:37 +01:00
|
|
|
*/
|
2018-03-16 10:02:39 +01:00
|
|
|
static void test_nanocoap__hdr(void)
|
2018-01-12 11:41:37 +01:00
|
|
|
{
|
2018-10-01 07:10:28 +02:00
|
|
|
uint8_t buf[_BUF_SIZE];
|
2018-01-12 11:41:37 +01:00
|
|
|
uint16_t msgid = 0xABCD;
|
2018-03-16 10:02:39 +01:00
|
|
|
char path[] = "/test/abcd/efgh";
|
2018-08-23 11:48:51 +02:00
|
|
|
char loc_path[] = "/foo/bar";
|
2018-03-16 10:02:39 +01:00
|
|
|
unsigned char path_tmp[64] = {0};
|
2018-01-12 11:41:37 +01:00
|
|
|
|
|
|
|
uint8_t *pktpos = &buf[0];
|
2018-09-26 12:33:22 +02:00
|
|
|
pktpos += coap_build_hdr((coap_hdr_t *)pktpos, COAP_TYPE_CON, NULL, 0,
|
2018-08-23 11:48:51 +02:00
|
|
|
COAP_METHOD_GET, msgid);
|
|
|
|
pktpos += coap_opt_put_location_path(pktpos, 0, loc_path);
|
|
|
|
pktpos += coap_opt_put_uri_path(pktpos, COAP_OPT_LOCATION_PATH, path);
|
2018-01-12 11:41:37 +01:00
|
|
|
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
coap_parse(&pkt, &buf[0], pktpos - &buf[0]);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(msgid, coap_get_id(&pkt));
|
2018-03-16 10:02:39 +01:00
|
|
|
|
2018-08-23 11:48:51 +02:00
|
|
|
int res = coap_get_uri_path(&pkt, path_tmp);
|
2018-03-16 10:02:39 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT(sizeof(path), res);
|
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)path, (char *)path_tmp);
|
2018-08-23 11:48:51 +02:00
|
|
|
|
|
|
|
res = coap_get_location_path(&pkt, path_tmp, 64);
|
|
|
|
TEST_ASSERT_EQUAL_INT(sizeof(loc_path), res);
|
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)loc_path, (char *)path_tmp);
|
2018-01-12 11:41:37 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 19:02:02 +02:00
|
|
|
/*
|
|
|
|
* Client GET request with simple path. Test request generation.
|
|
|
|
* Request /time resource from libcoap example
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__get_req(void)
|
|
|
|
{
|
2018-10-01 07:10:28 +02:00
|
|
|
uint8_t buf[_BUF_SIZE];
|
2018-05-04 19:02:02 +02:00
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char path[] = "/time";
|
|
|
|
size_t total_hdr_len = 6;
|
|
|
|
size_t total_opt_len = 5;
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_GET, msgid);
|
|
|
|
TEST_ASSERT_EQUAL_INT(total_hdr_len, len);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(total_hdr_len, coap_get_total_hdr_len(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(122, pkt.payload_len);
|
|
|
|
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
|
|
|
|
TEST_ASSERT_EQUAL_INT(total_opt_len, len);
|
|
|
|
|
|
|
|
char uri[10] = {0};
|
2018-08-23 11:48:51 +02:00
|
|
|
coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
|
2018-05-04 19:02:02 +02:00
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri);
|
|
|
|
|
|
|
|
len = coap_opt_finish(&pkt, COAP_OPT_FINISH_NONE);
|
|
|
|
TEST_ASSERT_EQUAL_INT(total_hdr_len + total_opt_len, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builds on get_req test, to test payload and Content-Format option.
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__put_req(void)
|
|
|
|
{
|
2018-10-01 07:10:28 +02:00
|
|
|
uint8_t buf[_BUF_SIZE];
|
2018-05-04 19:02:02 +02:00
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char path[] = "/value";
|
|
|
|
size_t total_hdr_len = 6;
|
|
|
|
size_t uri_opt_len = 6;
|
|
|
|
size_t fmt_opt_len = 1;
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_PUT, msgid);
|
|
|
|
TEST_ASSERT_EQUAL_INT(total_hdr_len, len);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_METHOD_PUT, coap_get_code(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(122, pkt.payload_len);
|
|
|
|
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
|
|
|
|
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
|
|
|
|
|
|
|
|
len = coap_opt_add_uint(&pkt, COAP_OPT_CONTENT_FORMAT, COAP_FORMAT_TEXT);
|
|
|
|
TEST_ASSERT_EQUAL_INT(fmt_opt_len, len);
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_FORMAT_TEXT, coap_get_content_type(&pkt));
|
|
|
|
|
|
|
|
len = coap_opt_finish(&pkt, COAP_OPT_FINISH_PAYLOAD);
|
|
|
|
TEST_ASSERT_EQUAL_INT(total_hdr_len + uri_opt_len + fmt_opt_len + 1, len);
|
|
|
|
TEST_ASSERT_EQUAL_INT(0xFF, *(pkt.payload - 1));
|
2018-10-01 07:10:28 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(&buf[0] + _BUF_SIZE - pkt.payload, pkt.payload_len);
|
2018-05-04 19:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builds on get_req test, to test path with multiple segments.
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__get_multi_path(void)
|
|
|
|
{
|
2018-10-01 07:10:28 +02:00
|
|
|
uint8_t buf[_BUF_SIZE];
|
2018-05-04 19:02:02 +02:00
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char path[] = "/ab/cde";
|
|
|
|
size_t uri_opt_len = 7;
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_GET, msgid);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
|
|
|
|
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
|
|
|
|
|
|
|
|
char uri[10] = {0};
|
2018-08-23 11:48:51 +02:00
|
|
|
coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
|
2018-05-04 19:02:02 +02:00
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri);
|
|
|
|
}
|
|
|
|
|
2018-09-08 11:46:57 +02:00
|
|
|
/*
|
|
|
|
* Builds on get_req test, to test path with trailing slash.
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__get_path_trailing_slash(void)
|
|
|
|
{
|
2018-10-01 07:10:28 +02:00
|
|
|
uint8_t buf[_BUF_SIZE];
|
2018-09-08 11:46:57 +02:00
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char path[] = "/time/";
|
|
|
|
size_t uri_opt_len = 6;
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_GET, msgid);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
|
|
|
|
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
|
|
|
|
|
|
|
|
char uri[10] = {0};
|
|
|
|
coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
|
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri);
|
|
|
|
}
|
2018-06-27 07:08:39 +02:00
|
|
|
/*
|
|
|
|
* Builds on get_req test, to test '/' path. This path is the default when
|
|
|
|
* otherwise not specified.
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__get_root_path(void)
|
|
|
|
{
|
2018-10-01 07:10:28 +02:00
|
|
|
uint8_t buf[_BUF_SIZE];
|
2018-06-27 07:08:39 +02:00
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char path[] = "/";
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_GET, msgid);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
char uri[10] = {0};
|
2018-08-23 11:48:51 +02:00
|
|
|
coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
|
2018-06-27 07:08:39 +02:00
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builds on get_req test, to test max length path.
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__get_max_path(void)
|
|
|
|
{
|
2018-10-01 07:10:28 +02:00
|
|
|
uint8_t buf[_BUF_SIZE];
|
2018-06-27 07:08:39 +02:00
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char path[] = "/23456789012345678901234567890123456789012345678901234567890123";
|
|
|
|
/* includes extra byte for option length > 12 */
|
|
|
|
size_t uri_opt_len = 64;
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_GET, msgid);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
|
|
|
|
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
|
|
|
|
|
|
|
|
char uri[NANOCOAP_URI_MAX] = {0};
|
2018-08-23 11:48:51 +02:00
|
|
|
coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
|
2018-06-27 07:08:39 +02:00
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builds on get_req test, to test path longer than NANOCOAP_URI_MAX. We
|
2018-08-23 11:48:51 +02:00
|
|
|
* expect coap_get_uri_path() to return -ENOSPC.
|
2018-06-27 07:08:39 +02:00
|
|
|
*/
|
|
|
|
static void test_nanocoap__get_path_too_long(void)
|
|
|
|
{
|
2018-10-01 07:10:28 +02:00
|
|
|
uint8_t buf[_BUF_SIZE];
|
2018-06-27 07:08:39 +02:00
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char path[] = "/234567890123456789012345678901234567890123456789012345678901234";
|
|
|
|
/* includes extra byte for option length > 12 */
|
|
|
|
size_t uri_opt_len = 65;
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_GET, msgid);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
|
|
|
|
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
|
|
|
|
|
|
|
|
char uri[NANOCOAP_URI_MAX] = {0};
|
2018-08-23 11:48:51 +02:00
|
|
|
int get_len = coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
|
2018-06-27 07:08:39 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT(-ENOSPC, get_len);
|
|
|
|
}
|
|
|
|
|
2018-10-22 14:34:43 +02:00
|
|
|
/*
|
|
|
|
* Builds on get_req test, to test Uri-Query option.
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__get_query(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[_BUF_SIZE];
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char path[] = "/time";
|
|
|
|
size_t path_opt_len = 5;
|
|
|
|
char qs[] = "ab=cde";
|
|
|
|
size_t query_opt_len = 7;
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_GET, msgid);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
|
|
|
|
TEST_ASSERT_EQUAL_INT(path_opt_len, len);
|
|
|
|
|
|
|
|
uint8_t *query_pos = &pkt.payload[0];
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_QUERY, &qs[0], '&');
|
|
|
|
TEST_ASSERT_EQUAL_INT(query_opt_len, len);
|
|
|
|
|
|
|
|
char uri[10] = {0};
|
|
|
|
coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
|
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri);
|
|
|
|
|
|
|
|
char query[10] = {0};
|
|
|
|
coap_get_uri_query(&pkt, (uint8_t *)&query[0]);
|
|
|
|
/* skip initial '&' from coap_get_uri_query() */
|
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)qs, &query[1]);
|
|
|
|
|
|
|
|
/* overwrite query to test buffer-based put */
|
|
|
|
coap_opt_put_uri_query(query_pos, COAP_OPT_URI_PATH, qs);
|
|
|
|
coap_get_uri_query(&pkt, (uint8_t *)&query[0]);
|
|
|
|
/* skip initial '&' from coap_get_uri_query() */
|
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)qs, &query[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builds on get_query test, to test multiple Uri-Query options.
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__get_multi_query(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[_BUF_SIZE];
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char qs[] = "ab=cde&f=gh";
|
|
|
|
size_t query_opt_len = 13; /* first opt header is 2 bytes long */
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_GET, msgid);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
uint8_t *query_pos = &pkt.payload[0];
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_QUERY, &qs[0], '&');
|
|
|
|
TEST_ASSERT_EQUAL_INT(query_opt_len, len);
|
|
|
|
|
|
|
|
char query[20] = {0};
|
|
|
|
coap_get_uri_query(&pkt, (uint8_t *)&query[0]);
|
|
|
|
/* skip initial '&' from coap_get_uri_query() */
|
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)qs, &query[1]);
|
|
|
|
|
|
|
|
/* overwrite query to test buffer-based put */
|
|
|
|
coap_opt_put_uri_query(query_pos, COAP_OPT_URI_PATH, qs);
|
|
|
|
coap_get_uri_query(&pkt, (uint8_t *)&query[0]);
|
|
|
|
/* skip initial '&' from coap_get_uri_query() */
|
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)qs, &query[1]);
|
|
|
|
}
|
|
|
|
|
2019-01-18 12:34:04 +01:00
|
|
|
/*
|
|
|
|
* Builds on get_req test, to test building a PDU that completely fills the
|
|
|
|
* buffer.
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__option_add_buffer_max(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[70]; /* header 4, token 2, path 64 */
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint16_t msgid = 0xABCD;
|
|
|
|
uint8_t token[2] = {0xDA, 0xEC};
|
|
|
|
char path[] = "/23456789012345678901234567890123456789012345678901234567890123";
|
|
|
|
|
|
|
|
size_t uri_opt_len = 64; /* option hdr 2, option value 62 */
|
|
|
|
|
|
|
|
size_t len = coap_build_hdr((coap_hdr_t *)&buf[0], COAP_TYPE_NON,
|
|
|
|
&token[0], 2, COAP_METHOD_GET, msgid);
|
|
|
|
|
|
|
|
coap_pkt_init(&pkt, &buf[0], sizeof(buf), len);
|
|
|
|
|
|
|
|
len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/');
|
|
|
|
TEST_ASSERT_EQUAL_INT(uri_opt_len, len);
|
|
|
|
}
|
|
|
|
|
2018-10-01 07:10:28 +02:00
|
|
|
/*
|
|
|
|
* Helper for server_get tests below.
|
|
|
|
* GET Request for nanocoap server example /riot/value resource.
|
|
|
|
* Includes 2-byte token; non-confirmable.
|
|
|
|
* Generated with libcoap.
|
|
|
|
*/
|
|
|
|
static int _read_riot_value_req(coap_pkt_t *pkt, uint8_t *buf)
|
|
|
|
{
|
|
|
|
uint8_t pkt_data[] = {
|
|
|
|
0x52, 0x01, 0x9e, 0x6b, 0x35, 0x61, 0xb4, 0x72,
|
|
|
|
0x69, 0x6f, 0x74, 0x05, 0x76, 0x61, 0x6c, 0x75,
|
|
|
|
0x65
|
|
|
|
};
|
|
|
|
memcpy(buf, pkt_data, sizeof(pkt_data));
|
|
|
|
|
|
|
|
return coap_parse(pkt, buf, sizeof(pkt_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Server GET request success case. */
|
|
|
|
static void test_nanocoap__server_get_req(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[_BUF_SIZE];
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
char path[] = "/riot/value";
|
|
|
|
|
|
|
|
int res = _read_riot_value_req(&pkt, &buf[0]);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, res);
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, pkt.payload_len);
|
|
|
|
|
|
|
|
char uri[64] = {0};
|
|
|
|
coap_get_uri_path(&pkt, (uint8_t *)&uri[0]);
|
|
|
|
TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Response for server GET request using coap_reply_simple(). */
|
|
|
|
static void test_nanocoap__server_reply_simple(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[_BUF_SIZE];
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
char *payload = "0";
|
|
|
|
|
|
|
|
int res = _read_riot_value_req(&pkt, &buf[0]);
|
|
|
|
|
|
|
|
coap_reply_simple(&pkt, COAP_CODE_CONTENT, buf, _BUF_SIZE,
|
|
|
|
COAP_FORMAT_TEXT, (uint8_t *)payload, 1);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, res);
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_CODE_CONTENT, coap_get_code_raw(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pkt));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for server_get tests below.
|
|
|
|
* GET request for nanocoap server example /riot/value resource.
|
|
|
|
* Includes 2-byte token; confirmable.
|
|
|
|
* Generated with libcoap.
|
|
|
|
*/
|
|
|
|
static int _read_riot_value_req_con(coap_pkt_t *pkt, uint8_t *buf)
|
|
|
|
{
|
|
|
|
uint8_t pkt_data[] = {
|
|
|
|
0x42, 0x01, 0xbe, 0x16, 0x35, 0x61, 0xb4, 0x72,
|
|
|
|
0x69, 0x6f, 0x74, 0x05, 0x76, 0x61, 0x6c, 0x75,
|
|
|
|
0x65
|
|
|
|
};
|
|
|
|
memcpy(buf, pkt_data, sizeof(pkt_data));
|
|
|
|
|
|
|
|
return coap_parse(pkt, buf, sizeof(pkt_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Builds on test_nanocoap__server_get_req to test confirmable request. */
|
|
|
|
static void test_nanocoap__server_get_req_con(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[_BUF_SIZE];
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
|
|
|
|
int res = _read_riot_value_req_con(&pkt, &buf[0]);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, res);
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pkt));
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_TYPE_CON, coap_get_type(&pkt));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Builds on test_nanocoap__server_reply_simple to test confirmable request. */
|
|
|
|
static void test_nanocoap__server_reply_simple_con(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[_BUF_SIZE];
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
char *payload = "0";
|
|
|
|
|
|
|
|
_read_riot_value_req_con(&pkt, &buf[0]);
|
|
|
|
|
|
|
|
coap_reply_simple(&pkt, COAP_CODE_CONTENT, buf, _BUF_SIZE,
|
|
|
|
COAP_FORMAT_TEXT, (uint8_t *)payload, 1);
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(COAP_TYPE_ACK, coap_get_type(&pkt));
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:27:50 +01:00
|
|
|
static void test_nanocoap__server_option_count_overflow_check(void)
|
|
|
|
{
|
|
|
|
/* this test passes a forged CoAP packet containing 42 options (provided by
|
2019-01-12 13:39:39 +01:00
|
|
|
* @nmeum in #10753, 42 is a random number which just needs to be higher
|
|
|
|
* than NANOCOAP_NOPTS_MAX) to coap_parse(). The used coap_pkt_t is part
|
|
|
|
* of a struct, followed by an array of 42 coap_option_t. The array is
|
|
|
|
* cleared before the call to coap_parse(). If the overflow protection is
|
|
|
|
* working, the array must still be clear after parsing the packet, and the
|
|
|
|
* proper error code (-ENOMEM) is returned. Otherwise, the parsing wrote
|
|
|
|
* past scratch.pkt, thus the array is not zeroed anymore.
|
2019-01-11 11:27:50 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
static uint8_t pkt_data[] = {
|
|
|
|
0x40, 0x01, 0x09, 0x26, 0x01, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17,
|
|
|
|
0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17,
|
|
|
|
0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17,
|
|
|
|
0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17,
|
|
|
|
0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17,
|
|
|
|
0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17,
|
|
|
|
0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17, 0x11, 0x17,
|
|
|
|
0x11, 0x17, 0x11, 0x17 };
|
|
|
|
|
|
|
|
/* ensure NANOCOAP_NOPTS_MAX is actually lower than 42 */
|
|
|
|
TEST_ASSERT(NANOCOAP_NOPTS_MAX < 42);
|
|
|
|
|
|
|
|
struct {
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
uint8_t guard_data[42 * sizeof(coap_optpos_t)];
|
|
|
|
} scratch;
|
|
|
|
|
|
|
|
memset(&scratch, 0, sizeof(scratch));
|
|
|
|
|
|
|
|
int res = coap_parse(&scratch.pkt, pkt_data, sizeof(pkt_data));
|
|
|
|
|
|
|
|
/* check if any byte of the guard_data array is non-zero */
|
|
|
|
int dirty = 0;
|
|
|
|
volatile uint8_t *pos = scratch.guard_data;
|
|
|
|
for (size_t i = 0; i < sizeof(scratch.guard_data); i++) {
|
|
|
|
if (*pos) {
|
|
|
|
dirty = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, dirty);
|
|
|
|
TEST_ASSERT_EQUAL_INT(-ENOMEM, res);
|
|
|
|
}
|
|
|
|
|
2019-01-12 13:39:39 +01:00
|
|
|
/*
|
|
|
|
* Verifies that coap_parse() recognizes inclusion of too many options.
|
|
|
|
*/
|
|
|
|
static void test_nanocoap__server_option_count_overflow(void)
|
|
|
|
{
|
|
|
|
/* base pkt is a GET for /riot/value, which results in two options for the
|
|
|
|
* path, but only 1 entry in the options array.
|
|
|
|
* Size buf to accept an extra 2-byte option */
|
|
|
|
unsigned base_len = 17;
|
|
|
|
uint8_t buf[17 + (2 * NANOCOAP_NOPTS_MAX)] = {
|
|
|
|
0x42, 0x01, 0xbe, 0x16, 0x35, 0x61, 0xb4, 0x72,
|
|
|
|
0x69, 0x6f, 0x74, 0x05, 0x76, 0x61, 0x6c, 0x75,
|
|
|
|
0x65
|
|
|
|
};
|
|
|
|
coap_pkt_t pkt;
|
|
|
|
|
|
|
|
/* nonsense filler option that contains a single byte of data */
|
|
|
|
uint8_t fill_opt[] = { 0x11, 0x01 };
|
|
|
|
|
|
|
|
/* fill pkt with maximum options; should succeed */
|
|
|
|
int i = 0;
|
|
|
|
for (; i < (2 * (NANOCOAP_NOPTS_MAX - 1)); i+=2) {
|
|
|
|
memcpy(&buf[base_len+i], fill_opt, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't read final two bytes, where overflow option will be added later */
|
|
|
|
int res = coap_parse(&pkt, buf, sizeof(buf) - 2);
|
|
|
|
TEST_ASSERT_EQUAL_INT(0, res);
|
|
|
|
|
|
|
|
/* add option to overflow */
|
|
|
|
memcpy(&buf[base_len+i], fill_opt, 2);
|
|
|
|
|
|
|
|
res = coap_parse(&pkt, buf, sizeof(buf));
|
|
|
|
TEST_ASSERT(res < 0);
|
|
|
|
}
|
|
|
|
|
2018-01-12 11:41:37 +01:00
|
|
|
Test *tests_nanocoap_tests(void)
|
|
|
|
{
|
|
|
|
EMB_UNIT_TESTFIXTURES(fixtures) {
|
2018-03-16 10:02:39 +01:00
|
|
|
new_TestFixture(test_nanocoap__hdr),
|
2018-05-04 19:02:02 +02:00
|
|
|
new_TestFixture(test_nanocoap__get_req),
|
|
|
|
new_TestFixture(test_nanocoap__put_req),
|
|
|
|
new_TestFixture(test_nanocoap__get_multi_path),
|
2018-09-08 11:46:57 +02:00
|
|
|
new_TestFixture(test_nanocoap__get_path_trailing_slash),
|
2018-06-27 07:08:39 +02:00
|
|
|
new_TestFixture(test_nanocoap__get_root_path),
|
|
|
|
new_TestFixture(test_nanocoap__get_max_path),
|
|
|
|
new_TestFixture(test_nanocoap__get_path_too_long),
|
2018-10-22 14:34:43 +02:00
|
|
|
new_TestFixture(test_nanocoap__get_query),
|
|
|
|
new_TestFixture(test_nanocoap__get_multi_query),
|
2019-01-18 12:34:04 +01:00
|
|
|
new_TestFixture(test_nanocoap__option_add_buffer_max),
|
2018-10-01 07:10:28 +02:00
|
|
|
new_TestFixture(test_nanocoap__server_get_req),
|
|
|
|
new_TestFixture(test_nanocoap__server_reply_simple),
|
|
|
|
new_TestFixture(test_nanocoap__server_get_req_con),
|
|
|
|
new_TestFixture(test_nanocoap__server_reply_simple_con),
|
2019-01-11 11:27:50 +01:00
|
|
|
new_TestFixture(test_nanocoap__server_option_count_overflow_check),
|
2019-01-12 13:39:39 +01:00
|
|
|
new_TestFixture(test_nanocoap__server_option_count_overflow),
|
2018-01-12 11:41:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
EMB_UNIT_TESTCALLER(nanocoap_tests, NULL, NULL, fixtures);
|
|
|
|
|
|
|
|
return (Test *)&nanocoap_tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tests_nanocoap(void)
|
|
|
|
{
|
|
|
|
TESTS_RUN(tests_nanocoap_tests());
|
|
|
|
}
|
|
|
|
/** @} */
|