@@ -16,12 +16,8 @@ use fil_actors_runtime::test_utils::*;
16
16
#[ test]
17
17
fn construction ( ) {
18
18
fn construct ( addr : Address , exit_code : ExitCode ) {
19
- let mut rt = MockRuntime {
20
- receiver : Address :: new_id ( 100 ) ,
21
- caller : SYSTEM_ACTOR_ADDR ,
22
- caller_type : * SYSTEM_ACTOR_CODE_ID ,
23
- ..Default :: default ( )
24
- } ;
19
+ let mut rt = MockRuntime { receiver : Address :: new_id ( 100 ) , ..Default :: default ( ) } ;
20
+ rt. set_caller ( * SYSTEM_ACTOR_CODE_ID , SYSTEM_ACTOR_ADDR ) ;
25
21
rt. expect_validate_caller_addr ( vec ! [ SYSTEM_ACTOR_ADDR ] ) ;
26
22
27
23
if exit_code. is_success ( ) {
@@ -59,12 +55,8 @@ fn construction() {
59
55
60
56
#[ test]
61
57
fn token_receiver ( ) {
62
- let mut rt = MockRuntime {
63
- receiver : Address :: new_id ( 100 ) ,
64
- caller : SYSTEM_ACTOR_ADDR ,
65
- caller_type : * SYSTEM_ACTOR_CODE_ID ,
66
- ..Default :: default ( )
67
- } ;
58
+ let mut rt = MockRuntime { receiver : Address :: new_id ( 100 ) , ..Default :: default ( ) } ;
59
+ rt. set_caller ( * SYSTEM_ACTOR_CODE_ID , SYSTEM_ACTOR_ADDR ) ;
68
60
rt. expect_validate_caller_addr ( vec ! [ SYSTEM_ACTOR_ADDR ] ) ;
69
61
70
62
let param = Address :: new_secp256k1 ( & [ 2 ; fvm_shared:: address:: SECP_PUB_LEN ] ) . unwrap ( ) ;
@@ -74,6 +66,7 @@ fn token_receiver() {
74
66
)
75
67
. unwrap ( ) ;
76
68
69
+ rt. set_caller ( make_identity_cid ( b"1234" ) , Address :: new_id ( 1000 ) ) ;
77
70
rt. expect_validate_caller_any ( ) ;
78
71
let ret = rt. call :: < AccountActor > (
79
72
Method :: UniversalReceiverHook as MethodNum ,
@@ -83,25 +76,15 @@ fn token_receiver() {
83
76
assert_eq ! ( RawBytes :: default ( ) , ret. unwrap( ) ) ;
84
77
}
85
78
86
- fn check_state ( rt : & MockRuntime ) {
87
- let test_address = Address :: new_id ( 1000 ) ;
88
- let ( _, acc) = check_state_invariants ( & rt. get_state ( ) , & test_address) ;
89
- acc. assert_empty ( ) ;
90
- }
91
-
92
79
#[ test]
93
80
fn authenticate_message ( ) {
94
- let mut rt = MockRuntime {
95
- receiver : Address :: new_id ( 100 ) ,
96
- caller : SYSTEM_ACTOR_ADDR ,
97
- caller_type : * SYSTEM_ACTOR_CODE_ID ,
98
- ..Default :: default ( )
99
- } ;
81
+ let mut rt = MockRuntime { receiver : Address :: new_id ( 100 ) , ..Default :: default ( ) } ;
82
+ rt. set_caller ( * SYSTEM_ACTOR_CODE_ID , SYSTEM_ACTOR_ADDR ) ;
100
83
101
84
let addr = Address :: new_secp256k1 ( & [ 2 ; fvm_shared:: address:: SECP_PUB_LEN ] ) . unwrap ( ) ;
102
85
rt. expect_validate_caller_addr ( vec ! [ SYSTEM_ACTOR_ADDR ] ) ;
103
-
104
- rt . call :: < AccountActor > ( 1 , & RawBytes :: serialize ( addr ) . unwrap ( ) ) . unwrap ( ) ;
86
+ rt . call :: < AccountActor > ( Method :: Constructor as MethodNum , & RawBytes :: serialize ( addr ) . unwrap ( ) )
87
+ . unwrap ( ) ;
105
88
106
89
let state: State = rt. get_state ( ) ;
107
90
assert_eq ! ( state. address, addr) ;
@@ -110,26 +93,56 @@ fn authenticate_message() {
110
93
RawBytes :: serialize ( AuthenticateMessageParams { signature : vec ! [ ] , message : vec ! [ ] } )
111
94
. unwrap ( ) ;
112
95
96
+ // Valid signature
113
97
rt. expect_validate_caller_any ( ) ;
114
98
rt. expect_verify_signature ( ExpectedVerifySig {
115
99
sig : Signature :: new_secp256k1 ( vec ! [ ] ) ,
116
100
signer : addr,
117
101
plaintext : vec ! [ ] ,
118
102
result : Ok ( ( ) ) ,
119
103
} ) ;
120
- assert_eq ! ( RawBytes :: default ( ) , rt. call:: <AccountActor >( 3 , & params) . unwrap( ) ) ;
104
+ assert_eq ! (
105
+ RawBytes :: default ( ) ,
106
+ rt. call:: <AccountActor >( Method :: AuthenticateMessage as MethodNum , & params) . unwrap( )
107
+ ) ;
108
+ rt. verify ( ) ;
121
109
110
+ // Invalid signature
122
111
rt. expect_validate_caller_any ( ) ;
123
112
rt. expect_verify_signature ( ExpectedVerifySig {
124
113
sig : Signature :: new_secp256k1 ( vec ! [ ] ) ,
125
114
signer : addr,
126
115
plaintext : vec ! [ ] ,
127
116
result : Err ( anyhow ! ( "bad signature" ) ) ,
128
117
} ) ;
129
- assert_eq ! (
118
+ expect_abort_contains_message (
130
119
ExitCode :: USR_ILLEGAL_ARGUMENT ,
131
- rt. call:: <AccountActor >( 3 , & params) . unwrap_err( ) . exit_code( )
120
+ "bad signature" ,
121
+ rt. call :: < AccountActor > ( Method :: AuthenticateMessage as MethodNum , & params) ,
132
122
) ;
133
-
134
123
rt. verify ( ) ;
124
+
125
+ // Invalid caller of internal method number
126
+ rt. set_caller ( make_identity_cid ( b"1234" ) , Address :: new_id ( 1000 ) ) ;
127
+ expect_abort_contains_message (
128
+ ExitCode :: USR_FORBIDDEN ,
129
+ "must be built-in" ,
130
+ rt. call :: < AccountActor > ( Method :: AuthenticateMessage as MethodNum , & params) ,
131
+ ) ;
132
+
133
+ // Ok to call exported method number
134
+ rt. expect_validate_caller_any ( ) ;
135
+ rt. expect_verify_signature ( ExpectedVerifySig {
136
+ sig : Signature :: new_secp256k1 ( vec ! [ ] ) ,
137
+ signer : addr,
138
+ plaintext : vec ! [ ] ,
139
+ result : Ok ( ( ) ) ,
140
+ } ) ;
141
+ rt. call :: < AccountActor > ( Method :: AuthenticateMessageExported as MethodNum , & params) . unwrap ( ) ;
142
+ }
143
+
144
+ fn check_state ( rt : & MockRuntime ) {
145
+ let test_address = Address :: new_id ( 1000 ) ;
146
+ let ( _, acc) = check_state_invariants ( & rt. get_state ( ) , & test_address) ;
147
+ acc. assert_empty ( ) ;
135
148
}
0 commit comments