-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(transport-test): introduce new dedicated testing package
- Loading branch information
1 parent
d168d2b
commit f0a0601
Showing
19 changed files
with
411 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: "3.9" | ||
services: | ||
trezor-user-env-unix: | ||
image: ghcr.io/trezor/trezor-user-env | ||
environment: | ||
- SDL_VIDEODRIVER=dummy | ||
- XDG_RUNTIME_DIR=/var/tmp | ||
network_mode: host | ||
# in case local developement on mac is needed, these ports will be useful | ||
# ports: | ||
# - "9002:9002" | ||
# - "9001:9001" | ||
# - "21326:21326" | ||
# - "21325:21326" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
rules: { | ||
'no-nested-ternary': 'off', // useful in tests.. | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
This package contains a collection of end-to-end tests of the transport layer using both emulators and real devices. | ||
|
||
### Bridge tests | ||
|
||
| | needs tenv | manually connect device | manually start bridge | | ||
| ------------------------------------------------------------- | ---------- | ----------------------- | --------------------- | | ||
| yarn workspace @trezor/transport-test test:e2e:old-bridge:hw | no | yes | yes | | ||
| yarn workspace @trezor/transport-test test:e2e:old-bridge:emu | yes | no | no | | ||
| yarn workspace @trezor/transport-test test:e2e:new-bridge:hw | no | yes | no | | ||
| yarn workspace @trezor/transport-test test:e2e:new-bridge:emu | yes | no | no | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* eslint no-console: 0 */ | ||
|
||
import { WebUSB } from 'usb'; | ||
|
||
import { TrezorUserEnvLinkClass } from '@trezor/trezor-user-env-link'; | ||
import { scheduleAction, Log } from '@trezor/utils'; | ||
import { TrezordNode } from '@trezor/transport-bridge/src'; | ||
|
||
export const env = { | ||
USE_HW: process.env.USE_HW === 'true', | ||
USE_NODE_BRIDGE: process.env.USE_NODE_BRIDGE === 'true', | ||
}; | ||
|
||
console.log('env', env); | ||
|
||
const webusb = new WebUSB({ | ||
allowAllDevices: true, // return all devices, not only authorized | ||
}); | ||
|
||
/** | ||
* Controller based on TrezorUserEnvLink its main purpose is: | ||
* - to bypass communication with trezor-user-env and allow using local hw devices | ||
* - start and stop node bridge (node bridge should be implemented into trezor-user-env however) | ||
*/ | ||
class Controller extends TrezorUserEnvLinkClass { | ||
private logger: Console; | ||
private nodeBridge: TrezordNode | undefined = undefined; | ||
|
||
private originalApi: { | ||
connect: typeof TrezorUserEnvLinkClass.prototype.connect; | ||
startBridge: typeof TrezorUserEnvLinkClass.prototype.startBridge; | ||
stopBridge: typeof TrezorUserEnvLinkClass.prototype.stopBridge; | ||
startEmu: typeof TrezorUserEnvLinkClass.prototype.startEmu; | ||
stopEmu: typeof TrezorUserEnvLinkClass.prototype.stopEmu; | ||
}; | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.logger = console; | ||
|
||
this.originalApi = { | ||
connect: super.connect.bind(this), | ||
startBridge: super.startBridge.bind(this), | ||
stopBridge: super.stopBridge.bind(this), | ||
startEmu: super.startEmu.bind(this), | ||
stopEmu: super.stopEmu.bind(this), | ||
}; | ||
|
||
this.connect = !env.USE_HW | ||
? this.originalApi.connect | ||
: () => { | ||
return Promise.resolve(null); | ||
}; | ||
|
||
this.startBridge = | ||
!env.USE_HW && !env.USE_NODE_BRIDGE | ||
? (version?: string) => this.originalApi.startBridge(version) | ||
: env.USE_NODE_BRIDGE | ||
? async () => { | ||
this.nodeBridge = new TrezordNode({ | ||
port: 21325, | ||
api: !env.USE_HW ? 'udp' : 'usb', | ||
logger: new Log('test-bridge', false), | ||
}); | ||
|
||
await this.nodeBridge.start(); | ||
|
||
// todo: this shouldn't be here, nodeBridge should be started when start resolves | ||
await this.waitForBridgeIsRunning(true); | ||
|
||
return null; | ||
} | ||
: () => this.waitForBridgeIsRunning(true); | ||
|
||
this.stopBridge = | ||
!env.USE_HW && !env.USE_NODE_BRIDGE | ||
? this.originalApi.stopBridge | ||
: env.USE_NODE_BRIDGE | ||
? async () => { | ||
await this.nodeBridge?.stop(); | ||
|
||
// todo: this shouldn't be here, nodeBridge should be stopped when stop resolves | ||
await this.waitForBridgeIsRunning(false); | ||
|
||
return null; | ||
} | ||
: () => this.waitForBridgeIsRunning(false); | ||
|
||
this.startEmu = !env.USE_HW | ||
? this.originalApi.startEmu | ||
: env.USE_HW | ||
? () => this.waitForNumberOfDevices(1) | ||
: () => { | ||
console.log('todo: ping local emu'); | ||
|
||
return Promise.resolve(null); | ||
}; | ||
|
||
this.stopEmu = !env.USE_HW | ||
? this.originalApi.stopEmu | ||
: env.USE_HW | ||
? () => this.waitForNumberOfDevices(0) | ||
: () => { | ||
console.log('todo: ping local emu'); | ||
|
||
return Promise.resolve(null); | ||
}; | ||
} | ||
|
||
private waitForNumberOfDevices = (expected: number) => { | ||
this.logger.log( | ||
`${env.USE_HW ? '[MANUAL ACTION REQUIRED] ' : ''} waiting for ${expected} device to be connected`, | ||
); | ||
|
||
return scheduleAction( | ||
async () => { | ||
const devices = await webusb.getDevices(); | ||
if (devices.filter(d => d.productName === 'TREZOR').length === expected) { | ||
return null; | ||
} | ||
throw new Error('Condition not met'); | ||
}, | ||
{ | ||
deadline: Date.now() + 60_000, | ||
gap: 1000, | ||
}, | ||
); | ||
}; | ||
|
||
private waitForBridgeIsRunning = (expected: boolean) => { | ||
this.logger.log( | ||
`${env.USE_HW && !env.USE_NODE_BRIDGE ? '[MANUAL ACTION REQUIRED] ' : ''} waiting for bridge ${expected ? 'start' : 'stop'}`, | ||
); | ||
|
||
return scheduleAction( | ||
() => { | ||
return fetch('http://localhost:21325/', { | ||
method: 'POST', | ||
headers: { | ||
['Origin']: 'https://wallet.trezor.io', | ||
}, | ||
}) | ||
.then(res => { | ||
if (res.ok !== expected) { | ||
throw new Error('Condition not met'); | ||
} | ||
|
||
return res.text(); | ||
}) | ||
.then((text: string) => { | ||
console.log(`running bridge: ${text}`); | ||
|
||
return null; | ||
}) | ||
.catch(err => { | ||
if (err.message === 'Condition not met') { | ||
throw err; | ||
} | ||
if (expected) { | ||
throw err; | ||
} | ||
|
||
return null; | ||
}); | ||
}, | ||
{ | ||
deadline: Date.now() + 60_000, | ||
gap: 1000, | ||
}, | ||
); | ||
}; | ||
} | ||
|
||
export const controller = new Controller(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { DEVICE_TYPE } from '@trezor/transport/src/api/abstract'; | ||
|
||
import { env } from './controller'; | ||
|
||
const { USE_HW, USE_NODE_BRIDGE } = env; | ||
|
||
const debug = USE_NODE_BRIDGE ? undefined : USE_HW ? false : true; | ||
const debugSession = USE_NODE_BRIDGE ? undefined : null; | ||
const path = USE_NODE_BRIDGE ? expect.any(String) : '1'; | ||
const product = USE_HW ? 21441 : USE_NODE_BRIDGE ? undefined : 0; | ||
const vendor = USE_NODE_BRIDGE ? undefined : USE_HW ? 4617 : 0; | ||
const type = | ||
USE_NODE_BRIDGE && USE_HW | ||
? expect.any(Number) | ||
: USE_NODE_BRIDGE && !USE_HW | ||
? DEVICE_TYPE.TypeEmulator | ||
: undefined; | ||
|
||
/** | ||
* emu '127.0.0.1:21324' (15) | ||
* hw old bridge '1' (1) | ||
* hw new bridge '185B982B5F37F9D96706EC49' (24) | ||
*/ | ||
export const pathLength = USE_HW && USE_NODE_BRIDGE ? 24 : !USE_HW && USE_NODE_BRIDGE ? 15 : 1; | ||
|
||
export const descriptor = { debug, debugSession, path, product, vendor, type }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 8 additions & 5 deletions
13
packages/transport/e2e/run.ts → packages/transport-test/e2e/run.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.