Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions tests/tests_u2f_standard.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@


static bool arg_hasButton = true; // fob has button
int U_TESTS_FAIL = 0;

struct U2Fob *device;

Expand All @@ -40,7 +41,7 @@ static void WaitForUserPresence(struct U2Fob *dev, bool hasButton)
U2Fob_close(dev);
if (U2Fob_liveDeviceTesting()) {
if (hasButton) {
printf("Hit enter then touch the device button...");
PRINT_MESSAGE("Hit enter then SHORT touch the device button...");
}
if (scanf("%c", msg)) {
(void) msg;
Expand Down Expand Up @@ -291,31 +292,50 @@ static void check_CounterUpdate(void)

sha256_Raw((const uint8_t *)tests_pwd, strlens(tests_pwd), KEY_STANDARD);
sha256_Raw(KEY_STANDARD, 32, KEY_STANDARD);

PRINT_INFO("Resetting device...");
api_format_send_cmd(cmd_str(CMD_password), tests_pwd, NULL);
api_format_send_cmd(cmd_str(CMD_backup), "erase", KEY_STANDARD);

if (U2Fob_liveDeviceTesting()) {
PRINT_MESSAGE("Seeding device. LONG touch if LED is lit...\n");
}
snprintf(cmd, sizeof(cmd),
"{\"source\":\"create\", \"filename\":\"hwwcountertest.pdf\", \"key\":\"key\"}");
api_format_send_cmd(cmd_str(CMD_seed), cmd, KEY_STANDARD);
ASSERT_SUCCESS;

if (U2Fob_liveDeviceTesting()) {
PRINT_MESSAGE("Creating new U2F key. LONG touch to continue...\n");
}
snprintf(cmd, sizeof(cmd),
"{\"source\":\"U2F_create\", \"key\":\"key\", \"filename\":\"u2fcountertest.pdf\", \"U2F_counter\":%u}",
c);
api_format_send_cmd(cmd_str(CMD_seed), cmd, KEY_STANDARD);
ASSERT_SUCCESS;

// Ctr should be c + 1.
// Need to re-enroll after device reset
test_Enroll(0x9000, 0);
ctr = test_Sign(0x9000, false);
WaitForUserPresence(device, arg_hasButton);
PASS(test_Enroll(0x9000, 0));
WaitForUserPresence(device, arg_hasButton);
PASS(ctr = test_Sign(0x9000, false));
CHECK_EQ(ctr, c + 1);

// Update U2F counter using HWW interface `U2F_load`.
if (U2Fob_liveDeviceTesting()) {
PRINT_MESSAGE("Reloading previous U2F key. LONG touch to continue...\n");
}
c = 0xF;
snprintf(cmd, sizeof(cmd),
"{\"source\":\"U2F_load\", \"key\":\"key\", \"filename\":\"u2fcountertest.pdf\", \"U2F_counter\":%u}",
c);
api_format_send_cmd(cmd_str(CMD_seed), cmd, KEY_STANDARD);
ASSERT_SUCCESS;

// Ctr should be c + 1.
ctr = test_Sign(0x9000, false);
WaitForUserPresence(device, arg_hasButton);
PASS(ctr = test_Sign(0x9000, false));
CHECK_EQ(ctr, c + 1);
}

Expand All @@ -338,8 +358,7 @@ static void run_tests(void)
// Fob with button should need touch.
if (U2Fob_liveDeviceTesting() && arg_hasButton) {
// Timeout
fprintf(stderr, "Wait for device timeout.\n");
fflush(stderr);
PRINT_MESSAGE("WAIT for device timeout.\n");
PASS(test_Enroll(0x6985, 1));
}
WaitForUserPresence(device, arg_hasButton);
Expand All @@ -348,8 +367,7 @@ static void run_tests(void)
// Fob with button should have consumed touch.
if (U2Fob_liveDeviceTesting() && arg_hasButton) {
// Timeout
fprintf(stderr, "Wait for device timeout.\n");
fflush(stderr);
PRINT_MESSAGE("WAIT for device timeout.\n");
PASS(test_Sign(0x6985, false));
}

Expand All @@ -374,8 +392,7 @@ static void run_tests(void)
PASS(ctr1 = test_Sign(0x9000, false));
if (U2Fob_liveDeviceTesting()) {
// Timeout
fprintf(stderr, "Wait for device timeout.\n");
fflush(stderr);
PRINT_MESSAGE("WAIT for device timeout.\n");
PASS(test_Sign(0x6985, false));
}

Expand All @@ -391,7 +408,7 @@ static void run_tests(void)
PASS(check_CounterUpdate());

} else {
printf("\n\nNot testing HID API. A device is not connected.\n\n");
PRINT_MESSAGE("\n\nNot testing HID API. A device is not connected.\n\n");
return;
}

Expand All @@ -405,7 +422,7 @@ uint32_t __stack_chk_guard = 0;
extern void __attribute__((noreturn)) __stack_chk_fail(void);
void __attribute__((noreturn)) __stack_chk_fail(void)
{
printf("\n\nError: stack smashing detected!\n\n");
PRINT_MESSAGE("\n\nError: stack smashing detected!\n\n");
abort();
}

