Skip to content

Commit 5c4e0e2

Browse files
committed
Add pre-processing script
1 parent f9a3a57 commit 5c4e0e2

23 files changed

Lines changed: 795 additions & 395 deletions

.github/workflows/build-and-test.yml

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,69 +32,114 @@ jobs:
3232
repository: wolfssl/wolfssl
3333
path: wolfssl
3434

35-
- name: Build and test refactor
36-
run: cd test-refactor/posix && make clean && make -j WOLFSSL_DIR=../../wolfssl && make run
37-
3835
# Build and test standard build
3936
- name: Build and test
4037
run: cd test && make clean && make -j WOLFSSL_DIR=../wolfssl && make run
4138

39+
- name: Build and test refactor
40+
run: cd test-refactor/posix && make clean && make -j WOLFSSL_DIR=../../wolfssl && make run
41+
4242
# Build and test standard build, with DMA and ASAN enabled
4343
- name: Build and test DMA ASAN
4444
run: cd test && make clean && make -j DMA=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
4545

46+
- name: Build and test refactor DMA ASAN
47+
run: cd test-refactor/posix && make clean && make -j DMA=1 ASAN=1 WOLFSSL_DIR=../../wolfssl && make run
48+
4649
# Build and test ASAN build, with wolfCrypt tests enabled.
4750
- name: Build and test ASAN TESTWOLFCRYPT
4851
run: cd test && make clean && make -j ASAN=1 TESTWOLFCRYPT=1 WOLFSSL_DIR=../wolfssl && make run
4952

53+
- name: Build and test refactor ASAN TESTWOLFCRYPT
54+
run: cd test-refactor/posix && make clean && make -j ASAN=1 TESTWOLFCRYPT=1 WOLFSSL_DIR=../../wolfssl && make run
55+
5056
# Build and test ASAN build, with wolfCrypt tests enabled and using the DMA devId.
5157
- name: Build and test ASAN TESTWOLFCRYPT TESTWOLFCRYPT_DMA
5258
run: cd test && make clean && make -j ASAN=1 TESTWOLFCRYPT=1 TESTWOLFCRYPT_DMA=1 DMA=1 WOLFSSL_DIR=../wolfssl && make run
5359

60+
- name: Build and test refactor ASAN TESTWOLFCRYPT TESTWOLFCRYPT_DMA
61+
run: cd test-refactor/posix && make clean && make -j ASAN=1 TESTWOLFCRYPT=1 TESTWOLFCRYPT_DMA=1 DMA=1 WOLFSSL_DIR=../../wolfssl && make run
62+
5463
# Build and test debug build with ASAN and NOCRYPTO
5564
- name: Build and test ASAN DEBUG NOCRYPTO
5665
run: cd test && make clean && make -j DEBUG=1 ASAN=1 NOCRYPTO=1 WOLFSSL_DIR=../wolfssl && make run
5766

67+
- name: Build and test refactor ASAN DEBUG NOCRYPTO
68+
run: cd test-refactor/posix && make clean && make -j DEBUG=1 ASAN=1 NOCRYPTO=1 WOLFSSL_DIR=../../wolfssl && make run
69+
5870
# Build and test debug build with ASAN and DMA
5971
- name: Build and test ASAN DEBUG DMA
6072
run: cd test && make clean && make -j DEBUG=1 ASAN=1 DMA=1 WOLFSSL_DIR=../wolfssl && make run
6173

74+
- name: Build and test refactor ASAN DEBUG DMA
75+
run: cd test-refactor/posix && make clean && make -j DEBUG=1 ASAN=1 DMA=1 WOLFSSL_DIR=../../wolfssl && make run
76+
6277
# Build and test with SHE and ASAN
6378
- name: Build and test ASAN SHE
6479
run: cd test && make clean && make -j SHE=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
6580

81+
- name: Build and test refactor ASAN SHE
82+
run: cd test-refactor/posix && make clean && make -j SHE=1 ASAN=1 WOLFSSL_DIR=../../wolfssl && make run
83+
6684
# Build and test with DEBUG=1
6785
- name: Build and test with DEBUG
6886
run: cd test && make clean && make -j DEBUG=1 WOLFSSL_DIR=../wolfssl && make run
6987

