diff options
-rw-r--r-- | NK_C_API.cc | 16 | ||||
-rw-r--r-- | NK_C_API.h | 22 | ||||
-rw-r--r-- | NitrokeyManager.cc | 15 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | libnitrokey/NitrokeyManager.h | 1 | ||||
-rw-r--r-- | unittest/test_HOTP.cc | 3 |
6 files changed, 57 insertions, 2 deletions
diff --git a/NK_C_API.cc b/NK_C_API.cc index 64355f5..b245940 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -161,6 +161,22 @@ extern "C" { return 0; } + NK_C_API int NK_login_enum(NK_device_model device_model) { + const char *model_string; + switch (device_model) { + case NK_PRO: + model_string = "P"; + break; + case NK_STORAGE: + model_string = "S"; + break; + default: + /* no such enum value -- return error code */ + return 0; + } + return NK_login(model_string); + } + NK_C_API int NK_logout() { auto m = NitrokeyManager::instance(); return get_without_result([&]() { @@ -34,6 +34,21 @@ #ifdef __cplusplus extern "C" { #endif + + /** + * The Nitrokey device models supported by the API. + */ + enum NK_device_model { + /** + * Nitrokey Pro. + */ + NK_PRO, + /** + * Nitrokey Storage. + */ + NK_STORAGE + }; + /** * Set debug level of messages written on stderr * @param state state=True - most messages, state=False - only errors level @@ -54,6 +69,13 @@ extern "C" { NK_C_API int NK_login(const char *device_model); /** + * Connect to device of given model. Currently library can be connected only to one device at once. + * @param device_model NK_device_model: NK_PRO: Nitrokey Pro, NK_STORAGE: Nitrokey Storage + * @return 1 if connected, 0 if wrong model or cannot connect + */ + NK_C_API int NK_login_enum(NK_device_model device_model); + + /** * Connect to first available device, starting checking from Pro 1st to Storage 2nd. * @return 1 if connected, 0 if wrong model or cannot connect */ diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 4008bd0..8bb1a2a 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -273,6 +273,21 @@ using nitrokey::misc::strcpyT; return device->connect(); } + bool NitrokeyManager::connect(device::DeviceModel device_model) { + const char *model_string; + switch (device_model) { + case device::DeviceModel::PRO: + model_string = "P"; + break; + case device::DeviceModel::STORAGE: + model_string = "S"; + break; + default: + throw std::runtime_error("Unknown model"); + } + return connect(model_string); + } + shared_ptr<NitrokeyManager> NitrokeyManager::instance() { static std::mutex mutex; std::lock_guard<std::mutex> lock(mutex); @@ -184,7 +184,7 @@ For additional documentation please check the following for [py.test installatio ## C++ tests There are also some unit tests implemented in C++, placed in unittest directory. They are not written as extensively as Python tests and are rather more a C++ low level interface check, often not using C++ API from `NitrokeyManager.cc`. Some of them are: [test_HOTP.cc](https://github.com/Nitrokey/libnitrokey/blob/master/unittest/test_HOTP.cc), [test.cc](https://github.com/Nitrokey/libnitrokey/blob/master/unittest/test.cc). -Unit tests were written and tested on Ubuntu 16.04/16.10/17.04. To run them just execute binaries built in ./libnitrokey/build dir after enabling them by passing `-DCOMPILE_TESTS` option like in `cmake .. -DCOMPILE_TESTS && make`. +Unit tests were written and tested on Ubuntu 16.04/16.10/17.04. To run them just execute binaries built in ./libnitrokey/build dir after enabling them by passing `-DCOMPILE_TESTS=ON` option like in `cmake .. -DCOMPILE_TESTS=ON && make`. The documentation of how it works could be found in nitrokey-app project's README on Github: diff --git a/libnitrokey/NitrokeyManager.h b/libnitrokey/NitrokeyManager.h index ca58d24..1f4cec4 100644 --- a/libnitrokey/NitrokeyManager.h +++ b/libnitrokey/NitrokeyManager.h @@ -80,6 +80,7 @@ char * strndup(const char* str, size_t maxlen); bool connect_with_ID(const std::string id); bool connect_with_path (std::string path); bool connect(const char *device_model); + bool connect(device::DeviceModel device_model); bool connect(); bool disconnect(); bool is_connected() throw() ; diff --git a/unittest/test_HOTP.cc b/unittest/test_HOTP.cc index 520faaf..c13fb8e 100644 --- a/unittest/test_HOTP.cc +++ b/unittest/test_HOTP.cc @@ -36,7 +36,8 @@ using namespace nitrokey::misc; void hexStringToByte(uint8_t data[], const char* hexString){ REQUIRE(strlen(hexString)%2==0); - char buf[2]; + char buf[3]; + buf[2] = '\0'; for(int i=0; i<strlen(hexString); i++){ buf[i%2] = hexString[i]; if (i%2==1){ |