Skip to content

Commit 77285da

Browse files
authored
Merge pull request #201 from wolfSSL/code-coverage
Added code coverage feature
2 parents 023e488 + cf3384e commit 77285da

File tree

5 files changed

+161
-5
lines changed

5 files changed

+161
-5
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Code Coverage
2+
3+
on:
4+
push:
5+
branches: [ 'master', 'main', 'release/**' ]
6+
pull_request:
7+
branches: [ '*' ]
8+
9+
jobs:
10+
coverage:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
# List compiler version
17+
- name: List compiler and gcov version
18+
run: |
19+
gcc --version
20+
gcov --version
21+
22+
# Install gcovr for coverage report generation
23+
- name: Install gcovr
24+
run: |
25+
sudo apt-get update
26+
sudo apt-get install -y gcovr
27+
28+
# Checkout wolfssl
29+
- name: Checkout wolfssl
30+
uses: actions/checkout@v4
31+
with:
32+
repository: wolfssl/wolfssl
33+
path: wolfssl
34+
35+
# Run coverage
36+
- name: Build and run tests with coverage
37+
run: cd test && make coverage WOLFSSL_DIR=../wolfssl
38+
39+
# Display coverage summary in the action log
40+
- name: Display coverage summary
41+
run: |
42+
echo "=== Coverage Summary ==="
43+
cd test
44+
gcovr Build --root .. --filter '\.\./src/.*' --filter '\.\./wolfhsm/.*' --print-summary
45+
46+
# Upload coverage report as artifact
47+
- name: Upload coverage report
48+
uses: actions/upload-artifact@v4
49+
with:
50+
name: coverage-report
51+
path: coverage/
52+
retention-days: 30
53+

.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: 36 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,29 @@ 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+
$(eval COVERAGE_TARGETS := $(filter-out coverage,$(MAKECMDGOALS)))
264+
$(MAKE) COVERAGE=1 $(if $(COVERAGE_TARGETS),$(COVERAGE_TARGETS),build_app)
265+
@echo "Running tests..."
266+
@if [ ! -f $(BUILD_DIR)/$(BIN).elf ]; then \
267+
echo "Error: $(BUILD_DIR)/$(BIN).elf not found. Build failed."; \
268+
exit 1; \
269+
fi
270+
$(BUILD_DIR)/$(BIN).elf
271+
@echo "Generating coverage report..."
272+
mkdir -p ../coverage && gcovr Build \
273+
--root .. \
274+
--gcov-executable gcov \
275+
--filter '\.\./src/.*' \
276+
--filter '\.\./wolfhsm/.*' \
277+
--html-details ../coverage/index.html \
278+
--print-summary
279+
@echo "Coverage report generated at ../coverage/index.html"
280+
281+
# Prevent make from trying to build these as targets
282+
%:
283+
@:

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,

0 commit comments

Comments
 (0)