evm.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: Apache-2.0
  2. use solang::{file_resolver::FileResolver, parse_and_resolve, sema::ast, Target};
  3. use std::ffi::OsStr;
  4. fn test_solidity(src: &str) -> ast::Namespace {
  5. let mut cache = FileResolver::new();
  6. cache.set_file_contents("test.sol", src.to_string());
  7. let ns = parse_and_resolve(OsStr::new("test.sol"), &mut cache, Target::EVM);
  8. ns.print_diagnostics_in_plain(&cache, false);
  9. ns
  10. }
  11. #[test]
  12. fn address() {
  13. let ns = test_solidity(
  14. "
  15. contract address_tester {
  16. function encode_const() public returns (address) {
  17. return 0x52908400098527886E0F7030069857D2E4169EE7;
  18. }
  19. function test_arg(address foo) public {
  20. assert(foo == 0x27b1fdb04752bbc536007a920d24acb045561c26);
  21. // this literal is a number
  22. int x = 0x27b1fdb047_52bbc536007a920d24acb045561C26;
  23. assert(int(foo) == x);
  24. }
  25. function allones() public returns (address) {
  26. return address(1);
  27. }
  28. }",
  29. );
  30. assert!(!ns.diagnostics.any_errors());
  31. }
  32. #[test]
  33. fn try_catch() {
  34. let ns = test_solidity(
  35. r##"
  36. contract b {
  37. int32 x;
  38. constructor(int32 a) public {
  39. x = a;
  40. }
  41. function get_x(int32 t) public returns (int32) {
  42. if (t == 0) {
  43. revert("cannot be zero");
  44. }
  45. return x * t;
  46. }
  47. }
  48. contract c {
  49. b x;
  50. constructor() public {
  51. x = new b(102);
  52. }
  53. function test() public returns (int32) {
  54. int32 state = 0;
  55. try x.get_x(0) returns (int32 l) {
  56. state = 1;
  57. } catch Error(string err) {
  58. if (err == "cannot be zero") {
  59. state = 2;
  60. } else {
  61. state = 3;
  62. }
  63. } catch (bytes ) {
  64. state = 4;
  65. }
  66. return state;
  67. }
  68. }"##,
  69. );
  70. assert!(!ns.diagnostics.any_errors());
  71. }
  72. #[test]
  73. fn selfdestruct() {
  74. let ns = test_solidity(
  75. r##"
  76. contract other {
  77. function goaway(address payable recipient) public returns (bool) {
  78. selfdestruct(recipient);
  79. }
  80. }
  81. contract c {
  82. other o;
  83. function step1() public {
  84. o = new other{value: 511}();
  85. }
  86. function step2() public {
  87. bool foo = o.goaway(payable(address(this)));
  88. }
  89. }"##,
  90. );
  91. assert!(!ns.diagnostics.any_errors());
  92. }
  93. #[test]
  94. fn eth_builtins() {
  95. let ns = test_solidity(
  96. r#"
  97. contract testing {
  98. function test_address() public view returns (uint256 ret) {
  99. assembly {
  100. let a := address()
  101. ret := a
  102. }
  103. }
  104. function test_balance() public view returns (uint256 ret) {
  105. assembly {
  106. let a := address()
  107. ret := balance(a)
  108. }
  109. }
  110. function test_selfbalance() public view returns (uint256 ret) {
  111. assembly {
  112. let a := selfbalance()
  113. ret := a
  114. }
  115. }
  116. function test_caller() public view returns (uint256 ret) {
  117. assembly {
  118. let a := caller()
  119. ret := a
  120. }
  121. }
  122. function test_callvalue() public view returns (uint256 ret) {
  123. assembly {
  124. let a := callvalue()
  125. ret := a
  126. }
  127. }
  128. function test_extcodesize() public view returns (uint256 ret) {
  129. assembly {
  130. let a := address()
  131. ret := extcodesize(a)
  132. }
  133. }
  134. }
  135. "#,
  136. );
  137. assert!(!ns.diagnostics.any_errors());
  138. }