Skip to content

Commit 5331d04

Browse files
committed
Addition of code coverage feature
1 parent 1eda4bc commit 5331d04

File tree

5 files changed

+102
-10
lines changed

5 files changed

+102
-10
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ compile_commands.json
1414
tools/static-analysis/reports/
1515
*.xml
1616
*.html
17+
18+
# Code coverage
19+
*.gcda
20+
*.gcno
21+
coverage/

test/Makefile

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ ifeq ($(ASAN),1)
7777
LDFLAGS += -fsanitize=address
7878
endif
7979

80+
# Add code coverage option
81+
ifeq ($(COVERAGE),1)
82+
CFLAGS += --coverage
83+
LDFLAGS += --coverage
84+
endif
85+
8086
## wolfSSL defines
8187
ifeq ($(DEBUG_WOLFSSL),1)
8288
DEF += -DDEBUG_WOLFSSL
@@ -190,7 +196,7 @@ vpath %.s $(dir $(SRC_ASM))
190196

191197
## Makefile Targets
192198

193-
.PHONY: build_app build_hex build_static clean run
199+
.PHONY: build_app build_hex build_static clean run coverage
194200

195201
build_app: $(BUILD_DIR) $(BUILD_DIR)/$(BIN).elf
196202
@echo Build complete.
@@ -238,7 +244,9 @@ clean:
238244
$(BUILD_DIR)/*.o \
239245
$(BUILD_DIR)/*.a \
240246
$(BUILD_DIR)/*.sym \
241-
$(BUILD_DIR)/*.disasm
247+
$(BUILD_DIR)/*.disasm \
248+
$(BUILD_DIR)/*.gcda \
249+
$(BUILD_DIR)/*.gcno
242250

243251
# No prereq's here to stop from rebuilding with different options
244252
run:
@@ -247,3 +255,22 @@ ifeq (,$(wildcard $(BUILD_DIR)/$(BIN).elf))
247255
else
248256
$(BUILD_DIR)/$(BIN).elf
249257
endif
258+
259+
# Coverage target: build with coverage, run tests, and generate report
260+
coverage:
261+
@echo "Building with coverage enabled..."
262+
$(MAKE) clean
263+
$(MAKE) COVERAGE=1 DEBUG=1
264+
@echo "Running tests..."
265+
@if [ ! -f $(BUILD_DIR)/$(BIN).elf ]; then \
266+
echo "Error: $(BUILD_DIR)/$(BIN).elf not found. Build failed."; \
267+
exit 1; \
268+
fi
269+
$(BUILD_DIR)/$(BIN).elf
270+
@echo "Generating coverage report..."
271+
gcovr --root .. \
272+
--filter 'src/.*' \
273+
--filter 'wolfhsm/.*' \
274+
--html-details $(BUILD_DIR)/coverage.html \
275+
--print-summary
276+
@echo "Coverage report generated at $(BUILD_DIR)/coverage.html"

test/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,67 @@ IMG_MGR ECC P256 Test completed successfully!
9797
IMG_MGR AES128 CMAC Test completed successfully!
9898
IMG_MGR RSA2048 Test completed successfully!
9999
```
100+
101+
## Code Coverage
102+
103+
The test suite supports code coverage analysis using gcovr. To generate coverage reports:
104+
105+
**Note**: The coverage directory must exist before generating reports. The `make coverage` target creates this automatically, but if running gcovr manually, create it first with:
106+
```bash
107+
mkdir -p ../coverage
108+
```
109+
110+
### Running Coverage
111+
112+
Use the convenient coverage target:
113+
```bash
114+
make coverage
115+
```
116+
117+
This will:
118+
1. Clean previous build artifacts
119+
2. Rebuild with coverage instrumentation enabled
120+
3. Run the test suite
121+
4. Generate an HTML coverage report
122+
123+
The coverage report will be generated at `../coverage/index.html`.
124+
125+
### Manual Coverage Workflow
126+
127+
Alternatively, you can run coverage manually:
128+
129+
```bash
130+
# Build with coverage enabled
131+
make clean
132+
make COVERAGE=1 DEBUG=1
133+
134+
# Run tests
135+
make run
136+
137+
# Create coverage directory and generate report (from repository root)
138+
cd ..
139+
mkdir -p coverage
140+
gcovr --root . \
141+
--filter 'src/.*' \
142+
--filter 'wolfhsm/.*' \
143+
--html-details coverage/index.html \
144+
--print-summary
145+
```
146+
147+
### Coverage Options
148+
149+
You can customize the coverage report generation:
150+
151+
```bash
152+
# Generate XML format (for CI/CD)
153+
gcovr --root . --filter 'src/.*' --filter 'wolfhsm/.*' --xml coverage.xml
154+
155+
# Generate JSON format
156+
gcovr --root . --filter 'src/.*' --filter 'wolfhsm/.*' --json coverage.json
157+
158+
# Include branch coverage
159+
gcovr --root . --filter 'src/.*' --filter 'wolfhsm/.*' --branches --print-summary
160+
161+
# Set minimum coverage threshold (fails if below 80%)
162+
gcovr --root . --filter 'src/.*' --filter 'wolfhsm/.*' --fail-under-line 80
163+
```

wolfhsm/wh_client.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,9 +1047,9 @@ int wh_Client_KeyUnwrapAndCacheRequest(whClientContext* ctx,
10471047
* key.
10481048
* @return int Returns 0 on success, or a negative error code on failure.
10491049
*/
1050-
int wh_Client_UnrapKeyAndCacheResponse(whClientContext* ctx,
1051-
enum wc_CipherType cipherType,
1052-
uint16_t* keyIdOut);
1050+
int wh_Client_KeyUnwrapAndCacheResponse(whClientContext* ctx,
1051+
enum wc_CipherType cipherType,
1052+
uint16_t* keyIdOut);
10531053

10541054
/* Counter functions */
10551055
int wh_Client_CounterInitRequest(whClientContext* c, whNvmId counterId,

wolfhsm/wh_server.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,8 @@ typedef whDmaOper whServerDmaOper;
120120
typedef whDmaFlags whServerDmaFlags;
121121
typedef whDmaAddr whServerDmaAddr;
122122
typedef whDmaAddrList whServerDmaAddrList;
123-
124123
#ifdef WOLFHSM_CFG_DMA_CUSTOM_CLIENT_COPY
125-
typedef enum {
126-
WH_DMA_COPY_OPER_CLIENT_READ = 0,
127-
WH_DMA_COPY_OPER_CLIENT_WRITE = 1,
128-
} whServerDmaCopyOper;
124+
typedef whDmaCopyOper whServerDmaCopyOper;
129125
#endif /* WOLFHSM_CFG_DMA_CUSTOM_CLIENT_COPY */
130126

131127
/* DMA callbacks invoked internally by wolfHSM before and after every client

0 commit comments

Comments
 (0)