diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2018-07-07 21:24:14 +0200 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2018-07-07 21:24:14 +0200 |
commit | 430e4b4116ec00c4875170642f8ae04bc3497d88 (patch) | |
tree | d158fe787ecf1b20cd906dda712ecee8f7285a59 | |
parent | 4b3b21dfe6958376db3b1fbcadf6e03f53291e4f (diff) | |
parent | 601f43bfa71b479e32a289eaeec75069cbc7c26f (diff) | |
download | libnitrokey-430e4b4116ec00c4875170642f8ae04bc3497d88.tar.gz libnitrokey-430e4b4116ec00c4875170642f8ae04bc3497d88.tar.bz2 |
Merge branch 'pr_116'v3.4
Allow to connect to device with model specified by enum.
Fixes #116
-rw-r--r-- | NK_C_API.cc | 20 | ||||
-rw-r--r-- | NK_C_API.h | 16 | ||||
-rw-r--r-- | unittest/conftest.py | 1 | ||||
-rw-r--r-- | unittest/test_C_API.cpp | 16 | ||||
-rw-r--r-- | unittest/test_pro.py | 5 |
5 files changed, 54 insertions, 4 deletions
diff --git a/NK_C_API.cc b/NK_C_API.cc index 1d9ff4f..7d0a10e 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -156,6 +156,7 @@ extern "C" { case NK_STORAGE: model_string = "S"; break; + case NK_DISCONNECTED: default: /* no such enum value -- return error code */ return 0; @@ -224,6 +225,25 @@ extern "C" { } + NK_C_API enum NK_device_model NK_get_device_model() { + auto m = NitrokeyManager::instance(); + try { + auto model = m->get_connected_device_model(); + switch (model) { + case DeviceModel::PRO: + return NK_PRO; + case DeviceModel::STORAGE: + return NK_STORAGE; + default: + /* unknown or not connected device */ + return NK_device_model::NK_DISCONNECTED; + } + } catch (const DeviceNotConnected& e) { + return NK_device_model::NK_DISCONNECTED; + } +} + + void clear_string(std::string &s) { std::fill(s.begin(), s.end(), ' '); } @@ -43,14 +43,18 @@ extern "C" { * The Nitrokey device models supported by the API. */ enum NK_device_model { + /** + * Use, if no supported device is connected + */ + NK_DISCONNECTED = 0, /** * Nitrokey Pro. */ - NK_PRO, + NK_PRO = 1, /** * Nitrokey Storage. */ - NK_STORAGE + NK_STORAGE = 2 }; /** @@ -203,6 +207,14 @@ extern "C" { NK_C_API int NK_logout(); /** + * Query the model of the connected device. + * Returns the model of the connected device or NK_DISCONNECTED. + * + * @return true if a device is connected and the out argument has been set + */ + NK_C_API enum NK_device_model NK_get_device_model(); + + /** * Return the debug status string. Debug purposes. * @return command processing error code */ diff --git a/unittest/conftest.py b/unittest/conftest.py index 35cc714..253e1d8 100644 --- a/unittest/conftest.py +++ b/unittest/conftest.py @@ -44,7 +44,6 @@ def C(request=None): cnt = 0 a = iter(declarations) for declaration in a: - if 'NK_device_model' in declaration: continue if declaration.strip().startswith('NK_C_API'): declaration = declaration.replace('NK_C_API', '').strip() while ';' not in declaration: diff --git a/unittest/test_C_API.cpp b/unittest/test_C_API.cpp index acfadd2..1964738 100644 --- a/unittest/test_C_API.cpp +++ b/unittest/test_C_API.cpp @@ -84,4 +84,18 @@ TEST_CASE("multiple devices with ID", "[BASIC]") { } free (string); -}
\ No newline at end of file +} + +TEST_CASE("Get device model", "[BASIC]") { + NK_logout(); + NK_device_model model = NK_get_device_model(); + REQUIRE(model == NK_device_model::NK_DISCONNECTED); + + auto success = NK_login_auto() == 1; + REQUIRE(success); + model = NK_get_device_model(); + REQUIRE(model != NK_device_model::NK_DISCONNECTED); + + REQUIRE((model == NK_PRO || model == NK_STORAGE)); + NK_logout(); +} diff --git a/unittest/test_pro.py b/unittest/test_pro.py index fb936f8..1c61399 100644 --- a/unittest/test_pro.py +++ b/unittest/test_pro.py @@ -921,3 +921,8 @@ def test_TOTP_codes_from_nitrokeyapp(secret, C): lib_at = lambda : bb(oath.totp(secret, period=period)) print (lib_at()) assert lib_at() == code_device + + +def test_get_device_model(C): + assert C.NK_get_device_model() != 0 + # assert C.NK_get_device_model() != C.NK_DISCONNECTED
\ No newline at end of file |