-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathtest_admin_implementation.rs
More file actions
239 lines (188 loc) Β· 8.56 KB
/
test_admin_implementation.rs
File metadata and controls
239 lines (188 loc) Β· 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env rust-script
//! Standalone test for admin role transfer implementation
//!
//! This tests the core logic we implemented without workspace dependencies
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)]
struct Address(String);
impl Address {
fn new(name: &str) -> Self {
Address(name.to_string())
}
}
/// Mock contract to test our admin role transfer logic
struct MockContract {
storage: HashMap<String, Address>,
}
impl MockContract {
fn new() -> Self {
Self {
storage: HashMap::new(),
}
}
fn get_upgrade_admin(&self) -> Option<Address> {
self.storage.get("UPG_ADM").cloned()
}
/// This implements the exact logic we added to the contracts
fn set_upgrade_admin(&mut self, caller: Address, new_admin: Address) -> Result<(), &'static str> {
let current_upgrade_admin = self.get_upgrade_admin();
// Authorization logic:
// 1. If no upgrade admin exists, caller must equal new_admin (bootstrap)
// 2. If upgrade admin exists, only current upgrade admin can transfer
match current_upgrade_admin {
None => {
// Bootstrap pattern - caller must be setting themselves as admin
if caller != new_admin {
return Err("Unauthorized: bootstrap requires caller == new_admin");
}
}
Some(current_admin) => {
// Admin transfer - only current admin can transfer
if current_admin != caller {
return Err("Unauthorized: only current upgrade admin can transfer");
}
}
}
self.storage.insert("UPG_ADM".to_string(), new_admin);
Ok(())
}
}
fn main() {
println!("π§ͺ Testing Admin Role Transfer Implementation");
println!("{}", "=".repeat(50));
// Test 1: Bootstrap Admin Setup
println!("\n1οΈβ£ Testing Bootstrap Admin Setup");
let mut contract = MockContract::new();
let admin = Address::new("admin1");
let result = contract.set_upgrade_admin(admin.clone(), admin.clone());
assert!(result.is_ok(), "Bootstrap should succeed when caller == new_admin");
let current_admin = contract.get_upgrade_admin();
assert_eq!(current_admin, Some(admin.clone()));
println!(" β
Bootstrap succeeded: {:?}", current_admin);
// Test 2: Unauthorized Bootstrap
println!("\n2οΈβ£ Testing Unauthorized Bootstrap");
let mut contract2 = MockContract::new();
let caller = Address::new("unauthorized");
let admin = Address::new("admin1");
let result = contract2.set_upgrade_admin(caller, admin);
assert!(result.is_err(), "Bootstrap should fail when caller != new_admin");
let current_admin = contract2.get_upgrade_admin();
assert_eq!(current_admin, None);
println!(" β
Unauthorized bootstrap blocked: {:?}", result.unwrap_err());
// Test 3: Authorized Admin Transfer
println!("\n3οΈβ£ Testing Authorized Admin Transfer");
let mut contract3 = MockContract::new();
let admin1 = Address::new("admin1");
let admin2 = Address::new("admin2");
// Setup initial admin
contract3.set_upgrade_admin(admin1.clone(), admin1.clone()).unwrap();
// Transfer to new admin
let result = contract3.set_upgrade_admin(admin1.clone(), admin2.clone());
assert!(result.is_ok(), "Transfer should succeed when current admin transfers");
let current_admin = contract3.get_upgrade_admin();
assert_eq!(current_admin, Some(admin2.clone()));
println!(" β
Admin transfer succeeded: {:?}", current_admin);
// Test 4: Unauthorized Admin Transfer
println!("\n4οΈβ£ Testing Unauthorized Admin Transfer");
let mut contract4 = MockContract::new();
let admin1 = Address::new("admin1");
let admin2 = Address::new("admin2");
let unauthorized = Address::new("unauthorized");
// Setup initial admin
contract4.set_upgrade_admin(admin1.clone(), admin1.clone()).unwrap();
// Attempt unauthorized transfer
let result = contract4.set_upgrade_admin(unauthorized, admin2);
assert!(result.is_err(), "Transfer should fail when unauthorized user attempts");
let current_admin = contract4.get_upgrade_admin();
assert_eq!(current_admin, Some(admin1.clone()));
println!(" β
Unauthorized transfer blocked: {:?}", result.unwrap_err());
// Test 5: Self-Transfer
println!("\n5οΈβ£ Testing Self-Transfer");
let mut contract5 = MockContract::new();
let admin = Address::new("admin1");
// Setup initial admin
contract5.set_upgrade_admin(admin.clone(), admin.clone()).unwrap();
// Self-transfer should succeed
let result = contract5.set_upgrade_admin(admin.clone(), admin.clone());
assert!(result.is_ok(), "Self-transfer should succeed");
let current_admin = contract5.get_upgrade_admin();
assert_eq!(current_admin, Some(admin.clone()));
println!(" β
Self-transfer succeeded: {:?}", current_admin);
// Test 6: Rapid Successive Transfers
println!("\n6οΈβ£ Testing Rapid Successive Transfers");
let mut contract6 = MockContract::new();
let admin1 = Address::new("admin1");
let admin2 = Address::new("admin2");
let admin3 = Address::new("admin3");
// Setup initial admin
contract6.set_upgrade_admin(admin1.clone(), admin1.clone()).unwrap();
// Transfer to admin2
let result = contract6.set_upgrade_admin(admin1, admin2.clone());
assert!(result.is_ok(), "First transfer should succeed");
// Immediately transfer to admin3
let result = contract6.set_upgrade_admin(admin2, admin3.clone());
assert!(result.is_ok(), "Second transfer should succeed");
let current_admin = contract6.get_upgrade_admin();
assert_eq!(current_admin, Some(admin3.clone()));
println!(" β
Rapid transfers succeeded: {:?}", current_admin);
println!("\nπ All Admin Role Transfer Tests Passed!");
println!("{}", "=".repeat(50));
println!("\nπ Test Summary:");
println!(" β
Bootstrap security (caller == new_admin)");
println!(" β
Unauthorized bootstrap prevention");
println!(" β
Authorized admin transfers");
println!(" β
Unauthorized transfer prevention");
println!(" β
Self-transfer capability");
println!(" β
Rapid successive transfers");
println!("\nπ Security Properties Validated:");
println!(" β’ No unauthorized bootstrap");
println!(" β’ Transfer isolation (only current admin can transfer)");
println!(" β’ State consistency (failed transfers don't change admin)");
println!(" β’ Edge case handling (self-transfer, rapid succession)");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bootstrap_admin_setup() {
let mut contract = MockContract::new();
let admin = Address::new("admin1");
let result = contract.set_upgrade_admin(admin.clone(), admin.clone());
assert!(result.is_ok());
let current_admin = contract.get_upgrade_admin();
assert_eq!(current_admin, Some(admin));
}
#[test]
fn test_unauthorized_bootstrap() {
let mut contract = MockContract::new();
let caller = Address::new("unauthorized");
let admin = Address::new("admin1");
let result = contract.set_upgrade_admin(caller, admin);
assert!(result.is_err());
let current_admin = contract.get_upgrade_admin();
assert_eq!(current_admin, None);
}
#[test]
fn test_authorized_transfer() {
let mut contract = MockContract::new();
let admin1 = Address::new("admin1");
let admin2 = Address::new("admin2");
contract.set_upgrade_admin(admin1.clone(), admin1.clone()).unwrap();
let result = contract.set_upgrade_admin(admin1, admin2.clone());
assert!(result.is_ok());
let current_admin = contract.get_upgrade_admin();
assert_eq!(current_admin, Some(admin2));
}
#[test]
fn test_unauthorized_transfer() {
let mut contract = MockContract::new();
let admin1 = Address::new("admin1");
let admin2 = Address::new("admin2");
let unauthorized = Address::new("unauthorized");
contract.set_upgrade_admin(admin1.clone(), admin1.clone()).unwrap();
let result = contract.set_upgrade_admin(unauthorized, admin2);
assert!(result.is_err());
let current_admin = contract.get_upgrade_admin();
assert_eq!(current_admin, Some(admin1));
}
}