Skip to content

Commit c1abfdd

Browse files
authored
Merge pull request #379 from danielinux/unit-test-extend
Added more unit tests. Added 'make cov'.
2 parents cd385df + b497ee3 commit c1abfdd

File tree

10 files changed

+1558
-25
lines changed

10 files changed

+1558
-25
lines changed

.github/workflows/test-units.yml

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,10 @@ jobs:
2222
run: |
2323
make keysclean && make -C tools/keytools clean && rm -f include/target.h
2424
25-
- name: Build wolfboot and test footprint
25+
- name: Build unit tests
2626
run: |
2727
make -C tools/unit-tests
2828
29-
30-
- name: Run manifest header parser unit tests
31-
run: |
32-
./tools/unit-tests/unit-parser
33-
34-
- name: Run non-encrypted ext_flash unit tests
35-
run: |
36-
./tools/unit-tests/unit-extflash
37-
38-
- name: Run aes128 ext_flash unit tests
39-
run: |
40-
./tools/unit-tests/unit-aes128
41-
42-
- name: Run aes256 ext_flash unit tests
43-
run: |
44-
./tools/unit-tests/unit-aes256
45-
46-
- name: Run chacha20 ext_flash unit tests
29+
- name: Run unit tests
4730
run: |
48-
./tools/unit-tests/unit-chacha20
31+
make -C tools/unit-tests run

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,10 @@ stage1/loader_stage1.ld
134134
debug/lauterbach
135135

136136
#cland cache
137-
.cache/*
137+
.cache/*
138+
139+
#gcov files
140+
*.gcov
141+
*.gcda
142+
*.gcno
143+
coverage.*

src/libwolfboot.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr)
398398
(void*)&wolfboot_magic_trail, sizeof(uint32_t));
399399
#endif
400400

401+
#ifndef MOCK_PARTITION_TRAILER
401402
#ifdef EXT_FLASH
402403

403404
/**
@@ -558,6 +559,7 @@ static void RAMFUNCTION set_partition_magic(uint8_t part)
558559
}
559560
}
560561
#endif /* EXT_FLASH */
562+
#endif /* MOCK_PARTITION_TRAILER */
561563

562564

563565