88+
- name: Build and test refactor with DEBUG
89+
run: cd test-refactor/posix && make clean && make -j DEBUG=1 WOLFSSL_DIR=../../wolfssl && make run
90+
7091
# Build and test with DEBUG_VERBOSE=1 (includes DEBUG)
7192
- name: Build and test with DEBUG_VERBOSE
7293
run: cd test && make clean && make -j DEBUG_VERBOSE=1 WOLFSSL_DIR=../wolfssl && make run
7394

95+
- name: Build and test refactor with DEBUG_VERBOSE
96+
run: cd test-refactor/posix && make clean && make -j DEBUG_VERBOSE=1 WOLFSSL_DIR=../../wolfssl && make run
97+
7498
# Build and test in multithreaded mode with everything enabled
7599
- name: Build and test with THREADSAFE and everything
76100
run: cd test && make clean && make -j THREADSAFE=1 DMA=1 SHE=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
77101

102+
- name: Build and test refactor with THREADSAFE and everything
103+
run: cd test-refactor/posix && make clean && make -j THREADSAFE=1 DMA=1 SHE=1 ASAN=1 WOLFSSL_DIR=../../wolfssl && make run
104+
78105
# Build and test in multithreaded mode with everything enabled and wolfCrypt tests
79106
- name: Build and test with THREADSAFE and TESTWOLFCRYPT and everything
80107
run: cd test && make clean && make -j THREADSAFE=1 TESTWOLFCRYPT=1 DMA=1 SHE=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
81108

109+
- name: Build and test refactor with THREADSAFE and TESTWOLFCRYPT and everything
110+
run: cd test-refactor/posix && make clean && make -j THREADSAFE=1 TESTWOLFCRYPT=1 DMA=1 SHE=1 ASAN=1 WOLFSSL_DIR=../../wolfssl && make run
111+
82112
# Build and test in multithreaded mode with everything enabled and wolfCrypt tests with dma
83113
- name: Build and test with THREADSAFE and TESTWOLFCRYPT with DMA
84114
run: cd test && make clean && make -j THREADSAFE=1 TESTWOLFCRYPT=1 TESTWOLFCRYPT_DMA=1 DMA=1 SHE=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
85115

116+
- name: Build and test refactor with THREADSAFE and TESTWOLFCRYPT with DMA
117+
run: cd test-refactor/posix && make clean && make -j THREADSAFE=1 TESTWOLFCRYPT=1 TESTWOLFCRYPT_DMA=1 DMA=1 SHE=1 ASAN=1 WOLFSSL_DIR=../../wolfssl && make run
118+
86119
# Build and test with AUTH=1
87120
- name: Build and test with AUTH
88121
run: cd test && make clean && make -j AUTH=1 WOLFSSL_DIR=../wolfssl && make run
89122

123+
- name: Build and test refactor with AUTH
124+
run: cd test-refactor/posix && make clean && make -j AUTH=1 WOLFSSL_DIR=../../wolfssl && make run
125+
90126
# Build and test with AUTH=1 and ASAN
91127
- name: Build and test with AUTH ASAN
92128
run: cd test && make clean && make -j AUTH=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
93129

130+
- name: Build and test refactor with AUTH ASAN
131+
run: cd test-refactor/posix && make clean && make -j AUTH=1 ASAN=1 WOLFSSL_DIR=../../wolfssl && make run
132+
94133
# Build and test with AUTH=1 and THREADSAFE
95134
- name: Build and test with AUTH THREADSAFE ASAN
96135
run: cd test && make clean && make -j AUTH=1 THREADSAFE=1 ASAN=1 WOLFSSL_DIR=../wolfssl && make run
97136

137+
- name: Build and test refactor with AUTH THREADSAFE ASAN
138+
run: cd test-refactor/posix && make clean && make -j AUTH=1 THREADSAFE=1 ASAN=1 WOLFSSL_DIR=../../wolfssl && make run
139+
98140
# Build and test with AUTH=1 and NOCRYPTO=1 (auth on, crypto off)
99141
- name: Build and test with AUTH NOCRYPTO
100142
run: cd test && make clean && make -j AUTH=1 NOCRYPTO=1 WOLFSSL_DIR=../wolfssl && make run
143+
144+
- name: Build and test refactor with AUTH NOCRYPTO
145+
run: cd test-refactor/posix && make clean && make -j AUTH=1 NOCRYPTO=1 WOLFSSL_DIR=../../wolfssl && make run

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ tools/static-analysis/reports/
2020
*.gcda
2121
*.gcno
2222
coverage/
23+
24+
# Test driver log (automake-style; produced by `make run` in test-refactor)
25+
test-suite.log

