StorageSlot.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const { constants, BN } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const StorageSlotMock = artifacts.require('StorageSlotMock');
  4. const slot = web3.utils.keccak256('some.storage.slot');
  5. const otherSlot = web3.utils.keccak256('some.other.storage.slot');
  6. contract('StorageSlot', function (accounts) {
  7. beforeEach(async function () {
  8. this.store = await StorageSlotMock.new();
  9. });
  10. describe('boolean storage slot', function () {
  11. beforeEach(async function () {
  12. this.value = true;
  13. });
  14. it('set', async function () {
  15. await this.store.setBoolean(slot, this.value);
  16. });
  17. describe('get', function () {
  18. beforeEach(async function () {
  19. await this.store.setBoolean(slot, this.value);
  20. });
  21. it('from right slot', async function () {
  22. expect(await this.store.getBoolean(slot)).to.be.equal(this.value);
  23. });
  24. it('from other slot', async function () {
  25. expect(await this.store.getBoolean(otherSlot)).to.be.equal(false);
  26. });
  27. });
  28. });
  29. describe('address storage slot', function () {
  30. beforeEach(async function () {
  31. this.value = accounts[1];
  32. });
  33. it('set', async function () {
  34. await this.store.setAddress(slot, this.value);
  35. });
  36. describe('get', function () {
  37. beforeEach(async function () {
  38. await this.store.setAddress(slot, this.value);
  39. });
  40. it('from right slot', async function () {
  41. expect(await this.store.getAddress(slot)).to.be.equal(this.value);
  42. });
  43. it('from other slot', async function () {
  44. expect(await this.store.getAddress(otherSlot)).to.be.equal(constants.ZERO_ADDRESS);
  45. });
  46. });
  47. });
  48. describe('bytes32 storage slot', function () {
  49. beforeEach(async function () {
  50. this.value = web3.utils.keccak256('some byte32 value');
  51. });
  52. it('set', async function () {
  53. await this.store.setBytes32(slot, this.value);
  54. });
  55. describe('get', function () {
  56. beforeEach(async function () {
  57. await this.store.setBytes32(slot, this.value);
  58. });
  59. it('from right slot', async function () {
  60. expect(await this.store.getBytes32(slot)).to.be.equal(this.value);
  61. });
  62. it('from other slot', async function () {
  63. expect(await this.store.getBytes32(otherSlot)).to.be.equal(constants.ZERO_BYTES32);
  64. });
  65. });
  66. });
  67. describe('uint256 storage slot', function () {
  68. beforeEach(async function () {
  69. this.value = new BN(1742);
  70. });
  71. it('set', async function () {
  72. await this.store.setUint256(slot, this.value);
  73. });
  74. describe('get', function () {
  75. beforeEach(async function () {
  76. await this.store.setUint256(slot, this.value);
  77. });
  78. it('from right slot', async function () {
  79. expect(await this.store.getUint256(slot)).to.be.bignumber.equal(this.value);
  80. });
  81. it('from other slot', async function () {
  82. expect(await this.store.getUint256(otherSlot)).to.be.bignumber.equal('0');
  83. });
  84. });
  85. });
  86. });