Skip to content

Commit 7608053

Browse files
committed
Add unit tests for Utils::toHex
1 parent f1a1be1 commit 7608053

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Run Unit Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
pull_request:
9+
workflow_dispatch:
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
16+
- name: Clone Repo
17+
uses: actions/checkout@v4
18+
19+
- name: Setup Build Environment
20+
uses: ./.github/actions/setup-build-environment
21+
22+
- name: Run Unit Tests
23+
run: pio test -e native -vv
24+
25+
- name: Upload Test Results
26+
# Upload test results even if the test step failed.
27+
if: always()
28+
uses: actions/upload-artifact@v4
29+
with:
30+
name: test-results
31+
path: .pio/build/native/

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ Here are some general principals you should try to adhere to:
9393
* No dynamic memory allocation, except during setup/begin functions.
9494
* Use the same brace and indenting style that's in the core source modules. (A .clang-format is prob going to be added soon, but please do NOT retroactively re-format existing code. This just creates unnecessary diffs that make finding problems harder)
9595

96+
### Running unit tests
97+
98+
To run unit tests, run the following command:
99+
100+
```bash
101+
pio test --environment native --verbose
102+
```
103+
96104
## Road-Map / To-Do
97105

98106
There are a number of fairly major features in the pipeline, with no particular time-frames attached yet. In very rough chronological order:

platformio.ini

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,17 @@ lib_deps =
138138
adafruit/Adafruit MLX90614 Library @ ^2.1.5
139139
adafruit/Adafruit_VL53L0X @ ^1.2.4
140140
stevemarple/MicroNMEA @ ^2.0.6
141+
142+
; ----------------- TESTING ---------------------
143+
144+
[env:native]
145+
platform = native
146+
build_flags = -std=c++14
147+
-I src
148+
-I test/mocks
149+
test_build_src = yes
150+
build_src_filter =
151+
-<*>
152+
+<../src/Utils.cpp>
153+
lib_deps =
154+
google/googletest @ ^1.15.2

test/mocks/AES.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <stddef.h>
5+
6+
// Mock AES128 class for testing
7+
// Provides minimal interface to allow Utils.cpp to compile
8+
class AES128 {
9+
public:
10+
void setKey(const uint8_t* key, size_t keySize) {}
11+
void encryptBlock(uint8_t* output, const uint8_t* input) {}
12+
void decryptBlock(uint8_t* output, const uint8_t* input) {}
13+
};

test/mocks/SHA256.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <stddef.h>
5+
6+
// Mock SHA256 class for testing
7+
// Provides minimal interface to allow Utils.cpp to compile
8+
class SHA256 {
9+
public:
10+
void update(const uint8_t* data, size_t len) {}
11+
void finalize(uint8_t* hash, size_t hashLen) {}
12+
void resetHMAC(const uint8_t* key, size_t keyLen) {}
13+
void finalizeHMAC(const uint8_t* key, size_t keyLen, uint8_t* hash, size_t hashLen) {}
14+
};

test/mocks/Stream.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
// Mock Stream class for native testing
4+
// Provides minimal interface needed by Utils.h
5+
6+
class Stream {
7+
public:
8+
virtual void print(char c) {}
9+
virtual void print(const char* str) {}
10+
};

test/test_utils/test_tohex.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <gtest/gtest.h>
2+
#include "Utils.h"
3+
4+
using namespace mesh;
5+
6+
#define HEX_BUFFER_SIZE(input) (sizeof(input) * 2 + 1)
7+
8+
TEST(UtilsToHex, ConvertSingleByte) {
9+
uint8_t input[] = {0xAB};
10+
char output[HEX_BUFFER_SIZE(input)];
11+
12+
Utils::toHex(output, input, sizeof(input));
13+
14+
EXPECT_STREQ("AB", output);
15+
}
16+
17+
TEST(UtilsToHex, ConvertMultipleBytes) {
18+
uint8_t input[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
19+
char output[HEX_BUFFER_SIZE(input)];
20+
21+
Utils::toHex(output, input, sizeof(input));
22+
23+
EXPECT_STREQ("0123456789ABCDEF", output);
24+
}
25+
26+
TEST(UtilsToHex, ConvertZeroByte) {
27+
uint8_t input[] = {0x00};
28+
char output[HEX_BUFFER_SIZE(input)];
29+
30+
Utils::toHex(output, input, sizeof(input));
31+
32+
EXPECT_STREQ("00", output);
33+
}
34+
35+
TEST(UtilsToHex, ConvertMaxByte) {
36+
uint8_t input[] = {0xFF};
37+
char output[HEX_BUFFER_SIZE(input)];
38+
39+
Utils::toHex(output, input, sizeof(input));
40+
41+
EXPECT_STREQ("FF", output);
42+
}
43+
44+
TEST(UtilsToHex, NullTerminatesOnEmptyInput) {
45+
uint8_t input[] = {0xAB};
46+
char output[] = "X"; // Pre-fill with X.
47+
48+
Utils::toHex(output, input, 0);
49+
50+
// Should just null-terminate at position 0
51+
EXPECT_EQ('\0', output[0]);
52+
}
53+
54+
int main(int argc, char **argv) {
55+
::testing::InitGoogleTest(&argc, argv);
56+
return RUN_ALL_TESTS();
57+
}

0 commit comments

Comments
 (0)