Skip to content

Commit

Permalink
add production tools (hidtest)
Browse files Browse the repository at this point in the history
  • Loading branch information
prusnak committed Jun 22, 2017
1 parent e7bc8bf commit 8d3b60e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions production/hidtest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.exe
21 changes: 21 additions & 0 deletions production/hidtest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# initialize from the image

FROM fedora:24

# update package repositories

RUN dnf update -y

# install tools

RUN dnf install -y make wget
RUN dnf install -y mingw32-gcc

# install dependencies for Windows build

RUN dnf install -y mingw32-hidapi-static

# install dependencies from COPR

RUN wget https://copr-be.cloud.fedoraproject.org/results/prusnak/private/fedora-24-x86_64/00365081-mingw-protobuf/mingw32-protobuf{,-static}-2.6.1-3.fc24.noarch.rpm
RUN dnf install -y mingw32-protobuf{,-static}-2.6.1-3.fc24.noarch.rpm
18 changes: 18 additions & 0 deletions production/hidtest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
VOL_MOUNT = -v $(shell pwd):/src
IMAGETAG = trezor-prod-test

all: .wrap

.wrap: .docker-image
docker run -i -t $(VOL_MOUNT) $(IMAGETAG) /bin/sh -c 'cd /src ; make build'

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

.docker-image:
$(info Preparing docker image ...)
docker build -t $(IMAGETAG) .

shell: .docker-image
docker run -i -t $(VOL_MOUNT) $(IMAGETAG) /bin/bash
80 changes: 80 additions & 0 deletions production/hidtest/test-trezor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <hidapi/hidapi.h>

void test_ok()
{
printf("TEST OK\n");
exit(0);
}

void test_failed()
{
printf("TEST FAILED\n");
exit(1);
}

int main()
{
int res;
uint32_t pos;
uint8_t buf[65];
uint8_t msg[1024];

hid_device *handle;

handle = hid_open(0x534c, 0x0001, NULL);
if (!handle) {
printf("No TREZOR found!\n");
test_failed();
}

printf("TREZOR found\n");

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

buf[0] = 0x00;
buf[1] = '?'; buf[2] = '#'; buf[3] = '#';
buf[4] = 0x00; buf[5] = 0x00;
buf[6] = 0x00; buf[7] = 0x00; buf[8] = 0x00; buf[9] = 0x00;

// write Initialize
printf("Writing message\n");
res = hid_write(handle, buf, 65);

printf("Reading response\n");
memset(msg, 0, sizeof(msg));
pos = 0;
// read answer
for (;;) {
memset(buf, 0, sizeof(buf));
res = hid_read_timeout(handle, buf, 64, 100);
if (res < 0) {
printf("Unable to read\n");
test_failed();
}
if (res == 0) {
printf("End of read\n");
break;
}
printf("Read %d bytes\n", res);
memcpy(msg + pos, buf + 1, res - 1);
pos += res - 1;
}
printf("Total read %d bytes\n", pos);

char msg_valid = 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];

if (msg_valid && msg_id == 17 && msg_len <= pos - 8) {
test_ok();
} else {
test_failed();
}

return 0;
}

0 comments on commit 8d3b60e

Please sign in to comment.