From 4ac5f8bbbd6e007ecd02be46bfbc0c11de010722 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 1 Aug 2016 22:11:07 +0200 Subject: C++ tests: use get_payload() function to clear packet before using Signed-off-by: Szczepan Zalega --- unittest/test.cc | 17 ++++++++++------- unittest/test_HOTP.cc | 13 +++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'unittest') diff --git a/unittest/test.cc b/unittest/test.cc index 02f742c..6267512 100644 --- a/unittest/test.cc +++ b/unittest/test.cc @@ -11,9 +11,11 @@ using namespace std; using namespace nitrokey::device; using namespace nitrokey::proto::stick10; using namespace nitrokey::log; +using namespace nitrokey::misc; + std::string getSlotName(Stick10 &stick, int slotNo) { - ReadSlot::CommandTransaction::CommandPayload slot_req; + auto slot_req = get_payload(); slot_req.slot_number = slotNo; auto slot = ReadSlot::CommandTransaction::run(stick, slot_req); std::string sName(reinterpret_cast(slot.slot_name)); @@ -29,16 +31,17 @@ TEST_CASE("Slot names are correct", "[slotNames]") { auto resp = GetStatus::CommandTransaction::run(stick); - FirstAuthenticate::CommandTransaction::CommandPayload authreq; + auto authreq = get_payload(); strcpy((char *)(authreq.card_password), "12345678"); FirstAuthenticate::CommandTransaction::run(stick, authreq); { - EnablePasswordSafe::CommandTransaction::CommandPayload authreq; - strcpy((char *)(authreq.password), "123456"); + auto authreq = get_payload(); + strcpy((char *)(authreq.user_password), "123456"); EnablePasswordSafe::CommandTransaction::run(stick, authreq); } + //assuming these values were set earlier, thus failing on normal use REQUIRE(getSlotName(stick, 0x20) == std::string("1")); REQUIRE(getSlotName(stick, 0x21) == std::string("slot2")); @@ -52,7 +55,7 @@ TEST_CASE("Slot names are correct", "[slotNames]") { } { - GetPasswordSafeSlotName::CommandTransaction::CommandPayload slot; + auto slot = get_payload(); slot.slot_number = 0; auto resp2 = GetPasswordSafeSlotName::CommandTransaction::run(stick, slot); std::string sName(reinterpret_cast(resp2.slot_name)); @@ -60,7 +63,7 @@ TEST_CASE("Slot names are correct", "[slotNames]") { } { - GetPasswordSafeSlotPassword::CommandTransaction::CommandPayload slot; + auto slot = get_payload(); slot.slot_number = 0; auto resp2 = GetPasswordSafeSlotPassword::CommandTransaction::run(stick, slot); @@ -69,7 +72,7 @@ TEST_CASE("Slot names are correct", "[slotNames]") { } { - GetPasswordSafeSlotLogin::CommandTransaction::CommandPayload slot; + auto slot = get_payload(); slot.slot_number = 0; auto resp2 = GetPasswordSafeSlotLogin::CommandTransaction::run(stick, slot); std::string sName(reinterpret_cast(resp2.slot_login)); diff --git a/unittest/test_HOTP.cc b/unittest/test_HOTP.cc index a961b24..f25bad4 100644 --- a/unittest/test_HOTP.cc +++ b/unittest/test_HOTP.cc @@ -5,11 +5,13 @@ #include "log.h" #include "stick10_commands.h" #include +#include "misc.h" using namespace std; using namespace nitrokey::device; using namespace nitrokey::proto::stick10; using namespace nitrokey::log; +using namespace nitrokey::misc; void hexStringToByte(uint8_t data[], const char* hexString){ assert(strlen(hexString)%2==0); @@ -43,7 +45,7 @@ TEST_CASE("Test HOTP codes according to RFC", "[HOTP]") { const char * temporary_password = "123456789012345678901234"; { - FirstAuthenticate::CommandTransaction::CommandPayload authreq; + auto authreq = get_payload(); strcpy((char *)(authreq.card_password), "12345678"); strcpy((char *)(authreq.temporary_password), temporary_password); FirstAuthenticate::CommandTransaction::run(stick, authreq); @@ -51,21 +53,20 @@ TEST_CASE("Test HOTP codes according to RFC", "[HOTP]") { //test according to https://tools.ietf.org/html/rfc4226#page-32 { - WriteToHOTPSlot::CommandTransaction::CommandPayload hwrite; + auto hwrite = get_payload(); hwrite.slot_number = 0x10; strcpy(reinterpret_cast(hwrite.slot_name), "rfc4226_lib"); //strcpy(reinterpret_cast(hwrite.slot_secret), ""); const char* secretHex = "3132333435363738393031323334353637383930"; hexStringToByte(hwrite.slot_secret, secretHex); - // reset the HOTP counter -// memset(hwrite.slot_counter, 0, 8); + //hwrite.slot_config; //TODO check various configs in separate test cases //strcpy(reinterpret_cast(hwrite.slot_token_id), ""); //strcpy(reinterpret_cast(hwrite.slot_counter), ""); //authorize writehotp first { - Authorize::CommandTransaction::CommandPayload auth; + auto auth = get_payload(); strcpy((char *)(auth.temporary_password), temporary_password); auth.crc_to_authorize = WriteToHOTPSlot::CommandTransaction::getCRC(hwrite); Authorize::CommandTransaction::run(stick, auth); @@ -80,7 +81,7 @@ TEST_CASE("Test HOTP codes according to RFC", "[HOTP]") { }; for( auto code: codes){ - GetHOTP::CommandTransaction::CommandPayload gh; + auto gh = get_payload(); gh.slot_number = 0x10; auto resp = GetHOTP::CommandTransaction::run(stick, gh); REQUIRE( resp.code == code); -- cgit v1.2.1