Expand All @@ -417,23 +434,29 @@ int main(void)

// Test the C code API
U2Fob_testLiveDevice(0);
TEST_LIVE_DEVICE = 0;
random_init();
__stack_chk_guard = random_uint32(0);
ecc_context_init();
memory_setup();
memory_setup(); // run twice
printf("\n\nInternal API Result:\n");
PRINT_MESSAGE("\n\nInternal API Result:\n");
run_tests();
ecc_context_destroy();

// Live test of the HID API
#ifndef CONTINUOUS_INTEGRATION
U2Fob_testLiveDevice(1);
printf("\n\nHID API Result:\n");
run_tests();
TEST_LIVE_DEVICE = 1;
if (api_hid_init() == DBB_ERROR) {
PRINT_MESSAGE("\n\nNot testing HID API. A device is not connected.\n\n");
} else {
PRINT_MESSAGE("\n\nHID API Result:\n");
run_tests();
}
#endif

printf("\nALL TESTS PASSED\n\n");
PRINT_MESSAGE("\nALL TESTS PASSED\n\n");
return 0;
}

20 changes: 10 additions & 10 deletions tests/u2f/u2f_util_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static int hid_read_timeout(void *dev, uint8_t *r, size_t r_len, int to)
}
#endif

static int TEST_LIVE_DEVICE = 0;
static int U2F_TEST_LIVE_DEVICE = 0;

#ifdef __APPLE__
#ifndef CLOCK_MONOTONIC
Expand Down Expand Up @@ -90,13 +90,13 @@ static void clock_gettime(int which, struct timespec *ts)

void U2Fob_testLiveDevice(uint8_t test)
{
TEST_LIVE_DEVICE = test;
U2F_TEST_LIVE_DEVICE = test;
}


uint8_t U2Fob_liveDeviceTesting(void)
{
return TEST_LIVE_DEVICE;
return U2F_TEST_LIVE_DEVICE;
}


Expand Down Expand Up @@ -186,7 +186,7 @@ static char *U2Fob_path(void)

int U2Fob_open(struct U2Fob *device)
{
if (!TEST_LIVE_DEVICE) {
if (!U2F_TEST_LIVE_DEVICE) {
return -U2FHID_ERR_NONE;
}
U2Fob_close(device);
Expand All @@ -198,7 +198,7 @@ int U2Fob_open(struct U2Fob *device)

void U2Fob_close(struct U2Fob *device)
{
if (TEST_LIVE_DEVICE && device->dev) {
if (U2F_TEST_LIVE_DEVICE && device->dev) {
hid_close(device->dev);
device->dev = NULL;
}
Expand All @@ -207,7 +207,7 @@ void U2Fob_close(struct U2Fob *device)

int U2Fob_reopen(struct U2Fob *device)
{
if (!TEST_LIVE_DEVICE) {
if (!U2F_TEST_LIVE_DEVICE) {
return -U2FHID_ERR_NONE;
}
U2Fob_close(device);
Expand All @@ -226,7 +226,7 @@ int U2Fob_sendHidFrame(struct U2Fob *device, USB_FRAME *f)
memcpy(d + 1, f, sizeof(USB_FRAME));
f->cid = ntohl(f->cid);

if (TEST_LIVE_DEVICE) {
if (U2F_TEST_LIVE_DEVICE) {
if (!device->dev) {
return -U2FHID_ERR_OTHER;
}
Expand All @@ -250,13 +250,13 @@ int U2Fob_receiveHidFrame(struct U2Fob *device, USB_FRAME *r, float to)
return -U2FHID_ERR_MSG_TIMEOUT;
}

if (TEST_LIVE_DEVICE && !device->dev) {
if (U2F_TEST_LIVE_DEVICE && !device->dev) {
return -U2FHID_ERR_OTHER;
}
memset((int8_t *)r, 0xEE, sizeof(USB_FRAME));

int res = 0;
if (TEST_LIVE_DEVICE) {
if (U2F_TEST_LIVE_DEVICE) {
res = hid_read_timeout(device->dev,
(uint8_t *) r, sizeof(USB_FRAME),
(int) (to * 1000));
Expand All @@ -272,7 +272,7 @@ int U2Fob_receiveHidFrame(struct U2Fob *device, USB_FRAME *r, float to)
}

if (res == sizeof(USB_FRAME)) {
if (TEST_LIVE_DEVICE) {
if (U2F_TEST_LIVE_DEVICE) {
r->cid = ntohl(r->cid);
}
return 0;
Expand Down
3 changes: 3 additions & 0 deletions tests/u2f/u2f_util_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ typedef void hid_device;
printf(__VA_ARGS__); \
printf("\x1b[0m \n");\
} while(0)
#define PRINT_MESSAGE(...) do { fprintf(stderr, __VA_ARGS__); \
fflush(stderr);\
} while(0)



Expand Down