|
| 1 | +use candid::Principal; |
| 2 | +use ic_cdk::api::*; |
| 3 | + |
| 4 | +#[export_name = "canister_update call_msg_arg_data"] |
| 5 | +fn call_msg_arg_data() { |
| 6 | + assert_eq!(msg_arg_data(), vec![42]); |
| 7 | + msg_reply(vec![]); |
| 8 | +} |
| 9 | + |
| 10 | +#[export_name = "canister_update call_msg_caller"] |
| 11 | +fn call_msg_caller() { |
| 12 | + assert_eq!(msg_caller(), Principal::anonymous()); |
| 13 | + msg_reply(vec![]); |
| 14 | +} |
| 15 | + |
| 16 | +#[export_name = "canister_update call_msg_reply"] |
| 17 | +fn call_msg_reply() { |
| 18 | + msg_reply(vec![42]); |
| 19 | +} |
| 20 | + |
| 21 | +#[export_name = "canister_update call_msg_reject"] |
| 22 | +fn call_msg_reject() { |
| 23 | + msg_reject("e2e test reject"); |
| 24 | +} |
| 25 | + |
| 26 | +#[export_name = "canister_update call_msg_cycles_available"] |
| 27 | +fn call_msg_cycles_available() { |
| 28 | + assert_eq!(msg_cycles_available(), 0); |
| 29 | + msg_reply(vec![]); |
| 30 | +} |
| 31 | + |
| 32 | +#[export_name = "canister_update call_msg_cycles_accept"] |
| 33 | +fn call_msg_cycles_accept() { |
| 34 | + // The available cycles are 0, so the actual cycles accepted are 0. |
| 35 | + assert_eq!(msg_cycles_accept(1000), 0); |
| 36 | + msg_reply(vec![]); |
| 37 | +} |
| 38 | + |
| 39 | +#[export_name = "canister_update call_cycles_burn"] |
| 40 | +fn call_cycles_burn() { |
| 41 | + assert_eq!(cycles_burn(1000), 1000); |
| 42 | + msg_reply(vec![]); |
| 43 | +} |
| 44 | + |
| 45 | +#[export_name = "canister_update call_canister_self"] |
| 46 | +fn call_canister_self() { |
| 47 | + let self_id = canister_self(); |
| 48 | + // The sender sended canister ID |
| 49 | + let data = msg_arg_data(); |
| 50 | + assert_eq!(self_id.as_slice(), data); |
| 51 | + msg_reply(vec![]); |
| 52 | +} |
| 53 | + |
| 54 | +#[export_name = "canister_update call_canister_cycle_balance"] |
| 55 | +fn call_canister_cycle_balance() { |
| 56 | + assert!(canister_cycle_balance() > 0); |
| 57 | + msg_reply(vec![]); |
| 58 | +} |
| 59 | + |
| 60 | +#[export_name = "canister_update call_canister_status"] |
| 61 | +fn call_canister_status() { |
| 62 | + assert_eq!(canister_status(), CanisterStatusCode::Running); |
| 63 | + assert_eq!(canister_status(), 1); |
| 64 | + msg_reply(vec![]); |
| 65 | +} |
| 66 | + |
| 67 | +#[export_name = "canister_update call_canister_version"] |
| 68 | +fn call_canister_version() { |
| 69 | + assert!(canister_version() > 0); |
| 70 | + msg_reply(vec![]); |
| 71 | +} |
| 72 | + |
| 73 | +#[export_name = "canister_inspect_message"] |
| 74 | +fn inspect_message() { |
| 75 | + assert!(msg_method_name().starts_with("call_")); |
| 76 | + accept_message(); |
| 77 | +} |
| 78 | + |
| 79 | +#[export_name = "canister_update call_stable"] |
| 80 | +fn call_stable() { |
| 81 | + assert_eq!(stable_size(), 0); |
| 82 | + assert_eq!(stable_grow(1), 0); |
| 83 | + let data = vec![42]; |
| 84 | + stable_write(0, &data); |
| 85 | + let mut read_buf = vec![0]; |
| 86 | + stable_read(0, &mut read_buf); |
| 87 | + assert_eq!(read_buf, data); |
| 88 | + msg_reply(vec![]); |
| 89 | +} |
| 90 | + |
| 91 | +#[export_name = "canister_update call_certified_data_set"] |
| 92 | +fn call_certified_data_set() { |
| 93 | + certified_data_set(vec![42]); |
| 94 | + msg_reply(vec![]); |
| 95 | +} |
| 96 | + |
| 97 | +#[export_name = "canister_query call_data_certificate"] |
| 98 | +fn call_data_certificate() { |
| 99 | + assert!(data_certificate().is_some()); |
| 100 | + msg_reply(vec![]); |
| 101 | +} |
| 102 | + |
| 103 | +#[export_name = "canister_update call_time"] |
| 104 | +fn call_time() { |
| 105 | + assert!(time() > 0); |
| 106 | + msg_reply(vec![]); |
| 107 | +} |
| 108 | + |
| 109 | +#[export_name = "canister_update call_performance_counter"] |
| 110 | +fn call_performance_counter() { |
| 111 | + let t0 = PerformanceCounterType::InstructionCounter; |
| 112 | + assert_eq!(t0, 0); |
| 113 | + let ic0 = performance_counter(0); |
| 114 | + let ic1 = performance_counter(t0); |
| 115 | + let ic2 = instruction_counter(); |
| 116 | + assert!(ic0 < ic1); |
| 117 | + assert!(ic1 < ic2); |
| 118 | + |
| 119 | + let t1 = PerformanceCounterType::CallContextInstructionCounter; |
| 120 | + assert_eq!(t1, 1); |
| 121 | + let ccic0 = performance_counter(1); |
| 122 | + let ccic1 = performance_counter(t1); |
| 123 | + let ccic2 = call_context_instruction_counter(); |
| 124 | + assert!(ccic0 < ccic1); |
| 125 | + assert!(ccic1 < ccic2); |
| 126 | + msg_reply(vec![]); |
| 127 | +} |
| 128 | + |
| 129 | +#[export_name = "canister_update call_is_controller"] |
| 130 | +fn call_is_controller() { |
| 131 | + // The canister was created by the anonymous principal. |
| 132 | + assert!(is_controller(&Principal::anonymous())); |
| 133 | + msg_reply(vec![]); |
| 134 | +} |
| 135 | + |
| 136 | +/// This entry point will be called by both update and query calls. |
| 137 | +/// The query call will return 0, and the update call will return 1. |
| 138 | +#[export_name = "canister_query call_in_replicated_execution"] |
| 139 | +fn call_in_replicated_execution() { |
| 140 | + let res = match in_replicated_execution() { |
| 141 | + true => 1, |
| 142 | + false => 0, |
| 143 | + }; |
| 144 | + msg_reply(vec![res]); |
| 145 | +} |
| 146 | + |
| 147 | +#[export_name = "canister_update call_debug_print"] |
| 148 | +fn call_debug_print() { |
| 149 | + debug_print("Hello, world!"); |
| 150 | + msg_reply(vec![]); |
| 151 | +} |
| 152 | + |
| 153 | +#[export_name = "canister_update call_trap"] |
| 154 | +fn call_trap() { |
| 155 | + trap("It's a trap!"); |
| 156 | +} |
| 157 | + |
| 158 | +fn main() {} |
0 commit comments