Skip to content

Commit cbef016

Browse files
committed
Add unit tests for Utils::toHex
1 parent ecd0cfc commit cbef016

7 files changed

Lines changed: 147 additions & 0 deletions

File tree

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
@@ -98,6 +98,14 @@ Here are some general principals you should try to adhere to:
9898

9999
Help us prioritize! Please react with thumbs-up to issues/PRs you care about most. We look at reaction counts when planning work.
100100

101+
### Running unit tests
102+
103+
To run unit tests, run the following command:
104+
105+
```bash
106+
pio test --environment native --verbose
107+
```
108+
101109
## Road-Map / To-Do
102110

103111
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
@@ -151,3 +151,17 @@ lib_deps =
151151
stevemarple/MicroNMEA @ ^2.0.6
152152
adafruit/Adafruit BME680 Library @ ^2.0.4
153153
adafruit/Adafruit BMP085 Library @ ^1.2.4
154+
155+
; ----------------- TESTING ---------------------
156+
157+
[env:native]
158+
platform = native
159+
build_flags = -std=c++14
160+
-I src
161+
-I test/mocks
162+
test_build_src = yes
163+
build_src_filter =
164+
-<*>
165+
+<../src/Utils.cpp>
166+
lib_deps =
167+
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)