Skip to content

Commit 1c5491d

Browse files
committed
simulator: separate from unit tests and move to its own folder
Simulator was placed in `test/unit-test` folder for ease of implementation. The mocks and the build instructions were already there. However, in ideal structure, simulator must be in its own directory since it is not actually a unit test. This commit opens a new directory named `simulator` under `test`, where the simulator implementation, necessary mocks and its own `CMakeLists.txt` configuration is implemented. To build only the simulator itself, now one can do `make simulator` directly. To build and also run the simulator `make run-simulator` is sufficient. The target executable still resides in `build-build/bin` folder, but its name is now changed to `simulator`. Signed-off-by: asi345 <inanata15@gmail.com>
1 parent b8ec18e commit 1c5491d

17 files changed

+960
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ if(CMAKE_CROSSCOMPILING)
377377
else()
378378
include(CTest)
379379
add_subdirectory(test/unit-test)
380+
add_subdirectory(test/simulator)
380381
if(COVERAGE)
381382
find_program(GCOVR gcovr)
382383
if(NOT GCOVR STREQUAL "GCOVR-NOTFOUND")

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ docs: | build
8585
$(MAKE) -C build doc
8686
rust-docs: | build
8787
$(MAKE) -C build rust-docs
88+
simulator: | build-build
89+
$(MAKE) -C build-build simulator
90+
run-simulator: | simulator
91+
./build-build/bin/simulator
8892
unit-test: | build-build
8993
$(MAKE) -C build-build
9094
# Must compile C tests before running them

