aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2016-07-27 12:31:08 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2016-08-01 13:54:57 +0200
commit547c02f0c5d4195a3efe454b9e98c0d0a84d739c (patch)
tree567ba270839328cc8fe6444a8dca130bd30d20be
parentd26f8fc73e2a15314c44688e372f9d4613dcfdd8 (diff)
downloadlibnitrokey-547c02f0c5d4195a3efe454b9e98c0d0a84d739c.tar.gz
libnitrokey-547c02f0c5d4195a3efe454b9e98c0d0a84d739c.tar.bz2
Test read/write config
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--NK_C_API.cc28
-rw-r--r--NK_C_API.h1
-rw-r--r--NitrokeyManager.cc7
-rw-r--r--include/NitrokeyManager.h3
-rw-r--r--unittest/test_bindings.py18
5 files changed, 56 insertions, 1 deletions
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
@@ -5,6 +5,24 @@ using namespace nitrokey;
static uint8_t NK_last_command_status = 0;
template <typename T>
+T* array_dup(std::vector<T>& v){
+ auto d = new T[v.size()];
+ std::copy(v.begin(), v.end(), d);
+ return d;
+}
+
+template <typename T>
+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 <typename T>
const char* get_with_string_result(T func){
try {
return func();
@@ -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<uint8_t> NitrokeyManager::read_config() {
+ auto responsePayload = GetStatus::CommandTransaction::run(*device);
+ vector<uint8_t> v = vector<uint8_t>(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 <vector>
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<uint8_t> 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)
+