From 45745ba86347ef5ac545c06edb0e3049cc663837 Mon Sep 17 00:00:00 2001 From: Juan Leni Date: Mon, 2 Sep 2024 19:28:52 +0200 Subject: [PATCH] update test structure --- .clang-format | 5 ++ .clang-tidy | 22 ++++++++ .editorconfig | 16 ------ .gitignore | 75 +++++++++---------------- CMakeLists.txt | 82 ++++++++++++++++++++-------- tests_zemu/.editorconfig | 12 ---- tests_zemu/.eslintrc.js | 24 -------- tests_zemu/.npmignore | 1 - tests_zemu/eslint.config.js | 36 ++++++++++++ tests_zemu/globalsetup.js | 25 +++++---- tests_zemu/jest.config.js | 2 + tests_zemu/jest.js | 2 - tests_zemu/tests/pullImageKillOld.ts | 7 ++- tests_zemu/tsconfig.json | 5 +- 14 files changed, 173 insertions(+), 141 deletions(-) create mode 100644 .clang-format create mode 100644 .clang-tidy delete mode 100644 .editorconfig delete mode 100644 tests_zemu/.editorconfig delete mode 100644 tests_zemu/.eslintrc.js delete mode 100644 tests_zemu/.npmignore create mode 100644 tests_zemu/eslint.config.js delete mode 100644 tests_zemu/jest.js diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..1f7d1915 --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +BasedOnStyle: Google +IndentWidth: 4 +ColumnLimit: 125 +DerivePointerAlignment: false +PointerAlignment: Right diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 00000000..2cefa098 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,22 @@ +Checks: "-*, + clang-diagnostic-*, + clang-analyzer-*, + cppcoreguidelines-init-variables, + google-runtime-int, + google-readability-avoid-underscore-in-googletest-name, + misc-*, + performance-*, + portability-*, + readability-*, + -misc-no-recursion, + -readability-function-cognitive-complexity" +WarningsAsErrors: "*" +CheckOptions: + - key: readability-identifier-length.MinimumVariableNameLength + value: 2 + - key: readability-identifier-length.MinimumParameterNameLength + value: 2 + - key: readability-identifier-length.MinimumLoopCounterNameLength + value: 1 + - key: readability-magic-numbers.IgnorePowersOf2IntegerValues + value: true diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 2d7db14f..00000000 --- a/.editorconfig +++ /dev/null @@ -1,16 +0,0 @@ -# top-most EditorConfig file -root = true - -[*] -charset = utf-8 -trim_trailing_whitespace = true -end_of_line = lf -insert_final_newline = true - -[*.{c,h,cpp,hpp}] -indent_style = space -indent_size = 4 - -[*.{yml,sh}] -indent_style = space -indent_size = 2 diff --git a/.gitignore b/.gitignore index 48621352..518767e2 100644 --- a/.gitignore +++ b/.gitignore @@ -47,55 +47,34 @@ # Kernel Module Compile Results *.mod* *.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf - -\cmake-build-debug -\.idea/ -/tmp/ -/deps/nano2-sdk/ - -# Created by cmake -googletest-download/ -googletest-src/ -googletest-build/ -CMakeFiles/ -CMakeCache.txt -unittests -*.cmake -Testing/ -cmake-build-fuzz/ - -# Others -/cmake-build-debug/ -/cmake-build-fuzz/ -\.idea -/app/bin/ -/app/debug/ -/app/obj/ - -\deps/* -!\deps/nanos-secure-sdk -!\deps/nanox-secure-sdk -!\deps/ledger-zxlib -!\deps/tinycbor -!\deps/tinycbor-ledger -!\deps/BLAKE - -app/src/glyphs.c - -app/src/glyphs.h -/build -.vscode/dryrun.log -.vscode/targets.log + +########################## + +tests_zemu/.yarn/* +tests_zemu/!.yarn/patches +tests_zemu/!.yarn/releases +tests_zemu/!.yarn/plugins +tests_zemu/!.yarn/sdks +tests_zemu/!.yarn/versions +tests_zemu/.pnp.* +######################### + +.vscode +.idea + +node_modules +fuzz/corpora + +!build/.gitkeep + + +build/* +cmake-build-debug app/build/* -.DS_Store +tests_zemu/snapshots-tmp +tests_zemu/yarn.lock +tests_tools/target fuzz-*.log -/fuzz/corpora -.vscode/ -tests_zemu/yarn.lock +/scan-build diff --git a/CMakeLists.txt b/CMakeLists.txt index 0faeae86..b58baaec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ #* See the License for the specific language governing permissions and #* limitations under the License. #******************************************************************************** -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.28) include("cmake/HunterGate.cmake") HunterGate( URL "https://github.com/cpp-pm/hunter/archive/v0.25.5.tar.gz" @@ -21,15 +21,25 @@ HunterGate( LOCAL ) -project(ledger-cosmos VERSION 0.0.0) -enable_testing() +if(CMAKE_GENERATOR MATCHES "Ninja") + message(FATAL_ERROR "This project does not support the Ninja generator. " + "Please use Unix Makefiles or another supported generator. " + "This error is typical in CLion. In this case, switch to generator Unix Makefiles.") +endif() + +######################################################## +project(ledger-spacemesh VERSION 0.0.0) + +set(CMAKE_CXX_STANDARD 17) cmake_policy(SET CMP0025 NEW) -set(CMAKE_CXX_STANDARD 11) +cmake_policy(SET CMP0144 NEW) set(HUNTER_STATUS_DEBUG ON) set(HUNTER_TLS_VERIFY OFF) +enable_testing() + option(ENABLE_FUZZING "Build with fuzzing instrumentation and build fuzz targets" OFF) option(ENABLE_COVERAGE "Build with source code coverage instrumentation" OFF) option(ENABLE_SANITIZERS "Build with ASAN and UBSAN" OFF) @@ -42,8 +52,10 @@ add_definitions(-DAPP_STANDARD) hunter_add_package(fmt) find_package(fmt CONFIG REQUIRED) + hunter_add_package(jsoncpp) find_package(jsoncpp CONFIG REQUIRED) + hunter_add_package(GTest) find_package(GTest CONFIG REQUIRED) @@ -66,9 +78,9 @@ if(ENABLE_FUZZING) endif() else() message(FATAL_ERROR - "You are using an unsupported compiler! Fuzzing only works with Clang 10.\n" + "Unsupported compiler! Fuzzing requires Clang 10.\n" "1. Install clang-10 \n" - "2. Pass -DCMAKE_C_COMPILER=clang-10 -DCMAKE_CXX_COMPILER=clang++-10") + "2. Use -DCMAKE_C_COMPILER=clang-10 -DCMAKE_CXX_COMPILER=clang++-10") endif() string(APPEND CMAKE_C_FLAGS " -fsanitize=fuzzer-no-link") @@ -88,12 +100,37 @@ if(ENABLE_SANITIZERS) string(APPEND CMAKE_LINKER_FLAGS " -fsanitize=address,undefined -fsanitize-recover=address,undefined") endif() +set (RETRIEVE_MAJOR_CMD + "cat ${CMAKE_CURRENT_SOURCE_DIR}/app/Makefile.version | grep APPVERSION_M | cut -b 14- | tr -d '\n'" +) +set (RETRIEVE_MINOR_CMD + "cat ${CMAKE_CURRENT_SOURCE_DIR}/app/Makefile.version | grep APPVERSION_N | cut -b 14- | tr -d '\n'" +) +execute_process( + COMMAND bash "-c" ${RETRIEVE_MAJOR_CMD} + RESULT_VARIABLE MAJOR_RESULT + OUTPUT_VARIABLE MAJOR_VERSION +) +execute_process( + COMMAND bash "-c" ${RETRIEVE_MINOR_CMD} + RESULT_VARIABLE MINOR_RESULT + OUTPUT_VARIABLE MINOR_VERSION +) + +message(STATUS "LEDGER_MAJOR_VERSION [${MAJOR_RESULT}]: ${MAJOR_VERSION}" ) +message(STATUS "LEDGER_MINOR_VERSION [${MINOR_RESULT}]: ${MINOR_VERSION}" ) + +add_definitions( + -DLEDGER_MAJOR_VERSION=${MAJOR_VERSION} + -DLEDGER_MINOR_VERSION=${MINOR_VERSION} +) + string(APPEND CMAKE_CXX_FLAGS " -fsanitize=address -fno-omit-frame-pointer") string(APPEND CMAKE_LINKER_FLAGS " -fsanitize=address -fno-omit-frame-pointer") ############################################################## ############################################################## -# static libs +# Static Libraries file(GLOB_RECURSE JSMN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/deps/jsmn/src/jsmn.c ) @@ -142,6 +179,19 @@ target_include_directories(app_lib PUBLIC target_link_libraries(app_lib PUBLIC) ############################################################## +# Fuzz Targets +if(ENABLE_FUZZING) + set(FUZZ_TARGETS + parser_parse + parser_message_parse + ) + + foreach(target ${FUZZ_TARGETS}) + add_executable(fuzz-${target} ${CMAKE_CURRENT_SOURCE_DIR}/fuzz/${target}.cpp) + target_link_libraries(fuzz-${target} PRIVATE app_lib) + target_link_options(fuzz-${target} PRIVATE "-fsanitize=fuzzer") + endforeach() +else() ############################################################## # Tests file(GLOB_RECURSE TESTS_SRC @@ -149,13 +199,15 @@ file(GLOB_RECURSE TESTS_SRC add_executable(unittests ${TESTS_SRC}) target_include_directories(unittests PRIVATE + ${gtest_SOURCE_DIR}/include + ${gmock_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/deps/jsmn/src ${CMAKE_CURRENT_SOURCE_DIR}/deps/tinycbor/src ) target_link_libraries(unittests PRIVATE - GTest::gtest_main app_lib + GTest::gtest_main fmt::fmt JsonCpp::JsonCpp) @@ -164,18 +216,4 @@ add_compile_definitions(APP_TESTING=1) add_compile_definitions(COMPILE_TEXTUAL=1) add_test(NAME unittests COMMAND unittests) set_tests_properties(unittests PROPERTIES WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests) - -############################################################## -############################################################## -# Fuzz Targets -if(ENABLE_FUZZING) - set(FUZZ_TARGETS - parser_parse - ) - - foreach(target ${FUZZ_TARGETS}) - add_executable(fuzz-${target} ${CMAKE_CURRENT_SOURCE_DIR}/fuzz/${target}.cpp) - target_link_libraries(fuzz-${target} PRIVATE app_lib) - target_link_options(fuzz-${target} PRIVATE "-fsanitize=fuzzer") - endforeach() endif() diff --git a/tests_zemu/.editorconfig b/tests_zemu/.editorconfig deleted file mode 100644 index 4a7ea303..00000000 --- a/tests_zemu/.editorconfig +++ /dev/null @@ -1,12 +0,0 @@ -root = true - -[*] -indent_style = space -indent_size = 2 -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -[*.md] -trim_trailing_whitespace = false diff --git a/tests_zemu/.eslintrc.js b/tests_zemu/.eslintrc.js deleted file mode 100644 index caf98057..00000000 --- a/tests_zemu/.eslintrc.js +++ /dev/null @@ -1,24 +0,0 @@ -module.exports = { - root: true, - parser: '@typescript-eslint/parser', - plugins: ['@typescript-eslint', 'jest'], - extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:jest/recommended', 'prettier'], - env: { - browser: true, - es6: true, - node: true, - }, - settings: {}, - globals: { - Atomics: 'readonly', - SharedArrayBuffer: 'readonly', - }, - parserOptions: {}, - rules: { - '@typescript-eslint/explicit-function-return-type': 'off', - '@typescript-eslint/explicit-module-boundary-types': 'off', - '@typescript-eslint/no-explicit-any': 'off', - '@typescript-eslint/no-var-requires': 0, - '@typescript-eslint/ban-ts-comment': 'off', - }, -} diff --git a/tests_zemu/.npmignore b/tests_zemu/.npmignore deleted file mode 100644 index 3c3629e6..00000000 --- a/tests_zemu/.npmignore +++ /dev/null @@ -1 +0,0 @@ -node_modules diff --git a/tests_zemu/eslint.config.js b/tests_zemu/eslint.config.js new file mode 100644 index 00000000..27568347 --- /dev/null +++ b/tests_zemu/eslint.config.js @@ -0,0 +1,36 @@ +module.exports = { + languageOptions: { + ecmaVersion: 'latest', + sourceType: 'module', + globals: { + window: 'readonly', + document: 'readonly', + process: 'readonly', + require: 'readonly', + }, + parserOptions: { + project: 'tsconfig.json', + }, + }, + ignores: ['dist/*', 'node_modules/*'], + plugins: { + 'unused-imports': require('eslint-plugin-unused-imports'), + '@typescript-eslint': require('@typescript-eslint/eslint-plugin'), + 'eslint-plugin-tsdoc': require('eslint-plugin-tsdoc'), + }, + rules: { + curly: 'warn', + 'prefer-const': 'warn', + 'no-else-return': 'warn', + complexity: ['warn', 1000], + 'no-unneeded-ternary': 'warn', + 'no-alert': 'warn', + 'no-empty': 'warn', + 'no-useless-catch': 'error', + 'require-await': 'warn', + 'no-continue': 'warn', + 'no-console': 'warn', + 'unused-imports/no-unused-imports': 'warn', + 'no-magic-numbers': 'off', + }, +} diff --git a/tests_zemu/globalsetup.js b/tests_zemu/globalsetup.js index a7875ea2..5837308b 100644 --- a/tests_zemu/globalsetup.js +++ b/tests_zemu/globalsetup.js @@ -1,16 +1,21 @@ -import Zemu from '@zondax/zemu' +const Zemu = require('@zondax/zemu') -const catchExit = async () => { - process.on('SIGINT', () => { - Zemu.stopAllEmuContainers(function () { - process.exit() - }) +/** + * Sets up a handler to stop all emulator containers when a SIGINT is received. + */ +const catchExit = () => { + process.on('SIGINT', async () => { + await Zemu.default.stopAllEmuContainers() + process.exit() }) } +/** + * Initializes the emulator environment by setting up exit handlers, + * pulling the latest emulator image, and stopping any running emulator containers. + */ module.exports = async () => { - await catchExit() - await Zemu.checkAndPullImage() - await Zemu.stopAllEmuContainers() + catchExit() + await Zemu.default.checkAndPullImage() + await Zemu.default.stopAllEmuContainers() } -// diff --git a/tests_zemu/jest.config.js b/tests_zemu/jest.config.js index c4a20eb0..0f3cac0a 100644 --- a/tests_zemu/jest.config.js +++ b/tests_zemu/jest.config.js @@ -2,4 +2,6 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', transformIgnorePatterns: ['^.+\\.js$'], + reporters: ['default', ['summary', { summaryThreshold: 1 }]], + globalSetup: './globalsetup.js', } diff --git a/tests_zemu/jest.js b/tests_zemu/jest.js deleted file mode 100644 index 2fa397e7..00000000 --- a/tests_zemu/jest.js +++ /dev/null @@ -1,2 +0,0 @@ -export default jest; -export const { expect, test } = global; diff --git a/tests_zemu/tests/pullImageKillOld.ts b/tests_zemu/tests/pullImageKillOld.ts index ddd30b71..12b86dcf 100644 --- a/tests_zemu/tests/pullImageKillOld.ts +++ b/tests_zemu/tests/pullImageKillOld.ts @@ -1,4 +1,7 @@ import Zemu from '@zondax/zemu' -Zemu.checkAndPullImage() -Zemu.stopAllEmuContainers() +async function main() { + await Zemu.checkAndPullImage() + await Zemu.stopAllEmuContainers() +} +main().catch(console.error) diff --git a/tests_zemu/tsconfig.json b/tests_zemu/tsconfig.json index 3435f95c..014ae6be 100644 --- a/tests_zemu/tsconfig.json +++ b/tests_zemu/tsconfig.json @@ -8,8 +8,5 @@ "skipLibCheck": true, "outDir": "./dist" }, - "exclude": [ - "node_modules", - "./dist/**" - ] + "exclude": ["node_modules", "./dist/**"] }