test-refactor/README.md

Lines changed: 36 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,51 @@
1-
# test-refactor
1+
# wolfHSM unit tests
22

3-
Prototype of the refactored wolfHSM test infrastructure.
3+
## TLDR
4+
To run the unit tests on a POSIX system (Linux/Mac):
45

5-
## Key differences from test/
6+
```
7+
cd test-refactor/posix
8+
make check
9+
```
610

7-
- **Runner** (`wh_test_runner.h/c`): generic suite executor.
8-
Each suite is a name + NULL-terminated test array, run
9-
either via `whTestRunner_Run` (suite owns its setup/cleanup)
10-
or `whTestRunner_RunWithCtx` (caller provides the live
11-
context). Group functions use the latter.
12-
- **App-owned init**: the port's main() brings up the server
13-
and client once at startup (mirroring real firmware boot)
14-
and hands the live contexts to the group functions. Suites
15-
no longer stand up their own fixtures.
16-
- **Port helpers** (`wh_test_helpers_server_<port>.h/c`,
17-
`wh_test_helpers_client_<port>.h/c`): per-port files that
18-
stand in for what a real target does at boot -- configure
19-
flash, init NVM/crypto, wire up a transport, bring up the
20-
server or client context.
21-
- **Groups** (`wh_test_groups.h/c`): three portable entry
22-
points (Misc/Server/Client) that main invokes. Each runs
23-
its gated suites and calls the caller-implemented reset
24-
hook between them.
25-
- **Threaded driver**: the POSIX port's main runs the server
26-
and client on separate threads. The server thread runs the
27-
server-only group first, then enters a `HandleRequestMessage`
28-
loop; the client thread runs the client-only group against
29-
the live server. Ports that already split server and client
30-
onto different cores/tasks do the same thing natively.
31-
- **Platform split**: platform-specific code is isolated in
32-
`wh_test_helpers_server_<port>.c`,
33-
`wh_test_helpers_client_<port>.c`, and
34-
`wh_test_main_<port>.c`. Test modules and groups are
35-
identical on all platforms.
11+
Results are printed via `WOLFHSM_CFG_PRINTF` from the wolfHSM build. `test-suite.log` contains the detailed output.
3612

37-
## Suites implemented so far
13+
## Overview
14+
The wolfHSM unit tests are split into 3 groups:
3815

39-
| Suite | Group | Description |
40-
|-------|-------|-------------|
41-
| Flash RamSim | misc | Write-lock, erase, program, verify, blank-check |
42-
| NVM Flash | misc | Flash unit ops, NVM add/overwrite/destroy/reclaim |
43-
| Cert | server | Server-side cert add/verify/chain/erase |
44-
| ClientServer | client-server | Echo round-trip, server info query |
45-
| ThreadSafe Stress | client-server | Phased multi-thread contention (unchanged internals) |
16+
- Client, for tests that run on the client core/process
17+
- Server, for tests that run on the server core/process
18+
- Misc, for tests that can run on either core/process
4619

47-
## Remaining tests to port
20+
The groups are organized in `wh_test_list.c`.
4821

49-
| Suite | Group | Description |
50-
|-------|-------|-------------|
51-
| Comm | client-server | Transport layer (mem, TCP, SHM) |
52-
| Crypto | client-server | AES, RSA, ECC, CMAC, curve25519, ed25519, etc. |
53-
| Crypto Affinity | client-server | Device ID operation routing |
54-
| SHE | client-server | Secure Hardware Extension key load, crypto, secure boot |
55-
| Keywrap | client-server | Key wrap/unwrap operations |
56-
| Log | misc | Logging frontend, ringbuf, POSIX file backends |
57-
| Lock | misc | Lock primitives with POSIX backend |
58-
| DMA | misc | DMA address translation and allow-list |
59-
| Server Img Mgr | server | Image manager verify/install/erase |
60-
| Timeout | client-server | POSIX timeout enforcement |
61-
| wolfCrypt Test | client-server | wolfCrypt test suite via wolfHSM transport |
62-
| MultiClient | client-server | 2 CS pairs, shared NVM, global/local key isolation |
22+
## Adding a port
23+
The unit tests run within the port's main application. As a prerequiste, setup a new port application as described in the [porting guide](docs/src/chapter08.md).
6324

