Add unit tests for Utils::toHex

This commit is contained in:
Michael Lynch
2025-10-12 11:11:05 -04:00
parent 9a1be5386d
commit 5e03c00397
7 changed files with 147 additions and 0 deletions

13
test/mocks/AES.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
// Mock AES128 class for testing
// Provides minimal interface to allow Utils.cpp to compile
class AES128 {
public:
void setKey(const uint8_t* key, size_t keySize) {}
void encryptBlock(uint8_t* output, const uint8_t* input) {}
void decryptBlock(uint8_t* output, const uint8_t* input) {}
};

14
test/mocks/SHA256.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
// Mock SHA256 class for testing
// Provides minimal interface to allow Utils.cpp to compile
class SHA256 {
public:
void update(const uint8_t* data, size_t len) {}
void finalize(uint8_t* hash, size_t hashLen) {}
void resetHMAC(const uint8_t* key, size_t keyLen) {}
void finalizeHMAC(const uint8_t* key, size_t keyLen, uint8_t* hash, size_t hashLen) {}
};

10
test/mocks/Stream.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
// Mock Stream class for native testing
// Provides minimal interface needed by Utils.h
class Stream {
public:
virtual void print(char c) {}
virtual void print(const char* str) {}
};

View File

@@ -0,0 +1,57 @@
#include <gtest/gtest.h>
#include "Utils.h"
using namespace mesh;
#define HEX_BUFFER_SIZE(input) (sizeof(input) * 2 + 1)
TEST(UtilsToHex, ConvertSingleByte) {
uint8_t input[] = {0xAB};
char output[HEX_BUFFER_SIZE(input)];
Utils::toHex(output, input, sizeof(input));
EXPECT_STREQ("AB", output);
}
TEST(UtilsToHex, ConvertMultipleBytes) {
uint8_t input[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
char output[HEX_BUFFER_SIZE(input)];
Utils::toHex(output, input, sizeof(input));
EXPECT_STREQ("0123456789ABCDEF", output);
}
TEST(UtilsToHex, ConvertZeroByte) {
uint8_t input[] = {0x00};
char output[HEX_BUFFER_SIZE(input)];
Utils::toHex(output, input, sizeof(input));
EXPECT_STREQ("00", output);
}
TEST(UtilsToHex, ConvertMaxByte) {
uint8_t input[] = {0xFF};
char output[HEX_BUFFER_SIZE(input)];
Utils::toHex(output, input, sizeof(input));
EXPECT_STREQ("FF", output);
}
TEST(UtilsToHex, NullTerminatesOnEmptyInput) {
uint8_t input[] = {0xAB};
char output[] = "X"; // Pre-fill with X.
Utils::toHex(output, input, 0);
// Should just null-terminate at position 0
EXPECT_EQ('\0', output[0]);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}