1c87c5fbaSopenharmony_ci/* libcoap unit tests 2c87c5fbaSopenharmony_ci * 3c87c5fbaSopenharmony_ci * Copyright (C) 2012,2015,2022-2023 Olaf Bergmann <bergmann@tzi.org> 4c87c5fbaSopenharmony_ci * 5c87c5fbaSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause 6c87c5fbaSopenharmony_ci * 7c87c5fbaSopenharmony_ci * This file is part of the CoAP library libcoap. Please see 8c87c5fbaSopenharmony_ci * README for terms of use. 9c87c5fbaSopenharmony_ci */ 10c87c5fbaSopenharmony_ci 11c87c5fbaSopenharmony_ci#include "test_common.h" 12c87c5fbaSopenharmony_ci#include "test_pdu.h" 13c87c5fbaSopenharmony_ci 14c87c5fbaSopenharmony_ci#include <assert.h> 15c87c5fbaSopenharmony_ci#include <stdio.h> 16c87c5fbaSopenharmony_ci#include <stdlib.h> 17c87c5fbaSopenharmony_ci#include <string.h> 18c87c5fbaSopenharmony_ci 19c87c5fbaSopenharmony_cicoap_pdu_t *pdu; /* Holds the parsed PDU for most tests */ 20c87c5fbaSopenharmony_ci 21c87c5fbaSopenharmony_ci/************************************************************************ 22c87c5fbaSopenharmony_ci ** PDU decoder 23c87c5fbaSopenharmony_ci ************************************************************************/ 24c87c5fbaSopenharmony_ci 25c87c5fbaSopenharmony_cistatic void 26c87c5fbaSopenharmony_cit_parse_pdu1(void) { 27c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x40, 0x01, 0x93, 0x34 }; 28c87c5fbaSopenharmony_ci int result; 29c87c5fbaSopenharmony_ci 30c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 31c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 32c87c5fbaSopenharmony_ci 33c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); 34c87c5fbaSopenharmony_ci CU_ASSERT(pdu->type == COAP_MESSAGE_CON); 35c87c5fbaSopenharmony_ci CU_ASSERT(pdu->e_token_length == 0); 36c87c5fbaSopenharmony_ci CU_ASSERT(pdu->code == COAP_REQUEST_CODE_GET); 37c87c5fbaSopenharmony_ci CU_ASSERT(pdu->mid == 0x9334); 38c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 39c87c5fbaSopenharmony_ci} 40c87c5fbaSopenharmony_ci 41c87c5fbaSopenharmony_cistatic void 42c87c5fbaSopenharmony_cit_parse_pdu2(void) { 43c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x55, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' }; 44c87c5fbaSopenharmony_ci int result; 45c87c5fbaSopenharmony_ci 46c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 47c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 48c87c5fbaSopenharmony_ci 49c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); 50c87c5fbaSopenharmony_ci CU_ASSERT(pdu->type == COAP_MESSAGE_NON); 51c87c5fbaSopenharmony_ci CU_ASSERT(pdu->e_token_length == 5); 52c87c5fbaSopenharmony_ci CU_ASSERT(pdu->code == 0x69); 53c87c5fbaSopenharmony_ci CU_ASSERT(pdu->mid == 0x1234); 54c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, teststr + 4, 5) == 0); 55c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 56c87c5fbaSopenharmony_ci} 57c87c5fbaSopenharmony_ci 58c87c5fbaSopenharmony_cistatic void 59c87c5fbaSopenharmony_cit_parse_pdu3(void) { 60c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x53, 0x69, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' }; 61c87c5fbaSopenharmony_ci int result; 62c87c5fbaSopenharmony_ci 63c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 64c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 65c87c5fbaSopenharmony_ci} 66c87c5fbaSopenharmony_ci 67c87c5fbaSopenharmony_cistatic void 68c87c5fbaSopenharmony_cit_parse_pdu4(void) { 69c87c5fbaSopenharmony_ci /* illegal token length (token only 8 bytes) */ 70c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x59, 0x69, 0x12, 0x34, 71c87c5fbaSopenharmony_ci 't', 'o', 'k', 'e', 'n', '1', '2', '3' 72c87c5fbaSopenharmony_ci }; 73c87c5fbaSopenharmony_ci int result; 74c87c5fbaSopenharmony_ci 75c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 76c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 77c87c5fbaSopenharmony_ci 78c87c5fbaSopenharmony_ci /* illegal token length */ 79c87c5fbaSopenharmony_ci teststr[0] = 0x5f; 80c87c5fbaSopenharmony_ci 81c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 82c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 83c87c5fbaSopenharmony_ci} 84c87c5fbaSopenharmony_ci 85c87c5fbaSopenharmony_cistatic void 86c87c5fbaSopenharmony_cit_parse_pdu5(void) { 87c87c5fbaSopenharmony_ci /* PDU with options */ 88c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x55, 0x73, 0x12, 0x34, 't', 'o', 'k', 'e', 89c87c5fbaSopenharmony_ci 'n', 0x00, 0xc1, 0x00 90c87c5fbaSopenharmony_ci }; 91c87c5fbaSopenharmony_ci int result; 92c87c5fbaSopenharmony_ci 93c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 94c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 95c87c5fbaSopenharmony_ci 96c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); 97c87c5fbaSopenharmony_ci CU_ASSERT(pdu->type == COAP_MESSAGE_NON); 98c87c5fbaSopenharmony_ci CU_ASSERT(pdu->e_token_length == 5); 99c87c5fbaSopenharmony_ci CU_ASSERT(pdu->code == 0x73); 100c87c5fbaSopenharmony_ci CU_ASSERT(pdu->mid == 0x1234); 101c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, teststr + 4, 5) == 0); 102c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 103c87c5fbaSopenharmony_ci 104c87c5fbaSopenharmony_ci /* FIXME: check options */ 105c87c5fbaSopenharmony_ci} 106c87c5fbaSopenharmony_ci 107c87c5fbaSopenharmony_cistatic void 108c87c5fbaSopenharmony_cit_parse_pdu6(void) { 109c87c5fbaSopenharmony_ci /* PDU with options that exceed the PDU */ 110c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x55, 0x73, 0x12, 0x34, 't', 'o', 'k', 'e', 111c87c5fbaSopenharmony_ci 'n', 0x00, 0xc1, 0x00, 0xae, 0xf0, 0x03 112c87c5fbaSopenharmony_ci }; 113c87c5fbaSopenharmony_ci int result; 114c87c5fbaSopenharmony_ci 115c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 116c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 117c87c5fbaSopenharmony_ci} 118c87c5fbaSopenharmony_ci 119c87c5fbaSopenharmony_cistatic void 120c87c5fbaSopenharmony_cit_parse_pdu7(void) { 121c87c5fbaSopenharmony_ci /* PDU with options and payload */ 122c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x55, 0x73, 0x12, 0x34, 't', 'o', 'k', 'e', 123c87c5fbaSopenharmony_ci 'n', 0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y', 124c87c5fbaSopenharmony_ci 'l', 'o', 'a', 'd' 125c87c5fbaSopenharmony_ci }; 126c87c5fbaSopenharmony_ci int result; 127c87c5fbaSopenharmony_ci 128c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 129c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 130c87c5fbaSopenharmony_ci 131c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); 132c87c5fbaSopenharmony_ci CU_ASSERT(pdu->type == COAP_MESSAGE_NON); 133c87c5fbaSopenharmony_ci CU_ASSERT(pdu->e_token_length == 5); 134c87c5fbaSopenharmony_ci CU_ASSERT(pdu->code == 0x73); 135c87c5fbaSopenharmony_ci CU_ASSERT(pdu->mid == 0x1234); 136c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, teststr + 4, 5) == 0); 137c87c5fbaSopenharmony_ci 138c87c5fbaSopenharmony_ci /* FIXME: check options */ 139c87c5fbaSopenharmony_ci 140c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == pdu->token + 9); 141c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->data, teststr + 13, 7) == 0); 142c87c5fbaSopenharmony_ci} 143c87c5fbaSopenharmony_ci 144c87c5fbaSopenharmony_cistatic void 145c87c5fbaSopenharmony_cit_parse_pdu8(void) { 146c87c5fbaSopenharmony_ci /* PDU without options but with payload */ 147c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x50, 0x73, 0x12, 0x34, 148c87c5fbaSopenharmony_ci 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 149c87c5fbaSopenharmony_ci 'd' 150c87c5fbaSopenharmony_ci }; 151c87c5fbaSopenharmony_ci int result; 152c87c5fbaSopenharmony_ci 153c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 154c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 155c87c5fbaSopenharmony_ci 156c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); 157c87c5fbaSopenharmony_ci CU_ASSERT(pdu->type == COAP_MESSAGE_NON); 158c87c5fbaSopenharmony_ci CU_ASSERT(pdu->e_token_length == 0); 159c87c5fbaSopenharmony_ci CU_ASSERT(pdu->code == 0x73); 160c87c5fbaSopenharmony_ci CU_ASSERT(pdu->mid == 0x1234); 161c87c5fbaSopenharmony_ci 162c87c5fbaSopenharmony_ci /* FIXME: check options */ 163c87c5fbaSopenharmony_ci 164c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == pdu->token + 1); 165c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->data, teststr + 5, 7) == 0); 166c87c5fbaSopenharmony_ci} 167c87c5fbaSopenharmony_ci 168c87c5fbaSopenharmony_cistatic void 169c87c5fbaSopenharmony_cit_parse_pdu9(void) { 170c87c5fbaSopenharmony_ci /* PDU without options and payload but with payload start marker */ 171c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x70, 0x00, 0x12, 0x34, 0xff }; 172c87c5fbaSopenharmony_ci int result; 173c87c5fbaSopenharmony_ci 174c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 175c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 176c87c5fbaSopenharmony_ci} 177c87c5fbaSopenharmony_ci 178c87c5fbaSopenharmony_cistatic void 179c87c5fbaSopenharmony_cit_parse_pdu10(void) { 180c87c5fbaSopenharmony_ci /* PDU without payload but with options and payload start marker */ 181c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x53, 0x73, 0x12, 0x34, 't', 'o', 'k', 182c87c5fbaSopenharmony_ci 0x31, 'a', 0xc1, 0x00, 0xff 183c87c5fbaSopenharmony_ci }; 184c87c5fbaSopenharmony_ci int result; 185c87c5fbaSopenharmony_ci 186c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 187c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 188c87c5fbaSopenharmony_ci} 189c87c5fbaSopenharmony_ci 190c87c5fbaSopenharmony_cistatic void 191c87c5fbaSopenharmony_cit_parse_pdu11(void) { 192c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x60, 0x00, 0x12, 0x34 }; 193c87c5fbaSopenharmony_ci int result; 194c87c5fbaSopenharmony_ci 195c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 196c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 197c87c5fbaSopenharmony_ci 198c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); 199c87c5fbaSopenharmony_ci CU_ASSERT(pdu->type == COAP_MESSAGE_ACK); 200c87c5fbaSopenharmony_ci CU_ASSERT(pdu->e_token_length == 0); 201c87c5fbaSopenharmony_ci CU_ASSERT(pdu->code == 0); 202c87c5fbaSopenharmony_ci CU_ASSERT(pdu->mid == 0x1234); 203c87c5fbaSopenharmony_ci} 204c87c5fbaSopenharmony_ci 205c87c5fbaSopenharmony_cistatic void 206c87c5fbaSopenharmony_cit_parse_pdu12(void) { 207c87c5fbaSopenharmony_ci /* RST */ 208c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x70, 0x00, 0x12, 0x34 }; 209c87c5fbaSopenharmony_ci int result; 210c87c5fbaSopenharmony_ci 211c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 212c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 213c87c5fbaSopenharmony_ci 214c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == sizeof(teststr) - 4); 215c87c5fbaSopenharmony_ci CU_ASSERT(pdu->type == COAP_MESSAGE_RST); 216c87c5fbaSopenharmony_ci CU_ASSERT(pdu->e_token_length == 0); 217c87c5fbaSopenharmony_ci CU_ASSERT(pdu->code == 0); 218c87c5fbaSopenharmony_ci CU_ASSERT(pdu->mid == 0x1234); 219c87c5fbaSopenharmony_ci} 220c87c5fbaSopenharmony_ci 221c87c5fbaSopenharmony_cistatic void 222c87c5fbaSopenharmony_cit_parse_pdu13(void) { 223c87c5fbaSopenharmony_ci /* RST with content */ 224c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x70, 0x00, 0x12, 0x34, 225c87c5fbaSopenharmony_ci 0xff, 'c', 'o', 'n', 't', 'e', 'n', 't' 226c87c5fbaSopenharmony_ci }; 227c87c5fbaSopenharmony_ci int result; 228c87c5fbaSopenharmony_ci 229c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 230c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 231c87c5fbaSopenharmony_ci} 232c87c5fbaSopenharmony_ci 233c87c5fbaSopenharmony_cistatic void 234c87c5fbaSopenharmony_cit_parse_pdu14(void) { 235c87c5fbaSopenharmony_ci /* ACK with content */ 236c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x60, 0x00, 0x12, 0x34, 237c87c5fbaSopenharmony_ci 0xff, 'c', 'o', 'n', 't', 'e', 'n', 't' 238c87c5fbaSopenharmony_ci }; 239c87c5fbaSopenharmony_ci int result; 240c87c5fbaSopenharmony_ci 241c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 242c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 243c87c5fbaSopenharmony_ci} 244c87c5fbaSopenharmony_ci 245c87c5fbaSopenharmony_ci/* 246c87c5fbaSopenharmony_ci * To test Issue #199 which reads one byte past the end of teststr[] 247c87c5fbaSopenharmony_ci * before fix to coap_opt_parse() as delta is two byte value and only 248c87c5fbaSopenharmony_ci * one byte left 249c87c5fbaSopenharmony_ci * Credit to OSS-Fuzz for finding this, work done by Bhargava Shastry 250c87c5fbaSopenharmony_ci */ 251c87c5fbaSopenharmony_cistatic void 252c87c5fbaSopenharmony_cit_parse_pdu15(void) { 253c87c5fbaSopenharmony_ci int result; 254c87c5fbaSopenharmony_ci uint8_t teststr[] = { 255c87c5fbaSopenharmony_ci 64, 91, 91, 91, 139, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 0, 256c87c5fbaSopenharmony_ci 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 91, 224, 224, 224, 257c87c5fbaSopenharmony_ci 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 258c87c5fbaSopenharmony_ci 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 1, 0, 0, 0, 259c87c5fbaSopenharmony_ci 0, 0, 0, 0, 224, 192, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 260c87c5fbaSopenharmony_ci 224, 224, 224, 224, 224, 224, 224, 224, 228, 224, 224, 224, 224, 224, 224, 224, 261c87c5fbaSopenharmony_ci 224, 224, 224, 224, 224, 224, 224, 224, 224, 91, 91, 91, 91, 91, 91, 91, 262c87c5fbaSopenharmony_ci 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 263c87c5fbaSopenharmony_ci 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 264c87c5fbaSopenharmony_ci 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 265c87c5fbaSopenharmony_ci 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 266c87c5fbaSopenharmony_ci 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 267c87c5fbaSopenharmony_ci 91, 91, 91, 91, 91, 91, 91, 224, 224, 224, 224, 224, 224, 224, 224, 224, 268c87c5fbaSopenharmony_ci 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 269c87c5fbaSopenharmony_ci 224, 224, 224, 224, 224, 224, 1, 0, 0, 0, 0, 0, 0, 0, 224, 224, 270c87c5fbaSopenharmony_ci 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 271c87c5fbaSopenharmony_ci 224, 224, 224, 224, 224 272c87c5fbaSopenharmony_ci }; 273c87c5fbaSopenharmony_ci 274c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 275c87c5fbaSopenharmony_ci 276c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 277c87c5fbaSopenharmony_ci 278c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 279c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 280c87c5fbaSopenharmony_ci} 281c87c5fbaSopenharmony_ci 282c87c5fbaSopenharmony_cistatic void 283c87c5fbaSopenharmony_cilog_handler(coap_log_t level, const char *message) { 284c87c5fbaSopenharmony_ci (void)level; 285c87c5fbaSopenharmony_ci (void)message; 286c87c5fbaSopenharmony_ci} 287c87c5fbaSopenharmony_ci 288c87c5fbaSopenharmony_ci/* 289c87c5fbaSopenharmony_ci * To test Issue #214 which allows the token size to be set larger than the 290c87c5fbaSopenharmony_ci * decoded PDU in coap_pdu_parse_header(). This then causes coap_show_pdu() 291c87c5fbaSopenharmony_ci * to access invalid memory. 292c87c5fbaSopenharmony_ci * Credit to OSS-Fuzz for finding this, work done by Bhargava Shastry 293c87c5fbaSopenharmony_ci */ 294c87c5fbaSopenharmony_cistatic void 295c87c5fbaSopenharmony_cit_parse_pdu16(void) { 296c87c5fbaSopenharmony_ci int result; 297c87c5fbaSopenharmony_ci coap_pdu_t *testpdu; 298c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x5a, 0x0a, 0x5b, 0x5b }; 299c87c5fbaSopenharmony_ci 300c87c5fbaSopenharmony_ci testpdu = coap_pdu_init(0, 0, 0, sizeof(teststr)); 301c87c5fbaSopenharmony_ci CU_ASSERT(testpdu != NULL); 302c87c5fbaSopenharmony_ci 303c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), testpdu); 304c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 305c87c5fbaSopenharmony_ci 306c87c5fbaSopenharmony_ci coap_set_show_pdu_output(0); 307c87c5fbaSopenharmony_ci coap_set_log_handler(log_handler); 308c87c5fbaSopenharmony_ci coap_show_pdu(COAP_LOG_ERR, testpdu); /* display PDU */ 309c87c5fbaSopenharmony_ci coap_set_log_handler(NULL); 310c87c5fbaSopenharmony_ci 311c87c5fbaSopenharmony_ci coap_delete_pdu(testpdu); 312c87c5fbaSopenharmony_ci} 313c87c5fbaSopenharmony_ci 314c87c5fbaSopenharmony_cistatic void 315c87c5fbaSopenharmony_cit_parse_pdu17(void) { 316c87c5fbaSopenharmony_ci uint8_t teststr[512] = { 0x40, 0x01, 0x93, 0x34 }; 317c87c5fbaSopenharmony_ci size_t idx; 318c87c5fbaSopenharmony_ci int result; 319c87c5fbaSopenharmony_ci 320c87c5fbaSopenharmony_ci /* 245 * option delta 268 > 65535, causing a overflow in the option 321c87c5fbaSopenharmony_ci * number */ 322c87c5fbaSopenharmony_ci for (idx = 4; idx < sizeof(teststr) - 4; idx += 2) { 323c87c5fbaSopenharmony_ci teststr[idx] = 0xd0; /* 1 byte option delta follows */ 324c87c5fbaSopenharmony_ci teststr[idx + 1] = 0xff; /* option delta 268 */ 325c87c5fbaSopenharmony_ci } 326c87c5fbaSopenharmony_ci 327c87c5fbaSopenharmony_ci result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), pdu); 328c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 329c87c5fbaSopenharmony_ci} 330c87c5fbaSopenharmony_ci 331c87c5fbaSopenharmony_ci/************************************************************************ 332c87c5fbaSopenharmony_ci ** PDU encoder 333c87c5fbaSopenharmony_ci ************************************************************************/ 334c87c5fbaSopenharmony_ci 335c87c5fbaSopenharmony_cistatic void 336c87c5fbaSopenharmony_cit_encode_pdu1(void) { 337c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x45, 0x01, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' }; 338c87c5fbaSopenharmony_ci int result; 339c87c5fbaSopenharmony_ci 340c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); 341c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_CON; 342c87c5fbaSopenharmony_ci pdu->code = COAP_REQUEST_CODE_GET; 343c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 344c87c5fbaSopenharmony_ci 345c87c5fbaSopenharmony_ci result = coap_add_token(pdu, 5, (const uint8_t *)"token"); 346c87c5fbaSopenharmony_ci 347c87c5fbaSopenharmony_ci CU_ASSERT(result == 1); 348c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 5); 349c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 350c87c5fbaSopenharmony_ci CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); 351c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); 352c87c5fbaSopenharmony_ci} 353c87c5fbaSopenharmony_ci 354c87c5fbaSopenharmony_cistatic void 355c87c5fbaSopenharmony_cit_encode_pdu2(void) { 356c87c5fbaSopenharmony_ci coap_log_t level = coap_get_log_level(); 357c87c5fbaSopenharmony_ci size_t old_max = pdu->max_size; 358c87c5fbaSopenharmony_ci int result; 359c87c5fbaSopenharmony_ci 360c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, 3); /* set very small PDU size */ 361c87c5fbaSopenharmony_ci 362c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_CON; 363c87c5fbaSopenharmony_ci pdu->code = COAP_REQUEST_CODE_GET; 364c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 365c87c5fbaSopenharmony_ci 366c87c5fbaSopenharmony_ci coap_set_log_level(COAP_LOG_CRIT); 367c87c5fbaSopenharmony_ci result = coap_add_token(pdu, 5, (const uint8_t *)"token"); 368c87c5fbaSopenharmony_ci coap_set_log_level(level); 369c87c5fbaSopenharmony_ci 370c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 371c87c5fbaSopenharmony_ci 372c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, old_max); /* restore PDU size */ 373c87c5fbaSopenharmony_ci} 374c87c5fbaSopenharmony_ci 375c87c5fbaSopenharmony_cistatic void 376c87c5fbaSopenharmony_cit_encode_pdu3(void) { 377c87c5fbaSopenharmony_ci int result; 378c87c5fbaSopenharmony_ci coap_bin_const_t check_token; 379c87c5fbaSopenharmony_ci 380c87c5fbaSopenharmony_ci result = coap_add_token(pdu, 15, (const uint8_t *)"123456789012345"); 381c87c5fbaSopenharmony_ci 382c87c5fbaSopenharmony_ci /* length of 15 triggers extension */ 383c87c5fbaSopenharmony_ci CU_ASSERT(result == 1 && pdu->actual_token.length == 15 && 384c87c5fbaSopenharmony_ci pdu->e_token_length == 16 && pdu->token[0] == 2); 385c87c5fbaSopenharmony_ci 386c87c5fbaSopenharmony_ci check_token = coap_pdu_get_token(pdu); 387c87c5fbaSopenharmony_ci CU_ASSERT(check_token.length == 15); 388c87c5fbaSopenharmony_ci} 389c87c5fbaSopenharmony_ci 390c87c5fbaSopenharmony_cistatic void 391c87c5fbaSopenharmony_cit_encode_pdu4(void) { 392c87c5fbaSopenharmony_ci /* PDU with options */ 393c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x60, 0x99, 0x12, 0x34, 0x3d, 0x05, 0x66, 0x61, 394c87c5fbaSopenharmony_ci 0x6e, 0x63, 0x79, 0x70, 0x72, 0x6f, 0x78, 0x79, 395c87c5fbaSopenharmony_ci 0x2e, 0x63, 0x6f, 0x61, 0x70, 0x2e, 0x6d, 0x65, 396c87c5fbaSopenharmony_ci 0x84, 0x70, 0x61, 0x74, 0x68, 0x00, 0xe8, 0x1e, 397c87c5fbaSopenharmony_ci 0x28, 0x66, 0x61, 0x6e, 0x63, 0x79, 0x6f, 0x70, 398c87c5fbaSopenharmony_ci 0x74 399c87c5fbaSopenharmony_ci }; 400c87c5fbaSopenharmony_ci int result; 401c87c5fbaSopenharmony_ci 402c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 403c87c5fbaSopenharmony_ci 404c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_ACK; 405c87c5fbaSopenharmony_ci pdu->code = 0x99; 406c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 407c87c5fbaSopenharmony_ci 408c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 409c87c5fbaSopenharmony_ci 410c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_URI_HOST, 411c87c5fbaSopenharmony_ci 18, (const uint8_t *)"fancyproxy.coap.me"); 412c87c5fbaSopenharmony_ci 413c87c5fbaSopenharmony_ci CU_ASSERT(result == 20); 414c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 3); 415c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 20); 416c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 417c87c5fbaSopenharmony_ci 418c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 419c87c5fbaSopenharmony_ci 4, (const uint8_t *)"path"); 420c87c5fbaSopenharmony_ci 421c87c5fbaSopenharmony_ci CU_ASSERT(result == 5); 422c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 11); 423c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 25); 424c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 425c87c5fbaSopenharmony_ci 426c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 0, NULL); 427c87c5fbaSopenharmony_ci 428c87c5fbaSopenharmony_ci CU_ASSERT(result == 1); 429c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 11); 430c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 26); 431c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 432c87c5fbaSopenharmony_ci 433c87c5fbaSopenharmony_ci result = coap_add_option(pdu, 8000, 8, (const uint8_t *)"fancyopt"); 434c87c5fbaSopenharmony_ci 435c87c5fbaSopenharmony_ci CU_ASSERT(result == 11); 436c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 8000); 437c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 37); 438c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 439c87c5fbaSopenharmony_ci 440c87c5fbaSopenharmony_ci CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); 441c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); 442c87c5fbaSopenharmony_ci} 443c87c5fbaSopenharmony_ci 444c87c5fbaSopenharmony_cistatic void 445c87c5fbaSopenharmony_cit_encode_pdu5(void) { 446c87c5fbaSopenharmony_ci /* PDU with token and options */ 447c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x68, 0x84, 0x12, 0x34, '1', '2', '3', '4', 448c87c5fbaSopenharmony_ci '5', '6', '7', '8', 0x18, 0x41, 0x42, 0x43, 449c87c5fbaSopenharmony_ci 0x44, 0x45, 0x46, 0x47, 0x48, 0xd1, 0x03, 0x12 450c87c5fbaSopenharmony_ci }; 451c87c5fbaSopenharmony_ci int result; 452c87c5fbaSopenharmony_ci 453c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 454c87c5fbaSopenharmony_ci 455c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_ACK; 456c87c5fbaSopenharmony_ci pdu->code = COAP_RESPONSE_CODE(404); 457c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 458c87c5fbaSopenharmony_ci 459c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 460c87c5fbaSopenharmony_ci 461c87c5fbaSopenharmony_ci result = coap_add_token(pdu, 8, (const uint8_t *)"12345678"); 462c87c5fbaSopenharmony_ci 463c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 8); 464c87c5fbaSopenharmony_ci 465c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_IF_MATCH, 466c87c5fbaSopenharmony_ci 8, (const uint8_t *)"ABCDEFGH"); 467c87c5fbaSopenharmony_ci 468c87c5fbaSopenharmony_ci CU_ASSERT(result == 9); 469c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 1); 470c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 17); 471c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 472c87c5fbaSopenharmony_ci 473c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_ACCEPT, 474c87c5fbaSopenharmony_ci 1, (const uint8_t *)"\x12"); 475c87c5fbaSopenharmony_ci 476c87c5fbaSopenharmony_ci CU_ASSERT(result == 3); 477c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 17); 478c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 20); 479c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 480c87c5fbaSopenharmony_ci 481c87c5fbaSopenharmony_ci CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); 482c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); 483c87c5fbaSopenharmony_ci} 484c87c5fbaSopenharmony_ci 485c87c5fbaSopenharmony_cistatic void 486c87c5fbaSopenharmony_cit_encode_pdu6(void) { 487c87c5fbaSopenharmony_ci /* PDU with data */ 488c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x50, 0x02, 0x12, 0x34, 0xff, '1', '2', '3', 489c87c5fbaSopenharmony_ci '4', '5', '6', '7', '8' 490c87c5fbaSopenharmony_ci }; 491c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 492c87c5fbaSopenharmony_ci 493c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_NON; 494c87c5fbaSopenharmony_ci pdu->code = COAP_REQUEST_CODE_POST; 495c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 496c87c5fbaSopenharmony_ci 497c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 498c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 499c87c5fbaSopenharmony_ci 500c87c5fbaSopenharmony_ci coap_add_data(pdu, 8, (const uint8_t *)"12345678"); 501c87c5fbaSopenharmony_ci 502c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 9); 503c87c5fbaSopenharmony_ci CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); 504c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); 505c87c5fbaSopenharmony_ci} 506c87c5fbaSopenharmony_ci 507c87c5fbaSopenharmony_cistatic void 508c87c5fbaSopenharmony_cit_encode_pdu7(void) { 509c87c5fbaSopenharmony_ci /* PDU with empty data */ 510c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x40, 0x43, 0x12, 0x34 }; 511c87c5fbaSopenharmony_ci int result; 512c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 513c87c5fbaSopenharmony_ci 514c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_CON; 515c87c5fbaSopenharmony_ci pdu->code = COAP_RESPONSE_CODE(203); 516c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 517c87c5fbaSopenharmony_ci 518c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 519c87c5fbaSopenharmony_ci 520c87c5fbaSopenharmony_ci result = coap_add_data(pdu, 0, NULL); 521c87c5fbaSopenharmony_ci 522c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 523c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 524c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 525c87c5fbaSopenharmony_ci 526c87c5fbaSopenharmony_ci CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); 527c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); 528c87c5fbaSopenharmony_ci} 529c87c5fbaSopenharmony_ci 530c87c5fbaSopenharmony_cistatic void 531c87c5fbaSopenharmony_cit_encode_pdu8(void) { 532c87c5fbaSopenharmony_ci /* PDU with token and data */ 533c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x42, 0x43, 0x12, 0x34, 0x00, 0x01, 0xff, 0x00 }; 534c87c5fbaSopenharmony_ci int result; 535c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 536c87c5fbaSopenharmony_ci 537c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_CON; 538c87c5fbaSopenharmony_ci pdu->code = COAP_RESPONSE_CODE(203); 539c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 540c87c5fbaSopenharmony_ci 541c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 542c87c5fbaSopenharmony_ci 543c87c5fbaSopenharmony_ci result = coap_add_token(pdu, 2, (const uint8_t *)"\x00\x01"); 544c87c5fbaSopenharmony_ci 545c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 546c87c5fbaSopenharmony_ci 547c87c5fbaSopenharmony_ci result = coap_add_data(pdu, 1, (const uint8_t *)"\0"); 548c87c5fbaSopenharmony_ci 549c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 550c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 4); 551c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == pdu->token + 3); 552c87c5fbaSopenharmony_ci 553c87c5fbaSopenharmony_ci CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); 554c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); 555c87c5fbaSopenharmony_ci} 556c87c5fbaSopenharmony_ci 557c87c5fbaSopenharmony_cistatic void 558c87c5fbaSopenharmony_cit_encode_pdu9(void) { 559c87c5fbaSopenharmony_ci /* PDU with options and data */ 560c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x60, 0x44, 0x12, 0x34, 0x48, 's', 'o', 'm', 561c87c5fbaSopenharmony_ci 'e', 'e', 't', 'a', 'g', 0x10, 0xdd, 0x11, 562c87c5fbaSopenharmony_ci 0x04, 's', 'o', 'm', 'e', 'r', 'a', 't', 563c87c5fbaSopenharmony_ci 'h', 'e', 'r', 'l', 'o', 'n', 'g', 'u', 564c87c5fbaSopenharmony_ci 'r', 'i', 0xff, 'd', 'a', 't', 'a' 565c87c5fbaSopenharmony_ci }; 566c87c5fbaSopenharmony_ci int result; 567c87c5fbaSopenharmony_ci 568c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 569c87c5fbaSopenharmony_ci 570c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_ACK; 571c87c5fbaSopenharmony_ci pdu->code = COAP_RESPONSE_CODE(204); 572c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 573c87c5fbaSopenharmony_ci 574c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 575c87c5fbaSopenharmony_ci 576c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_ETAG, 8, (const uint8_t *)"someetag"); 577c87c5fbaSopenharmony_ci 578c87c5fbaSopenharmony_ci CU_ASSERT(result == 9); 579c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 4); 580c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 9); 581c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 582c87c5fbaSopenharmony_ci 583c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_IF_NONE_MATCH, 0, NULL); 584c87c5fbaSopenharmony_ci 585c87c5fbaSopenharmony_ci CU_ASSERT(result == 1); 586c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 5); 587c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 10); 588c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 589c87c5fbaSopenharmony_ci 590c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_PROXY_URI, 591c87c5fbaSopenharmony_ci 17, (const uint8_t *)"someratherlonguri"); 592c87c5fbaSopenharmony_ci 593c87c5fbaSopenharmony_ci CU_ASSERT(result == 20); 594c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 35); 595c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 30); 596c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 597c87c5fbaSopenharmony_ci 598c87c5fbaSopenharmony_ci result = coap_add_data(pdu, 4, (const uint8_t *)"data"); 599c87c5fbaSopenharmony_ci 600c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 601c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 35); 602c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == pdu->token + 31); 603c87c5fbaSopenharmony_ci 604c87c5fbaSopenharmony_ci CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); 605c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); 606c87c5fbaSopenharmony_ci} 607c87c5fbaSopenharmony_ci 608c87c5fbaSopenharmony_cistatic void 609c87c5fbaSopenharmony_cit_encode_pdu10(void) { 610c87c5fbaSopenharmony_ci /* PDU with token, options and data */ 611c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x62, 0x44, 0x12, 0x34, 0x00, 0x00, 0x8d, 0xf2, 612c87c5fbaSopenharmony_ci 'c', 'o', 'a', 'p', ':', '/', '/', 'e', 613c87c5fbaSopenharmony_ci 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 614c87c5fbaSopenharmony_ci 'o', 'm', '/', '1', '2', '3', '4', '5', 615c87c5fbaSopenharmony_ci '/', '%', '3', 'F', 'x', 'y', 'z', '/', 616c87c5fbaSopenharmony_ci '3', '0', '4', '8', '2', '3', '4', '2', 617c87c5fbaSopenharmony_ci '3', '4', '/', '2', '3', '4', '0', '2', 618c87c5fbaSopenharmony_ci '3', '4', '8', '2', '3', '4', '/', '2', 619c87c5fbaSopenharmony_ci '3', '9', '0', '8', '4', '2', '3', '4', 620c87c5fbaSopenharmony_ci '-', '2', '3', '/', '%', 'A', 'B', '%', 621c87c5fbaSopenharmony_ci '3', '0', '%', 'a', 'f', '/', '+', '1', 622c87c5fbaSopenharmony_ci '2', '3', '/', 'h', 'f', 'k', 's', 'd', 623c87c5fbaSopenharmony_ci 'h', '/', '2', '3', '4', '8', '0', '-', 624c87c5fbaSopenharmony_ci '2', '3', '4', '-', '9', '8', '2', '3', 625c87c5fbaSopenharmony_ci '5', '/', '1', '2', '0', '4', '/', '2', 626c87c5fbaSopenharmony_ci '4', '3', '5', '4', '6', '3', '4', '5', 627c87c5fbaSopenharmony_ci '3', '4', '5', '2', '4', '3', '/', '0', 628c87c5fbaSopenharmony_ci '1', '9', '8', 's', 'd', 'n', '3', '-', 629c87c5fbaSopenharmony_ci 'a', '-', '3', '/', '/', '/', 'a', 'f', 630c87c5fbaSopenharmony_ci 'f', '0', '9', '3', '4', '/', '9', '7', 631c87c5fbaSopenharmony_ci 'u', '2', '1', '4', '1', '/', '0', '0', 632c87c5fbaSopenharmony_ci '0', '2', '/', '3', '9', '3', '2', '4', 633c87c5fbaSopenharmony_ci '2', '3', '5', '3', '2', '/', '5', '6', 634c87c5fbaSopenharmony_ci '2', '3', '4', '0', '2', '3', '/', '-', 635c87c5fbaSopenharmony_ci '-', '-', '-', '/', '=', '1', '2', '3', 636c87c5fbaSopenharmony_ci '4', '=', '/', '0', '9', '8', '1', '4', 637c87c5fbaSopenharmony_ci '1', '-', '9', '5', '6', '4', '6', '4', 638c87c5fbaSopenharmony_ci '3', '/', '2', '1', '9', '7', '0', '-', 639c87c5fbaSopenharmony_ci '-', '-', '-', '-', '/', '8', '2', '3', 640c87c5fbaSopenharmony_ci '6', '4', '9', '2', '3', '4', '7', '2', 641c87c5fbaSopenharmony_ci 'w', 'e', 'r', 'e', 'r', 'e', 'w', 'r', 642c87c5fbaSopenharmony_ci '0', '-', '9', '2', '1', '-', '3', '9', 643c87c5fbaSopenharmony_ci '1', '2', '3', '-', '3', '4', '/', 0x0d, 644c87c5fbaSopenharmony_ci 0x01, '/', '/', '4', '9', '2', '4', '0', 645c87c5fbaSopenharmony_ci '3', '-', '-', '0', '9', '8', '/', 0xc1, 646c87c5fbaSopenharmony_ci '*', 0xff, 'd', 'a', 't', 'a' 647c87c5fbaSopenharmony_ci }; 648c87c5fbaSopenharmony_ci int result; 649c87c5fbaSopenharmony_ci 650c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 651c87c5fbaSopenharmony_ci 652c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_ACK; 653c87c5fbaSopenharmony_ci pdu->code = COAP_RESPONSE_CODE(204); 654c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 655c87c5fbaSopenharmony_ci 656c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 657c87c5fbaSopenharmony_ci 658c87c5fbaSopenharmony_ci result = coap_add_token(pdu, 2, (const uint8_t *)"\0\0"); 659c87c5fbaSopenharmony_ci 660c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 661c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 255, 662c87c5fbaSopenharmony_ci (const uint8_t *) 663c87c5fbaSopenharmony_ci "coap://example.com/12345/%3Fxyz/3048234234/23402348234/239084234-23/%AB%30%af/+123/hfksdh/23480-234-98235/1204/243546345345243/0198sdn3-a-3///aff0934/97u2141/0002/3932423532/56234023/----/=1234=/098141-9564643/21970-----/82364923472wererewr0-921-39123-34/"); 664c87c5fbaSopenharmony_ci 665c87c5fbaSopenharmony_ci CU_ASSERT(result == 257); 666c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 8); 667c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 259); 668c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 669c87c5fbaSopenharmony_ci 670c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 14, 671c87c5fbaSopenharmony_ci (const uint8_t *)"//492403--098/"); 672c87c5fbaSopenharmony_ci 673c87c5fbaSopenharmony_ci CU_ASSERT(result == 16); 674c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 8); 675c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 275); 676c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 677c87c5fbaSopenharmony_ci 678c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_LOCATION_QUERY, 679c87c5fbaSopenharmony_ci 1, (const uint8_t *)"*"); 680c87c5fbaSopenharmony_ci 681c87c5fbaSopenharmony_ci CU_ASSERT(result == 2); 682c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 20); 683c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 277); 684c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 685c87c5fbaSopenharmony_ci 686c87c5fbaSopenharmony_ci result = coap_add_data(pdu, 4, (const uint8_t *)"data"); 687c87c5fbaSopenharmony_ci 688c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 689c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 282); 690c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == pdu->token + 278); 691c87c5fbaSopenharmony_ci 692c87c5fbaSopenharmony_ci CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); 693c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); 694c87c5fbaSopenharmony_ci} 695c87c5fbaSopenharmony_ci 696c87c5fbaSopenharmony_cistatic void 697c87c5fbaSopenharmony_cit_encode_pdu11(void) { 698c87c5fbaSopenharmony_ci coap_log_t level = coap_get_log_level(); 699c87c5fbaSopenharmony_ci /* data too long for PDU */ 700c87c5fbaSopenharmony_ci size_t old_max = pdu->max_size; 701c87c5fbaSopenharmony_ci int result; 702c87c5fbaSopenharmony_ci 703c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, 8); /* clear PDU, with small maximum */ 704c87c5fbaSopenharmony_ci 705c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 706c87c5fbaSopenharmony_ci coap_set_log_level(COAP_LOG_CRIT); 707c87c5fbaSopenharmony_ci result = coap_add_data(pdu, 10, (const uint8_t *)"0123456789"); 708c87c5fbaSopenharmony_ci coap_set_log_level(level); 709c87c5fbaSopenharmony_ci 710c87c5fbaSopenharmony_ci CU_ASSERT(result == 0); 711c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 712c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 713c87c5fbaSopenharmony_ci 714c87c5fbaSopenharmony_ci pdu->max_size = old_max; 715c87c5fbaSopenharmony_ci} 716c87c5fbaSopenharmony_ci 717c87c5fbaSopenharmony_cistatic void 718c87c5fbaSopenharmony_cit_encode_pdu12(void) { 719c87c5fbaSopenharmony_ci coap_optlist_t *optlist = NULL; 720c87c5fbaSopenharmony_ci int n; 721c87c5fbaSopenharmony_ci uint8_t opt_num[] = { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }; 722c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }; 723c87c5fbaSopenharmony_ci uint8_t opt_srt[] = { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }; 724c87c5fbaSopenharmony_ci coap_opt_iterator_t oi; 725c87c5fbaSopenharmony_ci coap_opt_t *option; 726c87c5fbaSopenharmony_ci 727c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 728c87c5fbaSopenharmony_ci 729c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 730c87c5fbaSopenharmony_ci 731c87c5fbaSopenharmony_ci for (n = 0; n < (int)sizeof(opt_num); n++) { 732c87c5fbaSopenharmony_ci coap_insert_optlist(&optlist, coap_new_optlist(opt_num[n], 733c87c5fbaSopenharmony_ci sizeof(opt_val[n]), &opt_val[n])); 734c87c5fbaSopenharmony_ci } 735c87c5fbaSopenharmony_ci coap_add_optlist_pdu(pdu, &optlist); 736c87c5fbaSopenharmony_ci 737c87c5fbaSopenharmony_ci /* Check options in pdu are in right order */ 738c87c5fbaSopenharmony_ci coap_option_iterator_init(pdu, &oi, COAP_OPT_ALL); 739c87c5fbaSopenharmony_ci for (n = 0; n < (int)sizeof(opt_num); n++) { 740c87c5fbaSopenharmony_ci option = coap_option_next(&oi); 741c87c5fbaSopenharmony_ci CU_ASSERT(oi.bad == 0); 742c87c5fbaSopenharmony_ci CU_ASSERT(option != NULL); 743c87c5fbaSopenharmony_ci CU_ASSERT(coap_opt_length(option) == 1); 744c87c5fbaSopenharmony_ci CU_ASSERT(*coap_opt_value(option) == opt_srt[n]); 745c87c5fbaSopenharmony_ci } 746c87c5fbaSopenharmony_ci option = coap_option_next(&oi); 747c87c5fbaSopenharmony_ci CU_ASSERT(oi.bad == 1); 748c87c5fbaSopenharmony_ci CU_ASSERT(option == NULL); 749c87c5fbaSopenharmony_ci coap_delete_optlist(optlist); 750c87c5fbaSopenharmony_ci} 751c87c5fbaSopenharmony_ci 752c87c5fbaSopenharmony_cistatic void 753c87c5fbaSopenharmony_cit_encode_pdu13(void) { 754c87c5fbaSopenharmony_ci coap_optlist_t *optlist = NULL; 755c87c5fbaSopenharmony_ci int n; 756c87c5fbaSopenharmony_ci uint8_t opt_num[] = { 59, 58, 57, 56, 55, 54, 53, 52, 51, 50 }; 757c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 59, 58, 57, 56, 55, 54, 53, 52, 51, 50 }; 758c87c5fbaSopenharmony_ci uint8_t opt_srt[] = { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }; 759c87c5fbaSopenharmony_ci coap_opt_iterator_t oi; 760c87c5fbaSopenharmony_ci coap_opt_t *option; 761c87c5fbaSopenharmony_ci 762c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 763c87c5fbaSopenharmony_ci 764c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 765c87c5fbaSopenharmony_ci 766c87c5fbaSopenharmony_ci for (n = 0; n < (int)sizeof(opt_num); n++) { 767c87c5fbaSopenharmony_ci coap_insert_optlist(&optlist, coap_new_optlist(opt_num[n], 768c87c5fbaSopenharmony_ci sizeof(opt_val[n]), &opt_val[n])); 769c87c5fbaSopenharmony_ci } 770c87c5fbaSopenharmony_ci coap_add_optlist_pdu(pdu, &optlist); 771c87c5fbaSopenharmony_ci 772c87c5fbaSopenharmony_ci /* Check options in pdu are in right order */ 773c87c5fbaSopenharmony_ci coap_option_iterator_init(pdu, &oi, COAP_OPT_ALL); 774c87c5fbaSopenharmony_ci for (n = 0; n < (int)sizeof(opt_num); n++) { 775c87c5fbaSopenharmony_ci option = coap_option_next(&oi); 776c87c5fbaSopenharmony_ci CU_ASSERT(oi.bad == 0); 777c87c5fbaSopenharmony_ci CU_ASSERT(option != NULL); 778c87c5fbaSopenharmony_ci CU_ASSERT(coap_opt_length(option) == 1); 779c87c5fbaSopenharmony_ci CU_ASSERT(*coap_opt_value(option) == opt_srt[n]); 780c87c5fbaSopenharmony_ci } 781c87c5fbaSopenharmony_ci option = coap_option_next(&oi); 782c87c5fbaSopenharmony_ci CU_ASSERT(oi.bad == 1); 783c87c5fbaSopenharmony_ci CU_ASSERT(option == NULL); 784c87c5fbaSopenharmony_ci coap_delete_optlist(optlist); 785c87c5fbaSopenharmony_ci} 786c87c5fbaSopenharmony_ci 787c87c5fbaSopenharmony_cistatic void 788c87c5fbaSopenharmony_cit_encode_pdu14(void) { 789c87c5fbaSopenharmony_ci coap_optlist_t *optlist = NULL; 790c87c5fbaSopenharmony_ci int n; 791c87c5fbaSopenharmony_ci uint8_t opt_num[] = { 53, 52, 51, 50, 51, 52, 52, 51, 50, 50 }; 792c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 59, 56, 53, 50, 54, 57, 58, 55, 51, 52 }; 793c87c5fbaSopenharmony_ci uint8_t opt_srt[] = { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }; 794c87c5fbaSopenharmony_ci coap_opt_iterator_t oi; 795c87c5fbaSopenharmony_ci coap_opt_t *option; 796c87c5fbaSopenharmony_ci 797c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 798c87c5fbaSopenharmony_ci 799c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 800c87c5fbaSopenharmony_ci 801c87c5fbaSopenharmony_ci for (n = 0; n < (int)sizeof(opt_num); n++) { 802c87c5fbaSopenharmony_ci coap_insert_optlist(&optlist, coap_new_optlist(opt_num[n], 803c87c5fbaSopenharmony_ci sizeof(opt_val[n]), &opt_val[n])); 804c87c5fbaSopenharmony_ci } 805c87c5fbaSopenharmony_ci coap_add_optlist_pdu(pdu, &optlist); 806c87c5fbaSopenharmony_ci 807c87c5fbaSopenharmony_ci /* Check options in pdu are in right order */ 808c87c5fbaSopenharmony_ci coap_option_iterator_init(pdu, &oi, COAP_OPT_ALL); 809c87c5fbaSopenharmony_ci for (n = 0; n < (int)sizeof(opt_num); n++) { 810c87c5fbaSopenharmony_ci option = coap_option_next(&oi); 811c87c5fbaSopenharmony_ci CU_ASSERT(oi.bad == 0); 812c87c5fbaSopenharmony_ci CU_ASSERT(option != NULL); 813c87c5fbaSopenharmony_ci CU_ASSERT(coap_opt_length(option) == 1); 814c87c5fbaSopenharmony_ci CU_ASSERT(*coap_opt_value(option) == opt_srt[n]); 815c87c5fbaSopenharmony_ci } 816c87c5fbaSopenharmony_ci option = coap_option_next(&oi); 817c87c5fbaSopenharmony_ci CU_ASSERT(oi.bad == 1); 818c87c5fbaSopenharmony_ci CU_ASSERT(option == NULL); 819c87c5fbaSopenharmony_ci coap_delete_optlist(optlist); 820c87c5fbaSopenharmony_ci} 821c87c5fbaSopenharmony_ci 822c87c5fbaSopenharmony_ci/* Check inserting options with random types get put into the PDU in the 823c87c5fbaSopenharmony_ci right order */ 824c87c5fbaSopenharmony_cistatic void 825c87c5fbaSopenharmony_cit_encode_pdu15(void) { 826c87c5fbaSopenharmony_ci size_t n; 827c87c5fbaSopenharmony_ci uint16_t opt_num[] = { 300, 13, 10, 7, 11, 268, 269, 12, 8, 9 }; 828c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 59, 56, 53, 50, 54, 57, 58, 55, 51, 52 }; 829c87c5fbaSopenharmony_ci uint8_t opt_srt[] = { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }; 830c87c5fbaSopenharmony_ci coap_opt_iterator_t oi; 831c87c5fbaSopenharmony_ci coap_opt_t *option; 832c87c5fbaSopenharmony_ci 833c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 834c87c5fbaSopenharmony_ci 835c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 836c87c5fbaSopenharmony_ci 837c87c5fbaSopenharmony_ci for (n = 0; n < (sizeof(opt_num)/sizeof(opt_num[0])); n++) { 838c87c5fbaSopenharmony_ci coap_insert_option(pdu, opt_num[n], 839c87c5fbaSopenharmony_ci sizeof(opt_val[n]), &opt_val[n]); 840c87c5fbaSopenharmony_ci } 841c87c5fbaSopenharmony_ci 842c87c5fbaSopenharmony_ci /* Check options in pdu are in right order */ 843c87c5fbaSopenharmony_ci coap_option_iterator_init(pdu, &oi, COAP_OPT_ALL); 844c87c5fbaSopenharmony_ci for (n = 0; n < (sizeof(opt_num)/sizeof(opt_num[0])); n++) { 845c87c5fbaSopenharmony_ci option = coap_option_next(&oi); 846c87c5fbaSopenharmony_ci CU_ASSERT(oi.bad == 0); 847c87c5fbaSopenharmony_ci CU_ASSERT(option != NULL); 848c87c5fbaSopenharmony_ci CU_ASSERT(coap_opt_length(option) == 1); 849c87c5fbaSopenharmony_ci CU_ASSERT(*coap_opt_value(option) == opt_srt[n]); 850c87c5fbaSopenharmony_ci } 851c87c5fbaSopenharmony_ci option = coap_option_next(&oi); 852c87c5fbaSopenharmony_ci CU_ASSERT(oi.bad == 1); 853c87c5fbaSopenharmony_ci CU_ASSERT(option == NULL); 854c87c5fbaSopenharmony_ci} 855c87c5fbaSopenharmony_ci 856c87c5fbaSopenharmony_ci/* Check changing value of options works */ 857c87c5fbaSopenharmony_cistatic void 858c87c5fbaSopenharmony_cit_encode_pdu16(void) { 859c87c5fbaSopenharmony_ci size_t n; 860c87c5fbaSopenharmony_ci uint16_t opt_num[] = { 300, 10, 7 }; 861c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 53, 51, 50 }; 862c87c5fbaSopenharmony_ci uint8_t data[] = { 'd', 'a', 't', 'a' }; 863c87c5fbaSopenharmony_ci uint8_t data1[] = { 0x71, 0x32, 0x31, 0x33, 0xe1, 0x00, 0x15, 0x35, 864c87c5fbaSopenharmony_ci 0xff, 0x64, 0x61, 0x74, 0x61 865c87c5fbaSopenharmony_ci }; 866c87c5fbaSopenharmony_ci uint8_t data2[] = { 0x71, 0x32, 0x33, 0x01, 0x23, 0x45, 0xe1, 0x00, 867c87c5fbaSopenharmony_ci 0x15, 0x35, 0xff, 0x64, 0x61, 0x74, 0x61 868c87c5fbaSopenharmony_ci }; 869c87c5fbaSopenharmony_ci uint8_t data3[] = { 0x70, 0x31, 0x33, 0xe1, 0x00, 0x15, 0x35, 0xff, 870c87c5fbaSopenharmony_ci 0x64, 0x61, 0x74, 0x61 871c87c5fbaSopenharmony_ci }; 872c87c5fbaSopenharmony_ci uint8_t data4[] = { 0x71, 0x32, 0x31, 0x33, 0xe4, 0x00, 0x15, 0x06, 873c87c5fbaSopenharmony_ci 0x54, 0x32, 0x10, 0xff, 0x64, 0x61, 0x74, 0x61 874c87c5fbaSopenharmony_ci }; 875c87c5fbaSopenharmony_ci int new_val; 876c87c5fbaSopenharmony_ci unsigned char buf[4]; 877c87c5fbaSopenharmony_ci 878c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 879c87c5fbaSopenharmony_ci 880c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 881c87c5fbaSopenharmony_ci 882c87c5fbaSopenharmony_ci for (n = 0; n < (sizeof(opt_num)/sizeof(opt_num[0])); n++) { 883c87c5fbaSopenharmony_ci coap_add_option(pdu, opt_num[n], 884c87c5fbaSopenharmony_ci sizeof(opt_val[n]), &opt_val[n]); 885c87c5fbaSopenharmony_ci } 886c87c5fbaSopenharmony_ci coap_add_data(pdu, sizeof(data), data); 887c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 888c87c5fbaSopenharmony_ci /* Now update an option in the middle */ 889c87c5fbaSopenharmony_ci new_val = 0x12345; 890c87c5fbaSopenharmony_ci coap_update_option(pdu, 10, 891c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 892c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data2, pdu->used_size) == 0); 893c87c5fbaSopenharmony_ci /* Shrink it back again */ 894c87c5fbaSopenharmony_ci new_val = 51; 895c87c5fbaSopenharmony_ci coap_update_option(pdu, 10, 896c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 897c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 898c87c5fbaSopenharmony_ci /* Now update an option at the start */ 899c87c5fbaSopenharmony_ci new_val = 0; 900c87c5fbaSopenharmony_ci coap_update_option(pdu, 7, 901c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 902c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data3, pdu->used_size) == 0); 903c87c5fbaSopenharmony_ci /* put it back again */ 904c87c5fbaSopenharmony_ci new_val = 50; 905c87c5fbaSopenharmony_ci coap_update_option(pdu, 7, 906c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 907c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 908c87c5fbaSopenharmony_ci /* Now update an option at the end */ 909c87c5fbaSopenharmony_ci new_val = 0x6543210; 910c87c5fbaSopenharmony_ci coap_update_option(pdu, 300, 911c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 912c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data4, pdu->used_size) == 0); 913c87c5fbaSopenharmony_ci /* put it back again */ 914c87c5fbaSopenharmony_ci new_val = 53; 915c87c5fbaSopenharmony_ci coap_update_option(pdu, 300, 916c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 917c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 918c87c5fbaSopenharmony_ci} 919c87c5fbaSopenharmony_ci 920c87c5fbaSopenharmony_ci/* Same as t_encode_pdu16, but without any data, but with a token */ 921c87c5fbaSopenharmony_cistatic void 922c87c5fbaSopenharmony_cit_encode_pdu17(void) { 923c87c5fbaSopenharmony_ci size_t n; 924c87c5fbaSopenharmony_ci uint8_t token[] = { 't' }; 925c87c5fbaSopenharmony_ci uint16_t opt_num[] = { 300, 10, 7 }; 926c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 53, 51, 50 }; 927c87c5fbaSopenharmony_ci uint8_t data1[] = { 0x74, 0x71, 0x32, 0x31, 0x33, 0xe1, 0x00, 0x15, 928c87c5fbaSopenharmony_ci 0x35 929c87c5fbaSopenharmony_ci }; 930c87c5fbaSopenharmony_ci uint8_t data2[] = { 0x74, 0x71, 0x32, 0x33, 0x01, 0x23, 0x45, 0xe1, 931c87c5fbaSopenharmony_ci 0x00, 0x15, 0x35 932c87c5fbaSopenharmony_ci }; 933c87c5fbaSopenharmony_ci uint8_t data3[] = { 0x74, 0x70, 0x31, 0x33, 0xe1, 0x00, 0x15, 0x35 }; 934c87c5fbaSopenharmony_ci uint8_t data4[] = { 0x74, 0x71, 0x32, 0x31, 0x33, 0xe4, 0x00, 0x15, 935c87c5fbaSopenharmony_ci 0x06, 0x54, 0x32, 0x10 936c87c5fbaSopenharmony_ci }; 937c87c5fbaSopenharmony_ci int new_val; 938c87c5fbaSopenharmony_ci unsigned char buf[4]; 939c87c5fbaSopenharmony_ci 940c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 941c87c5fbaSopenharmony_ci 942c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 943c87c5fbaSopenharmony_ci 944c87c5fbaSopenharmony_ci coap_add_token(pdu, sizeof(token), token); 945c87c5fbaSopenharmony_ci for (n = 0; n < (sizeof(opt_num)/sizeof(opt_num[0])); n++) { 946c87c5fbaSopenharmony_ci coap_add_option(pdu, opt_num[n], 947c87c5fbaSopenharmony_ci sizeof(opt_val[n]), &opt_val[n]); 948c87c5fbaSopenharmony_ci } 949c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 950c87c5fbaSopenharmony_ci /* Now update an option in the middle */ 951c87c5fbaSopenharmony_ci new_val = 0x12345; 952c87c5fbaSopenharmony_ci coap_update_option(pdu, 10, 953c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 954c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data2, pdu->used_size) == 0); 955c87c5fbaSopenharmony_ci /* Shrink it back again */ 956c87c5fbaSopenharmony_ci new_val = 51; 957c87c5fbaSopenharmony_ci coap_update_option(pdu, 10, 958c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 959c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 960c87c5fbaSopenharmony_ci /* Now update an option at the start */ 961c87c5fbaSopenharmony_ci new_val = 0; 962c87c5fbaSopenharmony_ci coap_update_option(pdu, 7, 963c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 964c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data3, pdu->used_size) == 0); 965c87c5fbaSopenharmony_ci /* put it back again */ 966c87c5fbaSopenharmony_ci new_val = 50; 967c87c5fbaSopenharmony_ci coap_update_option(pdu, 7, 968c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 969c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 970c87c5fbaSopenharmony_ci /* Now update an option at the end */ 971c87c5fbaSopenharmony_ci new_val = 0x6543210; 972c87c5fbaSopenharmony_ci coap_update_option(pdu, 300, 973c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 974c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data4, pdu->used_size) == 0); 975c87c5fbaSopenharmony_ci /* put it back again */ 976c87c5fbaSopenharmony_ci new_val = 53; 977c87c5fbaSopenharmony_ci coap_update_option(pdu, 300, 978c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 979c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 980c87c5fbaSopenharmony_ci} 981c87c5fbaSopenharmony_ci 982c87c5fbaSopenharmony_cistatic void 983c87c5fbaSopenharmony_cit_encode_pdu18(void) { 984c87c5fbaSopenharmony_ci /* PDU with token, options and data */ 985c87c5fbaSopenharmony_ci uint8_t teststr[] = { 0x62, 0x44, 0x12, 0x34, 0x00, 0x00, 0x8d, 0xf2, 986c87c5fbaSopenharmony_ci 'c', 'o', 'a', 'p', ':', '/', '/', 'e', 987c87c5fbaSopenharmony_ci 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 988c87c5fbaSopenharmony_ci 'o', 'm', '/', '1', '2', '3', '4', '5', 989c87c5fbaSopenharmony_ci '/', '%', '3', 'F', 'x', 'y', 'z', '/', 990c87c5fbaSopenharmony_ci '3', '0', '4', '8', '2', '3', '4', '2', 991c87c5fbaSopenharmony_ci '3', '4', '/', '2', '3', '4', '0', '2', 992c87c5fbaSopenharmony_ci '3', '4', '8', '2', '3', '4', '/', '2', 993c87c5fbaSopenharmony_ci '3', '9', '0', '8', '4', '2', '3', '4', 994c87c5fbaSopenharmony_ci '-', '2', '3', '/', '%', 'A', 'B', '%', 995c87c5fbaSopenharmony_ci '3', '0', '%', 'a', 'f', '/', '+', '1', 996c87c5fbaSopenharmony_ci '2', '3', '/', 'h', 'f', 'k', 's', 'd', 997c87c5fbaSopenharmony_ci 'h', '/', '2', '3', '4', '8', '0', '-', 998c87c5fbaSopenharmony_ci '2', '3', '4', '-', '9', '8', '2', '3', 999c87c5fbaSopenharmony_ci '5', '/', '1', '2', '0', '4', '/', '2', 1000c87c5fbaSopenharmony_ci '4', '3', '5', '4', '6', '3', '4', '5', 1001c87c5fbaSopenharmony_ci '3', '4', '5', '2', '4', '3', '/', '0', 1002c87c5fbaSopenharmony_ci '1', '9', '8', 's', 'd', 'n', '3', '-', 1003c87c5fbaSopenharmony_ci 'a', '-', '3', '/', '/', '/', 'a', 'f', 1004c87c5fbaSopenharmony_ci 'f', '0', '9', '3', '4', '/', '9', '7', 1005c87c5fbaSopenharmony_ci 'u', '2', '1', '4', '1', '/', '0', '0', 1006c87c5fbaSopenharmony_ci '0', '2', '/', '3', '9', '3', '2', '4', 1007c87c5fbaSopenharmony_ci '2', '3', '5', '3', '2', '/', '5', '6', 1008c87c5fbaSopenharmony_ci '2', '3', '4', '0', '2', '3', '/', '-', 1009c87c5fbaSopenharmony_ci '-', '-', '-', '/', '=', '1', '2', '3', 1010c87c5fbaSopenharmony_ci '4', '=', '/', '0', '9', '8', '1', '4', 1011c87c5fbaSopenharmony_ci '1', '-', '9', '5', '6', '4', '6', '4', 1012c87c5fbaSopenharmony_ci '3', '/', '2', '1', '9', '7', '0', '-', 1013c87c5fbaSopenharmony_ci '-', '-', '-', '-', '/', '8', '2', '3', 1014c87c5fbaSopenharmony_ci '6', '4', '9', '2', '3', '4', '7', '2', 1015c87c5fbaSopenharmony_ci 'w', 'e', 'r', 'e', 'r', 'e', 'w', 'r', 1016c87c5fbaSopenharmony_ci '0', '-', '9', '2', '1', '-', '3', '9', 1017c87c5fbaSopenharmony_ci '1', '2', '3', '-', '3', '4', '/', 0x0d, 1018c87c5fbaSopenharmony_ci 0x01, '/', '/', '4', '9', '2', '4', '0', 1019c87c5fbaSopenharmony_ci '3', '-', '-', '0', '9', '8', '/', 0xc1, 1020c87c5fbaSopenharmony_ci '*', 0xff, 'd', 'a', 't', 'a' 1021c87c5fbaSopenharmony_ci }; 1022c87c5fbaSopenharmony_ci int result; 1023c87c5fbaSopenharmony_ci 1024c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 1025c87c5fbaSopenharmony_ci 1026c87c5fbaSopenharmony_ci pdu->type = COAP_MESSAGE_ACK; 1027c87c5fbaSopenharmony_ci pdu->code = COAP_RESPONSE_CODE(204); 1028c87c5fbaSopenharmony_ci pdu->mid = 0x1234; 1029c87c5fbaSopenharmony_ci 1030c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 0); 1031c87c5fbaSopenharmony_ci 1032c87c5fbaSopenharmony_ci result = coap_add_token(pdu, 2, (const uint8_t *)"\0\0"); 1033c87c5fbaSopenharmony_ci 1034c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 1035c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 255, 1036c87c5fbaSopenharmony_ci (const uint8_t *) 1037c87c5fbaSopenharmony_ci "coap://example.com/12345/%3Fxyz/3048234234/23402348234/239084234-23/%AB%30%af/+123/hfksdh/23480-234-98235/1204/243546345345243/0198sdn3-a-3///aff0934/97u2141/0002/3932423532/56234023/----/=1234=/098141-9564643/21970-----/82364923472wererewr0-921-39123-34/"); 1038c87c5fbaSopenharmony_ci 1039c87c5fbaSopenharmony_ci CU_ASSERT(result == 257); 1040c87c5fbaSopenharmony_ci CU_ASSERT(pdu->max_opt == 8); 1041c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 259); 1042c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 1043c87c5fbaSopenharmony_ci 1044c87c5fbaSopenharmony_ci result = coap_add_option(pdu, COAP_OPTION_LOCATION_QUERY, 1045c87c5fbaSopenharmony_ci 1, (const uint8_t *)"*"); 1046c87c5fbaSopenharmony_ci 1047c87c5fbaSopenharmony_ci CU_ASSERT(result == 2); 1048c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 261); 1049c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 1050c87c5fbaSopenharmony_ci 1051c87c5fbaSopenharmony_ci result = coap_insert_option(pdu, COAP_OPTION_LOCATION_PATH, 14, 1052c87c5fbaSopenharmony_ci (const uint8_t *)"//492403--098/"); 1053c87c5fbaSopenharmony_ci 1054c87c5fbaSopenharmony_ci CU_ASSERT(result == 16); 1055c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 277); 1056c87c5fbaSopenharmony_ci CU_ASSERT_PTR_NULL(pdu->data); 1057c87c5fbaSopenharmony_ci 1058c87c5fbaSopenharmony_ci result = coap_add_data(pdu, 4, (const uint8_t *)"data"); 1059c87c5fbaSopenharmony_ci 1060c87c5fbaSopenharmony_ci CU_ASSERT(result > 0); 1061c87c5fbaSopenharmony_ci CU_ASSERT(pdu->used_size == 282); 1062c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == pdu->token + 278); 1063c87c5fbaSopenharmony_ci 1064c87c5fbaSopenharmony_ci CU_ASSERT(coap_pdu_encode_header(pdu, COAP_PROTO_UDP) == 4); 1065c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token - 4, teststr, sizeof(teststr)) == 0); 1066c87c5fbaSopenharmony_ci 1067c87c5fbaSopenharmony_ci} 1068c87c5fbaSopenharmony_ci 1069c87c5fbaSopenharmony_ci/* Remove an option (no data) */ 1070c87c5fbaSopenharmony_ci/* 1071c87c5fbaSopenharmony_ci * Next Delta New Delta 1072c87c5fbaSopenharmony_ci * 4 18 1073c87c5fbaSopenharmony_ci * 18 25 1074c87c5fbaSopenharmony_ci */ 1075c87c5fbaSopenharmony_cistatic void 1076c87c5fbaSopenharmony_cit_encode_pdu19(void) { 1077c87c5fbaSopenharmony_ci size_t n; 1078c87c5fbaSopenharmony_ci uint8_t token[] = { 't' }; 1079c87c5fbaSopenharmony_ci uint16_t opt_num[] = { 300, 7, 21, 25 }; 1080c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 54, 50, 52, 53 }; 1081c87c5fbaSopenharmony_ci uint8_t data1[] = { 0x74, 0x71, 0x32, 0xd1, 0x01, 0x34, 0x41, 0x35, 1082c87c5fbaSopenharmony_ci 0xe1, 0x00, 0x06, 0x36 1083c87c5fbaSopenharmony_ci }; 1084c87c5fbaSopenharmony_ci uint8_t data2[] = { 0x74, 0x71, 0x32, 0xd1, 0x05, 0x35, 0xe1, 0x00, 1085c87c5fbaSopenharmony_ci 0x06, 0x36 1086c87c5fbaSopenharmony_ci }; 1087c87c5fbaSopenharmony_ci uint8_t data3[] = { 0x74, 0xd1, 0x0c, 0x35, 0xe1, 0x00, 0x06, 0x36 }; 1088c87c5fbaSopenharmony_ci uint8_t data4[] = { 0x74, 0xd1, 0x0c, 0x35 }; 1089c87c5fbaSopenharmony_ci uint8_t data5[] = { 0x74 }; 1090c87c5fbaSopenharmony_ci uint8_t data6[] = { 0x74, 0xd1, 0x0c, 0x0a }; 1091c87c5fbaSopenharmony_ci int new_val; 1092c87c5fbaSopenharmony_ci unsigned char buf[4]; 1093c87c5fbaSopenharmony_ci 1094c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 1095c87c5fbaSopenharmony_ci 1096c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 1097c87c5fbaSopenharmony_ci 1098c87c5fbaSopenharmony_ci coap_add_token(pdu, sizeof(token), token); 1099c87c5fbaSopenharmony_ci for (n = 0; n < (sizeof(opt_num)/sizeof(opt_num[0])); n++) { 1100c87c5fbaSopenharmony_ci coap_add_option(pdu, opt_num[n], 1101c87c5fbaSopenharmony_ci sizeof(opt_val[n]), &opt_val[n]); 1102c87c5fbaSopenharmony_ci } 1103c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 1104c87c5fbaSopenharmony_ci 1105c87c5fbaSopenharmony_ci /* Now remove an option in the middle */ 1106c87c5fbaSopenharmony_ci coap_remove_option(pdu, 21); 1107c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data2, pdu->used_size) == 0); 1108c87c5fbaSopenharmony_ci 1109c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1110c87c5fbaSopenharmony_ci coap_remove_option(pdu, 7); 1111c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data3, pdu->used_size) == 0); 1112c87c5fbaSopenharmony_ci 1113c87c5fbaSopenharmony_ci /* Now remove an option from the end */ 1114c87c5fbaSopenharmony_ci coap_remove_option(pdu, 300); 1115c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data4, pdu->used_size) == 0); 1116c87c5fbaSopenharmony_ci 1117c87c5fbaSopenharmony_ci /* Now remove the final option */ 1118c87c5fbaSopenharmony_ci coap_remove_option(pdu, 25); 1119c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data5, pdu->used_size) == 0); 1120c87c5fbaSopenharmony_ci 1121c87c5fbaSopenharmony_ci /* Now insert an option */ 1122c87c5fbaSopenharmony_ci new_val = 10; 1123c87c5fbaSopenharmony_ci coap_update_option(pdu, 25, 1124c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 1125c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data6, pdu->used_size) == 0); 1126c87c5fbaSopenharmony_ci} 1127c87c5fbaSopenharmony_ci 1128c87c5fbaSopenharmony_ci/* Remove an option (no data), but exercise delta boundaries (13 and 269) */ 1129c87c5fbaSopenharmony_ci/* 1130c87c5fbaSopenharmony_ci * Next Delta New Delta 1131c87c5fbaSopenharmony_ci * 11 12 if (opt_delta < 13) { 1132c87c5fbaSopenharmony_ci * 1 13 } else if (opt_delta < 269 && decode_next.delta < 13) { 1133c87c5fbaSopenharmony_ci * 254 268 } else if (opt_delta < 269) { 1134c87c5fbaSopenharmony_ci * 1 269 } else if (decode_next.delta < 13) { // opt_delta >= 269 1135c87c5fbaSopenharmony_ci * 71 350 } else if (decode_next.delta < 269) { // opt_delta >= 269 1136c87c5fbaSopenharmony_ci * 320 670 } else { // decode_next.delta >= 269 && opt_delta >= 269 1137c87c5fbaSopenharmony_ci */ 1138c87c5fbaSopenharmony_cistatic void 1139c87c5fbaSopenharmony_cit_encode_pdu20(void) { 1140c87c5fbaSopenharmony_ci size_t n; 1141c87c5fbaSopenharmony_ci uint8_t token[] = { 't' }; 1142c87c5fbaSopenharmony_ci uint16_t opt_num[] = { 1, 12, 13, 268, 269, 350, 670 }; 1143c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 50, 51, 52, 53, 54, 55, 56 }; 1144c87c5fbaSopenharmony_ci uint8_t data1[] = { 0x74, 0x11, 0x32, 0xb1, 0x33, 0x11, 0x34, 0xd1, 1145c87c5fbaSopenharmony_ci 0xf2, 0x35, 0x11, 0x36, 0xd1, 0x44, 0x37, 0xe1, 1146c87c5fbaSopenharmony_ci 0x00, 0x33, 0x38 1147c87c5fbaSopenharmony_ci }; 1148c87c5fbaSopenharmony_ci uint8_t data2[] = { 0x74, 0xc1, 0x33, 0x11, 0x34, 0xd1, 0xf2, 0x35, 1149c87c5fbaSopenharmony_ci 0x11, 0x36, 0xd1, 0x44, 0x37, 0xe1, 0x00, 0x33, 1150c87c5fbaSopenharmony_ci 0x38 1151c87c5fbaSopenharmony_ci }; 1152c87c5fbaSopenharmony_ci uint8_t data3[] = { 0x74, 0xd1, 0x00, 0x34, 0xd1, 0xf2, 0x35, 0x11, 1153c87c5fbaSopenharmony_ci 0x36, 0xd1, 0x44, 0x37, 0xe1, 0x00, 0x33, 0x38 1154c87c5fbaSopenharmony_ci }; 1155c87c5fbaSopenharmony_ci uint8_t data4[] = { 0x74, 0xd1, 0xff, 0x35, 0x11, 0x36, 0xd1, 0x44, 1156c87c5fbaSopenharmony_ci 0x37, 0xe1, 0x00, 0x33, 0x38 1157c87c5fbaSopenharmony_ci }; 1158c87c5fbaSopenharmony_ci uint8_t data5[] = { 0x74, 0xe1, 0x00, 0x00, 0x36, 0xd1, 0x44, 0x37, 1159c87c5fbaSopenharmony_ci 0xe1, 0x00, 0x33, 0x38 1160c87c5fbaSopenharmony_ci }; 1161c87c5fbaSopenharmony_ci uint8_t data6[] = { 0x74, 0xe1, 0x00, 0x51, 0x37, 0xe1, 0x00, 0x33, 1162c87c5fbaSopenharmony_ci 0x38 1163c87c5fbaSopenharmony_ci }; 1164c87c5fbaSopenharmony_ci uint8_t data7[] = { 0x74, 0xe1, 0x01, 0x91, 0x38 }; 1165c87c5fbaSopenharmony_ci uint8_t data8[] = { 0x74 }; 1166c87c5fbaSopenharmony_ci 1167c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 1168c87c5fbaSopenharmony_ci 1169c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 1170c87c5fbaSopenharmony_ci 1171c87c5fbaSopenharmony_ci coap_add_token(pdu, sizeof(token), token); 1172c87c5fbaSopenharmony_ci /* Put the options in in reverse order to test that logic */ 1173c87c5fbaSopenharmony_ci for (n = sizeof(opt_num)/sizeof(opt_num[0]); n > 0; n--) { 1174c87c5fbaSopenharmony_ci coap_insert_option(pdu, opt_num[n-1], 1175c87c5fbaSopenharmony_ci sizeof(opt_val[n-1]), &opt_val[n-1]); 1176c87c5fbaSopenharmony_ci } 1177c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 1178c87c5fbaSopenharmony_ci 1179c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1180c87c5fbaSopenharmony_ci coap_remove_option(pdu, 1); 1181c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data2, pdu->used_size) == 0); 1182c87c5fbaSopenharmony_ci 1183c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1184c87c5fbaSopenharmony_ci coap_remove_option(pdu, 12); 1185c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data3, pdu->used_size) == 0); 1186c87c5fbaSopenharmony_ci 1187c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1188c87c5fbaSopenharmony_ci coap_remove_option(pdu, 13); 1189c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data4, pdu->used_size) == 0); 1190c87c5fbaSopenharmony_ci 1191c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1192c87c5fbaSopenharmony_ci coap_remove_option(pdu, 268); 1193c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data5, pdu->used_size) == 0); 1194c87c5fbaSopenharmony_ci 1195c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1196c87c5fbaSopenharmony_ci coap_remove_option(pdu, 269); 1197c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data6, pdu->used_size) == 0); 1198c87c5fbaSopenharmony_ci 1199c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1200c87c5fbaSopenharmony_ci coap_remove_option(pdu, 350); 1201c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data7, pdu->used_size) == 0); 1202c87c5fbaSopenharmony_ci 1203c87c5fbaSopenharmony_ci /* Now remove the final option */ 1204c87c5fbaSopenharmony_ci coap_remove_option(pdu, 670); 1205c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data8, pdu->used_size) == 0); 1206c87c5fbaSopenharmony_ci} 1207c87c5fbaSopenharmony_ci 1208c87c5fbaSopenharmony_ci/* Remove an option (with data) */ 1209c87c5fbaSopenharmony_ci/* 1210c87c5fbaSopenharmony_ci * Next Delta New Delta 1211c87c5fbaSopenharmony_ci * 4 18 1212c87c5fbaSopenharmony_ci * 18 25 1213c87c5fbaSopenharmony_ci */ 1214c87c5fbaSopenharmony_cistatic void 1215c87c5fbaSopenharmony_cit_encode_pdu21(void) { 1216c87c5fbaSopenharmony_ci size_t n; 1217c87c5fbaSopenharmony_ci uint8_t token[] = { 't' }; 1218c87c5fbaSopenharmony_ci uint16_t opt_num[] = { 300, 7, 21, 25 }; 1219c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 54, 50, 52, 53 }; 1220c87c5fbaSopenharmony_ci uint8_t data[] = { 'd', 'a', 't', 'a' }; 1221c87c5fbaSopenharmony_ci uint8_t data1[] = { 0x74, 0x71, 0x32, 0xd1, 0x01, 0x34, 0x41, 0x35, 1222c87c5fbaSopenharmony_ci 0xe1, 0x00, 0x06, 0x36, 0xff, 0x64, 0x61, 0x74, 1223c87c5fbaSopenharmony_ci 0x61 1224c87c5fbaSopenharmony_ci }; 1225c87c5fbaSopenharmony_ci uint8_t data2[] = { 0x74, 0x71, 0x32, 0xd1, 0x05, 0x35, 0xe1, 0x00, 1226c87c5fbaSopenharmony_ci 0x06, 0x36, 0xff, 0x64, 0x61, 0x74, 0x61 1227c87c5fbaSopenharmony_ci }; 1228c87c5fbaSopenharmony_ci uint8_t data3[] = { 0x74, 0xd1, 0x0c, 0x35, 0xe1, 0x00, 0x06, 0x36, 1229c87c5fbaSopenharmony_ci 0xff, 0x64, 0x61, 0x74, 0x61 1230c87c5fbaSopenharmony_ci }; 1231c87c5fbaSopenharmony_ci uint8_t data4[] = { 0x74, 0xd1, 0x0c, 0x35, 0xff, 0x64, 0x61, 0x74, 1232c87c5fbaSopenharmony_ci 0x61 1233c87c5fbaSopenharmony_ci }; 1234c87c5fbaSopenharmony_ci uint8_t data5[] = { 0x74, 0xff, 0x64, 0x61, 0x74, 0x61 }; 1235c87c5fbaSopenharmony_ci uint8_t data6[] = { 0x74, 0xd1, 0x0c, 0x0a, 0xff, 0x64, 0x61, 0x74, 1236c87c5fbaSopenharmony_ci 0x61 1237c87c5fbaSopenharmony_ci }; 1238c87c5fbaSopenharmony_ci int new_val; 1239c87c5fbaSopenharmony_ci unsigned char buf[4]; 1240c87c5fbaSopenharmony_ci 1241c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 1242c87c5fbaSopenharmony_ci 1243c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 1244c87c5fbaSopenharmony_ci 1245c87c5fbaSopenharmony_ci coap_add_token(pdu, sizeof(token), token); 1246c87c5fbaSopenharmony_ci for (n = 0; n < (sizeof(opt_num)/sizeof(opt_num[0])); n++) { 1247c87c5fbaSopenharmony_ci coap_add_option(pdu, opt_num[n], 1248c87c5fbaSopenharmony_ci sizeof(opt_val[n]), &opt_val[n]); 1249c87c5fbaSopenharmony_ci } 1250c87c5fbaSopenharmony_ci coap_add_data(pdu, sizeof(data), data); 1251c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 1252c87c5fbaSopenharmony_ci 1253c87c5fbaSopenharmony_ci /* Now remove an option in the middle */ 1254c87c5fbaSopenharmony_ci coap_remove_option(pdu, 21); 1255c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data2, pdu->used_size) == 0); 1256c87c5fbaSopenharmony_ci 1257c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1258c87c5fbaSopenharmony_ci coap_remove_option(pdu, 7); 1259c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data3, pdu->used_size) == 0); 1260c87c5fbaSopenharmony_ci 1261c87c5fbaSopenharmony_ci /* Now remove an option from the end */ 1262c87c5fbaSopenharmony_ci coap_remove_option(pdu, 300); 1263c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data4, pdu->used_size) == 0); 1264c87c5fbaSopenharmony_ci 1265c87c5fbaSopenharmony_ci /* Now remove the final option */ 1266c87c5fbaSopenharmony_ci coap_remove_option(pdu, 25); 1267c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data5, pdu->used_size) == 0); 1268c87c5fbaSopenharmony_ci 1269c87c5fbaSopenharmony_ci /* Now insert an option */ 1270c87c5fbaSopenharmony_ci new_val = 10; 1271c87c5fbaSopenharmony_ci coap_update_option(pdu, 25, 1272c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), new_val), buf); 1273c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data6, pdu->used_size) == 0); 1274c87c5fbaSopenharmony_ci} 1275c87c5fbaSopenharmony_ci 1276c87c5fbaSopenharmony_ci/* Remove an option (with data), but exercise delta boundaries (13 and 269) */ 1277c87c5fbaSopenharmony_ci/* 1278c87c5fbaSopenharmony_ci * Next Delta New Delta 1279c87c5fbaSopenharmony_ci * 11 12 if (opt_delta < 13) { 1280c87c5fbaSopenharmony_ci * 1 13 } else if (opt_delta < 269 && decode_next.delta < 13) { 1281c87c5fbaSopenharmony_ci * 254 268 } else if (opt_delta < 269) { 1282c87c5fbaSopenharmony_ci * 1 269 } else if (decode_next.delta < 13) { // opt_delta >= 269 1283c87c5fbaSopenharmony_ci * 71 350 } else if (decode_next.delta < 269) { // opt_delta >= 269 1284c87c5fbaSopenharmony_ci * 320 670 } else { // decode_next.delta >= 269 && opt_delta >= 269 1285c87c5fbaSopenharmony_ci */ 1286c87c5fbaSopenharmony_cistatic void 1287c87c5fbaSopenharmony_cit_encode_pdu22(void) { 1288c87c5fbaSopenharmony_ci size_t n; 1289c87c5fbaSopenharmony_ci uint8_t token[] = { 't' }; 1290c87c5fbaSopenharmony_ci uint16_t opt_num[] = { 1, 12, 13, 268, 269, 350, 670 }; 1291c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 50, 51, 52, 53, 54, 55, 56 }; 1292c87c5fbaSopenharmony_ci uint8_t data[] = { 'd', 'a', 't', 'a' }; 1293c87c5fbaSopenharmony_ci uint8_t data1[] = { 0x74, 0x11, 0x32, 0xb1, 0x33, 0x11, 0x34, 0xd1, 1294c87c5fbaSopenharmony_ci 0xf2, 0x35, 0x11, 0x36, 0xd1, 0x44, 0x37, 0xe1, 1295c87c5fbaSopenharmony_ci 0x00, 0x33, 0x38, 0xff, 0x64, 0x61, 0x74, 0x61 1296c87c5fbaSopenharmony_ci }; 1297c87c5fbaSopenharmony_ci uint8_t data2[] = { 0x74, 0xc1, 0x33, 0x11, 0x34, 0xd1, 0xf2, 0x35, 1298c87c5fbaSopenharmony_ci 0x11, 0x36, 0xd1, 0x44, 0x37, 0xe1, 0x00, 0x33, 1299c87c5fbaSopenharmony_ci 0x38, 0xff, 0x64, 0x61, 0x74, 0x61 1300c87c5fbaSopenharmony_ci }; 1301c87c5fbaSopenharmony_ci uint8_t data3[] = { 0x74, 0xd1, 0x00, 0x34, 0xd1, 0xf2, 0x35, 0x11, 1302c87c5fbaSopenharmony_ci 0x36, 0xd1, 0x44, 0x37, 0xe1, 0x00, 0x33, 0x38, 1303c87c5fbaSopenharmony_ci 0xff, 0x64, 0x61, 0x74, 0x61 1304c87c5fbaSopenharmony_ci }; 1305c87c5fbaSopenharmony_ci uint8_t data4[] = { 0x74, 0xd1, 0xff, 0x35, 0x11, 0x36, 0xd1, 0x44, 1306c87c5fbaSopenharmony_ci 0x37, 0xe1, 0x00, 0x33, 0x38, 0xff, 0x64, 0x61, 1307c87c5fbaSopenharmony_ci 0x74, 0x61 1308c87c5fbaSopenharmony_ci }; 1309c87c5fbaSopenharmony_ci uint8_t data5[] = { 0x74, 0xe1, 0x00, 0x00, 0x36, 0xd1, 0x44, 0x37, 1310c87c5fbaSopenharmony_ci 0xe1, 0x00, 0x33, 0x38, 0xff, 0x64, 0x61, 0x74, 1311c87c5fbaSopenharmony_ci 0x61 1312c87c5fbaSopenharmony_ci }; 1313c87c5fbaSopenharmony_ci uint8_t data6[] = { 0x74, 0xe1, 0x00, 0x51, 0x37, 0xe1, 0x00, 0x33, 1314c87c5fbaSopenharmony_ci 0x38, 0xff, 0x64, 0x61, 0x74, 0x61 1315c87c5fbaSopenharmony_ci }; 1316c87c5fbaSopenharmony_ci uint8_t data7[] = { 0x74, 0xe1, 0x01, 0x91, 0x38, 0xff, 0x64, 0x61, 1317c87c5fbaSopenharmony_ci 0x74, 0x61 1318c87c5fbaSopenharmony_ci }; 1319c87c5fbaSopenharmony_ci uint8_t data8[] = { 0x74, 0xff, 0x64, 0x61, 0x74, 0x61 }; 1320c87c5fbaSopenharmony_ci 1321c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 1322c87c5fbaSopenharmony_ci 1323c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 1324c87c5fbaSopenharmony_ci 1325c87c5fbaSopenharmony_ci coap_add_token(pdu, sizeof(token), token); 1326c87c5fbaSopenharmony_ci 1327c87c5fbaSopenharmony_ci coap_add_data(pdu, sizeof(data), data); 1328c87c5fbaSopenharmony_ci 1329c87c5fbaSopenharmony_ci /* Put the options in in reverse order to test that logic */ 1330c87c5fbaSopenharmony_ci for (n = sizeof(opt_num)/sizeof(opt_num[0]); n > 0; n--) { 1331c87c5fbaSopenharmony_ci coap_insert_option(pdu, opt_num[n-1], 1332c87c5fbaSopenharmony_ci sizeof(opt_val[n-1]), &opt_val[n-1]); 1333c87c5fbaSopenharmony_ci } 1334c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 1335c87c5fbaSopenharmony_ci 1336c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1337c87c5fbaSopenharmony_ci coap_remove_option(pdu, 1); 1338c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data2, pdu->used_size) == 0); 1339c87c5fbaSopenharmony_ci 1340c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1341c87c5fbaSopenharmony_ci coap_remove_option(pdu, 12); 1342c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data3, pdu->used_size) == 0); 1343c87c5fbaSopenharmony_ci 1344c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1345c87c5fbaSopenharmony_ci coap_remove_option(pdu, 13); 1346c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data4, pdu->used_size) == 0); 1347c87c5fbaSopenharmony_ci 1348c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1349c87c5fbaSopenharmony_ci coap_remove_option(pdu, 268); 1350c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data5, pdu->used_size) == 0); 1351c87c5fbaSopenharmony_ci 1352c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1353c87c5fbaSopenharmony_ci coap_remove_option(pdu, 269); 1354c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data6, pdu->used_size) == 0); 1355c87c5fbaSopenharmony_ci 1356c87c5fbaSopenharmony_ci /* Now remove an option from the start */ 1357c87c5fbaSopenharmony_ci coap_remove_option(pdu, 350); 1358c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data7, pdu->used_size) == 0); 1359c87c5fbaSopenharmony_ci 1360c87c5fbaSopenharmony_ci /* Now remove the final option */ 1361c87c5fbaSopenharmony_ci coap_remove_option(pdu, 670); 1362c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data8, pdu->used_size) == 0); 1363c87c5fbaSopenharmony_ci} 1364c87c5fbaSopenharmony_ci 1365c87c5fbaSopenharmony_ci 1366c87c5fbaSopenharmony_ci/* Update token */ 1367c87c5fbaSopenharmony_cistatic void 1368c87c5fbaSopenharmony_cit_encode_pdu23(void) { 1369c87c5fbaSopenharmony_ci size_t n; 1370c87c5fbaSopenharmony_ci uint8_t token[] = { 't' }; 1371c87c5fbaSopenharmony_ci uint8_t new_token[] = { 't', 'o', 'k', 'e', 'n' }; 1372c87c5fbaSopenharmony_ci uint16_t opt_num[] = { 300, 10, 7, 21, 25 }; 1373c87c5fbaSopenharmony_ci uint8_t opt_val[] = { 54, 51, 50, 52, 53 }; 1374c87c5fbaSopenharmony_ci uint8_t data[] = { 'd', 'a', 't', 'a' }; 1375c87c5fbaSopenharmony_ci uint8_t data1[] = { 0x74, 0x71, 0x32, 0x31, 0x33, 0xb1, 0x34, 0x41, 1376c87c5fbaSopenharmony_ci 0x35, 0xe1, 0x00, 0x06, 0x36, 0xff, 0x64, 0x61, 1377c87c5fbaSopenharmony_ci 0x74, 0x61 1378c87c5fbaSopenharmony_ci }; 1379c87c5fbaSopenharmony_ci uint8_t data2[] = { 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x71, 0x32, 0x31, 1380c87c5fbaSopenharmony_ci 0x33, 0xb1, 0x34, 0x41, 0x35, 0xe1, 0x00, 0x06, 1381c87c5fbaSopenharmony_ci 0x36, 0xff, 0x64, 0x61, 0x74, 0x61 1382c87c5fbaSopenharmony_ci }; 1383c87c5fbaSopenharmony_ci uint8_t data3[] = { 0x71, 0x32, 0x31, 0x33, 0xb1, 0x34, 0x41, 0x35, 1384c87c5fbaSopenharmony_ci 0xe1, 0x00, 0x06, 0x36, 0xff, 0x64, 0x61, 0x74, 1385c87c5fbaSopenharmony_ci 0x61 1386c87c5fbaSopenharmony_ci }; 1387c87c5fbaSopenharmony_ci 1388c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 1389c87c5fbaSopenharmony_ci 1390c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 1391c87c5fbaSopenharmony_ci 1392c87c5fbaSopenharmony_ci coap_add_token(pdu, sizeof(token), token); 1393c87c5fbaSopenharmony_ci for (n = 0; n < (sizeof(opt_num)/sizeof(opt_num[0])); n++) { 1394c87c5fbaSopenharmony_ci coap_add_option(pdu, opt_num[n], 1395c87c5fbaSopenharmony_ci sizeof(opt_val[n]), &opt_val[n]); 1396c87c5fbaSopenharmony_ci } 1397c87c5fbaSopenharmony_ci coap_add_data(pdu, sizeof(data), data); 1398c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 1399c87c5fbaSopenharmony_ci 1400c87c5fbaSopenharmony_ci /* Now update token */ 1401c87c5fbaSopenharmony_ci coap_update_token(pdu, sizeof(new_token), new_token); 1402c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data2, pdu->used_size) == 0); 1403c87c5fbaSopenharmony_ci 1404c87c5fbaSopenharmony_ci /* Now restore token */ 1405c87c5fbaSopenharmony_ci coap_update_token(pdu, sizeof(token), token); 1406c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1, pdu->used_size) == 0); 1407c87c5fbaSopenharmony_ci 1408c87c5fbaSopenharmony_ci /* Now set token to zero length */ 1409c87c5fbaSopenharmony_ci coap_update_token(pdu, 0, NULL); 1410c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data3, pdu->used_size) == 0); 1411c87c5fbaSopenharmony_ci} 1412c87c5fbaSopenharmony_ci 1413c87c5fbaSopenharmony_ci/* insert option before (large) final one */ 1414c87c5fbaSopenharmony_cistatic void 1415c87c5fbaSopenharmony_cit_encode_pdu24(void) { 1416c87c5fbaSopenharmony_ci size_t n; 1417c87c5fbaSopenharmony_ci uint8_t token[] = { 't' }; 1418c87c5fbaSopenharmony_ci uint8_t buf[4]; 1419c87c5fbaSopenharmony_ci uint16_t opt_num[] = { 28, 28, 28, 28 }; 1420c87c5fbaSopenharmony_ci uint32_t opt_val[] = { 0x1, 0x100, 0x10000, 0x1000000 }; 1421c87c5fbaSopenharmony_ci uint8_t data1[][8] = { 1422c87c5fbaSopenharmony_ci { 0x74, 0xd1, 0x0f, 0x01 }, 1423c87c5fbaSopenharmony_ci { 0x74, 0xd2, 0x0f, 0x01, 0x00 }, 1424c87c5fbaSopenharmony_ci { 0x74, 0xd3, 0x0f, 0x01, 0x00, 0x00 }, 1425c87c5fbaSopenharmony_ci { 0x74, 0xd4, 0x0f, 0x01, 0x00, 0x00, 0x00 } 1426c87c5fbaSopenharmony_ci }; 1427c87c5fbaSopenharmony_ci uint8_t data2[][16] = { 1428c87c5fbaSopenharmony_ci { 0x74, 0xd3, 0x0a, 0xff, 0xff, 0xf6, 0x51, 0x01 }, 1429c87c5fbaSopenharmony_ci { 1430c87c5fbaSopenharmony_ci 0x74, 0xd3, 0x0a, 0xff, 0xff, 0xf6, 0x52, 0x01, 1431c87c5fbaSopenharmony_ci 0x00 1432c87c5fbaSopenharmony_ci }, 1433c87c5fbaSopenharmony_ci { 1434c87c5fbaSopenharmony_ci 0x74, 0xd3, 0x0a, 0xff, 0xff, 0xf6, 0x53, 0x01, 1435c87c5fbaSopenharmony_ci 0x00, 0x00 1436c87c5fbaSopenharmony_ci }, 1437c87c5fbaSopenharmony_ci { 1438c87c5fbaSopenharmony_ci 0x74, 0xd3, 0x0a, 0xff, 0xff, 0xf6, 0x54, 0x01, 1439c87c5fbaSopenharmony_ci 0x00, 0x00, 0x00 1440c87c5fbaSopenharmony_ci } 1441c87c5fbaSopenharmony_ci }; 1442c87c5fbaSopenharmony_ci 1443c87c5fbaSopenharmony_ci for (n = 0; n < (sizeof(opt_num)/sizeof(opt_num[0])); n++) { 1444c87c5fbaSopenharmony_ci coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ 1445c87c5fbaSopenharmony_ci 1446c87c5fbaSopenharmony_ci CU_ASSERT(pdu->data == NULL); 1447c87c5fbaSopenharmony_ci 1448c87c5fbaSopenharmony_ci coap_add_token(pdu, sizeof(token), token); 1449c87c5fbaSopenharmony_ci coap_add_option(pdu, opt_num[n], 1450c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), opt_val[n]), buf); 1451c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data1[n], pdu->used_size) == 0); 1452c87c5fbaSopenharmony_ci 1453c87c5fbaSopenharmony_ci /* Now insert option */ 1454c87c5fbaSopenharmony_ci coap_insert_option(pdu, 23, 1455c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), 0xfffff6), buf); 1456c87c5fbaSopenharmony_ci CU_ASSERT(memcmp(pdu->token, data2[n], pdu->used_size) == 0); 1457c87c5fbaSopenharmony_ci } 1458c87c5fbaSopenharmony_ci} 1459c87c5fbaSopenharmony_ci 1460c87c5fbaSopenharmony_cistatic int 1461c87c5fbaSopenharmony_cit_pdu_tests_create(void) { 1462c87c5fbaSopenharmony_ci pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU); 1463c87c5fbaSopenharmony_ci 1464c87c5fbaSopenharmony_ci return pdu == NULL; 1465c87c5fbaSopenharmony_ci} 1466c87c5fbaSopenharmony_ci 1467c87c5fbaSopenharmony_cistatic int 1468c87c5fbaSopenharmony_cit_pdu_tests_remove(void) { 1469c87c5fbaSopenharmony_ci coap_delete_pdu(pdu); 1470c87c5fbaSopenharmony_ci return 0; 1471c87c5fbaSopenharmony_ci} 1472c87c5fbaSopenharmony_ci 1473c87c5fbaSopenharmony_ciCU_pSuite 1474c87c5fbaSopenharmony_cit_init_pdu_tests(void) { 1475c87c5fbaSopenharmony_ci CU_pSuite suite[2]; 1476c87c5fbaSopenharmony_ci 1477c87c5fbaSopenharmony_ci suite[0] = CU_add_suite("pdu parser", t_pdu_tests_create, t_pdu_tests_remove); 1478c87c5fbaSopenharmony_ci if (!suite[0]) { /* signal error */ 1479c87c5fbaSopenharmony_ci fprintf(stderr, "W: cannot add pdu parser test suite (%s)\n", 1480c87c5fbaSopenharmony_ci CU_get_error_msg()); 1481c87c5fbaSopenharmony_ci 1482c87c5fbaSopenharmony_ci return NULL; 1483c87c5fbaSopenharmony_ci } 1484c87c5fbaSopenharmony_ci 1485c87c5fbaSopenharmony_ci#define PDU_TEST(s,t) \ 1486c87c5fbaSopenharmony_ci if (!CU_ADD_TEST(s,t)) { \ 1487c87c5fbaSopenharmony_ci fprintf(stderr, "W: cannot add pdu parser test (%s)\n", \ 1488c87c5fbaSopenharmony_ci CU_get_error_msg()); \ 1489c87c5fbaSopenharmony_ci } 1490c87c5fbaSopenharmony_ci 1491c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu1); 1492c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu2); 1493c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu3); 1494c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu4); 1495c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu5); 1496c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu6); 1497c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu7); 1498c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu8); 1499c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu9); 1500c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu10); 1501c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu11); 1502c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu12); 1503c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu13); 1504c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu14); 1505c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu15); 1506c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu16); 1507c87c5fbaSopenharmony_ci PDU_TEST(suite[0], t_parse_pdu17); 1508c87c5fbaSopenharmony_ci 1509c87c5fbaSopenharmony_ci suite[1] = CU_add_suite("pdu encoder", t_pdu_tests_create, t_pdu_tests_remove); 1510c87c5fbaSopenharmony_ci if (suite[1]) { 1511c87c5fbaSopenharmony_ci#define PDU_ENCODER_TEST(s,t) \ 1512c87c5fbaSopenharmony_ci if (!CU_ADD_TEST(s,t)) { \ 1513c87c5fbaSopenharmony_ci fprintf(stderr, "W: cannot add pdu encoder test (%s)\n", \ 1514c87c5fbaSopenharmony_ci CU_get_error_msg()); \ 1515c87c5fbaSopenharmony_ci } 1516c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu1); 1517c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu2); 1518c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu3); 1519c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu4); 1520c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu5); 1521c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu6); 1522c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu7); 1523c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu8); 1524c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu9); 1525c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu10); 1526c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu11); 1527c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu12); 1528c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu13); 1529c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu14); 1530c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu15); 1531c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu16); 1532c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu17); 1533c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu18); 1534c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu19); 1535c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu20); 1536c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu21); 1537c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu22); 1538c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu23); 1539c87c5fbaSopenharmony_ci PDU_ENCODER_TEST(suite[1], t_encode_pdu24); 1540c87c5fbaSopenharmony_ci 1541c87c5fbaSopenharmony_ci } else /* signal error */ 1542c87c5fbaSopenharmony_ci fprintf(stderr, "W: cannot add pdu parser test suite (%s)\n", 1543c87c5fbaSopenharmony_ci CU_get_error_msg()); 1544c87c5fbaSopenharmony_ci 1545c87c5fbaSopenharmony_ci return suite[0]; 1546c87c5fbaSopenharmony_ci} 1547