Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fa871ec

Browse files
committedOct 7, 2017
Merge branch '71-failing_tests'
Closes #71
2 parents 544f69c + b4f6ee9 commit fa871ec

File tree

5 files changed

+156
-56
lines changed

5 files changed

+156
-56
lines changed
 

‎.travis.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ matrix:
1313
- osx_image: xcode8.2
1414
- os: linux
1515
dist: trusty
16-
env: COMPILER_NAME=gcc CXX=g++-5 CC=gcc-5 CF=-DCOMPILE_TESTS=ON
16+
env: COMPILER_NAME=gcc CXX=g++-5 CC=gcc-5
1717
addons:
1818
apt:
1919
packages:
@@ -24,7 +24,7 @@ matrix:
2424
- ubuntu-toolchain-r-test
2525
- os: linux
2626
dist: trusty
27-
env: COMPILER_NAME=gcc CXX=g++-6 CC=gcc-6 CF=-DCOMPILE_TESTS=ON
27+
env: COMPILER_NAME=gcc CXX=g++-6 CC=gcc-6
2828
addons:
2929
apt:
3030
packages:
@@ -34,7 +34,7 @@ matrix:
3434
sources: *sources
3535
- os: linux
3636
dist: trusty
37-
env: COMPILER_NAME=clang CXX=clang++-3.8 CC=clang-3.8 CF=-DCOMPILE_TESTS=ON
37+
env: COMPILER_NAME=clang CXX=clang++-3.8 CC=clang-3.8
3838
addons:
3939
apt:
4040
packages:
@@ -55,4 +55,4 @@ install:
5555

5656
script:
5757
- make -j2
58-
# - make test # TODO add library device-less tests
58+
- ctest -VV

‎CMakeLists.txt