test/simulator/CMakeLists.txt

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# Copyright 2018 Shift Cryptosecurity AG
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
#-----------------------------------------------------------------------------
17+
# Build bitbox lib to use in tests
18+
19+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter -Wno-missing-prototypes -Wno-missing-declarations -Wno-implicit-function-declaration -Wno-bad-function-cast")
20+
21+
set(DBB-FILTERED-SOURCES
22+
${DBB-FIRMWARE-SOURCES}
23+
${DBB-FIRMWARE-UI-SOURCES}
24+
${FIRMWARE-U2F-SOURCES}
25+
${DBB-FIRMWARE-USB-SOURCES}
26+
)
27+
28+
set(IGNORE_SOURCES
29+
"src/screen.c"
30+
"src/memory/nvmctrl.c"
31+
"src/memory/smarteeprom.c"
32+
"src/memory.mpu.c"
33+
)
34+
#f9b3ceaa91e
35+
36+
# Exclude some files which depends on the hardware.
37+
foreach(SOURCEFILE ${IGNORE_SOURCES})
38+
list(FILTER DBB-FILTERED-SOURCES EXCLUDE REGEX ".*/${SOURCEFILE}$")
39+
endforeach()
40+
41+
message("FILTERED SOURCES: ${DBB-FILTERED-SOURCES}")
42+
43+
add_library(sd-mock-simulator
44+
STATIC
45+
framework/mock_diskio.c
46+
)
47+
target_include_directories(
48+
sd-mock-simulator
49+
PUBLIC
50+
$<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES>
51+
)
52+
53+
54+
# We create a CMake "object library" to track all the compiled sources so that
55+
# they can be reused between a normal library and the manually crafted "merged"
56+
# library.
57+
# See https://cmake.org/cmake/help/v3.10/command/add_library.html#object-libraries
58+
add_library(bitbox_objects-simulator
59+
OBJECT
60+
${DBB-FILTERED-SOURCES}
61+
${CTAES-SOURCES}
62+
${ETHEREUM-SOURCES}
63+
framework/mock_cipher.c
64+
framework/mock_memory.c
65+
framework/mock_screen.c
66+
framework/mock_smarteeprom.c
67+
framework/mock_securechip.c
68+
)
69+
70+
add_library(bitbox-simulator
71+
STATIC
72+
$<TARGET_OBJECTS:bitbox_objects-simulator>
73+
)
74+
75+
# Here we create the "merged" library, which starts with the c-lib created by
76+
# cargo in the "bitbox02-rust-c" project. That project produces a c-lib without
77+
# mangled symbols so that it is easier to use from C. We then extend that
78+
# library with all the object files declared in "bitbox_objects-simulator", all the code
79+
# we want to be able to call from tests.
80+
# By linking to "bitbox_merged-simulator" in the rust unit tests we get access to all our
81+
# code. (That linkage is done by cargo using a "build.rs" script in the
82+
# bitbox02 rust project.)
83+
add_custom_command(
84+
OUTPUT ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libbitbox_merged-simulator.a
85+
DEPENDS c-unit-tests_rust_c bitbox_objects-simulator $<TARGET_OBJECTS:bitbox_objects-simulator>
86+
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_PROPERTY:c-unit-tests_rust_c,IMPORTED_LOCATION> ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libbitbox_merged-simulator.a
87+
COMMAND ar q ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libbitbox_merged-simulator.a $<TARGET_OBJECTS:bitbox_objects-simulator>
88+
VERBATIM
89+
COMMAND_EXPAND_LISTS
90+
)
91+
92+
add_custom_target(bitbox_merged-simulator DEPENDS ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libbitbox_merged-simulator.a)
93+
94+
target_include_directories(
95+
bitbox_objects-simulator
96+
SYSTEM PUBLIC
97+
${SYSTEMINCLUDES}
98+
${CMAKE_SOURCE_DIR}/external
99+
${CMAKE_SOURCE_DIR}/external/ctaes
100+
${CMAKE_SOURCE_DIR}/external/fatfs/source
101+
$<TARGET_PROPERTY:wallycore,INTERFACE_INCLUDE_DIRECTORIES>
102+
$<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES>
103+
$<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES>
104+
)
105+
target_include_directories(
106+
bitbox_objects-simulator
107+
PUBLIC
108+
${INCLUDES}
109+
${CMAKE_CURRENT_SOURCE_DIR}/framework/includes
110+
${CMAKE_CURRENT_SOURCE_DIR}
111+
${CMAKE_BINARY_DIR}/src
112+
)
113+
114+
target_include_directories(
115+
bitbox-simulator
116+
SYSTEM PUBLIC
117+
${SYSTEMINCLUDES}
118+
${CMAKE_SOURCE_DIR}/external
119+
${CMAKE_SOURCE_DIR}/external/ctaes
120+
${CMAKE_SOURCE_DIR}/external/fatfs/source
121+
$<TARGET_PROPERTY:wallycore,INTERFACE_INCLUDE_DIRECTORIES>
122+
$<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES>
123+
$<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES>
124+
)
125+
126+
target_include_directories(
127+
bitbox-simulator
128+
PUBLIC
129+
${INCLUDES}
130+
${CMAKE_CURRENT_SOURCE_DIR}/framework/includes
131+
${CMAKE_CURRENT_SOURCE_DIR}
132+
${CMAKE_BINARY_DIR}/src
133+
)
134+
135+
add_dependencies(bitbox_objects-simulator
136+
rust-cbindgen
137+
wallycore
138+
secp256k1
139+
ctaes
140+
fatfs
141+
sd-mock-simulator
142+
)
143+
target_link_libraries(bitbox-simulator PRIVATE ${LIBBITBOX02_RUST} "-lm")
144+
145+
target_compile_definitions(bitbox_objects-simulator PUBLIC "PRODUCT_BITBOX_MULTI=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1")
146+
target_compile_definitions(bitbox_objects-simulator PUBLIC TESTING)
147+
148+
# Since wallycore is an external projects we need to specify the dependency
149+
add_dependencies(bitbox_objects-simulator libwally-core)
150+
151+
target_compile_definitions(bitbox-simulator PUBLIC "PRODUCT_BITBOX_MULTI=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1")
152+
target_compile_definitions(bitbox-simulator PUBLIC TESTING)
153+
154+
target_link_libraries(bitbox-simulator
155+
PUBLIC
156+
secp256k1
157+
PRIVATE
158+
wallycore
159+
ctaes
160+
fatfs
161+
sd-mock-simulator
162+
)
163+
164+
if(SANITIZE_ADDRESS)
165+
target_compile_options(bitbox_objects-simulator PUBLIC "-fsanitize=address")
166+
target_compile_options(bitbox-simulator PUBLIC "-fsanitize=address")
167+
endif()
168+
if(SANTIZE_UNDEFINED)
169+
target_compile_options(bitbox_objects-simulator PUBLIC "-fsanitize=undefined")
170+
target_compile_options(bitbox-simulator PUBLIC "-fsanitize=undefined")
171+
endif()
172+
if(COVERAGE)
173+
target_link_libraries(bitbox-simulator PUBLIC "--coverage")
174+
target_compile_options(bitbox_objects-simulator PUBLIC "--coverage")
175+
target_compile_options(bitbox-simulator PUBLIC "--coverage")
176+
endif()
177+
178+
#-----------------------------------------------------------------------------
179+
# Simulator
180+
181+
add_executable(simulator simulator.c framework/eh_personality.c)
182+
# asan must be first library in linking order
183+
target_link_libraries(simulator PRIVATE
184+
$<$<BOOL:${SANITIZE_ADDRESS}>:asan>
185+
$<$<BOOL:${SANITIZE_UNDEFINED}>:-fsanitize=undefined>
186+
-Wl,--start-group
187+
c-unit-tests_rust_c
188+
bitbox-simulator
189+
-Wl,--end-group
190+
""
191+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2022 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Needed to link the C unit test executables in /test/unit-test, which link to bitbox_merged.
16+
// `rust_eh_personality` is provided by Rust when building the firmware or running Rust unit tests
17+
//
18+
// See
19+
// https://doc.rust-lang.org/unstable-book/language-features/lang-items.html#writing-an-executable-without-stdlib.
20+
//
21+
// One could get rid of this and also considerably shrink the binary size by compiling core instead
22+
// of using pre-built binaries. See a proof of concept implementation here:
23+
// https://github.com/digitalbitbox/bitbox02-firmware/tree/build-std-PoC. We decided against doing
24+
// this for now as the feature seems immature and because of the warnings against using it in
25+
// production:
26+
// https://github.com/rust-lang/wg-cargo-std-aware/tree/81765f0eb744b9c47840c16f43a32c9f61fd7f0c#mvp-implementation
27+
void rust_eh_personality(void);
28+
void rust_eh_personality(void) {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2023 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef _MOCK_CIPHER_H_
16+
#define _MOCK_CIPHER_H_
17+
18+
#include <stdint.h>
19+
20+
void cipher_mock_iv(uint8_t* iv_out);
21+
22+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2019 Shift Cryptosecurity AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef _MOCK_MEMORY_H_
16+
#define _MOCK_MEMORY_H_
17+
18+
#include <stdbool.h>
19+
#include <stdint.h>
20+
21+
#include <flags.h>
22+
23+
void mock_memory_factoryreset(void);
24+
bool memory_write_to_address_mock(uint32_t base, uint32_t addr, const uint8_t* chunk);
25+
bool memory_write_chunk_mock(uint32_t chunk_num, const uint8_t* chunk);
26+
void memory_read_chunk_mock(uint32_t chunk_num, uint8_t* chunk_out);
27+
// Size: `FLASH_SHARED_DATA_LEN`.
28+
void memory_read_shared_bootdata_mock(uint8_t* chunk_out);
29+
void mock_memory_set_salt_root(const uint8_t* salt_root);
30+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2019 Shift Cryptosecurity AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef _QTOUCH_H_
16+
#define _QTOUCH_H_
17+
18+
#include <stdint.h>
19+
20+
void __wrap_qtouch_process(void);
21+
22+
uint8_t __wrap_qtouch_get_scroller_is_active(uint16_t sensor_node);
23+
24+
uint16_t __wrap_qtouch_get_scroller_position(uint16_t sensor_node);
25+
26+
void qtouch_process(void);
27+
28+
uint8_t qtouch_is_scroller_active(uint16_t sensor_node);
29+
30+
uint16_t qtouch_get_scroller_position(uint16_t sensor_node);
31+
32+
void qtouch_force_calibrate(void);
33+
34+
#endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2019 Shift Cryptosecurity AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef _TEST_SCREEN_STACK_H
16+
#define _TEST_SCREEN_STACK_H
17+
18+
void mock_screen_stack_assert_clean(void);
19+
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2019 Shift Cryptosecurity AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef _TEST_RANDOM_H
16+
#define _TEST_RANDOM_H
17+
18+
int __wrap_rand(void);
19+
int __wrap_wally_sha256(
20+
const unsigned char* bytes,
21+
size_t bytes_len,
22+
unsigned char* bytes_out,
23+
size_t len);
24+
25+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2023 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <string.h>
16+
17+
#include <mock_cipher.h>
18+
19+
void cipher_mock_iv(uint8_t* iv_out)
20+
{
21+
memset(iv_out, 'a', 32);
22+
}

0 commit comments

Comments
 (0)