/* * Builds on get_req test, to test payload and Content-Format option. */ static void test_nanocoap__put_req(void) { uint8_t buf[128]; 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)); TEST_ASSERT_EQUAL_INT(&buf[0] + 128 - pkt.payload, pkt.payload_len); }
ssize_t nanocoap_get(sock_udp_ep_t *remote, const char *path, uint8_t *buf, size_t len) { ssize_t res; coap_pkt_t pkt; uint8_t *pktpos = buf; pkt.hdr = (coap_hdr_t*)buf; pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, 1); pktpos += coap_opt_put_uri_path(pktpos, 0, path); pkt.payload = pktpos; pkt.payload_len = 0; res = nanocoap_request(&pkt, NULL, remote, len); if (res < 0) { return res; } else { res = coap_get_code(&pkt); if (res != 205) { res = -res; } else { if (pkt.payload_len) { memmove(buf, pkt.payload, pkt.payload_len); } res = pkt.payload_len; } } return res; }
/* * Validates encoded message ID byte order and put/get URI option. */ static void test_nanocoap__hdr(void) { uint8_t buf[_BUF_SIZE]; uint16_t msgid = 0xABCD; char path[] = "/test/abcd/efgh"; char loc_path[] = "/foo/bar"; unsigned char path_tmp[64] = {0}; uint8_t *pktpos = &buf[0]; pktpos += coap_build_hdr((coap_hdr_t *)pktpos, COAP_TYPE_CON, NULL, 0, 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); coap_pkt_t pkt; coap_parse(&pkt, &buf[0], pktpos - &buf[0]); TEST_ASSERT_EQUAL_INT(msgid, coap_get_id(&pkt)); int res = coap_get_uri_path(&pkt, path_tmp); TEST_ASSERT_EQUAL_INT(sizeof(path), res); TEST_ASSERT_EQUAL_STRING((char *)path, (char *)path_tmp); 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); }
/* * Client GET request with simple path. Test request generation. * Request /time resource from libcoap example */ static void test_nanocoap__get_req(void) { uint8_t buf[128]; 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}; coap_get_uri(&pkt, (uint8_t *)&uri[0]); 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_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]); }
/* * 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) { uint8_t buf[128]; 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}; coap_get_uri(&pkt, (uint8_t *)&uri[0]); TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri); }
/* * Validates encoded message ID byte order and put/get URI option. */ static void test_nanocoap__hdr(void) { uint8_t buf[128]; uint16_t msgid = 0xABCD; char path[] = "/test/abcd/efgh"; unsigned char path_tmp[64] = {0}; uint8_t *pktpos = &buf[0]; pktpos += coap_build_hdr((coap_hdr_t *)pktpos, COAP_REQ, NULL, 0, COAP_METHOD_GET, msgid); pktpos += coap_put_option_uri(pktpos, 0, path, COAP_OPT_URI_PATH); coap_pkt_t pkt; coap_parse(&pkt, &buf[0], pktpos - &buf[0]); TEST_ASSERT_EQUAL_INT(msgid, coap_get_id(&pkt)); int res = coap_get_uri(&pkt, path_tmp); TEST_ASSERT_EQUAL_INT(sizeof(path), res); TEST_ASSERT_EQUAL_STRING((char *)path, (char *)path_tmp); }
/* * Builds on get_req test, to test path with multiple segments. */ static void test_nanocoap__get_multi_path(void) { uint8_t buf[128]; 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}; coap_get_uri(&pkt, (uint8_t *)&uri[0]); TEST_ASSERT_EQUAL_STRING((char *)path, (char *)uri); }
ssize_t coap_build_reply(coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned payload_len) { unsigned tkl = coap_get_token_len(pkt); unsigned len = sizeof(coap_hdr_t) + tkl; if ((len + payload_len + 1) > rlen) { return -ENOSPC; } /* if code is COAP_CODE_EMPTY (zero), use RST as type, else RESP */ unsigned type = code ? COAP_RESP : COAP_RST; coap_build_hdr((coap_hdr_t *)rbuf, type, pkt->token, tkl, code, ntohs(pkt->hdr->id)); coap_hdr_set_type((coap_hdr_t *)rbuf, type); coap_hdr_set_code((coap_hdr_t *)rbuf, code); len += payload_len; return len; }
/* * Builds on get_req test, to test path longer than NANOCOAP_URI_MAX. We * expect coap_get_uri() to return -ENOSPC. */ static void test_nanocoap__get_path_too_long(void) { uint8_t buf[128]; 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}; int get_len = coap_get_uri(&pkt, (uint8_t *)&uri[0]); TEST_ASSERT_EQUAL_INT(-ENOSPC, get_len); }
/* * Builds on get_req test, to test building a PDU that completely fills the * buffer, and one that tries to overfill 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 */ ssize_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); /* shrink buffer to attempt overfill */ coap_pkt_init(&pkt, &buf[0], sizeof(buf) - 1, len); len = coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, &path[0], '/'); TEST_ASSERT_EQUAL_INT(-ENOSPC, len); }