runtime_errors.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: Apache-2.0
  2. import { Keypair, PublicKey, sendAndConfirmTransaction, SystemProgram, Transaction } from '@solana/web3.js';
  3. import expect from 'expect';
  4. import { loadContractAndCallConstructor } from './setup';
  5. import { Program, Provider, BN, AnchorProvider } from '@coral-xyz/anchor';
  6. import { createAccount } from "@solana/spl-token";
  7. describe('Runtime Errors', function () {
  8. this.timeout(150000);
  9. let program: Program;
  10. let storage: Keypair;
  11. let payer: Keypair;
  12. let provider: Provider;
  13. before(async function () {
  14. ({ program, storage, payer, provider } = await loadContractAndCallConstructor('RuntimeErrors'));
  15. });
  16. it('Prints runtime errors', async function () {
  17. try {
  18. let res = await program.methods.setStorageBytes().accounts({ dataAccount: storage.publicKey }).simulate();
  19. }
  20. catch (e: any) {
  21. const logs = e.simulationResponse.logs;
  22. expect(logs).toContain(`Program log: runtime_error: storage index out of bounds in runtime_errors.sol:41:11-12,
  23. `);
  24. }
  25. try {
  26. let res = await program.methods.getStorageBytes().accounts({ dataAccount: storage.publicKey }).simulate();;
  27. }
  28. catch (e: any) {
  29. const logs = e.simulationResponse.logs;
  30. expect(logs).toContain(`Program log: runtime_error: storage array index out of bounds in runtime_errors.sol:48:19-23,
  31. `);
  32. }
  33. try {
  34. let res = await program.methods.popEmptyStorage().accounts({ dataAccount: storage.publicKey }).simulate();
  35. } catch (e: any) {
  36. const logs = e.simulationResponse.logs;
  37. expect(logs).toContain(`Program log: runtime_error: pop from empty storage array in runtime_errors.sol:60:9-12,
  38. `)
  39. }
  40. try {
  41. let res = await program.methods.invalidInstruction().simulate();
  42. } catch (e: any) {
  43. const logs = e.simulationResponse.logs;
  44. expect(logs).toContain(`Program log: runtime_error: reached invalid instruction in runtime_errors.sol:101:13-22,
  45. `)
  46. }
  47. try {
  48. let res = await program.methods.byteCastFailure(new BN(33)).simulate();
  49. } catch (e: any) {
  50. const logs = e.simulationResponse.logs;
  51. expect(logs).toContain(`Program log: runtime_error: bytes cast error in runtime_errors.sol:107:23-40,
  52. `)
  53. }
  54. try {
  55. let res = await program.methods.iWillRevert().simulate();
  56. } catch (e: any) {
  57. const logs = e.simulationResponse.logs;
  58. expect(logs).toContain(`Program log: runtime_error: revert encountered in runtime_errors.sol:69:9-17,
  59. `)
  60. }
  61. try {
  62. let res = await program.methods.assertTest(new BN(9)).simulate();
  63. } catch (e: any) {
  64. const logs = e.simulationResponse.logs;
  65. expect(logs).toContain(`Program log: runtime_error: assert failure in runtime_errors.sol:34:16-24,
  66. `)
  67. }
  68. try {
  69. let res = await program.methods.writeIntegerFailure(new BN(1)).simulate();
  70. } catch (e: any) {
  71. const logs = e.simulationResponse.logs;
  72. expect(logs).toContain(`Program log: runtime_error: integer too large to write in buffer in runtime_errors.sol:74:18-31,
  73. `)
  74. }
  75. try {
  76. let res = await program.methods.writeBytesFailure(new BN(9)).simulate();
  77. } catch (e: any) {
  78. const logs = e.simulationResponse.logs;
  79. expect(logs).toContain(`Program log: runtime_error: data does not fit into buffer in runtime_errors.sol:80:18-28,
  80. `)
  81. }
  82. try {
  83. let res = await program.methods.readIntegerFailure(new BN(2)).simulate();
  84. } catch (e: any) {
  85. const logs = e.simulationResponse.logs;
  86. expect(logs).toContain(`Program log: runtime_error: read integer out of bounds in runtime_errors.sol:85:18-30,
  87. `)
  88. }
  89. try {
  90. let res = await program.methods.outOfBounds(new BN(19)).simulate();
  91. } catch (e: any) {
  92. const logs = e.simulationResponse.logs;
  93. expect(logs).toContain(`Program log: runtime_error: array index out of bounds in runtime_errors.sol:96:16-21,
  94. `)
  95. }
  96. try {
  97. let res = await program.methods.truncFailure(new BN(99999999999999)).simulate();
  98. } catch (e: any) {
  99. const logs = e.simulationResponse.logs;
  100. expect(logs).toContain(`Program log: runtime_error: truncated type overflows in runtime_errors.sol:90:37-42,
  101. `)
  102. }
  103. });
  104. });