From 44548b27880371c5fc927b6c463d693f03e2e5d1 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 7 Oct 2017 14:04:28 +0200 Subject: Correct exception type Signed-off-by: Szczepan Zalega --- NitrokeyManager.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 0d702eb..a03ef53 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -724,9 +724,8 @@ using nitrokey::misc::strcpyT; } DeviceModel NitrokeyManager::get_connected_device_model() const{ - //FIXME throw if no device is connected or return unknown/unconnected value if (device == nullptr){ - throw std::runtime_error("device not connected"); + throw DeviceNotConnected("device not connected"); } return device->get_device_model(); } -- cgit v1.2.1 From ddebcb953662c817461852689dd6f694418a55c5 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 7 Oct 2017 14:06:00 +0200 Subject: Add C++ offline tests and set them default Signed-off-by: Szczepan Zalega --- CMakeLists.txt | 5 +- unittest/test2.cc | 42 -------------- unittest/test_offline.cc | 140 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 43 deletions(-) create mode 100644 unittest/test_offline.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index d28425a..1248e23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,11 +150,14 @@ IF (COMPILE_TESTS) add_executable (test_issues unittest/test_issues.cc) target_link_libraries (test_issues ${EXTRA_LIBS} ${LIBNAME}-log catch) + add_executable (test_offline unittest/test_offline.cc) + target_link_libraries (test_offline ${EXTRA_LIBS} ${LIBNAME}-log catch) + #run with 'make test' or 'ctest' #needs connected PRO device for success #warning: it may delete data on the device include (CTest) - add_test (runs test_C_API) + add_test (runs test_offline) ENDIF() diff --git a/unittest/test2.cc b/unittest/test2.cc index 31dbce8..bbe5b07 100644 --- a/unittest/test2.cc +++ b/unittest/test2.cc @@ -80,48 +80,6 @@ TEST_CASE("long operation test", "[test_long]") { } -#include "test_command_ids_header.h" - -TEST_CASE("test device commands ids", "[fast]") { - -// REQUIRE(STICK20_CMD_START_VALUE == static_cast(CommandID::START_VALUE)); - REQUIRE(STICK20_CMD_ENABLE_CRYPTED_PARI == static_cast(CommandID::ENABLE_CRYPTED_PARI)); - REQUIRE(STICK20_CMD_DISABLE_CRYPTED_PARI == static_cast(CommandID::DISABLE_CRYPTED_PARI)); - REQUIRE(STICK20_CMD_ENABLE_HIDDEN_CRYPTED_PARI == static_cast(CommandID::ENABLE_HIDDEN_CRYPTED_PARI)); - REQUIRE(STICK20_CMD_DISABLE_HIDDEN_CRYPTED_PARI == static_cast(CommandID::DISABLE_HIDDEN_CRYPTED_PARI)); - REQUIRE(STICK20_CMD_ENABLE_FIRMWARE_UPDATE == static_cast(CommandID::ENABLE_FIRMWARE_UPDATE)); - REQUIRE(STICK20_CMD_EXPORT_FIRMWARE_TO_FILE == static_cast(CommandID::EXPORT_FIRMWARE_TO_FILE)); - REQUIRE(STICK20_CMD_GENERATE_NEW_KEYS == static_cast(CommandID::GENERATE_NEW_KEYS)); - REQUIRE(STICK20_CMD_FILL_SD_CARD_WITH_RANDOM_CHARS == static_cast(CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS)); - - REQUIRE(STICK20_CMD_WRITE_STATUS_DATA == static_cast(CommandID::WRITE_STATUS_DATA)); - REQUIRE(STICK20_CMD_ENABLE_READONLY_UNCRYPTED_LUN == static_cast(CommandID::ENABLE_READONLY_UNCRYPTED_LUN)); - REQUIRE(STICK20_CMD_ENABLE_READWRITE_UNCRYPTED_LUN == static_cast(CommandID::ENABLE_READWRITE_UNCRYPTED_LUN)); - - REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX == static_cast(CommandID::SEND_PASSWORD_MATRIX)); - REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX_PINDATA == static_cast(CommandID::SEND_PASSWORD_MATRIX_PINDATA)); - REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX_SETUP == static_cast(CommandID::SEND_PASSWORD_MATRIX_SETUP)); - - REQUIRE(STICK20_CMD_GET_DEVICE_STATUS == static_cast(CommandID::GET_DEVICE_STATUS)); - REQUIRE(STICK20_CMD_SEND_DEVICE_STATUS == static_cast(CommandID::SEND_DEVICE_STATUS)); - - REQUIRE(STICK20_CMD_SEND_HIDDEN_VOLUME_PASSWORD == static_cast(CommandID::SEND_HIDDEN_VOLUME_PASSWORD)); - REQUIRE(STICK20_CMD_SEND_HIDDEN_VOLUME_SETUP == static_cast(CommandID::SEND_HIDDEN_VOLUME_SETUP)); - REQUIRE(STICK20_CMD_SEND_PASSWORD == static_cast(CommandID::SEND_PASSWORD)); - REQUIRE(STICK20_CMD_SEND_NEW_PASSWORD == static_cast(CommandID::SEND_NEW_PASSWORD)); - REQUIRE(STICK20_CMD_CLEAR_NEW_SD_CARD_FOUND == static_cast(CommandID::CLEAR_NEW_SD_CARD_FOUND)); - - REQUIRE(STICK20_CMD_SEND_STARTUP == static_cast(CommandID::SEND_STARTUP)); - REQUIRE(STICK20_CMD_SEND_CLEAR_STICK_KEYS_NOT_INITIATED == static_cast(CommandID::SEND_CLEAR_STICK_KEYS_NOT_INITIATED)); - REQUIRE(STICK20_CMD_SEND_LOCK_STICK_HARDWARE == static_cast(CommandID::SEND_LOCK_STICK_HARDWARE)); - - REQUIRE(STICK20_CMD_PRODUCTION_TEST == static_cast(CommandID::PRODUCTION_TEST)); - REQUIRE(STICK20_CMD_SEND_DEBUG_DATA == static_cast(CommandID::SEND_DEBUG_DATA)); - - REQUIRE(STICK20_CMD_CHANGE_UPDATE_PIN == static_cast(CommandID::CHANGE_UPDATE_PIN)); - -} - TEST_CASE("test device internal status with various commands", "[fast]") { auto stick = make_shared(); bool connected = stick->connect(); diff --git a/unittest/test_offline.cc b/unittest/test_offline.cc new file mode 100644 index 0000000..a85a41f --- /dev/null +++ b/unittest/test_offline.cc @@ -0,0 +1,140 @@ +#include "catch.hpp" +#include +#include +#include "../NK_C_API.h" + +using namespace nitrokey::proto; +using namespace nitrokey::device; + +using namespace std; +using namespace nitrokey; + +//This test suite assumes no Pro or Storage devices are connected + +TEST_CASE("Return false on no device connected", "[fast]") { + INFO("This test case assumes no Pro or Storage devices are connected"); + auto stick = make_shared(); + bool connected = true; + REQUIRE_NOTHROW(connected = stick->connect()); + REQUIRE_FALSE(connected); + + auto stick_pro = make_shared(); + REQUIRE_NOTHROW(connected = stick_pro->connect()); + REQUIRE_FALSE(connected); + + + auto i = NitrokeyManager::instance(); + REQUIRE_NOTHROW(connected = i->connect()); + REQUIRE_FALSE(connected); + REQUIRE_FALSE(i->is_connected()); + REQUIRE_FALSE(i->disconnect()); + REQUIRE_FALSE(i->could_current_device_be_enumerated()); + + + int C_connected = 1; + REQUIRE_NOTHROW(C_connected = NK_login_auto()); + REQUIRE(0 == C_connected); +} + +TEST_CASE("Test C++ side behaviour in offline", "[fast]") { + auto i = NitrokeyManager::instance(); + + string serial_number; + REQUIRE_NOTHROW (serial_number = i->get_serial_number()); + REQUIRE(serial_number.empty()); + + REQUIRE_THROWS_AS( + i->get_status(), DeviceNotConnected + ); + + REQUIRE_THROWS_AS( + i->get_HOTP_code(0xFF, ""), InvalidSlotException + ); + + REQUIRE_THROWS_AS( + i->get_TOTP_code(0xFF, ""), InvalidSlotException + ); + + REQUIRE_THROWS_AS( + i->erase_hotp_slot(0xFF, ""), InvalidSlotException + ); + + REQUIRE_THROWS_AS( + i->erase_totp_slot(0xFF, ""), InvalidSlotException + ); + + REQUIRE_THROWS_AS( + i->get_totp_slot_name(0xFF), InvalidSlotException + ); + + REQUIRE_THROWS_AS( + i->get_hotp_slot_name(0xFF), InvalidSlotException + ); + + REQUIRE_THROWS_AS( + i->first_authenticate("123123", "123123"), DeviceNotConnected + ); + + REQUIRE_THROWS_AS( + i->get_connected_device_model(), DeviceNotConnected + ); + + REQUIRE_THROWS_AS( + i->clear_new_sd_card_warning("123123"), DeviceNotConnected + ); + +} + + +TEST_CASE("Test helper function - hex_string_to_byte", "[fast]") { + using namespace nitrokey::misc; + std::vector v; + REQUIRE_NOTHROW(v = hex_string_to_byte("00112233445566")); + const uint8_t test_data[] = "\x00\x11\x22\x33\x44\x55\x66"; + REQUIRE(v.size() == sizeof(test_data)-1); + for (int i = 0; i < v.size(); ++i) { + REQUIRE(v.data()[i] == test_data[i]); + } +} + +#include "test_command_ids_header.h" +TEST_CASE("Test device commands ids", "[fast]") { +// Make sure CommandID values are in sync with firmware's header + +// REQUIRE(STICK20_CMD_START_VALUE == static_cast(CommandID::START_VALUE)); + REQUIRE(STICK20_CMD_ENABLE_CRYPTED_PARI == static_cast(CommandID::ENABLE_CRYPTED_PARI)); + REQUIRE(STICK20_CMD_DISABLE_CRYPTED_PARI == static_cast(CommandID::DISABLE_CRYPTED_PARI)); + REQUIRE(STICK20_CMD_ENABLE_HIDDEN_CRYPTED_PARI == static_cast(CommandID::ENABLE_HIDDEN_CRYPTED_PARI)); + REQUIRE(STICK20_CMD_DISABLE_HIDDEN_CRYPTED_PARI == static_cast(CommandID::DISABLE_HIDDEN_CRYPTED_PARI)); + REQUIRE(STICK20_CMD_ENABLE_FIRMWARE_UPDATE == static_cast(CommandID::ENABLE_FIRMWARE_UPDATE)); + REQUIRE(STICK20_CMD_EXPORT_FIRMWARE_TO_FILE == static_cast(CommandID::EXPORT_FIRMWARE_TO_FILE)); + REQUIRE(STICK20_CMD_GENERATE_NEW_KEYS == static_cast(CommandID::GENERATE_NEW_KEYS)); + REQUIRE(STICK20_CMD_FILL_SD_CARD_WITH_RANDOM_CHARS == static_cast(CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS)); + + REQUIRE(STICK20_CMD_WRITE_STATUS_DATA == static_cast(CommandID::WRITE_STATUS_DATA)); + REQUIRE(STICK20_CMD_ENABLE_READONLY_UNCRYPTED_LUN == static_cast(CommandID::ENABLE_READONLY_UNCRYPTED_LUN)); + REQUIRE(STICK20_CMD_ENABLE_READWRITE_UNCRYPTED_LUN == static_cast(CommandID::ENABLE_READWRITE_UNCRYPTED_LUN)); + + REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX == static_cast(CommandID::SEND_PASSWORD_MATRIX)); + REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX_PINDATA == static_cast(CommandID::SEND_PASSWORD_MATRIX_PINDATA)); + REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX_SETUP == static_cast(CommandID::SEND_PASSWORD_MATRIX_SETUP)); + + REQUIRE(STICK20_CMD_GET_DEVICE_STATUS == static_cast(CommandID::GET_DEVICE_STATUS)); + REQUIRE(STICK20_CMD_SEND_DEVICE_STATUS == static_cast(CommandID::SEND_DEVICE_STATUS)); + + REQUIRE(STICK20_CMD_SEND_HIDDEN_VOLUME_PASSWORD == static_cast(CommandID::SEND_HIDDEN_VOLUME_PASSWORD)); + REQUIRE(STICK20_CMD_SEND_HIDDEN_VOLUME_SETUP == static_cast(CommandID::SEND_HIDDEN_VOLUME_SETUP)); + REQUIRE(STICK20_CMD_SEND_PASSWORD == static_cast(CommandID::SEND_PASSWORD)); + REQUIRE(STICK20_CMD_SEND_NEW_PASSWORD == static_cast(CommandID::SEND_NEW_PASSWORD)); + REQUIRE(STICK20_CMD_CLEAR_NEW_SD_CARD_FOUND == static_cast(CommandID::CLEAR_NEW_SD_CARD_FOUND)); + + REQUIRE(STICK20_CMD_SEND_STARTUP == static_cast(CommandID::SEND_STARTUP)); + REQUIRE(STICK20_CMD_SEND_CLEAR_STICK_KEYS_NOT_INITIATED == static_cast(CommandID::SEND_CLEAR_STICK_KEYS_NOT_INITIATED)); + REQUIRE(STICK20_CMD_SEND_LOCK_STICK_HARDWARE == static_cast(CommandID::SEND_LOCK_STICK_HARDWARE)); + + REQUIRE(STICK20_CMD_PRODUCTION_TEST == static_cast(CommandID::PRODUCTION_TEST)); + REQUIRE(STICK20_CMD_SEND_DEBUG_DATA == static_cast(CommandID::SEND_DEBUG_DATA)); + + REQUIRE(STICK20_CMD_CHANGE_UPDATE_PIN == static_cast(CommandID::CHANGE_UPDATE_PIN)); + +} -- cgit v1.2.1 From e46150ab77959dfa44905b6db1ef9f5aaecb4a3b Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 7 Oct 2017 14:07:32 +0200 Subject: Enable tests on CI Signed-off-by: Szczepan Zalega --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ba4465b..22b1d07 100644 --- a/.travis.yml +++ b/.travis.yml @@ -55,4 +55,4 @@ install: script: - make -j2 -# - make test # TODO add library device-less tests \ No newline at end of file + - make test \ No newline at end of file -- cgit v1.2.1 From badeb19f9d43fafe7a94e9028ad9e65ef4bc1770 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 7 Oct 2017 14:19:00 +0200 Subject: Enable offline test on all instances Signed-off-by: Szczepan Zalega --- .travis.yml | 2 +- CMakeLists.txt | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 22b1d07..f35c7f8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: generic sudo: false os: osx -env: CF= +env: CF=-DCOMPILE_TESTS=ON matrix: include: diff --git a/CMakeLists.txt b/CMakeLists.txt index 1248e23..c868fae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,11 +127,18 @@ install (FILES ${LIB_INCLUDES} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libnitrok install (TARGETS ${LIBNAME} DESTINATION ${CMAKE_INSTALL_LIBDIR}) install (TARGETS ${LIBNAME}-log DESTINATION ${CMAKE_INSTALL_LIBDIR}) -IF (COMPILE_TESTS) - include_directories(unittest/Catch/include) - add_library(catch STATIC unittest/catch_main.cpp ) +include_directories(unittest/Catch/include) +add_library(catch STATIC unittest/catch_main.cpp ) +add_executable (test_offline unittest/test_offline.cc) +target_link_libraries (test_offline ${EXTRA_LIBS} ${LIBNAME}-log catch) +#run with 'make test' or 'ctest' +include (CTest) +add_test (runs test_offline) +IF (COMPILE_TESTS) + #needs connected PRO device for success + #warning: it may delete data on the device add_executable (test_C_API unittest/test_C_API.cpp) target_link_libraries (test_C_API ${EXTRA_LIBS} ${LIBNAME}-log catch) @@ -150,14 +157,6 @@ IF (COMPILE_TESTS) add_executable (test_issues unittest/test_issues.cc) target_link_libraries (test_issues ${EXTRA_LIBS} ${LIBNAME}-log catch) - add_executable (test_offline unittest/test_offline.cc) - target_link_libraries (test_offline ${EXTRA_LIBS} ${LIBNAME}-log catch) - - #run with 'make test' or 'ctest' - #needs connected PRO device for success - #warning: it may delete data on the device - include (CTest) - add_test (runs test_offline) ENDIF() -- cgit v1.2.1 From e2e08e3e3ef9de6f3d5b1814a67d91ee68135cbb Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 7 Oct 2017 14:23:42 +0200 Subject: Make CTest verbose Signed-off-by: Szczepan Zalega --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f35c7f8..f8dea31 100644 --- a/.travis.yml +++ b/.travis.yml @@ -55,4 +55,4 @@ install: script: - make -j2 - - make test \ No newline at end of file + - ctest -VV \ No newline at end of file -- cgit v1.2.1 From da5606ff80dcd196deaa504626150c7e2a3b825f Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 7 Oct 2017 14:36:40 +0200 Subject: Enter bytes directly instead of using string Signed-off-by: Szczepan Zalega --- unittest/test_offline.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/unittest/test_offline.cc b/unittest/test_offline.cc index a85a41f..58c7555 100644 --- a/unittest/test_offline.cc +++ b/unittest/test_offline.cc @@ -90,9 +90,10 @@ TEST_CASE("Test helper function - hex_string_to_byte", "[fast]") { using namespace nitrokey::misc; std::vector v; REQUIRE_NOTHROW(v = hex_string_to_byte("00112233445566")); - const uint8_t test_data[] = "\x00\x11\x22\x33\x44\x55\x66"; - REQUIRE(v.size() == sizeof(test_data)-1); + const uint8_t test_data[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + REQUIRE(v.size() == sizeof(test_data)); for (int i = 0; i < v.size(); ++i) { + INFO("Position i: " << i); REQUIRE(v.data()[i] == test_data[i]); } } -- cgit v1.2.1 From b4f6ee9ad0c4da26fe2003de68905c0365893778 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 7 Oct 2017 16:27:27 +0200 Subject: Travis: do not compile all tests by default Signed-off-by: Szczepan Zalega --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index f8dea31..cd1c489 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: generic sudo: false os: osx -env: CF=-DCOMPILE_TESTS=ON +env: CF= matrix: include: @@ -13,7 +13,7 @@ matrix: - osx_image: xcode8.2 - os: linux dist: trusty - env: COMPILER_NAME=gcc CXX=g++-5 CC=gcc-5 CF=-DCOMPILE_TESTS=ON + env: COMPILER_NAME=gcc CXX=g++-5 CC=gcc-5 addons: apt: packages: @@ -24,7 +24,7 @@ matrix: - ubuntu-toolchain-r-test - os: linux dist: trusty - env: COMPILER_NAME=gcc CXX=g++-6 CC=gcc-6 CF=-DCOMPILE_TESTS=ON + env: COMPILER_NAME=gcc CXX=g++-6 CC=gcc-6 addons: apt: packages: @@ -34,7 +34,7 @@ matrix: sources: *sources - os: linux dist: trusty - env: COMPILER_NAME=clang CXX=clang++-3.8 CC=clang-3.8 CF=-DCOMPILE_TESTS=ON + env: COMPILER_NAME=clang CXX=clang++-3.8 CC=clang-3.8 addons: apt: packages: -- cgit v1.2.1