WIP: Integrated Unity test framework.

This commit is contained in:
Chad Attermann committed 2025-08-25 14:42:15 -06:00
1 parent b28962aaa9
commit bac53b9e6d
7 files changed
+507 -132

No files matched your search

+1 -1
View File
@@ -15,7 +15,7 @@
// Imported from Crypto.cpp
// Computes CRC-8/HITAG checksum
// CBA Doesn't appeart to support incremental checksum building
// CBA Doesn't appear to support incremental checksum building
extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
void testCryptoMain() {
+1
View File
@@ -28,6 +28,7 @@ lib_deps =
ArduinoJson@^7.4.2
MsgPack@^0.4.2
https://github.com/attermann/Crypto.git
test_framework = unity
[env:native]
platform = native
-100
View File
@@ -1,100 +0,0 @@
#include <unity.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>
#include <sys/time.h>
#include <stdio.h>
// Imported from Crypto.cpp
extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
void setUp(void)
{
// set stuff up here
}
void tearDown(void)
{
// clean stuff up here
}
void testCrc() {
char data[32];
uint8_t crc;
strcpy(data, "foo");
crc = crypto_crc8(0, data, sizeof(data));
TEST_ASSERT_EQUAL_UINT8(174, crc);
strcpy(data, "bar");
crc = crypto_crc8(0, data, sizeof(data));
TEST_ASSERT_EQUAL_UINT8(63, crc);
strcpy(data, "foo");
crc = crypto_crc8(0, data, sizeof(data));
TEST_ASSERT_EQUAL_UINT8(174, crc);
}
#ifndef ARDUINO
unsigned long millis() {
timeval time;
::gettimeofday(&time, NULL);
return (uint64_t)(time.tv_sec * 1000) + (uint64_t)(time.tv_usec / 1000);
return 0;
}
#endif
#ifdef ARDUINO
void testsleep(float seconds) { delay((uint32_t)(seconds * 1000)); }
#else
void testsleep(float seconds) { timespec time; time.tv_sec = (time_t)(seconds); time.tv_nsec = (seconds - (float)time.tv_sec) * 1000000000; ::nanosleep(&time, nullptr); }
#endif
uint64_t timeOffset = 0;
uint64_t ltime() {
// handle roll-over of 32-bit millis (approx. 49 days)
static uint32_t low32, high32;
uint32_t new_low32 = millis();
if (new_low32 < low32) high32++;
low32 = new_low32;
return ((uint64_t)high32 << 32 | low32) + timeOffset;
}
void testTime() {
//uint64_t start = RNS::Utilities::OS::ltime();
//RNS::Utilities::OS::sleep(0.1);
//TEST_ASSERT_TRUE((RNS::Utilities::OS::ltime() - start) > 100);
uint64_t start = ltime();
testsleep(0.1);
uint64_t end = ltime();
TEST_ASSERT_TRUE((end - start) > 50);
TEST_ASSERT_TRUE((end - start) < 200);
}
void testConvert() {
//uint64_t time = ltime();
//printf("time: %lld\n", time);
uint64_t num = 1234567890;
TEST_ASSERT_EQUAL_UINT64(1234567890, num);
char str[16];
snprintf(str, 16, "%lld", num);
TEST_ASSERT_EQUAL_STRING("1234567890", str);
char* buf = (char*)&num;
//uint64_t newnum = (uint64_t)(*buf);
uint64_t newnum = *(uint64_t*)buf;
TEST_ASSERT_EQUAL_UINT64(1234567890, newnum);
}
int main(void)
{
UNITY_BEGIN();
//RUN_TEST(testCrc);
//RUN_TEST(testTime);
RUN_TEST(testConvert);
return UNITY_END();
}
+232
View File
@@ -0,0 +1,232 @@
#include <unity.h>
#include "Bytes.h"
#include "Utilities/Crc.h"
#include "Cryptography/HMAC.h"
#include "Cryptography/PKCS7.h"
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>
#include <sys/time.h>
#include <stdio.h>
// Imported from Crypto.cpp
// Computes CRC-8/HITAG checksum
// CBA Doesn't appear to support incremental checksum building
extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
void setUp(void) {
// set stuff up here before each test
}
void tearDown(void) {
// clean stuff up here after each test
}
/*
void testHMAC() {
{
const char keystr[] = "key";
const char datastr[] = "The quick brown fox jumps over the lazy dog";
const uint8_t hasharr[] = {
0xf7, 0xbc, 0x83, 0xf4, 0x30, 0x53, 0x84, 0x24,
0xb1, 0x32, 0x98, 0xe6, 0xaa, 0x6f, 0xb1, 0x43,
0xef, 0x4d, 0x59, 0xa1, 0x49, 0x46, 0x17, 0x59,
0x97, 0x47, 0x9d, 0xbc, 0x2d, 0x1a, 0x3c, 0xd8
};
RNS::Bytes key(keystr);
RNS::Bytes data(datastr);
RNS::Bytes hash(hasharr, sizeof(hasharr));
//TRACEF("expected hash: %s", hash.toHex().c_str());
RNS::Cryptography::HMAC hmac(key, data);
RNS::Bytes result = hmac.digest();
//TRACEF("result hash: %s", result.toHex().c_str());
TEST_ASSERT_EQUAL_INT(0, memcmp(hash.data(), result.data(), result.size()));
}
}
void testPKCS7() {
const uint8_t str[] = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
// test buffer of half blocksize
{
size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE / 2;
RNS::Bytes bytes(str, len);
bytes = RNS::Cryptography::PKCS7::pad(bytes);
TEST_ASSERT_EQUAL_size_t(RNS::Cryptography::PKCS7::BLOCKSIZE, bytes.size());
bytes = RNS::Cryptography::PKCS7::unpad(bytes);
TEST_ASSERT_EQUAL_size_t(len, bytes.size());
TEST_ASSERT_EQUAL_INT(0, memcmp(bytes.data(), str, len));
}
// test buffer of one less blocksize
{
size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE - 1;
RNS::Bytes bytes(str, len);
bytes = RNS::Cryptography::PKCS7::pad(bytes);
TEST_ASSERT_EQUAL_size_t(RNS::Cryptography::PKCS7::BLOCKSIZE, bytes.size());
bytes = RNS::Cryptography::PKCS7::unpad(bytes);
TEST_ASSERT_EQUAL_size_t(len, bytes.size());
TEST_ASSERT_EQUAL_INT(0, memcmp(bytes.data(), str, len));
}
// test buffer of blocksize
{
size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE;
RNS::Bytes bytes(str, len);
bytes = RNS::Cryptography::PKCS7::pad(bytes);
TEST_ASSERT_EQUAL_size_t(RNS::Cryptography::PKCS7::BLOCKSIZE * 2, bytes.size());
bytes = RNS::Cryptography::PKCS7::unpad(bytes);
TEST_ASSERT_EQUAL_size_t(len, bytes.size());
TEST_ASSERT_EQUAL_INT(0, memcmp(bytes.data(), str, len));
}
// test inplace buffer of half blocksize
{
size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE / 2;
RNS::Bytes bytes(str, len);
RNS::Cryptography::PKCS7::inplace_pad(bytes);
TEST_ASSERT_EQUAL_size_t(RNS::Cryptography::PKCS7::BLOCKSIZE, bytes.size());
RNS::Cryptography::PKCS7::inplace_unpad(bytes);
TEST_ASSERT_EQUAL_size_t(len, bytes.size());
TEST_ASSERT_EQUAL_INT(0, memcmp(bytes.data(), str, len));
}
// test inplace buffer of one less blocksize
{
size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE - 1;
RNS::Bytes bytes(str, len);
RNS::Cryptography::PKCS7::inplace_pad(bytes);
TEST_ASSERT_EQUAL_size_t(RNS::Cryptography::PKCS7::BLOCKSIZE, bytes.size());
RNS::Cryptography::PKCS7::inplace_unpad(bytes);
TEST_ASSERT_EQUAL_size_t(len, bytes.size());
TEST_ASSERT_EQUAL_INT(0, memcmp(bytes.data(), str, len));
}
// test inplace buffer of blocksize
{
size_t len = RNS::Cryptography::PKCS7::BLOCKSIZE;
RNS::Bytes bytes(str, len);
RNS::Cryptography::PKCS7::inplace_pad(bytes);
TEST_ASSERT_EQUAL_size_t(RNS::Cryptography::PKCS7::BLOCKSIZE * 2, bytes.size());
RNS::Cryptography::PKCS7::inplace_unpad(bytes);
TEST_ASSERT_EQUAL_size_t(len, bytes.size());
TEST_ASSERT_EQUAL_INT(0, memcmp(bytes.data(), str, len));
}
}
*/
void testCrc8() {
char data[32];
uint8_t crc;
strcpy(data, "foo");
crc = crypto_crc8(0, data, strlen(data));
TEST_ASSERT_EQUAL_UINT8(0x48, crc);
strcpy(data, "bar");
crc = crypto_crc8(0, data, strlen(data));
TEST_ASSERT_EQUAL_UINT8(0xED, crc);
strcpy(data, "foo");
crc = crypto_crc8(0, data, strlen(data));
TEST_ASSERT_EQUAL_UINT8(0x48, crc);
strcpy(data, "foobarfoo");
crc = crypto_crc8(0, data, strlen(data));
TEST_ASSERT_EQUAL_UINT8(0x00, crc);
}
/*
void testCrc32() {
char data[16];
uint32_t crc;
strcpy(data, "foo");
crc = RNS::Utilities::Crc::crc32(0, data);
TEST_ASSERT_EQUAL_UINT32(0x8C736521, crc);
strcpy(data, "bar");
crc = RNS::Utilities::Crc::crc32(0, data);
TEST_ASSERT_EQUAL_UINT32(0x76FF8CAA, crc);
strcpy(data, "foo");
crc = RNS::Utilities::Crc::crc32(0, data);
TEST_ASSERT_EQUAL_UINT32(0x8C736521, crc);
strcpy(data, "foobarfoo");
crc = RNS::Utilities::Crc::crc32(0, data);
TEST_ASSERT_EQUAL_UINT32(0xEE2F4613, crc);
}
void testIncrementalCrc32() {
char data[16];
uint32_t crc;
strcpy(data, "foo");
crc = RNS::Utilities::Crc::crc32(0, data);
TEST_ASSERT_EQUAL_UINT32(0x8C736521, crc);
strcpy(data, "bar");
crc = RNS::Utilities::Crc::crc32(crc, data);
TEST_ASSERT_EQUAL_UINT32(0x9EF61F95, crc);
strcpy(data, "foo");
crc = RNS::Utilities::Crc::crc32(crc, data);
TEST_ASSERT_EQUAL_UINT32(0xEE2F4613, crc);
strcpy(data, "foobarfoo");
crc = RNS::Utilities::Crc::crc32(0, data);
TEST_ASSERT_EQUAL_UINT32(0xEE2F4613, crc);
}
void testByteCrc32() {
char data[16];
strcpy(data, "foobarfoo");
uint32_t crc = 0;
for (int i = 0; i < strlen(data); i++) {
crc = RNS::Utilities::Crc::crc32(crc, data[i]);
}
TEST_ASSERT_EQUAL_UINT32(0xEE2F4613, crc);
}
*/
int runUnityTests(void) {
UNITY_BEGIN();
RUN_TEST(testCrc8);
return UNITY_END();
}
// For native dev-platform or for some embedded frameworks
int main(void) {
return runUnityTests();
}
#ifdef ARDUINO
// For Arduino framework
void setup() {
// Wait ~2 seconds before the Unity test runner
// establishes connection with a board Serial interface
delay(2000);
runUnityTests();
}
void loop() {}
#endif
// For ESP-IDF framework
void app_main() {
runUnityTests();
}
+43
View File
@@ -0,0 +1,43 @@
#include <unity.h>
void setUp(void) {
// set stuff up here before each test
}
void tearDown(void) {
// clean stuff up here after each test
}
void testExample() {
bool success = true;
TEST_ASSERT_EQUAL(true, success);
}
int runUnityTests(void) {
UNITY_BEGIN();
RUN_TEST(testExample);
return UNITY_END();
}
// For native dev-platform or for some embedded frameworks
int main(void) {
return runUnityTests();
}
#ifdef ARDUINO
// For Arduino framework
void setup() {
// Wait ~2 seconds before the Unity test runner
// establishes connection with a board Serial interface
delay(2000);
runUnityTests();
}
void loop() {}
#endif
// For ESP-IDF framework
void app_main() {
runUnityTests();
}
@@ -1,6 +1,5 @@
#include <unity.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
@@ -8,31 +7,12 @@
#include <sys/time.h>
#include <stdio.h>
// Imported from Crypto.cpp
extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size);
void setUp(void)
{
// set stuff up here
void setUp(void) {
// set stuff up here before each test
}
void tearDown(void)
{
// clean stuff up here
}
void testCrc() {
char data[32];
uint8_t crc;
strcpy(data, "foo");
crc = crypto_crc8(0, data, sizeof(data));
TEST_ASSERT_EQUAL_UINT8(174, crc);
strcpy(data, "bar");
crc = crypto_crc8(0, data, sizeof(data));
TEST_ASSERT_EQUAL_UINT8(63, crc);
strcpy(data, "foo");
crc = crypto_crc8(0, data, sizeof(data));
TEST_ASSERT_EQUAL_UINT8(174, crc);
void tearDown(void) {
// clean stuff up here after each test
}
#ifndef ARDUINO
@@ -90,11 +70,31 @@ void testConvert() {
TEST_ASSERT_EQUAL_UINT64(1234567890, newnum);
}
int main(void)
{
UNITY_BEGIN();
//RUN_TEST(testCrc);
//RUN_TEST(testTime);
RUN_TEST(testConvert);
return UNITY_END();
int runUnityTests(void) {
UNITY_BEGIN();
RUN_TEST(testTime);
RUN_TEST(testConvert);
return UNITY_END();
}
// For native dev-platform or for some embedded frameworks
int main(void) {
return runUnityTests();
}
#ifdef ARDUINO
// For Arduino framework
void setup() {
// Wait ~2 seconds before the Unity test runner
// establishes connection with a board Serial interface
delay(2000);
runUnityTests();
}
void loop() {}
#endif
// For ESP-IDF framework
void app_main() {
runUnityTests();
}
+199
View File
@@ -0,0 +1,199 @@
#include <unity.h>
#include <MsgPack.h>
#include <string.h>
#include <stdint.h>
// Use MsgPack's native binary type
using BinaryBuffer = MsgPack::bin_t<uint8_t>;
void setUp(void) {
// set stuff up here before each test
}
void tearDown(void) {
// clean stuff up here after each test
}
void test_msgpack_array_packing(void) {
// Test data setup
float testFloat = 3.14159f;
// Create binary buffers using MsgPack's bin_t type
BinaryBuffer buffer1;
buffer1.resize(5);
memcpy(buffer1.data(), "Hello", 5);
BinaryBuffer buffer2;
buffer2.resize(5);
memcpy(buffer2.data(), "World", 5);
// Pack the array using MsgPack
MsgPack::Packer packer;
packer.to_array(testFloat, buffer1, buffer2);
// Get the packed data
size_t packed_size = packer.size();
const uint8_t* packed_data = packer.data();
// Basic validation - should have data
TEST_ASSERT_GREATER_THAN(0, packed_size);
TEST_ASSERT_NOT_NULL(packed_data);
// Expected MsgPack format:
// - Array header (fixarray with 3 elements): 0x93
// - Float (float32): 0xca + 4 bytes for 3.14159f
// - Binary 1 (bin8): 0xc4 + length(5) + "Hello"
// - Binary 2 (bin8): 0xc4 + length(5) + "World"
// Verify array header - fixarray with 3 elements
TEST_ASSERT_EQUAL_HEX8(0x93, packed_data[0]);
// Verify float marker (float32)
TEST_ASSERT_EQUAL_HEX8(0xca, packed_data[1]);
// Extract and verify the float value by unpacking
MsgPack::Unpacker unpacker;
unpacker.feed(packed_data, packed_size);
float unpacked_float;
MsgPack::bin_t<uint8_t> unpacked_buffer1;
MsgPack::bin_t<uint8_t> unpacked_buffer2;
// Unpack the array
bool unpack_success = unpacker.from_array(unpacked_float, unpacked_buffer1, unpacked_buffer2);
TEST_ASSERT_TRUE(unpack_success);
// Verify unpacked values match original data
TEST_ASSERT_EQUAL_FLOAT(testFloat, unpacked_float);
// Verify binary buffers
TEST_ASSERT_EQUAL_size_t(buffer1.size(), unpacked_buffer1.size());
TEST_ASSERT_EQUAL_MEMORY(buffer1.data(), unpacked_buffer1.data(), buffer1.size());
TEST_ASSERT_EQUAL_size_t(buffer2.size(), unpacked_buffer2.size());
TEST_ASSERT_EQUAL_MEMORY(buffer2.data(), unpacked_buffer2.data(), buffer2.size());
// Additional validation: verify total packed size makes sense
// Array header(1) + float marker(1) + float data(4) +
// bin1 header(1) + bin1 length(1) + bin1 data(5) +
// bin2 header(1) + bin2 length(1) + bin2 data(5) = 20 bytes
TEST_ASSERT_EQUAL_size_t(20, packed_size);
// Verify specific binary markers for the Hello and World strings
// After array header(1) + float marker(1) + float data(4) = offset 6
TEST_ASSERT_EQUAL_HEX8(0xc4, packed_data[6]); // bin8 marker for first buffer
TEST_ASSERT_EQUAL_HEX8(5, packed_data[7]); // length of "Hello"
TEST_ASSERT_EQUAL_MEMORY("Hello", &packed_data[8], 5);
// After first buffer: offset 6 + 1 + 1 + 5 = 13
TEST_ASSERT_EQUAL_HEX8(0xc4, packed_data[13]); // bin8 marker for second buffer
TEST_ASSERT_EQUAL_HEX8(5, packed_data[14]); // length of "World"
TEST_ASSERT_EQUAL_MEMORY("World", &packed_data[15], 5);
}
void test_msgpack_empty_buffer(void) {
// Test with empty buffer
float testFloat = -42.0f;
BinaryBuffer emptyBuffer; // Empty buffer
BinaryBuffer normalBuffer;
normalBuffer.resize(4);
memcpy(normalBuffer.data(), "Test", 4);
MsgPack::Packer packer;
packer.to_array(testFloat, emptyBuffer, normalBuffer);
size_t packed_size = packer.size();
const uint8_t* packed_data = packer.data();
TEST_ASSERT_GREATER_THAN(0, packed_size);
TEST_ASSERT_NOT_NULL(packed_data);
// Verify by unpacking
MsgPack::Unpacker unpacker;
unpacker.feed(packed_data, packed_size);
float unpacked_float;
MsgPack::bin_t<uint8_t> unpacked_empty;
MsgPack::bin_t<uint8_t> unpacked_normal;
bool unpack_success = unpacker.from_array(unpacked_float, unpacked_empty, unpacked_normal);
TEST_ASSERT_TRUE(unpack_success);
TEST_ASSERT_EQUAL_FLOAT(testFloat, unpacked_float);
TEST_ASSERT_EQUAL_size_t(0, unpacked_empty.size());
TEST_ASSERT_EQUAL_size_t(4, unpacked_normal.size());
TEST_ASSERT_EQUAL_MEMORY("Test", unpacked_normal.data(), 4);
}
void test_msgpack_binary_content(void) {
// Test with binary data (not just strings)
float testFloat = 1.0f;
uint8_t binaryData1[] = {0x00, 0xFF, 0xAA, 0x55};
uint8_t binaryData2[] = {0x01, 0x02, 0x03};
BinaryBuffer buffer1;
buffer1.resize(sizeof(binaryData1));
memcpy(buffer1.data(), binaryData1, sizeof(binaryData1));
BinaryBuffer buffer2;
buffer2.resize(sizeof(binaryData2));
memcpy(buffer2.data(), binaryData2, sizeof(binaryData2));
MsgPack::Packer packer;
packer.to_array(testFloat, buffer1, buffer2);
size_t packed_size = packer.size();
const uint8_t* packed_data = packer.data();
TEST_ASSERT_GREATER_THAN(0, packed_size);
TEST_ASSERT_NOT_NULL(packed_data);
// Verify by unpacking
MsgPack::Unpacker unpacker;
unpacker.feed(packed_data, packed_size);
float unpacked_float;
MsgPack::bin_t<uint8_t> unpacked_buffer1;
MsgPack::bin_t<uint8_t> unpacked_buffer2;
bool unpack_success = unpacker.from_array(unpacked_float, unpacked_buffer1, unpacked_buffer2);
TEST_ASSERT_TRUE(unpack_success);
TEST_ASSERT_EQUAL_FLOAT(testFloat, unpacked_float);
TEST_ASSERT_EQUAL_size_t(4, unpacked_buffer1.size());
TEST_ASSERT_EQUAL_MEMORY(binaryData1, unpacked_buffer1.data(), 4);
TEST_ASSERT_EQUAL_size_t(3, unpacked_buffer2.size());
TEST_ASSERT_EQUAL_MEMORY(binaryData2, unpacked_buffer2.data(), 3);
}
int runUnityTests(void) {
UNITY_BEGIN();
RUN_TEST(test_msgpack_array_packing);
RUN_TEST(test_msgpack_empty_buffer);
RUN_TEST(test_msgpack_binary_content);
return UNITY_END();
}
// For native dev-platform or for some embedded frameworks
int main(void) {
return runUnityTests();
}
#ifdef ARDUINO
// For Arduino framework
void setup() {
// Wait ~2 seconds before the Unity test runner
// establishes connection with a board Serial interface
delay(2000);
runUnityTests();
}
void loop() {}
#endif
// For ESP-IDF framework
void app_main() {
runUnityTests();
}