Skip to content

Commit

Permalink
production: add self-test to test-trezor
Browse files Browse the repository at this point in the history
  • Loading branch information
prusnak committed Jul 18, 2017
1 parent 76fcde6 commit e048924
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
3 changes: 1 addition & 2 deletions production/hidtest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ build-lin:
gcc test-trezor.c $(CFLAGS) -lhidapi-libusb -o test-trezor

build-win:
i686-w64-mingw32-gcc test-trezor.c $(CFLAGS) -Wl,-Bstatic -lhidapi -Wl,-Bdynamic -lsetupapi -mwindows -o test-trezor-32bit.exe
# /usr/i686-w64-mingw32/sys-root/mingw/lib/libhidapi.a
i686-w64-mingw32-gcc test-trezor.c $(CFLAGS) -Wl,-Bstatic -lhidapi -Wl,-Bdynamic -lsetupapi -mconsole -o test-trezor-32bit.exe

.docker-image:
$(info Preparing docker image ...)
Expand Down
80 changes: 73 additions & 7 deletions production/hidtest/test-trezor.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void write_Initialize(void)
{
INFO("Writing message: Initialize");

uint32_t msg_id = 0;
uint32_t msg_size = 0;
const uint32_t msg_id = 0;
const uint32_t msg_size = 0;

uint8_t buf[USB_PACKET_SIZE + 1] = {
0x00,
Expand All @@ -54,12 +54,13 @@ void read_Features(void)
uint8_t buf[USB_PACKET_SIZE];

memset(msg, 0, sizeof(msg));
memset(buf, 0, sizeof(buf));

INFO("Reading message: Features");

int res, pos = 0;
int pos = 0;
for (;;) {
res = hid_read_timeout(handle, buf, USB_PACKET_SIZE, USB_READ_TIMEOUT);
int res = hid_read_timeout(handle, buf, USB_PACKET_SIZE, USB_READ_TIMEOUT);
if (res == 0) {
break;
}
Expand All @@ -73,12 +74,74 @@ void read_Features(void)
pos += res - 1;
}

bool msg_valid = (msg[0] == '#') && (msg[1] == '#');
bool msg_valid_prefix = (msg[0] == '#') && (msg[1] == '#');
uint16_t msg_id = (msg[2] << 8) + msg[3];
uint32_t msg_len = (msg[4] << 24) + (msg[5]<< 16) + (msg[6] << 8) + msg[7];
uint32_t msg_len = (msg[4] << 24) + (msg[5] << 16) + (msg[6] << 8) + msg[7];
uint32_t aligned_len = (msg_len + 62) / 63 * 63;

if (msg_valid && msg_id == 17 && aligned_len == pos) {
if (msg_valid_prefix && msg_id == 17 && aligned_len == pos) {
INFO("Message read OK");
} else {
FAIL("Incorrect message");
}
}

void write_SelfTest(void)
{
INFO("Writing message: SelfTest");

const uint32_t msg_id = 0x20;
const uint32_t msg_size = 55;

uint8_t buf[USB_PACKET_SIZE + 1] = {
0x00,
'?', '#', '#',
(msg_id >> 8) & 0xFF, msg_id & 0xFF,
(msg_size >> 24) & 0xFF, (msg_size >> 16) & 0xFF, (msg_size >> 8) & 0xFF, msg_size & 0xFF,
0x0A,
53,
0x00, 0xFF, 0x55, 0xAA, 0x66, 0x99, 0x33, 0xCC,
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!',
0x00, 0xFF, 0x55, 0xAA, 0x66, 0x99, 0x33, 0xCC
};

int res = hid_write(handle, buf, USB_PACKET_SIZE + 1);
if (res == USB_PACKET_SIZE + 1) {
INFO("Message write OK");
} else {
FAIL("Write error");
}
}

void read_Success(void)
{
uint8_t buf[USB_PACKET_SIZE];

memset(buf, 0, sizeof(buf));

INFO("Reading message: Success");

for (;;) {
int res = hid_read_timeout(handle, buf, USB_PACKET_SIZE, USB_READ_TIMEOUT);
if (res == 0) {
continue;
}
if (res < 0) {
FAIL("Read error (failed)");
}
if (res != USB_PACKET_SIZE) {
FAIL("Read error (wrong size)");
}
break;
}

bool msg_valid_prefix = (buf[0] == '?') && (buf[1] == '#') && (buf[2] == '#');
uint16_t msg_id = (buf[3] << 8) + buf[4];
uint32_t msg_len = (buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];

if (msg_valid_prefix && msg_id == 2 && msg_len == 0) {
INFO("Message read OK");
} else {
FAIL("Incorrect message");
Expand All @@ -99,6 +162,9 @@ int main()
read_Features();
}

write_SelfTest();
read_Success();

hid_close(handle);
INFO("TEST OK");
}

0 comments on commit e048924

Please sign in to comment.