Skip to content

Commit 01fecd0

Browse files
committed
added invalid and failing tests
1 parent 06417eb commit 01fecd0

File tree

1 file changed

+206
-1
lines changed

1 file changed

+206
-1
lines changed

target_chains/stylus/contracts/pyth-receiver/src/pyth_governance_test.rs

Lines changed: 206 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ mod test {
232232
) {
233233
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
234234

235-
let hex_str = "01000000000100b441e497034be4ee82242a866461d5e6744082654f71301a96f579f629b6bf176cc0c1964cd7d4f792436b7a73fc7024d72b138869b4d81d449740bb08148238000000000100000000000100000000000000000000000000000000000000000000000000000000000000110000000000000001005054474d01010002010000000001009c9dc62e92fefe0806dce30b662a5d319417a62dccc700b5f2678306d39c005f7a5e74d11df287301d85d328a3d000c5d793c57161f3150c7eb1a17668946e6b010000000100000000000100000000000000000000000000000000000000000000000000000000000000110000000000000064005054474d0105000200000000";
235+
let hex_str = "010000000001006fc27ac424b300c23a564bcabe1d7888a898cba92b8aec62468c35025baaf4a87056c50d443fbc172c3caa30d28ec57cefc0bbabf4590ffe98c44dff040d0e02000000000100000000000200000000000000000000000000000000000000000000000000000000000011110000000000000001005054474d0105000200000001";
236236
let bytes = Vec::from_hex(hex_str).expect("Invalid hex string");
237237

238238
let result = pyth_contract
@@ -325,4 +325,209 @@ mod test {
325325
assert!(result.is_ok());
326326
}
327327
*/
328+
329+
#[motsu::test]
330+
fn test_invalid_wormhole_vaa_signature_reverts(
331+
pyth_contract: Contract<PythReceiver>,
332+
wormhole_contract: Contract<WormholeContract>,
333+
alice: Address,
334+
) {
335+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
336+
337+
let hex_str = "0100000000010067940f58a6a44c93606bd721701539e0da93d5ea1583a735fbb13ecbcf9c01fc70240de519ea76869af14d067d68c5f3f2230f565f41b7009f3c3e63749353ed000000000100000000000100000000000000000000000000000000000000000000000000000000000000110000000000000001005054474d0103000200000000000000050000000000000003";
338+
let bytes = Vec::from_hex(&hex_str).expect("Invalid hex string");
339+
340+
let result = pyth_contract
341+
.sender(alice)
342+
.execute_governance_instruction(bytes.clone());
343+
344+
assert!(
345+
result.is_err(),
346+
"Invalid VAA should revert the transaction"
347+
);
348+
}
349+
350+
#[motsu::test]
351+
fn test_invalid_wormhole_vaa_magic_reverts(
352+
pyth_contract: Contract<PythReceiver>,
353+
wormhole_contract: Contract<WormholeContract>,
354+
alice: Address,
355+
) {
356+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
357+
358+
// Changed the magic signature to an invalid one (6064474d instead of 5054474d)
359+
let hex_str = "0100000000010067940f58a6a44c93606bd721701539e0da93d5ea1583a735fbb13ecbcf9c01fc70240de519ea76869af14d067d68c5f3f2230f565f41b7009f3c3e63749353ed000000000100000000000100000000000000000000000000000000000000000000000000000000000000110000000000000001006064474d0103000200000000000000050000000000000003";
360+
let bytes = Vec::from_hex(&hex_str).expect("Invalid hex string");
361+
362+
let result = pyth_contract
363+
.sender(alice)
364+
.execute_governance_instruction(bytes.clone());
365+
366+
assert!(
367+
result.is_err(),
368+
"Invalid VAA should revert the transaction"
369+
);
370+
}
371+
372+
#[motsu::test]
373+
fn test_invalid_wormhole_vaa_random_byte_cut_reverts(
374+
pyth_contract: Contract<PythReceiver>,
375+
wormhole_contract: Contract<WormholeContract>,
376+
alice: Address,
377+
) {
378+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
379+
380+
let hex_str = "0100000000010067940f58a676869af14d067d68c5f3f2230f565f41b7009f3c3e63749353ed000000000100000000000100000000000000000000000000000000000000000000000000000000000000110000000000000001005054474d0103000200000000000000050000000000000003";
381+
let bytes = Vec::from_hex(&hex_str).expect("Invalid hex string");
382+
383+
let result = pyth_contract
384+
.sender(alice)
385+
.execute_governance_instruction(bytes.clone());
386+
387+
assert!(
388+
result.is_err(),
389+
"Invalid VAA should revert the transaction"
390+
);
391+
}
392+
393+
#[motsu::test]
394+
fn test_invalid_wormhole_vaa_invalid_version_number_reverts(
395+
pyth_contract: Contract<PythReceiver>,
396+
wormhole_contract: Contract<WormholeContract>,
397+
alice: Address,
398+
) {
399+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
400+
401+
// Changed the version number to an invalid one (2 instead of 1)
402+
let hex_str = "0200000000010067940f58a6a44c93606bd721701539e0da93d5ea1583a735fbb13ecbcf9c01fc70240de519ea76869af14d067d68c5f3f2230f565f41b7009f3c3e63749353ed000000000100000000000100000000000000000000000000000000000000000000000000000000000000110000000000000001005054474d0103000200000000000000050000000000000003";
403+
let bytes = Vec::from_hex(&hex_str).expect("Invalid hex string");
404+
405+
let result = pyth_contract
406+
.sender(alice)
407+
.execute_governance_instruction(bytes.clone());
408+
409+
assert!(
410+
result.is_err(),
411+
"Invalid VAA should revert the transaction"
412+
);
413+
}
414+
415+
#[motsu::test]
416+
fn test_different_emitter_chain_id_than_wormhole_reverts(
417+
pyth_contract: Contract<PythReceiver>,
418+
wormhole_contract: Contract<WormholeContract>,
419+
alice: Address,
420+
) {
421+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
422+
423+
// Changed the emitter chain ID to a different one (2 instead of 1)
424+
let hex_str = "0100000000010057940f58a6a44c93606bd721701539e0da93d5ea1583a735fbb13ecbcf9c01fc70240de519ea76869af14d067d68c5f3f2230f565f41b7009f3c3e63749353ed000000000100000000000200000000000000000000000000000000000000000000000000000000000000110000000000000001005054474d0103000200000000000000050000000000000003";
425+
let bytes = Vec::from_hex(&hex_str).expect("Invalid hex string");
426+
427+
let result = pyth_contract
428+
.sender(alice)
429+
.execute_governance_instruction(bytes.clone());
430+
431+
assert!(
432+
result.is_err(),
433+
"Invalid VAA should revert the transaction"
434+
);
435+
}
436+
437+
#[motsu::test]
438+
fn test_different_emitter_chain_address_than_wormhole_reverts(
439+
pyth_contract: Contract<PythReceiver>,
440+
wormhole_contract: Contract<WormholeContract>,
441+
alice: Address,
442+
) {
443+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
444+
445+
// Changed the emitter chain ID to a different one (...0011 to ...0022)
446+
let hex_str = "0100000000010057940f58a6a44c93606bd721701539e0da93d5ea1583a735fbb13ecbcf9c01fc70240de519ea76869af14d067d68c5f3f2230f565f41b7009f3c3e63749353ed000000000100000000000100000000000000000000000000000000000000000000000000000000000000220000000000000001005054474d0103000200000000000000050000000000000003";
447+
let bytes = Vec::from_hex(&hex_str).expect("Invalid hex string");
448+
449+
let result = pyth_contract
450+
.sender(alice)
451+
.execute_governance_instruction(bytes.clone());
452+
453+
assert!(
454+
result.is_err(),
455+
"Invalid VAA should revert the transaction"
456+
);
457+
}
458+
459+
#[motsu::test]
460+
fn test_sequence_number_greater_than_last_executed_reverts(
461+
pyth_contract: Contract<PythReceiver>,
462+
wormhole_contract: Contract<WormholeContract>,
463+
alice: Address,
464+
) {
465+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
466+
467+
let hex_str = "0100000000010057940f58a6a44c93606bd721701539e0da93d5ea1583a735fbb13ecbcf9c01fc70240de519ea76869af14d067d68c5f3f2230f565f41b7009f3c3e63749353ed000000000100000000000100000000000000000000000000000000000000000000000000000000000000110000000000000001005054474d0103000200000000000000050000000000000003";
468+
let bytes = Vec::from_hex(&hex_str).expect("Invalid hex string");
469+
470+
let result = pyth_contract
471+
.sender(alice)
472+
.execute_governance_instruction(bytes.clone());
473+
474+
assert!(
475+
result.is_ok(),
476+
"This is a valid VAA, should go through"
477+
);
478+
479+
let result = pyth_contract
480+
.sender(alice)
481+
.execute_governance_instruction(bytes.clone());
482+
483+
assert!(
484+
result.is_err(),
485+
"Cannot execute the same sequence number again, should revert"
486+
);
487+
}
488+
489+
#[motsu::test]
490+
fn test_target_chain_id_from_ethereum_to_solana_reverts(
491+
pyth_contract: Contract<PythReceiver>,
492+
wormhole_contract: Contract<WormholeContract>,
493+
alice: Address,
494+
) {
495+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
496+
497+
// This VAA is for a target chain ID of 1 (Solana), but the PythReceiver is on chain ID 1 (Ethereum)
498+
let hex_str = "0100000000010057940f58a6a44c93606bd721701539e0da93d5ea1583a735fbb13ecbcf9c01fc70240de519ea76869af14d067d68c5f3f2230f565f41b7009f3c3e63749353ed000000000100000000000100000000000000000000000000000000000000000000000000000000000000110000000000000001005054474d0103000100000000000000050000000000000003";
499+
let bytes = Vec::from_hex(&hex_str).expect("Invalid hex string");
500+
501+
let result = pyth_contract
502+
.sender(alice)
503+
.execute_governance_instruction(bytes.clone());
504+
505+
assert!(
506+
result.is_err(),
507+
"Incorrect target chain ID should revert the transaction"
508+
);
509+
}
510+
511+
#[motsu::test]
512+
fn test_unexpected_governance_action_id_reverts(
513+
pyth_contract: Contract<PythReceiver>,
514+
wormhole_contract: Contract<WormholeContract>,
515+
alice: Address,
516+
) {
517+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice, 0);
518+
519+
// Changes this action to be a SetDataSources action instead of a SetFee action
520+
let hex_str = "0100000000010057940f58a6a44c93606bd721701539e0da93d5ea1583a735fbb13ecbcf9c01fc70240de519ea76869af14d067d68c5f3f2230f565f41b7009f3c3e63749353ed000000000100000000000100000000000000000000000000000000000000000000000000000000000000110000000000000001005054474d0102000200000000000000050000000000000003";
521+
let bytes = Vec::from_hex(&hex_str).expect("Invalid hex string");
522+
523+
let result = pyth_contract
524+
.sender(alice)
525+
.execute_governance_instruction(bytes.clone());
526+
527+
assert!(
528+
result.is_err(),
529+
"Wrong action expected should lead to bad parsing"
530+
);
531+
}
532+
328533
}

0 commit comments

Comments
 (0)