From 547c02f0c5d4195a3efe454b9e98c0d0a84d739c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 27 Jul 2016 12:31:08 +0200 Subject: Test read/write config Signed-off-by: Szczepan Zalega --- NK_C_API.cc | 28 ++++++++++++++++++++++++++++ NK_C_API.h | 1 + NitrokeyManager.cc | 7 +++++++ include/NitrokeyManager.h | 3 +++ unittest/test_bindings.py | 18 +++++++++++++++++- 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/NK_C_API.cc b/NK_C_API.cc index e5febe1..d93fafc 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -4,6 +4,24 @@ using namespace nitrokey; static uint8_t NK_last_command_status = 0; +template +T* array_dup(std::vector& v){ + auto d = new T[v.size()]; + std::copy(v.begin(), v.end(), d); + return d; +} + +template +uint8_t * get_with_array_result(T func){ + try { + return func(); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return nullptr; + } +} + template const char* get_with_string_result(T func){ try { @@ -114,6 +132,16 @@ extern int NK_write_config(bool numlock, bool capslock, bool scrolllock, bool en }); } + +extern uint8_t* NK_read_config(){ + auto m = NitrokeyManager::instance(); + return get_with_array_result( [&](){ + auto v = m->read_config(); + return array_dup(v); + }); +} + + extern const char * NK_status() { auto m = NitrokeyManager::instance(); try { diff --git a/NK_C_API.h b/NK_C_API.h index 6058071..1aeeab6 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -21,6 +21,7 @@ extern int NK_factory_reset(const char* admin_password); extern int NK_build_aes_key(const char* admin_password); extern int NK_unlock_user_password(const char* admin_password); extern int NK_write_config(bool numlock, bool capslock, bool scrolllock, bool enable_user_password, bool delete_user_password, const char *admin_temporary_password); +extern uint8_t* NK_read_config(); //otp extern const char * NK_get_totp_slot_name(uint8_t slot_number); extern const char * NK_get_hotp_slot_name(uint8_t slot_number); diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 53e355e..2e2ad3d 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -337,4 +337,11 @@ namespace nitrokey{ WriteGeneralConfig::CommandTransaction::run(*device, p); } + vector NitrokeyManager::read_config() { + auto responsePayload = GetStatus::CommandTransaction::run(*device); + vector v = vector(responsePayload.general_config, + responsePayload.general_config+sizeof(responsePayload.general_config)); + return v; + } + } \ No newline at end of file diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 0f24b1f..a63b51f 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -5,6 +5,7 @@ #include "log.h" #include "device_proto.h" #include "stick10_commands.h" +#include namespace nitrokey { using namespace nitrokey::device; @@ -70,6 +71,8 @@ namespace nitrokey { void write_config(bool numlock, bool capslock, bool scrolllock, bool enable_user_password, bool delete_user_password, const char *admin_temporary_password); + vector read_config(); + private: NitrokeyManager(); ~NitrokeyManager(); diff --git a/unittest/test_bindings.py b/unittest/test_bindings.py index 839b9c3..5606f2f 100644 --- a/unittest/test_bindings.py +++ b/unittest/test_bindings.py @@ -213,9 +213,25 @@ def test_get_code_user_authorize(C): code = C.NK_get_totp_code(0, 0, 0, 0) assert code == 0 assert C.NK_get_last_command_status() == DeviceErrorCode.STATUS_NOT_AUTHORIZED - assert C.NK_first_authenticate(DefaultPasswords.ADMIN, DefaultPasswords.ADMIN_TEMP) == DeviceErrorCode.STATUS_OK # disable PIN protection with write_config + assert C.NK_first_authenticate(DefaultPasswords.ADMIN, DefaultPasswords.ADMIN_TEMP) == DeviceErrorCode.STATUS_OK assert C.NK_write_config(True, True, True, False, True, DefaultPasswords.ADMIN_TEMP) == DeviceErrorCode.STATUS_OK code = C.NK_get_totp_code(0, 0, 0, 0) assert code != 0 assert C.NK_get_last_command_status() == DeviceErrorCode.STATUS_OK + + +def cast_pointer_to_tuple(obj, typen, len): + # usage: + # config = cast_pointer_to_tuple(config_raw_data, 'uint8_t', 5) + return tuple(ffi.cast("%s [%d]" % (typen, len), obj)[0:len]) + + +def test_read_write_config(C): + C.NK_set_debug(True) + assert C.NK_first_authenticate(DefaultPasswords.ADMIN, DefaultPasswords.ADMIN_TEMP) == DeviceErrorCode.STATUS_OK + assert C.NK_write_config(True, True, True, False, True, DefaultPasswords.ADMIN_TEMP) == DeviceErrorCode.STATUS_OK + config_raw_data = C.NK_read_config() + config = cast_pointer_to_tuple(config_raw_data, 'uint8_t', 5) + assert config == (True, True, True, False, True) + -- cgit v1.2.3