@@ -626,6 +628,8 @@ int RAMFUNCTION wolfBoot_set_partition_state(uint8_t part, uint8_t newst)
626628
{
627629
uint32_t *magic;
628630
uint8_t *state;
631+
if (part == PART_NONE)
632+
return -1;
629633
magic = get_partition_magic(part);
630634
if (*magic != WOLFBOOT_MAGIC_TRAIL)
631635
set_partition_magic(part);
@@ -669,6 +673,8 @@ int RAMFUNCTION wolfBoot_get_partition_state(uint8_t part, uint8_t *st)
669673
{
670674
uint32_t *magic;
671675
uint8_t *state;
676+
if (part == PART_NONE)
677+
return -1;
672678
magic = get_partition_magic(part);
673679
if (*magic != WOLFBOOT_MAGIC_TRAIL)
674680
return -1;

tools/unit-tests/Makefile

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,50 @@ endif
77

88
CFLAGS=-I. -I../../src -I../../include -I../../lib/wolfssl
99
CFLAGS+=-g -ggdb
10+
CFLAGS+=-fprofile-arcs
11+
CFLAGS+=-ftest-coverage
12+
CFLAGS+=--coverage
13+
LDFLAGS+=-fprofile-arcs
14+
LDFLAGS+=-ftest-coverage
15+
WOLFCRYPT=../../lib/wolfssl/
1016

1117

1218

13-
all: unit-parser unit-extflash unit-aes128 unit-aes256 unit-chacha20
19+
TESTS:=unit-parser unit-extflash unit-aes128 unit-aes256 unit-chacha20 unit-pci \
20+
unit-mock-state unit-sectorflags unit-image
21+
22+
all: $(TESTS)
23+
24+
cov:
25+
gcovr -f "^\.\.\/\.\.\/src.*\.c" -r ../.. --verbose \
26+
--merge-mode-functions merge-use-line-0 \
27+
--html-medium-threshold 60 \
28+
--html-high-threshold 80 \
29+
--html-details coverage.html
30+
firefox coverage.html
31+
32+
run: $(TESTS)
33+
for unit in $(TESTS); do \
34+
./$$unit || exit 1; \
35+
done
1436

1537

1638
unit-aes128:CFLAGS+=-DEXT_ENCRYPTED -DENCRYPT_WITH_AES128
1739
unit-aes256:CFLAGS+=-DEXT_ENCRYPTED -DENCRYPT_WITH_AES256
1840
unit-chacha20:CFLAGS+=-DEXT_ENCRYPTED -DENCRYPT_WITH_CHACHA
41+
unit-parser:CFLAGS+=-DNVM_FLASH_WRITEONCE
42+
43+
44+
WOLFCRYPT_SRC:=$(WOLFCRYPT)/wolfcrypt/src/sha.c \
45+
$(WOLFCRYPT)/wolfcrypt/src/sha256.c \
46+
$(WOLFCRYPT)/wolfcrypt/src/sp_int.c \
47+
$(WOLFCRYPT)/wolfcrypt/src/sp_c64.c \
48+
$(WOLFCRYPT)/wolfcrypt/src/random.c \
49+
$(WOLFCRYPT)/wolfcrypt/src/memory.c
50+
51+
WOLFCRYPT_CFLAGS+=-DWOLFSSL_USER_SETTINGS -DWOLFBOOT_SIGN_ECC256 -DWOLFBOOT_SIGN_ECC256 -DHAVE_ECC_KEY_IMPORT -DWOLFBOOT_HASH_SHA256 -D__WOLFBOOT
52+
53+
1954

2055
../../include/target.h: FORCE
2156
cp -f target.h $@
@@ -24,7 +59,7 @@ unit-extflash.o: FORCE
2459
rm -f $@
2560
gcc -c -o $@ unit-extflash.c $(CFLAGS)
2661

27-
unit-parser: ../../include/target.h unit-parser.o
62+
unit-parser: ../../include/target.h unit-parser.c
2863
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
2964

3065
unit-extflash: ../../include/target.h unit-extflash.c
@@ -42,10 +77,23 @@ unit-chacha20: ../../include/target.h unit-extflash.c
4277
unit-pci: unit-pci.c ../../src/pci.c
4378
gcc -o $@ $< $(CFLAGS) $(LDFLAGS)
4479

80+
unit-mock-state: ../../include/target.h unit-mock-state.c
81+
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
82+
83+
unit-sectorflags: ../../include/target.h unit-sectorflags.c
84+
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
85+
86+
unit-image: unit-image.c unit-common.c $(WOLFCRYPT_SRC)
87+
gcc -o $@ $^ $(CFLAGS) $(WOLFCRYPT_CFLAGS) $(LDFLAGS)
88+
4589
%.o:%.c
4690
gcc -c -o $@ $^ $(CFLAGS)
4791

48-
clean:
49-
rm -f unit-parser unit-extflash *.o
92+
93+
covclean:
94+
rm -f *.gcov *.gcno *.gcda coverage.*
95+
96+
clean: covclean
97+
rm -f $(TESTS) *.o *.gcno *.gcda coverage.*
5098

5199
.PHONY: FORCE

tools/unit-tests/unit-common.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* unit-common.c
2+
*
3+
* Unit test common tools
4+
*
5+
*
6+
* Copyright (C) 2023 wolfSSL Inc.
7+
*
8+
* This file is part of wolfBoot.
9+
*
10+
* wolfBoot is free software; you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation; either version 2 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* wolfBoot is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program; if not, write to the Free Software
22+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
23+
*/
24+
#include <stdint.h>
25+
#include <stdio.h>
26+
#include <string.h>
27+
#include <check.h>
28+
#include "target.h"
29+
#define FLASH_SIZE (33 * 1024)
30+
31+
/* Emulation of external flash with a static buffer of 32KB (update) + 1KB (swap) */
32+
uint8_t flash[FLASH_SIZE];
33+
34+
/* Flash lock/unlock counter */
35+
static int elocked = 0;
36+
37+
#ifndef BACKUP_MOCKED
38+
39+
uint8_t image_backup(uint8_t part_id)
40+
{
41+
printf("Called image_backup\n");
42+
return 0xFF; /* PART_NONE */
43+
}
44+
#endif
45+
46+
#ifndef EXT_MOCKED
47+
48+
/* Mocks for ext_flash_read, ext_flash_write, and ext_flash_erase functions */
49+
int ext_flash_read(uintptr_t address, uint8_t *data, int len) {
50+
printf("Called ext_flash_read %p %p %d\n", address, data, len);
51+
52+
/* Check that the read address and size are within the bounds of the flash memory */
53+
ck_assert_int_le(address + len, FLASH_SIZE);
54+
55+
/* Copy the data from the flash memory to the output buffer */
56+
memcpy(data, &flash[address], len);
57+
58+
printf("Return value: %d\n", len);
59+
return len;
60+
}
61+
62+
int ext_flash_write(uintptr_t address, const uint8_t *data, int len) {
63+
printf("Called ext_flash_write %p %p %d\n", address, data, len);
64+
65+
66+
/* Check that the write address and size are within the bounds of the flash memory */
67+
ck_assert_int_le(address + len, FLASH_SIZE);
68+
69+
/* Copy the data from the input buffer to the flash memory */
70+
memcpy(&flash[address], data, len);
71+
72+
return 0;
73+
}
74+
75+
int ext_flash_erase(uintptr_t address, int len) {
76+
/* Check that the erase address and size are within the bounds of the flash memory */
77+
ck_assert_int_le(address + len, FLASH_SIZE);
78+
79+
/* Check that address is aligned to WOLFBOOT_SECTOR_SIZE */
80+
ck_assert_int_eq(address, address & ~(WOLFBOOT_SECTOR_SIZE - 1));
81+
82+
/* Check that len is aligned to WOLFBOOT_SECTOR_SIZE */
83+
ck_assert_int_eq(len, len & ~(WOLFBOOT_SECTOR_SIZE - 1));
84+
85+
86+
/* Erase the flash memory by setting each byte to 0xFF, WOLFBOOT_SECTOR_SIZE bytes at a time */
87+
uint32_t i;
88+
for (i = address; i < address + len; i += WOLFBOOT_SECTOR_SIZE) {
89+
memset(&flash[i], 0xFF, WOLFBOOT_SECTOR_SIZE);
90+
}
91+
92+
return 0;
93+
}
94+
95+
void ext_flash_unlock(void)
96+
{
97+
fail_unless(elocked, "Double ext unlock detected\n");
98+
elocked--;
99+
}
100+
void ext_flash_lock(void)
101+
{
102+
fail_if(elocked, "Double ext lock detected\n");
103+
elocked++;
104+
}
105+
106+
#endif /* EXT_MOCKED */

0 commit comments

Comments
 (0)