StorageSlot.test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. describe('string storage slot', function () {
  87. beforeEach(async function () {
  88. this.value = 'lorem ipsum';
  89. });
  90. it('set', async function () {
  91. await this.store.setString(slot, this.value);
  92. });
  93. describe('get', function () {
  94. beforeEach(async function () {
  95. await this.store.setString(slot, this.value);
  96. });
  97. it('from right slot', async function () {
  98. expect(await this.store.getString(slot)).to.be.equal(this.value);
  99. });
  100. it('from other slot', async function () {
  101. expect(await this.store.getString(otherSlot)).to.be.equal('');
  102. });
  103. });
  104. });
  105. describe('string storage pointer', function () {
  106. beforeEach(async function () {
  107. this.value = 'lorem ipsum';
  108. });
  109. it('set', async function () {
  110. await this.store.setStringStorage(slot, this.value);
  111. });
  112. describe('get', function () {
  113. beforeEach(async function () {
  114. await this.store.setStringStorage(slot, this.value);
  115. });
  116. it('from right slot', async function () {
  117. expect(await this.store.stringMap(slot)).to.be.equal(this.value);
  118. expect(await this.store.getStringStorage(slot)).to.be.equal(this.value);
  119. });
  120. it('from other slot', async function () {
  121. expect(await this.store.stringMap(otherSlot)).to.be.equal('');
  122. expect(await this.store.getStringStorage(otherSlot)).to.be.equal('');
  123. });
  124. });
  125. });
  126. describe('bytes storage slot', function () {
  127. beforeEach(async function () {
  128. this.value = web3.utils.randomHex(128);
  129. });
  130. it('set', async function () {
  131. await this.store.setBytes(slot, this.value);
  132. });
  133. describe('get', function () {
  134. beforeEach(async function () {
  135. await this.store.setBytes(slot, this.value);
  136. });
  137. it('from right slot', async function () {
  138. expect(await this.store.getBytes(slot)).to.be.equal(this.value);
  139. });
  140. it('from other slot', async function () {
  141. expect(await this.store.getBytes(otherSlot)).to.be.equal(null);
  142. });
  143. });
  144. });
  145. describe('bytes storage pointer', function () {
  146. beforeEach(async function () {
  147. this.value = web3.utils.randomHex(128);
  148. });
  149. it('set', async function () {
  150. await this.store.setBytesStorage(slot, this.value);
  151. });
  152. describe('get', function () {
  153. beforeEach(async function () {
  154. await this.store.setBytesStorage(slot, this.value);
  155. });
  156. it('from right slot', async function () {
  157. expect(await this.store.bytesMap(slot)).to.be.equal(this.value);
  158. expect(await this.store.getBytesStorage(slot)).to.be.equal(this.value);
  159. });
  160. it('from other slot', async function () {
  161. expect(await this.store.bytesMap(otherSlot)).to.be.equal(null);
  162. expect(await this.store.getBytesStorage(otherSlot)).to.be.equal(null);
  163. });
  164. });
  165. });
  166. });