From 2faa8f6782a2e6294ed8849048a281d12d60da1c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 4 Dec 2017 16:29:56 +0100 Subject: Initial support for multiple devices with C++ and test Signed-off-by: Szczepan Zalega --- unittest/test_multiple_devices.cc | 92 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 unittest/test_multiple_devices.cc (limited to 'unittest') diff --git a/unittest/test_multiple_devices.cc b/unittest/test_multiple_devices.cc new file mode 100644 index 0000000..f5b4d6e --- /dev/null +++ b/unittest/test_multiple_devices.cc @@ -0,0 +1,92 @@ + +static const char *const default_admin_pin = "12345678"; +static const char *const default_user_pin = "123456"; +const char * temporary_password = "123456789012345678901234"; +const char * RFC_SECRET = "12345678901234567890"; + +#include "catch.hpp" + +#include +#include +#include +#include + +using namespace nitrokey; + + +TEST_CASE("List devices", "[BASIC]") { + shared_ptr d = make_shared(); + auto v = d->enumerate(); + REQUIRE(v.size() > 0); + for (auto a : v){ + std::cout << a; + d->set_path(a); + d->connect(); + auto res = GetStatus::CommandTransaction::run(d); + auto res2 = GetDeviceStatus::CommandTransaction::run(d); + std::cout << " " << res.data().card_serial_u32 << " " + << res.data().get_card_serial_hex() + << " " << std::to_string(res2.data().versionInfo.minor) + << std::endl; + d->disconnect(); + } +} + +TEST_CASE("Regenerate AES keys", "[BASIC]") { + shared_ptr d = make_shared(); + auto v = d->enumerate(); + REQUIRE(v.size() > 0); + + std::vector> devices; + for (auto a : v){ + std::cout << a << endl; + d = make_shared(); + d->set_path(a); + d->connect(); + devices.push_back(d); + } + + for (auto d : devices){ + auto res2 = GetDeviceStatus::CommandTransaction::run(d); + std::cout << std::to_string(res2.data().versionInfo.minor) << std::endl; +// nitrokey::proto::stick20::CreateNewKeys::CommandPayload p; +// p.set_defaults(); +// memcpy(p.password, "12345678", 8); +// auto res3 = nitrokey::proto::stick20::CreateNewKeys::CommandTransaction::run(d, p); + } + + for (auto d : devices){ + //TODO watch out for multiple hid_exit calls + d->disconnect(); + } +} + + +TEST_CASE("Use API", "[BASIC]") { + auto nm = NitrokeyManager::instance(); + nm->set_loglevel(2); + auto v = nm->list_devices(); + REQUIRE(v.size() > 0); + + for (int i=0; i<10; i++){ + for (auto a : v) { + std::cout <<"Connect with: " << a << + " " << std::boolalpha << nm->connect_with_path(a) << " "; + try{ + auto status_storage = nm->get_status_storage(); + std::cout << status_storage.ActiveSmartCardID_u32 + << " " << status_storage.ActiveSD_CardID_u32 + << std::endl; + + nm->fill_SD_card_with_random_data("12345678"); + } + catch (const LongOperationInProgressException &e){ + std::cout << "long operation in progress on " << a + << " " << std::to_string(e.progress_bar_value) << std::endl; + this_thread::sleep_for(1000ms); + } + } + std::cout <<"Iteration: " << i << std::endl; + } + +} \ No newline at end of file -- cgit v1.2.3 From 388cf5fcb33f24bc04f79cd1fbea980214518d54 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 4 Dec 2017 19:21:09 +0100 Subject: List devices by unique SC:SD id Add C API and tests Add mutexes Signed-off-by: Szczepan Zalega --- NK_C_API.cc | 21 ++++++++++ NK_C_API.h | 6 +++ NitrokeyManager.cc | 87 +++++++++++++++++++++++++++++++++++---- include/NitrokeyManager.h | 3 ++ unittest/test_C_API.cpp | 28 +++++++++++++ unittest/test_multiple_devices.cc | 24 ++++++++++- 6 files changed, 160 insertions(+), 9 deletions(-) (limited to 'unittest') diff --git a/NK_C_API.cc b/NK_C_API.cc index f881caf..59247ba 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -608,6 +608,27 @@ extern "C" { }); } + NK_C_API const char* NK_list_devices_by_cpuID() { + auto nm = NitrokeyManager::instance(); + return get_with_string_result([&]() { + auto v = nm->list_devices_by_cpuID(); + std::string res; + for (const auto a : v){ + res += a+";"; + } + if (res.size()>0) res.pop_back(); // remove last delimeter char + return strndup(res.c_str(), 4096); + }); + } + + NK_C_API int NK_connect_with_ID(const char* id) { + auto m = NitrokeyManager::instance(); + return get_with_result([&]() { + return m->connect_with_ID(id) ? 1 : 0; + }); + } + + #ifdef __cplusplus } diff --git a/NK_C_API.h b/NK_C_API.h index 73022b2..5933c67 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -568,6 +568,12 @@ extern "C" { */ NK_C_API int NK_get_progress_bar_value(); + + NK_C_API const char* NK_list_devices_by_cpuID(); + NK_C_API int NK_connect_with_ID(const char* id); + + + #ifdef __cplusplus } #endif diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index da5f61a..4935076 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -81,6 +81,8 @@ using nitrokey::misc::strcpyT; set_debug(true); } NitrokeyManager::~NitrokeyManager() { + std::lock_guard lock(mex_dev_com_manager); + for (auto d : connected_devices){ if (d.second == nullptr) continue; d.second->disconnect(); @@ -104,27 +106,98 @@ using nitrokey::misc::strcpyT; } std::vector NitrokeyManager::list_devices(){ + std::lock_guard lock(mex_dev_com_manager); + auto p = make_shared(); return p->enumerate(); // make static } - bool NitrokeyManager::connect_with_path(std::string path) { + std::vector NitrokeyManager::list_devices_by_cpuID(){ std::lock_guard lock(mex_dev_com_manager); - if(connected_devices.find(path) != connected_devices.end() - && connected_devices[path] != nullptr) { - device = connected_devices[path]; - return true; + std::vector res; + auto d = make_shared(); + const auto v = d->enumerate(); + for (auto & p: v){ + d = make_shared(); + d->set_path(p); + try{ + if (d->connect()){ + device = d; + const auto status = get_status_storage(); + const auto sc_id = status.ActiveSmartCardID_u32; + const auto sd_id = status.ActiveSD_CardID_u32; + + auto id = std::to_string(sc_id) + ":" + std::to_string(sd_id); + connected_devices_byID[id] = d; + res.push_back(id); + } else{ + std::cout << "Could not connect to: " + p << std::endl; + } + } + catch (const DeviceCommunicationException &e){ + //ignore + std::cout << p << ": " << " Exception encountered" << std::endl; + } } + return res; + } +/** + * Connect to the device using unique smartcard:datacard id. + * Needs list_device_by_cpuID run first + * @param id + * @return + */ + bool NitrokeyManager::connect_with_ID(const std::string id) { + std::lock_guard lock(mex_dev_com_manager); + auto position = connected_devices_byID.find(id); + if (position == connected_devices_byID.end()) return false; + + auto d = connected_devices_byID[id]; + device = d; + + try{ + get_status(); + } + catch (const DeviceCommunicationException &){ + d->disconnect(); + connected_devices_byID[id] = nullptr; + connected_devices_byID.erase(position); + return false; + } + return true; + } + + /** + * Connects device to path. + * Assumes devices are not being disconnected and caches connections (param cache_connections). + * @param path os-dependent device path + * @return false, when could not connect, true otherwise + */ + bool NitrokeyManager::connect_with_path(std::string path) { + const bool cache_connections = false; + + std::lock_guard lock(mex_dev_com_manager); + + if (cache_connections){ + if(connected_devices.find(path) != connected_devices.end() + && connected_devices[path] != nullptr) { + device = connected_devices[path]; + return true; + } + } auto p = make_shared(); p->set_path(path); if(!p->connect()) return false; - connected_devices [path] = p; - device = p; + if(cache_connections){ + connected_devices [path] = p; + } + + device = p; //previous device will be disconnected automatically return true; } diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 2a79922..7ce432f 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -69,6 +69,8 @@ char * strndup(const char* str, size_t maxlen); bool erase_totp_slot(uint8_t slot_number, const char *temporary_password); bool erase_hotp_slot(uint8_t slot_number, const char *temporary_password); std::vector list_devices(); + std::vector list_devices_by_cpuID(); + bool connect_with_ID(const std::string id); bool connect_with_path (std::string path); bool connect(const char *device_model); bool connect(); @@ -202,6 +204,7 @@ char * strndup(const char* str, size_t maxlen); static shared_ptr _instance; std::shared_ptr device; std::unordered_map > connected_devices; + std::unordered_map > connected_devices_byID; stick10::ReadSlot::ResponsePayload get_OTP_slot_data(const uint8_t slot_number); diff --git a/unittest/test_C_API.cpp b/unittest/test_C_API.cpp index be47f08..2d83ef4 100644 --- a/unittest/test_C_API.cpp +++ b/unittest/test_C_API.cpp @@ -43,6 +43,7 @@ TEST_CASE("C API connect", "[BASIC]") { TEST_CASE("Check retry count", "[BASIC]") { REQUIRE(login != 0); + NK_set_debug_level(3); REQUIRE(NK_get_admin_retry_count() == 3); REQUIRE(NK_get_user_retry_count() == 3); } @@ -56,4 +57,31 @@ TEST_CASE("Check long strings", "[STANDARD]") { result = NK_change_user_PIN(pin, longPin); REQUIRE(result == TOO_LONG_STRING); CAPTURE(result); +} + +#include + +TEST_CASE("multiple devices with ID", "[BASIC]") { + NK_logout(); + NK_set_debug_level(3); + auto s = NK_list_devices_by_cpuID(); + REQUIRE(s!=nullptr); + REQUIRE(strnlen(s, 4096) < 4096); + REQUIRE(strnlen(s, 4096) > 2*4); + std::cout << s << std::endl; + + char *string, *token; + int t; + + string = strndup(s, 4096); + free ( (void*) s); + + while ((token = strsep(&string, ";")) != nullptr){ + if (strnlen(token, 4096) < 3) continue; + std::cout << token << " connecting: "; + std::cout << (t=NK_connect_with_ID(token)) << std::endl; + REQUIRE(t == 1); + } + + free (string); } \ No newline at end of file diff --git a/unittest/test_multiple_devices.cc b/unittest/test_multiple_devices.cc index f5b4d6e..235a24d 100644 --- a/unittest/test_multiple_devices.cc +++ b/unittest/test_multiple_devices.cc @@ -78,15 +78,35 @@ TEST_CASE("Use API", "[BASIC]") { << " " << status_storage.ActiveSD_CardID_u32 << std::endl; - nm->fill_SD_card_with_random_data("12345678"); +// nm->fill_SD_card_with_random_data("12345678"); } catch (const LongOperationInProgressException &e){ std::cout << "long operation in progress on " << a << " " << std::to_string(e.progress_bar_value) << std::endl; - this_thread::sleep_for(1000ms); +// this_thread::sleep_for(1000ms); } } std::cout <<"Iteration: " << i << std::endl; } +} + + +TEST_CASE("Use API ID", "[BASIC]") { + auto nm = NitrokeyManager::instance(); + nm->set_loglevel(2); + + auto v = nm->list_devices_by_cpuID(); + REQUIRE(v.size() > 0); + + for(int i=0; i<1000; i++) { + auto v = nm->list_devices_by_cpuID(); + REQUIRE(v.size() > 0); + for (auto i : v) { + nm->connect_with_ID(i); + auto retry_count = nm->get_admin_retry_count(); + std::cout << i << " " << to_string(retry_count) << std::endl; + } + } + std::cout << "finished" << std::endl; } \ No newline at end of file -- cgit v1.2.3 From e25a83d6d704db7b5505d113c2d89811c6c2fc60 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 8 Dec 2017 11:00:18 +0100 Subject: Add tests for refreshed devices list and not refreshed in the loop Add it to CMake Signed-off-by: Szczepan Zalega --- CMakeLists.txt | 2 +- unittest/test_multiple_devices.cc | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) (limited to 'unittest') diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d788cf..6037393 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,7 +192,7 @@ IF (COMPILE_TESTS) target_link_libraries (test_issues ${EXTRA_LIBS} nitrokey catch) add_executable (test_multiple_devices unittest/test_multiple_devices.cc) - target_link_libraries (test_multiple_devices ${EXTRA_LIBS} ${LIBNAME} catch) + target_link_libraries (test_multiple_devices ${EXTRA_LIBS} nitrokey catch) ENDIF() diff --git a/unittest/test_multiple_devices.cc b/unittest/test_multiple_devices.cc index 235a24d..65ae9ab 100644 --- a/unittest/test_multiple_devices.cc +++ b/unittest/test_multiple_devices.cc @@ -8,7 +8,6 @@ const char * RFC_SECRET = "12345678901234567890"; #include #include -#include #include using namespace nitrokey; @@ -99,13 +98,43 @@ TEST_CASE("Use API ID", "[BASIC]") { auto v = nm->list_devices_by_cpuID(); REQUIRE(v.size() > 0); - for(int i=0; i<1000; i++) { + //no refresh - should not reconnect to new devices + for (int j = 0; j < 100; j++) { + for (auto i : v) { + if (!nm->connect_with_ID(i)) continue; + int retry_count = 99; + try { + retry_count = nm->get_admin_retry_count(); + std::cout << j << " " << i << " " << to_string(retry_count) << std::endl; + } + catch (...) { + retry_count = 99; + //pass + } + } + } + std::cout << "finished" << std::endl; +} + +TEST_CASE("Use API ID refresh", "[BASIC]") { + auto nm = NitrokeyManager::instance(); + nm->set_loglevel(2); + + //refresh in each iteration - should reconnect to new devices + for(int j=0; j<100; j++) { auto v = nm->list_devices_by_cpuID(); REQUIRE(v.size() > 0); for (auto i : v) { nm->connect_with_ID(i); - auto retry_count = nm->get_admin_retry_count(); - std::cout << i << " " << to_string(retry_count) << std::endl; + int retry_count = 99; + try { + retry_count = nm->get_admin_retry_count(); + std::cout << j <<" " << i << " " << to_string(retry_count) << std::endl; + } + catch (...){ + retry_count = 99; + //pass + } } } std::cout << "finished" << std::endl; -- cgit v1.2.3 From d6ae8192be443749fcd1f593db0f9be4d039da93 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 8 Dec 2017 11:48:26 +0100 Subject: Python test running commands on all connected devices Signed-off-by: Szczepan Zalega --- unittest/test_multiple.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 unittest/test_multiple.py (limited to 'unittest') diff --git a/unittest/test_multiple.py b/unittest/test_multiple.py new file mode 100644 index 0000000..7ea195e --- /dev/null +++ b/unittest/test_multiple.py @@ -0,0 +1,60 @@ +import pprint +from time import sleep + +import pytest +from collections import defaultdict + +from tqdm import tqdm + +from conftest import skip_if_device_version_lower_than +from constants import DefaultPasswords, DeviceErrorCode, bb +from misc import gs, wait + +pprint = pprint.PrettyPrinter(indent=4).pprint + + +@pytest.mark.other +@pytest.mark.info +def test_get_status_storage_multiple(C): + ids = gs(C.NK_list_devices_by_cpuID()) + print(ids) + devices_list = ids.split(b';') + # + # for s in devices_list: + # res = C.NK_connect_with_ID(s) + # assert res == 1 + # # st = gs(C.NK_get_status_storage_as_string()) + # # print(len(st)) + # C.NK_lock_device() + # + for s in devices_list: + res = C.NK_connect_with_ID(s) + assert res == 1 + v = C.NK_fill_SD_card_with_random_data(b'12345678') + # print(v) + + devices_count = len(devices_list) + assert devices_count != 0 + b = 0 + + last_b = 0 + with tqdm(total=devices_count*100) as pbar: + while b/devices_count < 100: + pbar.update(b - last_b) + + b = defaultdict (lambda: 0) + + ids = gs(C.NK_list_devices_by_cpuID()) + devices_list = ids.split(b';') + devices_count = len(devices_list) + + for s in devices_list: + res = C.NK_connect_with_ID(s) + if res != 1: continue + b[s] += C.NK_get_progress_bar_value() + print(b) + b = sum(b.values()) + print('{}: {}'.format(b, int(b/devices_count) * '=')) + sleep(5) + + -- cgit v1.2.3 From 2b752acc507d60047576876c9ada4596442511b6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 28 Feb 2018 20:28:33 +0100 Subject: Add test's scenario descriptions Signed-off-by: Szczepan Zalega --- unittest/test_multiple_devices.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'unittest') diff --git a/unittest/test_multiple_devices.cc b/unittest/test_multiple_devices.cc index 65ae9ab..f497908 100644 --- a/unittest/test_multiple_devices.cc +++ b/unittest/test_multiple_devices.cc @@ -99,6 +99,12 @@ TEST_CASE("Use API ID", "[BASIC]") { REQUIRE(v.size() > 0); //no refresh - should not reconnect to new devices + // Scenario: + // 1. Run test + // 2. Remove one of the devices and reinsert it after a while + // 3. Device should not be reconnected and test should not crash + // 4. Remove all devices - test should continue + for (int j = 0; j < 100; j++) { for (auto i : v) { if (!nm->connect_with_ID(i)) continue; @@ -121,6 +127,11 @@ TEST_CASE("Use API ID refresh", "[BASIC]") { nm->set_loglevel(2); //refresh in each iteration - should reconnect to new devices + // Scenario: + // 1. Run test + // 2. Remove one of the devices and reinsert it after a while + // 3. Device should be reconnected + for(int j=0; j<100; j++) { auto v = nm->list_devices_by_cpuID(); REQUIRE(v.size() > 0); -- cgit v1.2.3 From c3d615b659b608f3a1d624f6fc78c303efbe1f8e Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 1 Mar 2018 19:41:35 +0100 Subject: Correct firmware version getting in Pro tests Done for Pro devices Signed-off-by: Szczepan Zalega --- unittest/misc.py | 7 ++++--- unittest/test_pro.py | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'unittest') diff --git a/unittest/misc.py b/unittest/misc.py index a41f2b2..8be915d 100644 --- a/unittest/misc.py +++ b/unittest/misc.py @@ -43,7 +43,7 @@ def cast_pointer_to_tuple(obj, typen, len): def get_devices_firmware_version(C): - firmware = C.NK_get_major_firmware_version() + firmware = C.NK_get_minor_firmware_version() return firmware @@ -54,7 +54,7 @@ def is_pro_rtm_07(C): def is_pro_rtm_08(C): firmware = get_devices_firmware_version(C) - return firmware == 8 + return firmware in [8,9] def is_storage(C): @@ -62,7 +62,8 @@ def is_storage(C): exact firmware storage is sent by other function """ # TODO identify connected device directly - return not is_pro_rtm_08(C) and not is_pro_rtm_07(C) + firmware = get_devices_firmware_version(C) + return firmware >= 45 def is_long_OTP_secret_handled(C): diff --git a/unittest/test_pro.py b/unittest/test_pro.py index 12a34e9..53588f6 100644 --- a/unittest/test_pro.py +++ b/unittest/test_pro.py @@ -164,9 +164,9 @@ def test_password_safe_slot_status(C): @pytest.mark.aes def test_issue_device_locks_on_second_key_generation_in_sequence(C): - if is_pro_rtm_07(C) or is_pro_rtm_08(C): - pytest.skip("issue to register: device locks up " - "after below commands sequence (reinsertion fixes), skipping for now") +# if is_pro_rtm_07(C) or is_pro_rtm_08(C): + pytest.skip("issue to register: device locks up " + "after below commands sequence (reinsertion fixes), skipping for now") assert C.NK_build_aes_key(DefaultPasswords.ADMIN) == DeviceErrorCode.STATUS_OK assert C.NK_build_aes_key(DefaultPasswords.ADMIN) == DeviceErrorCode.STATUS_OK -- cgit v1.2.3