Skip to content

Commit

Permalink
Refactor test files
Browse files Browse the repository at this point in the history
  • Loading branch information
isoosiss7 committed May 2, 2021
1 parent c8ae9e8 commit 5895ac5
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 67 deletions.
67 changes: 0 additions & 67 deletions packages/native-utils/tests/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,73 +102,6 @@ describe('Sign state', () => {
expect(wasmSigned.signature).toStrictEqual(oldSignature)
})

test('Peer states validate as expected', async () => {
const outcome = [
{
assetHolderAddress: '0x0000000000000000000000000000000000000000',
guarantee: {
targetChannelId:
'0x0000000000000000000000000000000000000000000000000000000000000000',
destinations: [
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x1111111111111111111111111111111111111111111111111111111111111111',
],
},
},
]

const currentState = {
...DEFAULT_STATE,
outcome,
}

const peerState = {
...DEFAULT_STATE,
outcome,
}

// First 2 updates
currentState.turnNum = 1;
peerState.turnNum = 2;

const nativeSigned1 = native.signState(peerState, PRIVATE_KEY2);
expect(native.validatePeerUpdate(currentState, peerState, nativeSigned1.signature)).toEqual("True")
expect(wasm.validatePeerUpdate(currentState, peerState, nativeSigned1.signature)).toEqual("True")

// Additional updates
currentState.turnNum = 3;
peerState.turnNum = 4;

const nativeSigned2 = native.signState(peerState, PRIVATE_KEY2);
expect(native.validatePeerUpdate(currentState, peerState, nativeSigned2.signature)).toEqual("NeedToCheckApp")
expect(wasm.validatePeerUpdate(currentState, peerState, nativeSigned2.signature)).toEqual("NeedToCheckApp")

// Signer mismatch
const nativeSigned3 = native.signState(peerState, PRIVATE_KEY1);
expect(() => native.validatePeerUpdate(currentState, peerState, nativeSigned3.signature)).toThrow('Signature verification failed');
expect(() => wasm.validatePeerUpdate(currentState, peerState, nativeSigned3.signature)).toThrow('Signature verification failed');

// turn number
currentState.turnNum = 2;
peerState.turnNum = 4;
const nativeSigned4 = native.signState(peerState, PRIVATE_KEY2);
expect(() => native.validatePeerUpdate(currentState, peerState, nativeSigned4.signature)).toThrow('turnNum must increment by one');
expect(() => wasm.validatePeerUpdate(currentState, peerState, nativeSigned4.signature)).toThrow('turnNum must increment by one');

// Nonce
currentState.turnNum = 3;
peerState.turnNum = 4;
peerState.channel = {
chainId: '1',
channelNonce: 2,
participants: ['0x63FaC9201494f0bd17B9892B9fae4d52fe3BD377', '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1'],
}
const nativeSigned5 = native.signState(peerState, PRIVATE_KEY2);

expect(() => native.validatePeerUpdate(currentState, peerState, nativeSigned5.signature)).toThrow('channelNonce must not change');
})


