Skip to content

Commit

Permalink
add more tests for pin canceled ref fibercrypto#381
Browse files Browse the repository at this point in the history
  • Loading branch information
stdevAlDen committed Mar 5, 2020
1 parent 343427e commit 5f32cc5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
5 changes: 5 additions & 0 deletions tiny-firmware/firmware/protect.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ ErrCode_t protectChangePinEx(ErrCode_t (*funcRequestPin)(PinMatrixRequestType, c
strlcpy(pin_compare, pin, sizeof(pin_compare));
memset(pin, 0, sizeof(pin));
err = funcRequestPin(PinMatrixRequestType_PinMatrixRequestType_NewSecond, _("Please re-enter new PIN:"), pin);
if (err != ErrOk) {
memset(pin_compare, 0, sizeof(pin_compare));
memset(pin, 0, sizeof(pin));
return err;
}
{
char empty_pin[sizeof(pin)] = {0};
if (!memcmp(pin, empty_pin, sizeof(pin))) {
Expand Down
30 changes: 25 additions & 5 deletions tiny-firmware/tests/test_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,33 @@ START_TEST(test_msgChangePinSecondRejected)
}
END_TEST

START_TEST(test_msgChangePinShouldReturnCanceledAccordingToPinReader)
START_TEST(test_msgChangePinShouldReturnCanceledAccordingToPinReaderNewPin)
{
ChangePin msg = ChangePin_init_zero;
storage_wipe();

// Pin mismatch
ck_assert_int_eq(msgChangePinImpl(&msg, &pin_reader_canceled), ErrPinCancelled);
ck_assert_int_eq(storage_hasPin(), false);
ck_assert_int_eq(msgChangePinImpl(&msg, &pin_reader_new_canceled), ErrPinCancelled);
ck_assert_int_eq(storage_hasPin(), false);
}
END_TEST

START_TEST(test_msgChangePinShouldReturnCanceledAccordingToPinReaderButPreserveAPreviuosOne)
{
ErrCode_t (*pin_readers[2])(PinMatrixRequestType, const char*, char*) = {
&pin_reader_new_canceled, &pin_reader_confirm_canceled};
for (size_t i = 0; i < sizeof(pin_readers) / sizeof(*pin_readers); ++i) {
ChangePin msg = ChangePin_init_zero;
storage_wipe();
ck_assert_int_eq(storage_hasPin(), false);

ck_assert_int_eq(msgChangePinImpl(&msg, &pin_reader_ok), ErrOk);
ck_assert_int_eq(storage_hasPin(), true);
ck_assert_str_eq(storage_getPin(), TEST_PIN1);

ck_assert_int_eq(msgChangePinImpl(&msg, pin_readers[i]), ErrPinCancelled);
ck_assert_int_eq(storage_hasPin(), true);
ck_assert_str_eq(storage_getPin(), TEST_PIN1);
}
}
END_TEST

Expand Down Expand Up @@ -543,7 +562,8 @@ TCase* add_fsm_tests(TCase* tc)
tcase_add_test(tc, test_msgEntropyAckChgMixerNotInternal);
tcase_add_test(tc, test_msgChangePinSuccess);
tcase_add_test(tc, test_msgChangePinSecondRejected);
tcase_add_test(tc, test_msgChangePinShouldReturnCanceledAccordingToPinReader);
tcase_add_test(tc, test_msgChangePinShouldReturnCanceledAccordingToPinReaderNewPin);
tcase_add_test(tc, test_msgChangePinShouldReturnCanceledAccordingToPinReaderButPreserveAPreviuosOne);
tcase_add_test(tc, test_msgChangePinEditSuccess);
tcase_add_test(tc, test_msgChangePinRemoveSuccess);
tcase_add_test(tc, test_isSha256DigestHex);
Expand Down
21 changes: 19 additions & 2 deletions tiny-firmware/tests/test_pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,31 @@ ErrCode_t pin_reader_wrong(PinMatrixRequestType pinReqType, const char* text, ch
return ErrPinMismatch;
}

ErrCode_t pin_reader_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out)
ErrCode_t pin_reader_new_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out)
{
(void)text;
(void)pin_out;
switch (pinReqType) {
case PinMatrixRequestType_PinMatrixRequestType_NewFirst:
return ErrPinCancelled;
case PinMatrixRequestType_PinMatrixRequestType_Current:
case PinMatrixRequestType_PinMatrixRequestType_NewSecond:
strcpy(pin_out, TEST_PIN1);
return ErrOk;
default:
return ErrInvalidArg;
}
}

ErrCode_t pin_reader_confirm_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out)
{
(void)text;
switch (pinReqType) {
case PinMatrixRequestType_PinMatrixRequestType_NewSecond:
return ErrPinCancelled;
case PinMatrixRequestType_PinMatrixRequestType_Current:
case PinMatrixRequestType_PinMatrixRequestType_NewFirst:
strcpy(pin_out, TEST_PIN1);
return ErrOk;
default:
return ErrInvalidArg;
}
Expand Down
4 changes: 3 additions & 1 deletion tiny-firmware/tests/test_pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ ErrCode_t pin_reader_alt(PinMatrixRequestType pinReqType, const char* text, char

ErrCode_t pin_reader_wrong(PinMatrixRequestType pinReqType, const char* text, char* pin_out);

ErrCode_t pin_reader_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out);
ErrCode_t pin_reader_new_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out);

ErrCode_t pin_reader_confirm_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out);

0 comments on commit 5f32cc5

Please sign in to comment.