forked from LedgerHQ/app-recovery-check
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for shamir functions
- Loading branch information
Showing
11 changed files
with
387 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Editor Files and Folders | ||
|
||
.idea/ | ||
.vscode/ | ||
.DS_Store | ||
*~ | ||
\#*# | ||
|
||
# Build Files and Binaries | ||
|
||
*.log | ||
*.o | ||
*.so | ||
*.dll | ||
*.dylib | ||
cmake-build-*/ | ||
*build/ | ||
build/ | ||
|
||
# Coverage file | ||
coverage.info | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
if(${CMAKE_VERSION} VERSION_LESS 3.10) | ||
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) | ||
endif() | ||
|
||
# project information | ||
project(unit_tests | ||
VERSION 0.1 | ||
DESCRIPTION "Unit tests for app-seed-tool Ledger Application" | ||
LANGUAGES C) | ||
|
||
|
||
# guard against bad build-type strings | ||
if (NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE "Debug") | ||
endif() | ||
|
||
include(CTest) | ||
ENABLE_TESTING() | ||
|
||
# specify C standard | ||
set(CMAKE_C_STANDARD 11) | ||
set(CMAKE_C_STANDARD_REQUIRED True) | ||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -pedantic -g -O0 --coverage") | ||
|
||
set(GCC_COVERAGE_LINK_FLAGS "--coverage -lgcov") | ||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}") | ||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}") | ||
|
||
# guard against in-source builds | ||
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR}) | ||
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt. ") | ||
endif() | ||
|
||
# Fetch cmocka | ||
find_package(cmocka QUIET) | ||
include(FetchContent) | ||
FetchContent_Declare( | ||
cmocka | ||
GIT_REPOSITORY https://git.cryptomilk.org/projects/cmocka.git | ||
GIT_TAG cmocka-1.1.5 | ||
GIT_SHALLOW 1 | ||
) | ||
set(WITH_STATIC_LIB ON CACHE BOOL "CMocka: Build with a static library" FORCE) | ||
set(WITH_CMOCKERY_SUPPORT OFF CACHE BOOL "CMocka: Install a cmockery header" FORCE) | ||
set(WITH_EXAMPLES OFF CACHE BOOL "CMocka: Build examples" FORCE) | ||
set(UNIT_TESTING OFF CACHE BOOL "CMocka: Build with unit testing" FORCE) | ||
set(PICKY_DEVELOPER OFF CACHE BOOL "CMocka: Build with picky developer flags" FORCE) | ||
FetchContent_MakeAvailable(cmocka) | ||
|
||
add_compile_definitions(TEST DEBUG=0 SKIP_FOR_CMOCKA) | ||
add_compile_definitions(HAVE_HASH HAVE_HMAC HAVE_SHA256) | ||
|
||
include_directories(./lib ../../src/bc-sskr/bc-shamir $ENV{LEDGER_SECURE_SDK}/include $ENV{LEDGER_SECURE_SDK}/lib_cxng/include $ENV{LEDGER_SECURE_SDK}/lib_cxng/src) | ||
|
||
# add src | ||
set(LIB_SOURCES ./lib/random.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_ram.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_hash.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_sha256.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_hmac.c $ENV{LEDGER_SECURE_SDK}/lib_cxng/src/cx_utils.c) | ||
add_library(unittest SHARED ${LIB_SOURCES} ) | ||
|
||
# add cmocka tests | ||
set(LIB_SOURCES ../../src/bc-sskr/bc-shamir/shamir.c ../../src/bc-sskr/bc-shamir/interpolate.c ../../src/bc-sskr/bc-shamir/hazmat.c) | ||
add_library(shamir SHARED ${LIB_SOURCES} ) | ||
add_executable(test_shamir tests/shamir.c) | ||
target_link_libraries(test_shamir PUBLIC cmocka gcov shamir unittest) | ||
|
||
foreach(target test_shamir) | ||
add_test(NAME ${target} COMMAND ${target}) | ||
endforeach() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
MAKEFLAGS += --no-print-directory | ||
|
||
RM ?= rm -f | ||
ECHO = `which echo` | ||
|
||
ifneq (,$(findstring xterm,${TERM})) | ||
GREEN := \e[0;32m | ||
RED := \e[0;31m | ||
CYAN := \e[0;36m | ||
RESET := \e[0m | ||
else | ||
GREEN := "" | ||
RED := "" | ||
RESET := "" | ||
endif | ||
|
||
BUILD_DIRECTORY = $(realpath build/) | ||
|
||
DIRECTORY_BUILD = build | ||
|
||
all: | ||
@cmake -B ${DIRECTORY_BUILD} -H. | ||
@make -C ${DIRECTORY_BUILD} | ||
@CTEST_OUTPUT_ON_FAILURE=1 make -C ${DIRECTORY_BUILD} test | ||
|
||
coverage: all | ||
@lcov --directory . -b "${BUILD_DIRECTORY}" --capture --initial -o coverage.base | ||
@lcov --rc lcov_branch_coverage=1 --directory . -b "${BUILD_DIRECTORY}" --capture -o coverage.capture | ||
@lcov --directory . -b "${BUILD_DIRECTORY}" --add-tracefile coverage.base --add-tracefile coverage.capture -o coverage.info | ||
@lcov --directory . -b "${BUILD_DIRECTORY}" --remove coverage.info '*/build/_deps/cmocka-src/src/*' '*/unit/lib/*' '*/unit/tests/*' '${LEDGER_SECURE_SDK}/lib_cxng/src/*' -o coverage.info | ||
@$(ECHO) -e "${GREEN}[ OK ]${RESET} Generated 'coverage.info'." | ||
@genhtml coverage.info -o coverage | ||
@if [ -f coverage.base ]; then $(ECHO) -e "${RED}[ RM ]${RESET}" coverage.base && $(RM) -r coverage.base ; fi; | ||
@if [ -f coverage.capture ]; then $(ECHO) -e "${RED}[ RM ]${RESET}" coverage.capture && $(RM) -r coverage.capture ; fi; | ||
|
||
clean: | ||
@if [ -d ${DIRECTORY_BUILD} ]; then $(ECHO) -e "${RED}[ RM ]${RESET}" ${DIRECTORY_BUILD} && $(RM) -r ${DIRECTORY_BUILD} ; fi; | ||
@if [ -d coverage ]; then $(ECHO) -e "${RED}[ RM ]${RESET}" coverage && $(RM) -r coverage ; fi; | ||
@if [ -f coverage.info ]; then $(ECHO) -e "${RED}[ RM ]${RESET}" coverage.info && $(RM) -r coverage.info ; fi; | ||
|
||
.PHONY: all coverage clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Unit tests | ||
|
||
It is important to unit test your functions. | ||
This also allows you to document how your functions work. | ||
We use the library [**cmocka**](https://cmocka.org/#features) | ||
|
||
## Requirement | ||
|
||
- [CMake >= 3.10](https://cmake.org/download/) | ||
- [lcov >= 1.14](http://ltp.sourceforge.net/coverage/lcov.php) | ||
|
||
Don't worry, you don't necessarily need to install the `cmocka library` because the **cmakelist automatically fetches** the library | ||
|
||
## Add new test | ||
|
||
Create new file into `tests` folder and follow [this initiation](https://cmocka.org/talks/cmocka_unit_testing_and_mocking.pdf) | ||
|
||
Now go to the `CMakeLists.txt` file and add your test with the specific file you want to test. | ||
|
||
## Usage | ||
|
||
### Build | ||
|
||
The `default rules` of makefile will compile the tests and run them. | ||
|
||
```sh | ||
make | ||
``` | ||
|
||
The `coverage rule` will launch the default rules and generate the coverage and you will be **automatically redirected** to the generated .html | ||
```sh | ||
make coverage | ||
``` | ||
|
||
The `clean rule` will delete the folders and files generated | ||
```sh | ||
make clean | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <stdint.h> | ||
#include <stddef.h> | ||
|
||
unsigned char *fake_rng(uint8_t *buffer, size_t len) | ||
{ | ||
for (size_t i = 0; i < len; i++) { | ||
buffer[i] = i % 256; | ||
} | ||
return buffer; | ||
} |
Oops, something went wrong.