64-
## Platforms requiring update
25+
For the unit test port, see `wh_test_posix_main.c` and the two `wh_test_posix_*.c` sources as a reference implementation.
6526

66-
Each platform with test infrastructure needs its own
67-
`wh_test_helpers_server_<port>.c`,
68-
`wh_test_helpers_client_<port>.c`, and
69-
`wh_test_main_<port>.c` (see "Porting" below).
27+
### Client
28+
1. Implement `main()` which creates a client context
29+
1. Implement `whTestGroup_ResetClient` which resets the context between tests. Can be empty.
30+
2. Optionally call `whTestGroup_Misc()`
31+
3. Call `whTestGroup_Client(&clientCtx)`
7032

71-
| Platform | Vendor | Test files |
72-
|----------|--------|------------|
73-
| POSIX | wolfSSL | `test-refactor/posix/` (done) |
74-
| Bernina | STMicro | `bernina-server/src/bh_test.c` |
75-
| SR6 | STMicro | (no test files found) |
76-
| TC3xx | Infineon | `port/client/wolfhsm_tests.c`, `port/server/ccb_tests.c` |
77-
| RH850 F1KM | Renesas | `rh850_test2_1/`, `rh850_test2_2/` |
78-
| PIC32CZ | Microchip | `czhsm-client/tests/`, `czhsm-server/` |
79-
| TDA4VH | TI | (no test files found) |
80-
| New Eagle | Customer | (no test files found) |
33+
### Server
34+
The client unit tests can be run against the normal server application -- no special modifications required.
8135

82-
## File layout
36+
To add the server side unit tests:
37+
1. Implement `whTestGroup_ResetServer` which resets the context between tests. Can be empty.
38+
2. Call `whTestGroup_Server(&serverCtx)` prior to entering the main request handling loop.
8339

84-
```
85-
Portable (ships in wolfHSM):
86-
wh_test_runner.h/c - suite runner
87-
wh_test_groups.h/c - Misc/Server/Client entry points
88-
server/wh_test_*.c/h - server-only test modules
89-
client-server/wh_test_*.c/h - client-server test modules
90-
misc/wh_test_*.c/h - standalone test modules
91-
92-
Platform-specific (one directory per platform, e.g. posix/):
93-
<port>/wh_test_helpers_misc_<port>.h/c - misc fixtures
94-
<port>/wh_test_helpers_server_<port>.h/c - server bringup
95-
<port>/wh_test_helpers_client_<port>.h/c - client bringup
96-
<port>/wh_test_main_<port>.c - init, group
97-
dispatch, reset
98-
hooks
99-
<port>/Makefile - build rules
100-
```
40+
## Adding a test
41+
1. Create a new function which returns `int` (0 for success) with a context argument (`whClientContext*` for client tests, `whServerContext*` for server tests, or none for misc tests).
42+
2. In wh_test_list.c, add a line with `WH_TEST_DECL(<function>)`
43+
3. In wh_test_list.c, add the function to the appropriate `whTestCase` array.
10144

102-
## Porting to other platforms
45+
**Note**: if the test is specific to a platform, do not add it to the common list as shown above. Port-specific tests live within the port (not this directory), and are called from the port-specific code.
10346

