65 lines
1.7 KiB
C++
65 lines
1.7 KiB
C++
#include "core/tcm_frame.hpp"
|
|
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
void test_rotate_frame_encoding() {
|
|
const std::vector<uint8_t> payload{0x00, 0x05};
|
|
const std::vector<uint8_t> expected{
|
|
0x7e, 0x00, 0x07, 0x85, 0x02, 0x77, 0x00, 0x01, 0x00, 0x05, 0x7f};
|
|
|
|
const auto encoded = mag160c::core::encode_tcm_frame(0x02, 0x77, 0x0001, payload);
|
|
|
|
assert(encoded == expected);
|
|
}
|
|
|
|
void test_decode_rejects_bad_header_checksum() {
|
|
auto encoded = mag160c::core::encode_tcm_frame(0x02, 0x77, 0x0001, {0x00, 0x05});
|
|
encoded[3] ^= 0xff;
|
|
|
|
mag160c::core::TcmFrame decoded;
|
|
assert(mag160c::core::decode_tcm_frame(encoded.data(), encoded.size(), &decoded) ==
|
|
MAG160C_ERR_CHECKSUM);
|
|
}
|
|
|
|
void test_decode_roundtrip() {
|
|
const std::vector<uint8_t> payload{0x10, 0x20, 0x30};
|
|
const auto encoded = mag160c::core::encode_tcm_frame(0xa1, 0xb2, 0xc3d4, payload);
|
|
|
|
mag160c::core::TcmFrame decoded;
|
|
assert(mag160c::core::decode_tcm_frame(encoded.data(), encoded.size(), &decoded) == MAG160C_OK);
|
|
|
|
assert(decoded.main_cmd == 0xa1);
|
|
assert(decoded.sub_cmd == 0xb2);
|
|
assert(decoded.frame_id == 0xc3d4);
|
|
assert(decoded.payload == payload);
|
|
}
|
|
|
|
void test_encode_rejects_oversized_payload() {
|
|
const std::vector<uint8_t> payload(65531U, 0xaa);
|
|
|
|
bool threw_length_error = false;
|
|
try {
|
|
(void)mag160c::core::encode_tcm_frame(0x02, 0x77, 0x0001, payload);
|
|
} catch (const std::length_error&) {
|
|
threw_length_error = true;
|
|
}
|
|
|
|
assert(threw_length_error);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
test_rotate_frame_encoding();
|
|
test_decode_rejects_bad_header_checksum();
|
|
test_decode_roundtrip();
|
|
test_encode_rejects_oversized_payload();
|
|
|
|
return 0;
|
|
}
|