+10-8
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,18 @@ install (FILES ${LIB_INCLUDES} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libnitrok
127127
install (TARGETS ${LIBNAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
128128
install (TARGETS ${LIBNAME}-log DESTINATION ${CMAKE_INSTALL_LIBDIR})
129129

130-
IF (COMPILE_TESTS)
131-
include_directories(unittest/Catch/include)
132130

133-
add_library(catch STATIC unittest/catch_main.cpp )
131+
include_directories(unittest/Catch/include)
132+
add_library(catch STATIC unittest/catch_main.cpp )
133+
add_executable (test_offline unittest/test_offline.cc)
134+
target_link_libraries (test_offline ${EXTRA_LIBS} ${LIBNAME}-log catch)
135+
#run with 'make test' or 'ctest'
136+
include (CTest)
137+
add_test (runs test_offline)
134138

139+
IF (COMPILE_TESTS)
140+
#needs connected PRO device for success
141+
#warning: it may delete data on the device
135142
add_executable (test_C_API unittest/test_C_API.cpp)
136143
target_link_libraries (test_C_API ${EXTRA_LIBS} ${LIBNAME}-log catch)
137144

@@ -150,11 +157,6 @@ IF (COMPILE_TESTS)
150157
add_executable (test_issues unittest/test_issues.cc)
151158
target_link_libraries (test_issues ${EXTRA_LIBS} ${LIBNAME}-log catch)
152159

153-
#run with 'make test' or 'ctest'
154-
#needs connected PRO device for success
155-
#warning: it may delete data on the device
156-
include (CTest)
157-
add_test (runs test_C_API)
158160
ENDIF()
159161

160162

‎NitrokeyManager.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,8 @@ using nitrokey::misc::strcpyT;
724724
}
725725

726726
DeviceModel NitrokeyManager::get_connected_device_model() const{
727-
//FIXME throw if no device is connected or return unknown/unconnected value
728727
if (device == nullptr){
729-
throw std::runtime_error("device not connected");
728+
throw DeviceNotConnected("device not connected");
730729
}
731730
return device->get_device_model();
732731
}

‎unittest/test2.cc

-42
Original file line numberDiff line numberDiff line change
@@ -80,48 +80,6 @@ TEST_CASE("long operation test", "[test_long]") {
8080
}
8181

8282

83-
#include "test_command_ids_header.h"
84-
85-
TEST_CASE("test device commands ids", "[fast]") {
86-
87-
// REQUIRE(STICK20_CMD_START_VALUE == static_cast<uint8_t>(CommandID::START_VALUE));
88-
REQUIRE(STICK20_CMD_ENABLE_CRYPTED_PARI == static_cast<uint8_t>(CommandID::ENABLE_CRYPTED_PARI));
89-
REQUIRE(STICK20_CMD_DISABLE_CRYPTED_PARI == static_cast<uint8_t>(CommandID::DISABLE_CRYPTED_PARI));
90-
REQUIRE(STICK20_CMD_ENABLE_HIDDEN_CRYPTED_PARI == static_cast<uint8_t>(CommandID::ENABLE_HIDDEN_CRYPTED_PARI));
91-
REQUIRE(STICK20_CMD_DISABLE_HIDDEN_CRYPTED_PARI == static_cast<uint8_t>(CommandID::DISABLE_HIDDEN_CRYPTED_PARI));
92-
REQUIRE(STICK20_CMD_ENABLE_FIRMWARE_UPDATE == static_cast<uint8_t>(CommandID::ENABLE_FIRMWARE_UPDATE));
93-
REQUIRE(STICK20_CMD_EXPORT_FIRMWARE_TO_FILE == static_cast<uint8_t>(CommandID::EXPORT_FIRMWARE_TO_FILE));
94-
REQUIRE(STICK20_CMD_GENERATE_NEW_KEYS == static_cast<uint8_t>(CommandID::GENERATE_NEW_KEYS));
95-
REQUIRE(STICK20_CMD_FILL_SD_CARD_WITH_RANDOM_CHARS == static_cast<uint8_t>(CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS));
96-
97-
REQUIRE(STICK20_CMD_WRITE_STATUS_DATA == static_cast<uint8_t>(CommandID::WRITE_STATUS_DATA));
98-
REQUIRE(STICK20_CMD_ENABLE_READONLY_UNCRYPTED_LUN == static_cast<uint8_t>(CommandID::ENABLE_READONLY_UNCRYPTED_LUN));
99-
REQUIRE(STICK20_CMD_ENABLE_READWRITE_UNCRYPTED_LUN == static_cast<uint8_t>(CommandID::ENABLE_READWRITE_UNCRYPTED_LUN));
100-
101-
REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX == static_cast<uint8_t>(CommandID::SEND_PASSWORD_MATRIX));
102-
REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX_PINDATA == static_cast<uint8_t>(CommandID::SEND_PASSWORD_MATRIX_PINDATA));
103-
REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX_SETUP == static_cast<uint8_t>(CommandID::SEND_PASSWORD_MATRIX_SETUP));
104-
105-
REQUIRE(STICK20_CMD_GET_DEVICE_STATUS == static_cast<uint8_t>(CommandID::GET_DEVICE_STATUS));
106-
REQUIRE(STICK20_CMD_SEND_DEVICE_STATUS == static_cast<uint8_t>(CommandID::SEND_DEVICE_STATUS));
107-
108-
REQUIRE(STICK20_CMD_SEND_HIDDEN_VOLUME_PASSWORD == static_cast<uint8_t>(CommandID::SEND_HIDDEN_VOLUME_PASSWORD));
109-
REQUIRE(STICK20_CMD_SEND_HIDDEN_VOLUME_SETUP == static_cast<uint8_t>(CommandID::SEND_HIDDEN_VOLUME_SETUP));
110-
REQUIRE(STICK20_CMD_SEND_PASSWORD == static_cast<uint8_t>(CommandID::SEND_PASSWORD));
111-
REQUIRE(STICK20_CMD_SEND_NEW_PASSWORD == static_cast<uint8_t>(CommandID::SEND_NEW_PASSWORD));
112-
REQUIRE(STICK20_CMD_CLEAR_NEW_SD_CARD_FOUND == static_cast<uint8_t>(CommandID::CLEAR_NEW_SD_CARD_FOUND));
113-
114-
REQUIRE(STICK20_CMD_SEND_STARTUP == static_cast<uint8_t>(CommandID::SEND_STARTUP));
115-
REQUIRE(STICK20_CMD_SEND_CLEAR_STICK_KEYS_NOT_INITIATED == static_cast<uint8_t>(CommandID::SEND_CLEAR_STICK_KEYS_NOT_INITIATED));
116-
REQUIRE(STICK20_CMD_SEND_LOCK_STICK_HARDWARE == static_cast<uint8_t>(CommandID::SEND_LOCK_STICK_HARDWARE));
117-
118-
REQUIRE(STICK20_CMD_PRODUCTION_TEST == static_cast<uint8_t>(CommandID::PRODUCTION_TEST));
119-
REQUIRE(STICK20_CMD_SEND_DEBUG_DATA == static_cast<uint8_t>(CommandID::SEND_DEBUG_DATA));
120-
121-
REQUIRE(STICK20_CMD_CHANGE_UPDATE_PIN == static_cast<uint8_t>(CommandID::CHANGE_UPDATE_PIN));
122-
123-
}
124-
12583
TEST_CASE("test device internal status with various commands", "[fast]") {
12684
auto stick = make_shared<Stick20>();
12785
bool connected = stick->connect();

‎unittest/test_offline.cc

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include "catch.hpp"
2+
#include <NitrokeyManager.h>
3+
#include <memory>
4+
#include "../NK_C_API.h"
5+
6+
using namespace nitrokey::proto;
7+
using namespace nitrokey::device;
8+
9+
using namespace std;
10+
using namespace nitrokey;
11+
12+
//This test suite assumes no Pro or Storage devices are connected
13+
14+
TEST_CASE("Return false on no device connected", "[fast]") {
15+
INFO("This test case assumes no Pro or Storage devices are connected");
16+
auto stick = make_shared<Stick20>();
17+
bool connected = true;
18+
REQUIRE_NOTHROW(connected = stick->connect());
19+
REQUIRE_FALSE(connected);
20+
21+
auto stick_pro = make_shared<Stick10>();
22+
REQUIRE_NOTHROW(connected = stick_pro->connect());
23+
REQUIRE_FALSE(connected);
24+
25+
26+
auto i = NitrokeyManager::instance();
27+
REQUIRE_NOTHROW(connected = i->connect());
28+
REQUIRE_FALSE(connected);
29+
REQUIRE_FALSE(i->is_connected());
30+
REQUIRE_FALSE(i->disconnect());
31+
REQUIRE_FALSE(i->could_current_device_be_enumerated());
32+
33+
34+
int C_connected = 1;
35+
REQUIRE_NOTHROW(C_connected = NK_login_auto());
36+
REQUIRE(0 == C_connected);
37+
}
38+
39+
TEST_CASE("Test C++ side behaviour in offline", "[fast]") {
40+
auto i = NitrokeyManager::instance();
41+
42+
string serial_number;
43+
REQUIRE_NOTHROW (serial_number = i->get_serial_number());
44+
REQUIRE(serial_number.empty());
45+
46+
REQUIRE_THROWS_AS(
47+
i->get_status(), DeviceNotConnected
48+
);
49+
50+
REQUIRE_THROWS_AS(
51+
i->get_HOTP_code(0xFF, ""), InvalidSlotException
52+
);
53+
54+
REQUIRE_THROWS_AS(
55+
i->get_TOTP_code(0xFF, ""), InvalidSlotException
56+
);
57+
58+
REQUIRE_THROWS_AS(
59+
i->erase_hotp_slot(0xFF, ""), InvalidSlotException
60+
);
61+
62+
REQUIRE_THROWS_AS(
63+
i->erase_totp_slot(0xFF, ""), InvalidSlotException
64+
);
65+
66+
REQUIRE_THROWS_AS(
67+
i->get_totp_slot_name(0xFF), InvalidSlotException
68+
);
69+
70+
REQUIRE_THROWS_AS(
71+
i->get_hotp_slot_name(0xFF), InvalidSlotException
72+
);
73+
74+
REQUIRE_THROWS_AS(
75+
i->first_authenticate("123123", "123123"), DeviceNotConnected
76+
);
77+
78+
REQUIRE_THROWS_AS(
79+
i->get_connected_device_model(), DeviceNotConnected
80+
);
81+
82+
REQUIRE_THROWS_AS(
83+
i->clear_new_sd_card_warning("123123"), DeviceNotConnected
84+
);
85+
86+
}
87+
88+
89+
TEST_CASE("Test helper function - hex_string_to_byte", "[fast]") {
90+
using namespace nitrokey::misc;
91+
std::vector<uint8_t> v;
92+
REQUIRE_NOTHROW(v = hex_string_to_byte("00112233445566"));
93+
const uint8_t test_data[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
94+
REQUIRE(v.size() == sizeof(test_data));
95+
for (int i = 0; i < v.size(); ++i) {
96+
INFO("Position i: " << i);
97+
REQUIRE(v.data()[i] == test_data[i]);
98+
}
99+
}
100+
101+
#include "test_command_ids_header.h"
102+
TEST_CASE("Test device commands ids", "[fast]") {
103+
// Make sure CommandID values are in sync with firmware's header
104+
105+
// REQUIRE(STICK20_CMD_START_VALUE == static_cast<uint8_t>(CommandID::START_VALUE));
106+
REQUIRE(STICK20_CMD_ENABLE_CRYPTED_PARI == static_cast<uint8_t>(CommandID::ENABLE_CRYPTED_PARI));
107+
REQUIRE(STICK20_CMD_DISABLE_CRYPTED_PARI == static_cast<uint8_t>(CommandID::DISABLE_CRYPTED_PARI));
108+
REQUIRE(STICK20_CMD_ENABLE_HIDDEN_CRYPTED_PARI == static_cast<uint8_t>(CommandID::ENABLE_HIDDEN_CRYPTED_PARI));
109+
REQUIRE(STICK20_CMD_DISABLE_HIDDEN_CRYPTED_PARI == static_cast<uint8_t>(CommandID::DISABLE_HIDDEN_CRYPTED_PARI));
110+
REQUIRE(STICK20_CMD_ENABLE_FIRMWARE_UPDATE == static_cast<uint8_t>(CommandID::ENABLE_FIRMWARE_UPDATE));
111+
REQUIRE(STICK20_CMD_EXPORT_FIRMWARE_TO_FILE == static_cast<uint8_t>(CommandID::EXPORT_FIRMWARE_TO_FILE));
112+
REQUIRE(STICK20_CMD_GENERATE_NEW_KEYS == static_cast<uint8_t>(CommandID::GENERATE_NEW_KEYS));
113+
REQUIRE(STICK20_CMD_FILL_SD_CARD_WITH_RANDOM_CHARS == static_cast<uint8_t>(CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS));
114+
115+
REQUIRE(STICK20_CMD_WRITE_STATUS_DATA == static_cast<uint8_t>(CommandID::WRITE_STATUS_DATA));
116+
REQUIRE(STICK20_CMD_ENABLE_READONLY_UNCRYPTED_LUN == static_cast<uint8_t>(CommandID::ENABLE_READONLY_UNCRYPTED_LUN));
117+
REQUIRE(STICK20_CMD_ENABLE_READWRITE_UNCRYPTED_LUN == static_cast<uint8_t>(CommandID::ENABLE_READWRITE_UNCRYPTED_LUN));
118+
119+
REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX == static_cast<uint8_t>(CommandID::SEND_PASSWORD_MATRIX));
120+
REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX_PINDATA == static_cast<uint8_t>(CommandID::SEND_PASSWORD_MATRIX_PINDATA));
121+
REQUIRE(STICK20_CMD_SEND_PASSWORD_MATRIX_SETUP == static_cast<uint8_t>(CommandID::SEND_PASSWORD_MATRIX_SETUP));
122+
123+
REQUIRE(STICK20_CMD_GET_DEVICE_STATUS == static_cast<uint8_t>(CommandID::GET_DEVICE_STATUS));
124+
REQUIRE(STICK20_CMD_SEND_DEVICE_STATUS == static_cast<uint8_t>(CommandID::SEND_DEVICE_STATUS));
125+
126+
REQUIRE(STICK20_CMD_SEND_HIDDEN_VOLUME_PASSWORD == static_cast<uint8_t>(CommandID::SEND_HIDDEN_VOLUME_PASSWORD));
127+
REQUIRE(STICK20_CMD_SEND_HIDDEN_VOLUME_SETUP == static_cast<uint8_t>(CommandID::SEND_HIDDEN_VOLUME_SETUP));
128+
REQUIRE(STICK20_CMD_SEND_PASSWORD == static_cast<uint8_t>(CommandID::SEND_PASSWORD));
129+
REQUIRE(STICK20_CMD_SEND_NEW_PASSWORD == static_cast<uint8_t>(CommandID::SEND_NEW_PASSWORD));
130+
REQUIRE(STICK20_CMD_CLEAR_NEW_SD_CARD_FOUND == static_cast<uint8_t>(CommandID::CLEAR_NEW_SD_CARD_FOUND));
131+
132+
REQUIRE(STICK20_CMD_SEND_STARTUP == static_cast<uint8_t>(CommandID::SEND_STARTUP));
133+
REQUIRE(STICK20_CMD_SEND_CLEAR_STICK_KEYS_NOT_INITIATED == static_cast<uint8_t>(CommandID::SEND_CLEAR_STICK_KEYS_NOT_INITIATED));
134+
REQUIRE(STICK20_CMD_SEND_LOCK_STICK_HARDWARE == static_cast<uint8_t>(CommandID::SEND_LOCK_STICK_HARDWARE));
135+
136+
REQUIRE(STICK20_CMD_PRODUCTION_TEST == static_cast<uint8_t>(CommandID::PRODUCTION_TEST));
137+
REQUIRE(STICK20_CMD_SEND_DEBUG_DATA == static_cast<uint8_t>(CommandID::SEND_DEBUG_DATA));
138+
139+
REQUIRE(STICK20_CMD_CHANGE_UPDATE_PIN == static_cast<uint8_t>(CommandID::CHANGE_UPDATE_PIN));
140+
141+
}

0 commit comments

Comments
 (0)
Please sign in to comment.