104-
1. Implement the init helpers for the side(s) the target
105-
needs. These stand in for what your firmware's normal
106-
boot flow already does -- if it's simpler to call your
107-
existing init code directly from main, that works too:
108-
- `whTestHelperPosix_Server_Init/Cleanup` (reference):
109-
bring up flash/NVM/crypto/transport/server.
110-
- `whTestHelperPosix_Client_Init/Cleanup` (reference):
111-
bring up client comm + handshake. On single-process
112-
targets, the server runs in its own thread and pumps
113-
`HandleRequestMessage` itself.
114-
2. Provide a `main()` that:
115-
- Calls `whTestGroup_Misc()` for standalone tests.
116-
- Brings up the server/client contexts once.
117-
- Calls `whTestGroup_Server(&server)` and/or
118-
`whTestGroup_Client(&client)` with the live handles.
119-
- Tears the contexts down.
120-
- Implements `whTestGroup_ResetServer` and/or
121-
`whTestGroup_ResetClient` -- called between suites to
122-
scrub persistent state.
123-
3. Add the portable `.c` files and your port files to your
124-
build system.
47+
## Next steps
48+
- Redesign the nvm_flash test to remove the dependency on the ramsim backend and become port-agnostic and runable on embedded targets, and move to the server group.
49+
- Redesign `whTest_LockConfig` and `whTest_LogBackend_RunAll` to fit into the misc group, likely with a context param.
50+
- Translate remaining tests from wolfHSM/test to this design, and confirm equivalent code coverage.
12551

126-
See `wh_test_main_posix.c` and the two `*_posix.c` helpers as
127-
a reference implementation.
128-
129-
## Build and run (POSIX)
130-
131-
```
132-
cd posix
133-
make run
134-
make run DEBUG=1
135-
make run THREADSAFE=1 # enables stress test gate
136-
```

test-refactor/client-server/wh_test_crypto.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
#include "wolfhsm/wh_client.h"
4242

4343
#include "wh_test_common.h"
44+
#include "wh_test_list.h"
4445

4546
#ifndef NO_SHA256
46-
WH_TEST_CLIENT int whTest_CryptoSha256(whClientContext* ctx)
47+
int whTest_CryptoSha256(whClientContext* ctx)
4748
{
4849
int devId = WH_DEV_ID;
4950
int ret = WH_ERROR_OK;
@@ -95,7 +96,7 @@ WH_TEST_CLIENT int whTest_CryptoSha256(whClientContext* ctx)
9596

9697

9798
#if !defined(NO_AES) && defined(HAVE_AES_CBC)
98-
WH_TEST_CLIENT int whTestCrypto_Aes(whClientContext* ctx)
99+
int whTest_CryptoAes(whClientContext* ctx)
99100
{
100101
int devId = WH_DEV_ID;
101102
int ret = 0;
@@ -161,7 +162,7 @@ WH_TEST_CLIENT int whTestCrypto_Aes(whClientContext* ctx)
161162

162163

163164
#if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
164-
WH_TEST_CLIENT int whTestCrypto_Ecc256(whClientContext* ctx)
165+
int whTest_CryptoEcc256(whClientContext* ctx)
165166
{
166167
int devId = WH_DEV_ID;
167168
int ret = 0;

test-refactor/client-server/wh_test_echo.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@
3131
#include "wolfhsm/wh_client.h"
3232

3333
#include "wh_test_common.h"
34+
#include "wh_test_list.h"
3435

3536
#define REPEAT_COUNT 10
3637

3738
/*
3839
* Echo a message to the server and verify the response
3940
* matches. Repeats several times with different payloads.
4041
*/
41-
WH_TEST_CLIENT int test_echo(whClientContext* ctx)
42+
int whTest_Echo(whClientContext* ctx)
4243
{
4344
char send_buf[WOLFHSM_CFG_COMM_DATA_LEN];
4445
char recv_buf[WOLFHSM_CFG_COMM_DATA_LEN];

test-refactor/client-server/wh_test_server_info.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@
3131
#include "wolfhsm/wh_message_comm.h"
3232

3333
#include "wh_test_common.h"
34+
#include "wh_test_list.h"
3435

3536

3637
/*
3738
* Query server info and verify the response contains
3839
* valid data.
3940
*/
40-
WH_TEST_CLIENT int test_server_info(whClientContext* ctx)
41+
int whTest_ServerInfo(whClientContext* ctx)
4142
{
4243
uint8_t version[WH_INFO_VERSION_LEN + 1] = {0};
4344
uint8_t build[WH_INFO_VERSION_LEN + 1] = {0};

0 commit comments

Comments
 (0)