Skip to content

Commit

Permalink
Merge pull request #2 from LedgerHQ/fix-tests
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
TamtamHero authored Sep 8, 2021
2 parents 87d0035 + 0fdc35e commit 6987a0f
Show file tree
Hide file tree
Showing 21 changed files with 691 additions and 380 deletions.
16 changes: 15 additions & 1 deletion src/paraswap_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ static void handle_init_contract(void *parameters) {
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;

if (msg->interfaceVersion != ETH_PLUGIN_INTERFACE_VERSION_LATEST) {
PRINTF("Wrong interface version: expected %d got %d\n",
ETH_PLUGIN_INTERFACE_VERSION_LATEST,
msg->interfaceVersion);
msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE;
return;
}

if (msg->pluginContextLength < sizeof(paraswap_parameters_t)) {
PRINTF("Paraswap context size too big: expected %d got %d\n",
sizeof(paraswap_parameters_t),
msg->pluginContextLength);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
Expand Down Expand Up @@ -102,7 +108,6 @@ static void handle_init_contract(void *parameters) {
static void handle_finalize(void *parameters) {
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
paraswap_parameters_t *context = (paraswap_parameters_t *) msg->pluginContext;
PRINTF("eth2 plugin finalize\n");
if (context->valid) {
msg->numScreens = 2;
if (context->selectorIndex == SIMPLE_SWAP || context->selectorIndex == SIMPLE_BUY)
Expand All @@ -116,6 +121,15 @@ static void handle_finalize(void *parameters) {
PRINTF("Setting address sent to: %.*H\n",
ADDRESS_LENGTH,
context->contract_address_sent);

// The user is not swapping ETH, so make sure there's no ETH being sent in this tx.
if (!allzeroes(msg->pluginSharedRO->txContent->value.value,
msg->pluginSharedRO->txContent->value.length)) {
PRINTF("ETH attached to tx when token being swapped is %.*H\n",
sizeof(context->contract_address_sent),
context->contract_address_sent);
msg->result = ETH_PLUGIN_RESULT_ERROR;
}
} else {
msg->tokenLookup1 = NULL;
}
Expand Down
4 changes: 0 additions & 4 deletions src/provide_parameters.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,6 @@ static void handle_megaswap(ethPluginProvideParameter_t *msg, paraswap_parameter
void handle_provide_parameter(void *parameters) {
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
paraswap_parameters_t *context = (paraswap_parameters_t *) msg->pluginContext;
PRINTF("eth2 plugin provide parameter %d %.*H\n",
msg->parameterOffset,
PARAMETER_LENGTH,
msg->parameter);

msg->result = ETH_PLUGIN_RESULT_OK;

Expand Down
43 changes: 43 additions & 0 deletions tests/build_local_test_elfs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# FILL THESE WITH YOUR OWN SDKs PATHS and APP-ETHEREUM's ROOT
# NANOS_SDK=
# NANOX_SDK=
# APP_ETHEREUM=

# create elfs folder if it doesn't exist
mkdir -p elfs

# move to repo's root to build apps
cd ..

echo "*Building elfs for Nano S..."

echo "**Building app-paraswap for Nano S..."
make clean BOLOS_SDK=$NANOS_SDK
make -j DEBUG=1 BOLOS_SDK=$NANOS_SDK
cp bin/app.elf "tests/elfs/paraswap_nanos.elf"

echo "**Building app-ethereum for Nano S..."
cd $APP_ETHEREUM
make clean BOLOS_SDK=$NANOS_SDK
make -j DEBUG=1 BOLOS_SDK=$NANOS_SDK CHAIN=ethereum
cd -
cp "${APP_ETHEREUM}/bin/app.elf" "tests/elfs/ethereum_nanos.elf"


echo "*Building elfs for Nano X..."

echo "**Building app-paraswap for Nano X..."
make clean BOLOS_SDK=$NANOX_SDK
make -j DEBUG=1 BOLOS_SDK=$NANOX_SDK
cp bin/app.elf "tests/elfs/paraswap_nanox.elf"

echo "**Building app-ethereum for Nano X..."
cd $APP_ETHEREUM
make clean BOLOS_SDK=$NANOX_SDK
make -j DEBUG=1 BOLOS_SDK=$NANOX_SDK CHAIN=ethereum
cd -
cp "${APP_ETHEREUM}/bin/app.elf" "tests/elfs/ethereum_nanox.elf"

echo "done"
Binary file removed tests/elfs/ethereum.elf
Binary file not shown.
Binary file added tests/elfs/ethereum_nanos.elf
Binary file not shown.
Binary file added tests/elfs/ethereum_nanox.elf
Binary file not shown.
Binary file removed tests/elfs/paraswap.elf
Binary file not shown.
Binary file added tests/elfs/paraswap_nanos.elf
Binary file not shown.
Binary file added tests/elfs/paraswap_nanox.elf
Binary file not shown.
20 changes: 0 additions & 20 deletions tests/eth2_input_data_hashing.py

This file was deleted.

4 changes: 3 additions & 1 deletion tests/globalsetup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Zemu from "@zondax/zemu";
import fsExtra from "fs-extra";

const catchExit = async () => {
process.on("SIGINT", () => {
Expand All @@ -12,4 +13,5 @@ module.exports = async () => {
await catchExit();
await Zemu.checkAndPullImage();
await Zemu.stopAllEmuContainers();
};
fsExtra.emptyDirSync("snapshots/tmp")
};
6 changes: 6 additions & 0 deletions tests/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ module.exports = {
"**/?(*.)+(spec|test).[tj]s?(x)",
"**/?(*.)+(ispec|test).[tj]s?(x)",
],

// Path of the file where tests can be """decorated"""
setupFilesAfterEnv: ['<rootDir>/setupTests.js'],

// Stop immediatly when a test fail
bail: true,
};
6 changes: 3 additions & 3 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"license": "ISC",
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@ledgerhq/hw-app-eth": "^5.30.0",
"@ledgerhq/hw-app-eth": "^6.5.0",
"@ledgerhq/hw-transport-http": "^4.74.2",
"@ledgerhq/logs": "^5.30.0",
"@zondax/zemu": "0.13.0",
"@ledgerhq/logs": "^5.50.0",
"@zondax/zemu": "0.16.5",
"bignumber.js": "^9.0.0",
"bip32-path": "^0.4.2",
"core-js": "^3.7.0",
Expand Down
19 changes: 19 additions & 0 deletions tests/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import expect from 'expect'

expect.extend({
toMatchSnapshot(received, original) {

if(received.data.equals(original.data)){
return {
message: () => `snapshots are equal`,
pass: true
}
} else {
console.log("snapshots are not equal")
return {
message: () => `snapshots are not equal`,
pass: false
}
}
},
});
33 changes: 33 additions & 0 deletions tests/src/generic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const sim_options_nanos = {
model: "nanos",
logging: true,
start_delay: 2000,
X11: true,
custom: "",
};

const sim_options_nanox = {
model: "nanox",
logging: true,
start_delay: 2000,
X11: true,
custom: "",
};

const Resolve = require("path").resolve;
const NANOS_ETH_ELF_PATH = Resolve("elfs/ethereum_nanos.elf");
const NANOX_ETH_ELF_PATH = Resolve("elfs/ethereum_nanox.elf");
const NANOS_PARASWAP_LIB = { Paraswap: Resolve("elfs/paraswap_nanos.elf") };
const NANOX_PARASWAP_LIB = { Paraswap: Resolve("elfs/paraswap_nanox.elf") };

const TIMEOUT = 1000000;

module.exports = {
NANOS_ETH_ELF_PATH,
NANOX_ETH_ELF_PATH,
NANOS_PARASWAP_LIB,
NANOX_PARASWAP_LIB,
sim_options_nanos,
sim_options_nanox,
TIMEOUT,
}
Loading

0 comments on commit 6987a0f

Please sign in to comment.