errors.rs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::build_solidity_with_options;
  3. use parity_scale_codec::Encode;
  4. #[test]
  5. fn errors() {
  6. let mut runtime = build_solidity_with_options(
  7. r#"contract RuntimeErrors {
  8. bytes b = hex"0000_00fa";
  9. uint256[] arr;
  10. child public c;
  11. child public c2;
  12. callee public cal;
  13. constructor() public payable {}
  14. function print_test(int8 num) public returns (int8) {
  15. print("Hello world!");
  16. return num;
  17. }
  18. function math_overflow(int8 num) public returns (int8) {
  19. int8 ovf = num + 120;
  20. return ovf;
  21. }
  22. function require_test(int8 num) public returns (int8) {
  23. require(num > 10, "sesa");
  24. return 0;
  25. }
  26. // assert failure
  27. function assert_test(int8 num) public returns (int8) {
  28. assert(num > 10);
  29. return 0;
  30. }
  31. // storage index out of bounds
  32. function set_storage_bytes() public returns (bytes) {
  33. bytes sesa = new bytes(1);
  34. b[5] = sesa[0];
  35. return sesa;
  36. }
  37. // storage array index out of bounds
  38. function get_storage_bytes() public returns (bytes) {
  39. bytes sesa = new bytes(1);
  40. sesa[0] = b[5];
  41. return sesa;
  42. }
  43. // value transfer failure
  44. function transfer_abort() public {
  45. address a = address(0);
  46. payable(a).transfer(10);
  47. }
  48. // pop from empty storage array
  49. function pop_empty_storage() public {
  50. arr.pop();
  51. }
  52. // external call failed
  53. function call_ext() public {
  54. //cal = new callee();
  55. cal.callee_func{gas: 1e15}();
  56. }
  57. // contract creation failed (contract was deplyed with no value)
  58. function create_child() public {
  59. c = new child{value: 900e15, salt:2}();
  60. c2 = new child{value: 900e15, salt:2}();
  61. uint128 x = address(this).balance;
  62. //print("sesa");
  63. print("x = {}".format(x));
  64. }
  65. // non payable function dont_pay_me received value
  66. function dont_pay_me() public {}
  67. function pay_me() public payable {
  68. print("PAYED");
  69. uint128 x = address(this).balance;
  70. //print("sesa");
  71. print("x = {}".format(x));
  72. }
  73. function i_will_revert() public {
  74. revert();
  75. }
  76. function write_integer_failure(uint8 buf_size) public {
  77. bytes smol_buf = new bytes(buf_size);
  78. smol_buf.writeUint32LE(350, 20);
  79. }
  80. function write_bytes_failure(uint8 buf_size) public {
  81. bytes data = new bytes(10);
  82. bytes smol_buf = new bytes(buf_size);
  83. smol_buf.writeBytes(data, 0);
  84. }
  85. function read_integer_failure(uint32 offset) public {
  86. bytes smol_buf = new bytes(1);
  87. smol_buf.readUint16LE(offset);
  88. }
  89. // truncated type overflows
  90. function trunc_failure(uint128 input) public returns (uint256) {
  91. uint256[] a = new uint256[](input);
  92. return a[0];
  93. }
  94. function out_of_bounds(uint8 input) public returns (uint256) {
  95. uint256[] a = new uint256[](input);
  96. return a[20];
  97. }
  98. function invalid_instruction() public {
  99. assembly {
  100. invalid()
  101. }
  102. }
  103. function byte_cast_failure(uint8 num) public returns (bytes) {
  104. bytes smol_buf = new bytes(num);
  105. //bytes32 b32 = new bytes(num);
  106. bytes32 b32 = bytes32(smol_buf);
  107. return b32;
  108. }
  109. }
  110. contract callee {
  111. constructor() {}
  112. function callee_func() public {
  113. revert();
  114. }
  115. }
  116. contract child {
  117. constructor() {}
  118. function say_my_name() public pure returns (string memory) {
  119. print("say_my_name");
  120. return "child";
  121. }
  122. }
  123. "#,
  124. false,
  125. true,
  126. );
  127. runtime.function_expect_failure("write_bytes_failure", 9u8.encode());
  128. assert_eq!(
  129. runtime.printbuf,
  130. "runtime_error: data does not fit into buffer in test.sol:95:22-32,\n"
  131. );
  132. runtime.printbuf.clear();
  133. runtime.function_expect_failure("math_overflow", 10u8.encode());
  134. assert_eq!(
  135. runtime.printbuf,
  136. "runtime_error: math overflow in test.sol:16:24-33,\n"
  137. );
  138. runtime.printbuf.clear();
  139. runtime.function_expect_failure("require_test", 9u8.encode());
  140. assert_eq!(
  141. runtime.printbuf,
  142. "runtime_error: sesa require condition failed in test.sol:21:31-37,\n"
  143. );
  144. runtime.printbuf.clear();
  145. runtime.function_expect_failure("assert_test", 9u8.encode());
  146. assert_eq!(
  147. runtime.printbuf,
  148. "runtime_error: assert failure in test.sol:27:20-28,\n"
  149. );
  150. runtime.printbuf.clear();
  151. runtime.function_expect_failure("set_storage_bytes", Vec::new());
  152. assert_eq!(
  153. runtime.printbuf,
  154. "runtime_error: storage index out of bounds in test.sol:34:15-16,\n"
  155. );
  156. runtime.printbuf.clear();
  157. runtime.function_expect_failure("get_storage_bytes", Vec::new());
  158. assert_eq!(
  159. runtime.printbuf,
  160. "runtime_error: storage array index out of bounds in test.sol:41:23-27,\n"
  161. );
  162. runtime.printbuf.clear();
  163. runtime.function_expect_failure("transfer_abort", Vec::new());
  164. assert_eq!(
  165. runtime.printbuf,
  166. "runtime_error: value transfer failure in test.sol:48:33-35,\n"
  167. );
  168. runtime.printbuf.clear();
  169. runtime.function_expect_failure("pop_empty_storage", Vec::new());
  170. assert_eq!(
  171. runtime.printbuf,
  172. "runtime_error: pop from empty storage array in test.sol:53:17-20,\n"
  173. );
  174. runtime.vm.value = 3500;
  175. runtime.constructor(0, Vec::new());
  176. runtime.printbuf.clear();
  177. runtime.vm.value = 0;
  178. runtime.function_expect_failure("create_child", Vec::new());
  179. assert_eq!(
  180. runtime.printbuf,
  181. "runtime_error: contract creation failed in test.sol:65:18-52,\n"
  182. );
  183. runtime.printbuf.clear();
  184. runtime.function_expect_failure("i_will_revert", Vec::new());
  185. assert_eq!(
  186. runtime.printbuf,
  187. "runtime_error: revert encountered in test.sol:84:13-19,\n"
  188. );
  189. runtime.printbuf.clear();
  190. runtime.function_expect_failure("write_integer_failure", 1u8.encode());
  191. assert_eq!(
  192. runtime.printbuf,
  193. "runtime_error: integer too large to write in buffer in test.sol:89:22-35,\n"
  194. );
  195. runtime.printbuf.clear();
  196. runtime.function_expect_failure("invalid_instruction", Vec::new());
  197. assert_eq!(
  198. runtime.printbuf,
  199. "runtime_error: reached invalid instruction in test.sol:116:17-26,\n"
  200. );
  201. runtime.printbuf.clear();
  202. runtime.function_expect_failure("out_of_bounds", 19u8.encode());
  203. assert_eq!(
  204. runtime.printbuf,
  205. "runtime_error: array index out of bounds in test.sol:111:20-25,\n"
  206. );
  207. runtime.printbuf.clear();
  208. runtime.function_expect_failure("trunc_failure", u128::MAX.encode());
  209. assert_eq!(
  210. runtime.printbuf,
  211. "runtime_error: truncated type overflows in test.sol:105:41-46,\n"
  212. );
  213. runtime.printbuf.clear();
  214. runtime.function_expect_failure("byte_cast_failure", 33u8.encode());
  215. assert_eq!(
  216. runtime.printbuf,
  217. "runtime_error: bytes cast error in test.sol:124:27-44,\n"
  218. );
  219. runtime.printbuf.clear();
  220. runtime.function_expect_failure("read_integer_failure", 2u32.encode());
  221. assert_eq!(
  222. runtime.printbuf,
  223. "runtime_error: read integer out of bounds in test.sol:100:22-34,\n"
  224. );
  225. runtime.printbuf.clear();
  226. runtime.function_expect_failure("call_ext", Vec::new());
  227. assert_eq!(
  228. runtime.printbuf,
  229. "runtime_error: external call failed in test.sol:59:13-41,\n"
  230. );
  231. runtime.printbuf.clear();
  232. runtime.vm.value = 1;
  233. runtime.function_expect_failure("dont_pay_me", Vec::new());
  234. assert_eq!(
  235. runtime.printbuf,
  236. "runtime_error: non payable function dont_pay_me received value,\n"
  237. );
  238. }