Skip to content

Commit

Permalink
Merge pull request #379 from danielinux/unit-test-extend
Browse files Browse the repository at this point in the history
Added more unit tests. Added 'make cov'.
  • Loading branch information
dgarske authored Oct 13, 2023
2 parents cd385df + b497ee3 commit c1abfdd
Show file tree
Hide file tree
Showing 10 changed files with 1,558 additions and 25 deletions.
23 changes: 3 additions & 20 deletions .github/workflows/test-units.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,10 @@ jobs:
run: |
make keysclean && make -C tools/keytools clean && rm -f include/target.h
- name: Build wolfboot and test footprint
- name: Build unit tests
run: |
make -C tools/unit-tests
- name: Run manifest header parser unit tests
run: |
./tools/unit-tests/unit-parser
- name: Run non-encrypted ext_flash unit tests
run: |
./tools/unit-tests/unit-extflash
- name: Run aes128 ext_flash unit tests
run: |
./tools/unit-tests/unit-aes128
- name: Run aes256 ext_flash unit tests
run: |
./tools/unit-tests/unit-aes256
- name: Run chacha20 ext_flash unit tests
- name: Run unit tests
run: |
./tools/unit-tests/unit-chacha20
make -C tools/unit-tests run
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,10 @@ stage1/loader_stage1.ld
debug/lauterbach

#cland cache
.cache/*
.cache/*

#gcov files
*.gcov
*.gcda
*.gcno
coverage.*
6 changes: 6 additions & 0 deletions src/libwolfboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr)
(void*)&wolfboot_magic_trail, sizeof(uint32_t));
#endif

#ifndef MOCK_PARTITION_TRAILER
#ifdef EXT_FLASH

/**
Expand Down Expand Up @@ -558,6 +559,7 @@ static void RAMFUNCTION set_partition_magic(uint8_t part)
}
}
#endif /* EXT_FLASH */
#endif /* MOCK_PARTITION_TRAILER */



Expand Down Expand Up @@ -626,6 +628,8 @@ int RAMFUNCTION wolfBoot_set_partition_state(uint8_t part, uint8_t newst)
{
uint32_t *magic;
uint8_t *state;
if (part == PART_NONE)
return -1;
magic = get_partition_magic(part);
if (*magic != WOLFBOOT_MAGIC_TRAIL)
set_partition_magic(part);
Expand Down Expand Up @@ -669,6 +673,8 @@ int RAMFUNCTION wolfBoot_get_partition_state(uint8_t part, uint8_t *st)
{
uint32_t *magic;
uint8_t *state;
if (part == PART_NONE)
return -1;
magic = get_partition_magic(part);
if (*magic != WOLFBOOT_MAGIC_TRAIL)
return -1;
Expand Down
56 changes: 52 additions & 4 deletions tools/unit-tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,50 @@ endif

CFLAGS=-I. -I../../src -I../../include -I../../lib/wolfssl
CFLAGS+=-g -ggdb
CFLAGS+=-fprofile-arcs
CFLAGS+=-ftest-coverage
CFLAGS+=--coverage
LDFLAGS+=-fprofile-arcs
LDFLAGS+=-ftest-coverage
WOLFCRYPT=../../lib/wolfssl/



all: unit-parser unit-extflash unit-aes128 unit-aes256 unit-chacha20
TESTS:=unit-parser unit-extflash unit-aes128 unit-aes256 unit-chacha20 unit-pci \
unit-mock-state unit-sectorflags unit-image

all: $(TESTS)

cov:
gcovr -f "^\.\.\/\.\.\/src.*\.c" -r ../.. --verbose \
--merge-mode-functions merge-use-line-0 \
--html-medium-threshold 60 \
--html-high-threshold 80 \
--html-details coverage.html
firefox coverage.html

run: $(TESTS)
for unit in $(TESTS); do \
./$$unit || exit 1; \
done


unit-aes128:CFLAGS+=-DEXT_ENCRYPTED -DENCRYPT_WITH_AES128
unit-aes256:CFLAGS+=-DEXT_ENCRYPTED -DENCRYPT_WITH_AES256
unit-chacha20:CFLAGS+=-DEXT_ENCRYPTED -DENCRYPT_WITH_CHACHA
unit-parser:CFLAGS+=-DNVM_FLASH_WRITEONCE


WOLFCRYPT_SRC:=$(WOLFCRYPT)/wolfcrypt/src/sha.c \
$(WOLFCRYPT)/wolfcrypt/src/sha256.c \
$(WOLFCRYPT)/wolfcrypt/src/sp_int.c \
$(WOLFCRYPT)/wolfcrypt/src/sp_c64.c \
$(WOLFCRYPT)/wolfcrypt/src/random.c \
$(WOLFCRYPT)/wolfcrypt/src/memory.c

WOLFCRYPT_CFLAGS+=-DWOLFSSL_USER_SETTINGS -DWOLFBOOT_SIGN_ECC256 -DWOLFBOOT_SIGN_ECC256 -DHAVE_ECC_KEY_IMPORT -DWOLFBOOT_HASH_SHA256 -D__WOLFBOOT



../../include/target.h: FORCE
cp -f target.h $@
Expand All @@ -24,7 +59,7 @@ unit-extflash.o: FORCE
rm -f $@
gcc -c -o $@ unit-extflash.c $(CFLAGS)

unit-parser: ../../include/target.h unit-parser.o
unit-parser: ../../include/target.h unit-parser.c
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)

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

unit-mock-state: ../../include/target.h unit-mock-state.c
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)

