11#[test_only]
22module pyth_lazer ::pyth_lazer_tests {
33 use std::signer;
4+ use std::vector;
45 use aptos_framework::account;
56 use aptos_framework::coin;
67 use aptos_framework::timestamp;
78 use aptos_framework::aptos_coin::AptosCoin ;
89 use aptos_std::ed25519;
9- use pyth_lazer::pyth_lazer;
10+ use pyth_lazer::pyth_lazer::{ Self , EINVALID_SIGNER , EINSUFFICIENT_FEE } ;
1011
1112 // Test accounts
12- const TOP_AUTHORITY : address = @0x3374049c3b46a907ff2fc6b62af51975fb9dc572b7e73eb1b255ed5edcd7cee0 ;
13+ const TOP_AUTHORITY : address =
14+ @0x3374049c3b46a907ff2fc6b62af51975fb9dc572b7e73eb1b255ed5edcd7cee0 ;
1315 const TREASURY : address = @0x456 ;
1416 const USER : address = @0x789 ;
1517
@@ -20,13 +22,14 @@ module pyth_lazer::pyth_lazer_tests {
2022
2123 #[test_only]
2224 fun setup_aptos_coin (framework: &signer ): coin::MintCapability <AptosCoin > {
23- let (burn_cap, freeze_cap, mint_cap) = coin::initialize <AptosCoin >(
24- framework,
25- std::string ::utf8 (b"Aptos Coin "),
26- std::string ::utf8 (b"APT "),
27- 8 ,
28- false ,
29- );
25+ let (burn_cap, freeze_cap, mint_cap) =
26+ coin::initialize <AptosCoin >(
27+ framework,
28+ std::string ::utf8 (b"Aptos Coin "),
29+ std::string ::utf8 (b"APT "),
30+ 8 ,
31+ false
32+ );
3033 coin::destroy_burn_cap (burn_cap);
3134 coin::destroy_freeze_cap (freeze_cap);
3235 mint_cap
@@ -69,7 +72,7 @@ module pyth_lazer::pyth_lazer_tests {
6972 }
7073
7174 #[test]
72- fun test_verify_message_success () {
75+ fun test_verify_message_succeeds () {
7376 let (top_authority, _treasury, user) = setup ();
7477
7578 // Add a valid signer
@@ -79,47 +82,70 @@ module pyth_lazer::pyth_lazer_tests {
7982 // Create a valid ed25519 signature
8083 let signature = ed25519::new_signature_from_bytes (TEST_SIGNATURE );
8184 let pubkey = ed25519::new_unvalidated_public_key_from_bytes (TEST_PUBKEY );
82- assert !(ed25519::signature_verify_strict (&signature, &pubkey, TEST_MESSAGE ), 0 );
85+ assert !(
86+ ed25519::signature_verify_strict (&signature, &pubkey, TEST_MESSAGE ),
87+ 0
88+ );
8389
8490 // This should succeed as we have a valid signer and sufficient fee
85- pyth_lazer::verify_message (&user, TEST_MESSAGE , TEST_SIGNATURE , TEST_PUBKEY );
86- }
87-
88- #[test]
89- fun test_update_add_signer () {
90- let (top_authority, _treasury, _) = setup ();
91-
92- // Add signer
93- let expires_at = timestamp::now_seconds () + 1000 ;
94- pyth_lazer::update_trusted_signer (&top_authority, TEST_PUBKEY , expires_at);
95-
96- // Update signer
97- let new_expires_at = timestamp::now_seconds () + 2000 ;
98- pyth_lazer::update_trusted_signer (&top_authority, TEST_PUBKEY , new_expires_at);
99-
100- // Remove signer
101- pyth_lazer::update_trusted_signer (&top_authority, TEST_PUBKEY , 0 );
91+ pyth_lazer::verify_message (
92+ &user,
93+ TEST_MESSAGE ,
94+ TEST_SIGNATURE ,
95+ TEST_PUBKEY
96+ );
10297 }
10398
10499 #[test]
105- #[expected_failure(abort_code = pyth_lazer::ENO_SPACE)]
106- fun test_max_signers () {
100+ fun test_add_update_remove_signers_succeeds () {
107101 let (top_authority, _treasury, _) = setup ();
108102
103+ // Add multiple signers
109104 let expires_at = timestamp::now_seconds () + 1000 ;
110105 let pubkey1 = x"1111111111111111111111111111111111111111111111111111111111111111 ";
111106 let pubkey2 = x"2222222222222222222222222222222222222222222222222222222222222222 ";
112107 let pubkey3 = x"3333333333333333333333333333333333333333333333333333333333333333 ";
113108
114109 pyth_lazer::update_trusted_signer (&top_authority, pubkey1, expires_at);
115110 pyth_lazer::update_trusted_signer (&top_authority, pubkey2, expires_at);
116- // This should fail as we already have 2 signers
117111 pyth_lazer::update_trusted_signer (&top_authority, pubkey3, expires_at);
112+
113+ // Verify signers were added
114+ let trusted_signers = pyth_lazer::get_trusted_signers ();
115+ assert !(vector ::length (&trusted_signers) == 3 , 0 );
116+
117+ // Verify first signer
118+ let signer_info = vector ::borrow (&trusted_signers, 0 );
119+ let signer_pubkey = pyth_lazer::get_signer_pubkey (signer_info);
120+ let signer_expires_at = pyth_lazer::get_signer_expires_at (signer_info);
121+ assert !(signer_pubkey == pubkey1, 0 );
122+ assert !(signer_expires_at == expires_at, 0 );
123+
124+ // Update second signer
125+ let new_expires_at = timestamp::now_seconds () + 2000 ;
126+ pyth_lazer::update_trusted_signer (&top_authority, pubkey2, new_expires_at);
127+
128+ // Verify second signer was updated
129+ let trusted_signers = pyth_lazer::get_trusted_signers ();
130+ let signer_info = vector ::borrow (&trusted_signers, 1 );
131+ let signer_pubkey = pyth_lazer::get_signer_pubkey (signer_info);
132+ let signer_expires_at = pyth_lazer::get_signer_expires_at (signer_info);
133+ assert !(signer_pubkey == pubkey2, 0 );
134+ assert !(signer_expires_at == new_expires_at, 0 );
135+
136+ // Remove all signers
137+ pyth_lazer::update_trusted_signer (&top_authority, pubkey1, 0 );
138+ pyth_lazer::update_trusted_signer (&top_authority, pubkey2, 0 );
139+ pyth_lazer::update_trusted_signer (&top_authority, pubkey3, 0 );
140+
141+ // Verify all signers were removed
142+ let trusted_signers = pyth_lazer::get_trusted_signers ();
143+ assert !(vector ::length (&trusted_signers) == 0 , 0 );
118144 }
119145
120146 #[test]
121- #[expected_failure(abort_code = pyth_lazer:: EINVALID_SIGNER)]
122- fun test_expired_signer () {
147+ #[expected_failure(abort_code = EINVALID_SIGNER)]
148+ fun test_expired_signer_throws_error () {
123149 let (top_authority, _treasury, user) = setup ();
124150
125151 // Add signer that expires in 1000 seconds
@@ -130,19 +156,29 @@ module pyth_lazer::pyth_lazer_tests {
130156 timestamp::fast_forward_seconds (2000 );
131157
132158 // This should fail as the signer is expired
133- pyth_lazer::verify_message (&user, TEST_MESSAGE , TEST_SIGNATURE , TEST_PUBKEY );
159+ pyth_lazer::verify_message (
160+ &user,
161+ TEST_MESSAGE ,
162+ TEST_SIGNATURE ,
163+ TEST_PUBKEY
164+ );
134165 }
135166
136167 #[test]
137- #[expected_failure(abort_code = pyth_lazer:: EINSUFFICIENT_FEE)]
138- fun test_insufficient_fee () {
168+ #[expected_failure(abort_code = EINSUFFICIENT_FEE)]
169+ fun test_insufficient_fee_throws_error () {
139170 let (_top_authority, _treasury, user) = setup ();
140171
141172 // Drain user's balance
142173 let user_balance = coin::balance <AptosCoin >(signer ::address_of (&user));
143174 coin::transfer <AptosCoin >(&user, TREASURY , user_balance);
144175
145176 // This should fail due to insufficient fee
146- pyth_lazer::verify_message (&user, TEST_MESSAGE , TEST_SIGNATURE , TEST_PUBKEY );
177+ pyth_lazer::verify_message (
178+ &user,
179+ TEST_MESSAGE ,
180+ TEST_SIGNATURE ,
181+ TEST_PUBKEY
182+ );
147183 }
148184}
0 commit comments