1/* libcoap unit tests 2 * 3 * Copyright (C) 2019-2023 Olaf Bergmann <bergmann@tzi.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 * 7 * This file is part of the CoAP library libcoap. Please see 8 * README for terms of use. 9 */ 10 11#include "test_common.h" 12#include "test_encode.h" 13 14#include <stdio.h> 15#include <stdlib.h> 16#include <string.h> 17 18/************************************************************************ 19 ** decoder tests 20 ************************************************************************/ 21 22static void 23t_decode1(void) { 24 const coap_binary_t teststr = { 0, NULL }; 25 unsigned int result; 26 27 result = coap_decode_var_bytes(teststr.s, teststr.length); 28 29 CU_ASSERT(result == 0); 30} 31 32static void 33t_decode2(void) { 34 uint8_t data[] = { 0x01 }; 35 coap_binary_t teststr = { sizeof(data), data }; 36 unsigned int result; 37 38 result = coap_decode_var_bytes8(teststr.s, teststr.length); 39 40 CU_ASSERT(result == 1); 41} 42 43static void 44t_decode3(void) { 45 uint8_t data[] = { 0x01, 0x00, 0x00, 0x00 }; 46 coap_binary_t teststr = { sizeof(data), data }; 47 unsigned int result; 48 49 result = coap_decode_var_bytes(teststr.s, teststr.length); 50 51 CU_ASSERT(result == 0x01000000); 52} 53 54static void 55t_decode4(void) { 56 uint8_t data[] = { 0x05, 0x06, 0x07, 0x08 }; 57 coap_binary_t teststr = { sizeof(data), data }; 58 unsigned int result; 59 60 result = coap_decode_var_bytes(teststr.s, teststr.length); 61 62 CU_ASSERT(result == 0x05060708); 63} 64 65static void 66t_decode5(void) { 67 const coap_binary_t teststr = { 0, NULL }; 68 uint64_t result; 69 70 result = coap_decode_var_bytes8(teststr.s, teststr.length); 71 72 CU_ASSERT(result == 0); 73} 74 75static void 76t_decode6(void) { 77 uint8_t data[] = { 0x01 }; 78 coap_binary_t teststr = { sizeof(data), data }; 79 uint64_t result; 80 81 result = coap_decode_var_bytes8(teststr.s, teststr.length); 82 83 CU_ASSERT(result == 1); 84} 85 86static void 87t_decode7(void) { 88 uint8_t data[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 89 coap_binary_t teststr = { sizeof(data), data }; 90 uint64_t result; 91 92 result = coap_decode_var_bytes8(teststr.s, teststr.length); 93 94 CU_ASSERT(result == 0x0100000000000000); 95} 96 97static void 98t_decode8(void) { 99 uint8_t data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; 100 coap_binary_t teststr = { sizeof(data), data }; 101 uint64_t result; 102 103 result = coap_decode_var_bytes8(teststr.s, teststr.length); 104 105 CU_ASSERT(result == 0x0102030405060708); 106} 107 108/************************************************************************ 109 ** encoder tests 110 ************************************************************************/ 111 112static void 113t_encode1(void) { 114 uint8_t buf[16]; 115 uint8_t data[] = { 0x00 }; 116 unsigned int result; 117 118 result = coap_encode_var_safe(buf, sizeof(buf), 0x00); 119 120 CU_ASSERT(result == 0); 121 122 CU_ASSERT(memcmp(buf, data, result) == 0); 123} 124 125static void 126t_encode2(void) { 127 uint8_t buf[16]; 128 uint8_t data[] = { 0x01 }; 129 unsigned int result; 130 131 result = coap_encode_var_safe(buf, sizeof(buf), 0x01); 132 133 CU_ASSERT(result == sizeof(data)); 134 135 CU_ASSERT(memcmp(buf, data, result) == 0); 136} 137 138static void 139t_encode3(void) { 140 uint8_t buf[16]; 141 uint8_t data[] = { 0x05, 0x06, 0x07, 0x08 }; 142 unsigned int result; 143 144 result = coap_encode_var_safe8(buf, sizeof(buf), 0x05060708); 145 146 CU_ASSERT(result == sizeof(data)); 147 148 CU_ASSERT(memcmp(buf, data, result) == 0); 149} 150 151static void 152t_encode4(void) { 153 unsigned int result = 0; 154 /* This check will abort the program if configured with --enable-assert */ 155#ifdef NDEBUG 156 uint8_t buf[16]; 157 result = coap_encode_var_safe(buf, 2, 0x01020304); 158#endif /* NDEBUG */ 159 CU_ASSERT(result == 0); 160} 161 162static void 163t_encode5(void) { 164 uint8_t buf[16]; 165 uint8_t data[] = { 0x00 }; 166 unsigned int result; 167 168 result = coap_encode_var_safe8(buf, sizeof(buf), 0x00); 169 170 CU_ASSERT(result == 0); 171 172 CU_ASSERT(memcmp(buf, data, result) == 0); 173} 174 175static void 176t_encode6(void) { 177 uint8_t buf[16]; 178 uint8_t data[] = { 0x01 }; 179 unsigned int result; 180 181 result = coap_encode_var_safe8(buf, sizeof(buf), 0x01); 182 183 CU_ASSERT(result == sizeof(data)); 184 185 CU_ASSERT(memcmp(buf, data, result) == 0); 186} 187 188static void 189t_encode7(void) { 190 uint8_t buf[16]; 191 uint8_t data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; 192 unsigned int result; 193 194 result = coap_encode_var_safe8(buf, sizeof(buf), 0x0102030405060708); 195 196 CU_ASSERT(result == sizeof(data)); 197 198 CU_ASSERT(memcmp(buf, data, result) == 0); 199} 200 201static void 202t_encode8(void) { 203 unsigned int result = 0; 204 /* This check will abort the program if configured with --enable-assert */ 205#ifdef NDEBUG 206 uint8_t buf[16]; 207 result = coap_encode_var_safe8(buf, 2, 0x0102030405060708); 208#endif /* NDEBUG */ 209 CU_ASSERT(result == 0); 210} 211 212/************************************************************************ 213 ** initialization 214 ************************************************************************/ 215 216CU_pSuite 217t_init_encode_tests(void) { 218 CU_pSuite suite[5]; 219 220 suite[0] = CU_add_suite("byte value decoder", NULL, NULL); 221 if (!suite[0]) { /* signal error */ 222 fprintf(stderr, "W: cannot add byte value decoder test suite (%s)\n", 223 CU_get_error_msg()); 224 225 return NULL; 226 } 227 228#define DECODE_TEST(n,s) \ 229 if (!CU_add_test(suite[0], s, t_decode##n)) { \ 230 fprintf(stderr, "W: cannot add decoder test (%s)\n", \ 231 CU_get_error_msg()); \ 232 } 233 234 DECODE_TEST(1, "decode value #1"); 235 DECODE_TEST(2, "decode value #2"); 236 DECODE_TEST(3, "decode value #3"); 237 DECODE_TEST(4, "decode value #4"); 238 DECODE_TEST(5, "decode value #5"); 239 DECODE_TEST(6, "decode value #6"); 240 DECODE_TEST(7, "decode value #7"); 241 DECODE_TEST(8, "decode value #8"); 242 243 if ((suite[1] = CU_add_suite("byte value encoder", NULL, NULL))) { 244#define ENCODE_TEST(n,s) \ 245 if (!CU_add_test(suite[1], s, t_encode##n)) { \ 246 fprintf(stderr, "W: cannot add encoder test (%s)\n", \ 247 CU_get_error_msg()); \ 248 } 249 250 ENCODE_TEST(1, "encode value #1"); 251 ENCODE_TEST(2, "encode value #2"); 252 ENCODE_TEST(3, "encode value #3"); 253 ENCODE_TEST(4, "encode value #4"); 254 ENCODE_TEST(5, "encode value #5"); 255 ENCODE_TEST(6, "encode value #6"); 256 ENCODE_TEST(7, "encode value #7"); 257 ENCODE_TEST(8, "encode value #8"); 258 259 } else { 260 fprintf(stderr, "W: cannot add byte value encoder test suite (%s)\n", 261 CU_get_error_msg()); 262 } 263 264 return suite[0]; 265} 266