unit-sectorflags: ../../include/target.h unit-sectorflags.c
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)

unit-image: unit-image.c unit-common.c $(WOLFCRYPT_SRC)
gcc -o $@ $^ $(CFLAGS) $(WOLFCRYPT_CFLAGS) $(LDFLAGS)

%.o:%.c
gcc -c -o $@ $^ $(CFLAGS)

clean:
rm -f unit-parser unit-extflash *.o

covclean:
rm -f *.gcov *.gcno *.gcda coverage.*

clean: covclean
rm -f $(TESTS) *.o *.gcno *.gcda coverage.*

.PHONY: FORCE
106 changes: 106 additions & 0 deletions tools/unit-tests/unit-common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* unit-common.c
*
* Unit test common tools
*
*
* Copyright (C) 2023 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <check.h>
#include "target.h"
#define FLASH_SIZE (33 * 1024)

/* Emulation of external flash with a static buffer of 32KB (update) + 1KB (swap) */
uint8_t flash[FLASH_SIZE];

/* Flash lock/unlock counter */
static int elocked = 0;

#ifndef BACKUP_MOCKED

uint8_t image_backup(uint8_t part_id)
{
printf("Called image_backup\n");
return 0xFF; /* PART_NONE */
}
#endif

#ifndef EXT_MOCKED

/* Mocks for ext_flash_read, ext_flash_write, and ext_flash_erase functions */
int ext_flash_read(uintptr_t address, uint8_t *data, int len) {
printf("Called ext_flash_read %p %p %d\n", address, data, len);

/* Check that the read address and size are within the bounds of the flash memory */
ck_assert_int_le(address + len, FLASH_SIZE);

/* Copy the data from the flash memory to the output buffer */
memcpy(data, &flash[address], len);

printf("Return value: %d\n", len);
return len;
}

int ext_flash_write(uintptr_t address, const uint8_t *data, int len) {
printf("Called ext_flash_write %p %p %d\n", address, data, len);


/* Check that the write address and size are within the bounds of the flash memory */
ck_assert_int_le(address + len, FLASH_SIZE);

/* Copy the data from the input buffer to the flash memory */
memcpy(&flash[address], data, len);

return 0;
}

int ext_flash_erase(uintptr_t address, int len) {
/* Check that the erase address and size are within the bounds of the flash memory */
ck_assert_int_le(address + len, FLASH_SIZE);

/* Check that address is aligned to WOLFBOOT_SECTOR_SIZE */
ck_assert_int_eq(address, address & ~(WOLFBOOT_SECTOR_SIZE - 1));

/* Check that len is aligned to WOLFBOOT_SECTOR_SIZE */
ck_assert_int_eq(len, len & ~(WOLFBOOT_SECTOR_SIZE - 1));


/* Erase the flash memory by setting each byte to 0xFF, WOLFBOOT_SECTOR_SIZE bytes at a time */
uint32_t i;
for (i = address; i < address + len; i += WOLFBOOT_SECTOR_SIZE) {
memset(&flash[i], 0xFF, WOLFBOOT_SECTOR_SIZE);
}

return 0;
}

void ext_flash_unlock(void)
{
fail_unless(elocked, "Double ext unlock detected\n");
elocked--;
}
void ext_flash_lock(void)
{
fail_if(elocked, "Double ext lock detected\n");
elocked++;
}

#endif /* EXT_MOCKED */
Loading

0 comments on commit c1abfdd

Please sign in to comment.