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.1 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 --- unittest/test_C_API.cpp | 28 ++++++++++++++++++++++++++++ unittest/test_multiple_devices.cc | 24 ++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) (limited to 'unittest') 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.1 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 --- unittest/test_multiple_devices.cc | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'unittest') 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.1 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.1 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.1 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.1