test('Catches invalid private key', async () => {
// Invalid signature length
expect(() => native.signState(DEFAULT_STATE, '0x00')).toThrow('invalid private key')
Expand Down
172 changes: 172 additions & 0 deletions packages/native-utils/tests/transition.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { State } from '@statechannels/nitro-protocol'
import * as wasm from '@statechannels/wasm-utils'
import * as native from '..'

const OUTCOME = [
{
assetHolderAddress: '0x0000000000000000000000000000000000000000',
guarantee: {
targetChannelId:
'0x0000000000000000000000000000000000000000000000000000000000000000',
destinations: [
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x1111111111111111111111111111111111111111111111111111111111111111',
],
},
},
]

const CURRENT_STATE: State = {
turnNum: 5,
isFinal: false,
channel: {
chainId: '1',
channelNonce: 1,
participants: ['0x63FaC9201494f0bd17B9892B9fae4d52fe3BD377', '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1'],
},
challengeDuration: 1,
outcome: OUTCOME,
appDefinition: '0x0000000000000000000000000000000000000000',
appData: '0x0000000000000000000000000000000000000000000000000000000000000000',
}

const NEXT_STATE: State = {
turnNum: 6,
isFinal: false,
channel: {
chainId: '1',
channelNonce: 1,
participants: ['0x63FaC9201494f0bd17B9892B9fae4d52fe3BD377', '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1'],
},
challengeDuration: 1,
outcome: OUTCOME,
appDefinition: '0x0000000000000000000000000000000000000000',
appData: '0x0000000000000000000000000000000000000000000000000000000000000000',
}

const PRIVATE_KEY1 = '0x8da4ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f'
const PRIVATE_KEY2 = '0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d'


describe('Validate state transitions', () => {
test('Pre fund setup passes', async () => {

let currentState = {
...CURRENT_STATE,
}

let peerState = {
...NEXT_STATE,
}

// First 2 updates
currentState.turnNum = 1;
peerState.turnNum = 2;

const nativeSigned = native.signState(peerState, PRIVATE_KEY2);
expect(native.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toEqual("True")
expect(wasm.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toEqual("True")
})

test('state transition passes', async () => {
let currentState = {
...CURRENT_STATE,
}

let peerState = {
...NEXT_STATE,
}

currentState.turnNum = 5;
peerState.turnNum = 6;

const nativeSigned = native.signState(peerState, PRIVATE_KEY2);
expect(native.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toEqual("NeedToCheckApp")
expect(wasm.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toEqual("NeedToCheckApp")

})

test('signer mismatch fails', async () => {
let currentState = {
...CURRENT_STATE,
}

let peerState = {
...NEXT_STATE,
}

const nativeSigned = native.signState(peerState, PRIVATE_KEY1);
expect(() => native.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toThrow('Signature verification failed');
expect(() => wasm.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toThrow('Signature verification failed');
});

test('turn number mismatch fails', async () => {
let currentState = {
...CURRENT_STATE,
}

let peerState = {
...NEXT_STATE,
}

currentState.turnNum = 4;
peerState.turnNum = 6;
const nativeSigned4 = native.signState(peerState, PRIVATE_KEY2);
expect(() => native.validatePeerUpdate(currentState, peerState, nativeSigned4.signature)).toThrow('turnNum must increment by one');
expect(() => wasm.validatePeerUpdate(currentState, peerState, nativeSigned4.signature)).toThrow('turnNum must increment by one');
});

test('final transit to non-final fails fails', async () => {
let currentState = {
...CURRENT_STATE,
}

let peerState = {
...NEXT_STATE,
}

currentState.turnNum = 5;
currentState.isFinal = true;
peerState.turnNum = 6;
peerState.isFinal = false;
const nativeSigned = native.signState(peerState, PRIVATE_KEY2);
expect(() => native.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toThrow('transition from a final state to a non-final state');
expect(() => wasm.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toThrow('transition from a final state to a non-final state');
});

test('Outcome mismatch mismatch fails', async () => {
const otheroutcome = [
{
assetHolderAddress: '0x0000000000000000000000000000000000000000',
guarantee: {
targetChannelId:
'0x0000000000000000000000000000000000000000000000000000000000000000',
destinations: [
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x2222222222222222222222222222222222222222222222220222222222222222',
],
},
},
]

let currentState = {
...CURRENT_STATE,
}

let peerState = {
...NEXT_STATE,
}

currentState.isFinal = true
currentState.turnNum = 5;

peerState.outcome = otheroutcome
peerState.isFinal = true
peerState.turnNum = 6;

const nativeSigned = native.signState(peerState, PRIVATE_KEY2);

expect(() => native.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toThrow('Outcome change forbidden');
expect(() => wasm.validatePeerUpdate(currentState, peerState, nativeSigned.signature)).toThrow('Outcome change forbidden');
})
})

0 comments on commit 5895ac5

Please sign in to comment.