From 5910ec72e0f7abd3f6c876e04a07057b0dd8edaa Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 21 May 2018 00:20:15 +0200 Subject: Correct return value for NitrokeyManager::connect() NitrokeyManager::connect() currently returns true if the device pointer is set. Yet this does not mean that the connection was successful. For example, NitrokeyManger::connect(const char*) sets the device pointer even if it was not successful. This patch introduces a variable that keeps track of the connection instead of checking the device pointer. This corrects the return value without changing the behavior of the connect method (returning the Storage device if both a Pro and a Storage device are present). --- NitrokeyManager.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 085bf78..f86a3eb 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -234,12 +234,14 @@ using nitrokey::misc::strcpyT; bool NitrokeyManager::connect() { std::lock_guard lock(mex_dev_com_manager); vector< shared_ptr > devices = { make_shared(), make_shared() }; + bool connected = false; for( auto & d : devices ){ if (d->connect()){ device = std::shared_ptr(d); + connected = true; } } - return device != nullptr; + return connected; } -- cgit v1.2.1 From 8ea740f3cc872b2585a4ecf97a29127aa9316137 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 20 Jun 2018 10:14:41 +0200 Subject: Fix log warning regarding repeated disconnection Signed-off-by: Szczepan Zalega --- device.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/device.cc b/device.cc index da54e33..80e4b38 100644 --- a/device.cc +++ b/device.cc @@ -69,8 +69,10 @@ bool Device::_disconnect() { LOG(std::string(__FUNCTION__) + std::string(m_model == DeviceModel::PRO ? "PRO" : "STORAGE"), Loglevel::DEBUG_L2); LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); - LOG(std::string("Disconnection: handle already freed: ") + std::to_string(mp_devhandle == nullptr) + " ("+m_path+")", Loglevel::DEBUG_L1); - if(mp_devhandle == nullptr) return false; + if(mp_devhandle == nullptr) { + LOG(std::string("Disconnection: handle already freed: ") + std::to_string(mp_devhandle == nullptr) + " ("+m_path+")", Loglevel::DEBUG_L1); + return false; + } hid_close(mp_devhandle); mp_devhandle = nullptr; -- cgit v1.2.1 From a71507ba0ca4643201c4ceb3d4dfc0f6dd5ae770 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 20 Jun 2018 10:15:26 +0200 Subject: Add extended test for the connect() return value in offline mode Signed-off-by: Szczepan Zalega --- unittest/test_offline.cc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/unittest/test_offline.cc b/unittest/test_offline.cc index 468849e..e34eeb4 100644 --- a/unittest/test_offline.cc +++ b/unittest/test_offline.cc @@ -160,3 +160,29 @@ TEST_CASE("Test device commands ids", "[fast]") { REQUIRE(STICK20_CMD_CHANGE_UPDATE_PIN == static_cast(CommandID::CHANGE_UPDATE_PIN)); } + + +TEST_CASE("Connect should not return true after the second attempt", "[fast]") { + int result = 0; + + result = NK_login("S"); + REQUIRE(result == 0); + + result = NK_login_auto(); + REQUIRE(result == 0); + + result = NK_logout(); + REQUIRE(result == 0); + + result = NK_logout(); + REQUIRE(result == 0); + + result = NK_login("P"); + REQUIRE(result == 0); + + result = NK_login_auto(); + REQUIRE(result == 0); + + result = NK_logout(); + REQUIRE(result == 0); +} -- cgit v1.2.1