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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
//! The test infrastructure module provides alternative implementations of //! `HasInitContext`, `HasReceiveContext`, `HasParameter`, `HasActions`, and //! `HasContractState` traits intended for testing. //! //! They allow writing unit tests directly in contract modules with little to no //! external tooling, depending on what is required. //! //! //! # Example //! //! ```rust //! // Some contract //! #[init(contract = "noop")] //! fn contract_init<I: HasInitContext<()>, L: HasLogger>( //! ctx: &I, //! _amount: Amount, //! _logger: &mut L, //! ) -> InitResult<State> { ... } //! //! #[receive(contract = "noop", name = "receive")] //! fn contract_receive<R: HasReceiveContext<()>, L: HasLogger, A: HasActions>( //! ctx: &R, //! amount: Amount, //! logger: &mut L, //! state: &mut State, //! ) -> ReceiveResult<A> { ... } //! //! #[cfg(test)] //! mod tests { //! use super::*; //! use concordium_sc_base::test_infrastructure::*; //! #[test] //! fn test_init() { //! let mut ctx = InitContextTest::empty(); //! ctx.set_init_origin(AccountAddress([0u8; 32])); //! ... //! let mut logger = LogRecorder::init(); //! let result = contract_init(&ctx, 0, &mut logger); //! claim!(...) //! ... //! } //! //! #[test] //! fn test_receive() { //! let mut ctx = ReceiveContextTest::empty(); //! ctx.set_owner(AccountAddress([0u8; 32])); //! ... //! let mut logger = LogRecorder::init(); //! let result: ReceiveResult<ActionsTree> = contract_receive(&ctx, 0, &mut logger, state); //! claim!(...) //! ... //! } //! } //! ``` use crate::*; #[cfg(not(feature = "std"))] use alloc::boxed::Box; #[cfg(feature = "std")] use std::boxed::Box; /// Placeholder for the context chain meta data. /// All the fields are optionally set and the getting an unset field will result /// in test failing. /// For most cases it is used as part of either /// [`InitContextTest`](struct.InitContextTest.html) or /// [`ReceiveContextTest`](struct.ReceiveContextTest.html). /// Use only in unit tests! /// /// Defaults to having all of the fields unset #[derive(Default, Clone)] pub struct ChainMetaTest { pub(crate) slot_number: Option<SlotNumber>, pub(crate) block_height: Option<BlockHeight>, pub(crate) finalized_height: Option<FinalizedHeight>, pub(crate) slot_time: Option<SlotTime>, } /// Placeholder for the initial context. All the fields can be set optionally /// and the getting an unset field will result in calling /// [`fail!`](../macro.fail.html). Use only in tests! /// /// # Setters /// Every field have a setter function prefixed with `set_`. /// /// ### Example /// Creating an empty context and setting the `init_origin`. /// ``` /// let mut ctx = InitContextTest::empty(); /// ctx.set_init_origin(AccountAddress([0u8; 32])); /// ``` /// ## Set chain meta data /// Chain meta data is set using setters on the public field `metadata` /// (see [`ChainMetaTest`](struct.ChainMetaTest.html)). /// /// ### Example /// Creating an empty context and setting the `slot_time` metadata. /// ``` /// let mut ctx = InitContextTest::empty(); /// ctx.metadata.set_slot_time(1609459200); /// ``` /// /// # Use case example /// /// ```rust /// #[init(contract = "noop")] /// fn contract_init<I: HasInitContext<()>, L: HasLogger>( /// ctx: &I, /// _amount: Amount, /// _logger: &mut L, /// ) -> InitResult<()> { /// let init_origin = ctx.init_origin(); /// let parameter: SomeParameterType = ctx.parameter_cursor().get()?; /// Ok(()) /// } /// /// #[cfg(test)] /// mod tests { /// use super::*; /// use concordium_sc_base::test_infrastructure::*; /// #[test] /// fn test() { /// let mut ctx = InitContextTest::empty(); /// ctx.set_init_origin(AccountAddress([0u8; 32])); /// ... /// let result = contract_init(&ctx, 0, &mut logger); /// // Reads the init_origin without any problems. /// // But then fails because the parameter is not set. /// } /// } /// ``` /// # Default /// Defaults to having all the fields unset, and constructing /// [`ChainMetaTest`](struct.ChainMetaTest.html) using default. #[derive(Default, Clone)] pub struct InitContextTest<'a> { pub metadata: ChainMetaTest, pub(crate) parameter: Option<&'a [u8]>, pub(crate) init_origin: Option<AccountAddress>, } /// Placeholder for the receiving context. All the fields can be set optionally /// and the getting an unset field will result in calling /// [`fail!`](../macro.fail.html). Use only in tests! /// /// # Setters /// Every field have a setter function prefixed with `set_`. /// /// ### Example /// Creating an empty context and setting the `init_origin`. /// ``` /// let owner = AccountAddress([0u8; 32]); /// let mut ctx = ReceiveContextTest::empty(); /// ctx.set_owner(owner); /// ctx.set_sender(Address::Account(owner)); /// ``` /// ## Set chain meta data /// Chain meta data is set using setters on the public field `metadata` /// (see [`ChainMetaTest`](struct.ChainMetaTest.html)). /// /// ### Example /// Creating an empty context and setting the `slot_time` metadata. /// ``` /// let mut ctx = ReceiveContextTest::empty(); /// ctx.metadata.set_slot_time(1609459200); /// ``` /// /// # Use case example /// Creating a context for running unit tests /// ```rust /// #[receive(contract = "mycontract", name = "receive")] /// fn contract_receive<R: HasReceiveContext<()>, L: HasLogger, A: HasActions>( /// ctx: &R, /// amount: Amount, /// logger: &mut L, /// state: &mut State, /// ) -> ReceiveResult<A> { /// ensure!(ctx.sender().matches_account(&ctx.owner()), "Only the owner can increment."); /// Ok(A::accept()) /// } /// /// #[cfg(test)] /// mod tests { /// use super::*; /// use concordium_sc_base::test_infrastructure::*; /// #[test] /// fn test() { /// let owner = AccountAddress([0u8; 32]); /// let mut ctx = ReceiveContextTest::empty(); /// ctx.set_owner(owner); /// ctx.set_sender(Address::Account(owner)); /// ... /// let result: ReceiveResult<ActionsTree> = contract_receive(&ctx, 0, &mut logger, state); /// } /// } /// ``` #[derive(Default, Clone)] pub struct ReceiveContextTest<'a> { pub metadata: ChainMetaTest, pub(crate) parameter: Option<&'a [u8]>, pub(crate) invoker: Option<AccountAddress>, pub(crate) self_address: Option<ContractAddress>, pub(crate) self_balance: Option<Amount>, pub(crate) sender: Option<Address>, pub(crate) owner: Option<AccountAddress>, } // Setters for testing-context impl ChainMetaTest { /// Create an `ChainMetaTest` where every field is unset, and getting any of /// the fields will result in [`fail!`](../macro.fail.html). pub fn empty() -> Self { Default::default() } /// Set the block slot time pub fn set_slot_time(&mut self, value: SlotTime) -> &mut Self { self.slot_time = Some(value); self } /// Set the block height pub fn set_block_height(&mut self, value: BlockHeight) -> &mut Self { self.block_height = Some(value); self } /// Set the finalized block height pub fn set_finalized_height(&mut self, value: FinalizedHeight) -> &mut Self { self.finalized_height = Some(value); self } /// Set the slot number pub fn set_slot_number(&mut self, value: SlotNumber) -> &mut Self { self.slot_number = Some(value); self } } impl<'a> InitContextTest<'a> { /// Create an `InitContextTest` where every field is unset, and getting any /// of the fields will result in [`fail!`](../macro.fail.html). pub fn empty() -> Self { Default::default() } /// Set the parameter bytes of the `InitContextTest` pub fn set_parameter(&mut self, value: &'a [u8]) -> &mut Self { self.parameter = Some(value); self } /// Set `init_origin` in the `InitContextTest` pub fn set_init_origin(&mut self, value: AccountAddress) -> &mut Self { self.init_origin = Some(value); self } } impl<'a> ReceiveContextTest<'a> { /// Create an `InitContextTest` where every field is unset, and getting any /// of the fields will result in [`fail!`](../macro.fail.html). pub fn empty() -> Self { Default::default() } pub fn set_parameter(&mut self, value: &'a [u8]) -> &mut Self { self.parameter = Some(value); self } pub fn set_invoker(&mut self, value: AccountAddress) -> &mut Self { self.invoker = Some(value); self } pub fn set_self_address(&mut self, value: ContractAddress) -> &mut Self { self.self_address = Some(value); self } pub fn set_self_balance(&mut self, value: Amount) -> &mut Self { self.self_balance = Some(value); self } pub fn set_sender(&mut self, value: Address) -> &mut Self { self.sender = Some(value); self } pub fn set_owner(&mut self, value: AccountAddress) -> &mut Self { self.owner = Some(value); self } } // Error handling when unwrapping fn unwrap_ctx_field<A>(opt: Option<A>, name: &str) -> A { match opt { Some(v) => v, None => fail!( "Unset field on test context '{}', make sure to set all the field necessary for the \ contract", name ), } } // Getters for testing-context impl HasChainMetadata for ChainMetaTest { fn slot_time(&self) -> SlotTime { unwrap_ctx_field(self.slot_time, "metadata.slot_time") } fn block_height(&self) -> BlockHeight { unwrap_ctx_field(self.block_height, "metadata.block_height") } fn finalized_height(&self) -> FinalizedHeight { unwrap_ctx_field(self.finalized_height, "metadata.finalized_height") } fn slot_number(&self) -> SlotNumber { unwrap_ctx_field(self.slot_number, "metadata.slot_number") } } impl<'a> HasInitContext<()> for InitContextTest<'a> { type InitData = (); type MetadataType = ChainMetaTest; type ParamType = Cursor<&'a [u8]>; fn open(_data: Self::InitData) -> Self { InitContextTest::default() } fn init_origin(&self) -> AccountAddress { unwrap_ctx_field(self.init_origin, "init_origin") } fn parameter_cursor(&self) -> Self::ParamType { Cursor::new(unwrap_ctx_field(self.parameter, "parameter")) } fn metadata(&self) -> &Self::MetadataType { &self.metadata } } impl<'a> HasReceiveContext<()> for ReceiveContextTest<'a> { type MetadataType = ChainMetaTest; type ParamType = Cursor<&'a [u8]>; type ReceiveData = (); fn open(_data: Self::ReceiveData) -> Self { ReceiveContextTest::default() } fn parameter_cursor(&self) -> Self::ParamType { Cursor::new(unwrap_ctx_field(self.parameter, "parameter")) } fn metadata(&self) -> &Self::MetadataType { &self.metadata } fn invoker(&self) -> AccountAddress { unwrap_ctx_field(self.invoker, "invoker") } fn self_address(&self) -> ContractAddress { unwrap_ctx_field(self.self_address, "self_address") } fn self_balance(&self) -> Amount { unwrap_ctx_field(self.self_balance, "self_balance") } fn sender(&self) -> Address { unwrap_ctx_field(self.sender, "sender") } fn owner(&self) -> AccountAddress { unwrap_ctx_field(self.owner, "owner") } } impl<'a> HasParameter for Cursor<&'a [u8]> { fn size(&self) -> u32 { self.data.len() as u32 } } /// A logger that simply accumulates all the logged items to be inspected at the /// end of execution. pub struct LogRecorder { pub logs: Vec<Vec<u8>>, } impl HasLogger for LogRecorder { fn init() -> Self { Self { logs: Vec::new(), } } fn log_bytes(&mut self, event: &[u8]) { self.logs.push(event.to_vec()) } } /// An actions tree, used to provide a more simple presentation for testing. #[derive(Eq, PartialEq, Debug)] pub enum ActionsTree { Accept, SimpleTransfer { to: AccountAddress, amount: Amount, }, Send { to: ContractAddress, receive_name: String, amount: Amount, parameter: Vec<u8>, }, AndThen { left: Box<ActionsTree>, right: Box<ActionsTree>, }, OrElse { left: Box<ActionsTree>, right: Box<ActionsTree>, }, } impl HasActions for ActionsTree { fn accept() -> Self { ActionsTree::Accept } fn simple_transfer(acc: &AccountAddress, amount: Amount) -> Self { ActionsTree::SimpleTransfer { to: *acc, amount, } } fn send(ca: &ContractAddress, receive_name: &str, amount: Amount, parameter: &[u8]) -> Self { ActionsTree::Send { to: *ca, receive_name: receive_name.to_string(), amount, parameter: parameter.to_vec(), } } fn and_then(self, then: Self) -> Self { ActionsTree::AndThen { left: Box::new(self), right: Box::new(then), } } fn or_else(self, el: Self) -> Self { ActionsTree::OrElse { left: Box::new(self), right: Box::new(el), } } } /// Reports back an error to the host when compiled to wasm /// Used internally, not meant to be called directly by contract writers #[doc(hidden)] #[cfg(all(feature = "wasm-test", target_arch = "wasm32"))] pub fn report_error(message: &str, filename: &str, line: u32, column: u32) { let msg_bytes = message.as_bytes(); let filename_bytes = filename.as_bytes(); unsafe { crate::prims::report_error( msg_bytes.as_ptr(), msg_bytes.len() as u32, filename_bytes.as_ptr(), filename_bytes.len() as u32, line, column, ) }; } /// Reports back an error to the host when compiled to wasm /// Used internally, not meant to be called directly by contract writers #[doc(hidden)] #[cfg(not(all(feature = "wasm-test", target_arch = "wasm32")))] pub fn report_error(_message: &str, _filename: &str, _line: u32, _column: u32) {}