Files

131 lines
4.5 KiB
C

// T-mode 3-of-6 codec + long-frame CRC path tests (host, sanitizers).
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "wmbus.h"
static void test_codec_roundtrip_all_values(void)
{
uint8_t data[256], coded[512], back[256];
for (int i = 0; i < 256; i++) data[i] = (uint8_t)i;
int clen = wmbus_3of6_encode(data, 256, coded, sizeof(coded));
assert(clen == wmbus_3of6_coded_len(256));
assert(clen == 384); // 256 * 1.5
assert(wmbus_3of6_decode(coded, (size_t)clen, back, 256) == 0);
assert(memcmp(data, back, 256) == 0);
assert(wmbus_3of6_first_byte(coded, (size_t)clen) == 0x00);
}
static void test_codec_detects_any_single_chip_flip(void)
{
// 3-of-6 codes have constant weight 3: one flipped chip changes the
// weight, so every single-bit corruption inside the coded payload
// must surface as an invalid code.
uint8_t data[32], coded[64], back[32];
for (int i = 0; i < 32; i++) data[i] = (uint8_t)(i * 37 + 5);
int clen = wmbus_3of6_encode(data, 32, coded, sizeof(coded));
assert(clen == 48);
for (int bit = 0; bit < 32 * 12; bit++) {
coded[bit >> 3] ^= (uint8_t)(0x80u >> (bit & 7));
assert(wmbus_3of6_decode(coded, (size_t)clen, back, 32) > 0);
coded[bit >> 3] ^= (uint8_t)(0x80u >> (bit & 7));
}
assert(wmbus_3of6_decode(coded, (size_t)clen, back, 32) == 0);
}
static void test_codec_bounds(void)
{
uint8_t coded[8] = {0};
uint8_t out[8];
assert(wmbus_3of6_decode(coded, 2, out, 2) == -1); // 24 bits > 16
assert(wmbus_3of6_first_byte(coded, 1) == -1);
assert(wmbus_3of6_first_byte(coded, 2) == -1); // 000000 invalid
uint8_t d[4] = {0};
assert(wmbus_3of6_encode(d, 4, coded, 5) == -1); // needs 6
}
static void test_t1_frame_roundtrip(void)
{
uint8_t frame[256], coded[512], back[256];
int total = wmbus_build_test_frame(frame, sizeof(frame), 42, 100);
assert(total > 0);
int clen = wmbus_3of6_encode(frame, total, coded, sizeof(coded));
assert(clen == wmbus_3of6_coded_len(total));
assert(wmbus_3of6_first_byte(coded, (size_t)clen) == frame[0]);
assert(wmbus_3of6_decode(coded, (size_t)clen, back, total) == 0);
assert(memcmp(frame, back, total) == 0);
assert(wmbus_check_crc(WMBUS_FRAME_A, back, total));
}
static void build_b_frame(uint8_t *out, uint8_t lfield)
{
int total = 1 + lfield;
out[0] = lfield;
for (int i = 1; i < total; i++) out[i] = (uint8_t)(i * 73 + 11);
if (total > 128) {
// two chunks: CRC over first 126 at [126..127], rest at the tail
int rem = total - 128;
assert(rem >= 3);
uint16_t c = wmbus_crc16(out, 126);
out[126] = c >> 8; out[127] = c & 0xFF;
c = wmbus_crc16(out + 128, rem - 2);
out[total - 2] = c >> 8; out[total - 1] = c & 0xFF;
} else {
uint16_t c = wmbus_crc16(out, total - 2);
out[total - 2] = c >> 8; out[total - 1] = c & 0xFF;
}
}
static void test_format_b_long(void)
{
uint8_t f[256];
// > 128 bytes: two CRC chunks
build_b_frame(f, 200);
assert(wmbus_frame_total_len(WMBUS_FRAME_B, 200) == 201);
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 201));
// corruption in either chunk must fail
f[50] ^= 1;
assert(!wmbus_check_crc(WMBUS_FRAME_B, f, 201));
f[50] ^= 1;
f[150] ^= 1;
assert(!wmbus_check_crc(WMBUS_FRAME_B, f, 201));
f[150] ^= 1;
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 201));
// exactly one full chunk
build_b_frame(f, 127);
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 128));
// short frame
build_b_frame(f, 20);
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 21));
// maximum that fits the radio payload
build_b_frame(f, 252);
assert(wmbus_frame_total_len(WMBUS_FRAME_B, 252) == 253);
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 253));
}
static void test_format_a_long(void)
{
uint8_t frame[256];
int total = wmbus_build_test_frame(frame, sizeof(frame), 7, 200);
assert(total == 227); // 1 + 200 + 2 * (1 + ceil(191/16))
assert(wmbus_check_crc(WMBUS_FRAME_A, frame, total));
frame[220] ^= 1;
assert(!wmbus_check_crc(WMBUS_FRAME_A, frame, total));
// beyond the fixed radio capture: must be rejected, not truncated
assert(wmbus_frame_total_len(WMBUS_FRAME_A, 255) == -1);
}
int main(void)
{
test_codec_roundtrip_all_values();
test_codec_detects_any_single_chip_flip();
test_codec_bounds();
test_t1_frame_roundtrip();
test_format_b_long();
test_format_a_long();
puts("t1 + long-frame tests: PASS");
return 0;
}