Initializable.test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const {
  4. bigint: { MAX_UINT64 },
  5. } = require('../../helpers/constants');
  6. describe('Initializable', function () {
  7. describe('basic testing without inheritance', function () {
  8. beforeEach('deploying', async function () {
  9. this.mock = await ethers.deployContract('InitializableMock');
  10. });
  11. describe('before initialize', function () {
  12. it('initializer has not run', async function () {
  13. expect(await this.mock.initializerRan()).to.be.false;
  14. });
  15. it('_initializing returns false before initialization', async function () {
  16. expect(await this.mock.isInitializing()).to.be.false;
  17. });
  18. });
  19. describe('after initialize', function () {
  20. beforeEach('initializing', async function () {
  21. await this.mock.initialize();
  22. });
  23. it('initializer has run', async function () {
  24. expect(await this.mock.initializerRan()).to.be.true;
  25. });
  26. it('_initializing returns false after initialization', async function () {
  27. expect(await this.mock.isInitializing()).to.be.false;
  28. });
  29. it('initializer does not run again', async function () {
  30. await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  31. });
  32. });
  33. describe('nested under an initializer', function () {
  34. it('initializer modifier reverts', async function () {
  35. await expect(this.mock.initializerNested()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  36. });
  37. it('onlyInitializing modifier succeeds', async function () {
  38. await this.mock.onlyInitializingNested();
  39. expect(await this.mock.onlyInitializingRan()).to.be.true;
  40. });
  41. });
  42. it('cannot call onlyInitializable function outside the scope of an initializable function', async function () {
  43. await expect(this.mock.initializeOnlyInitializing()).to.be.revertedWithCustomError(this.mock, 'NotInitializing');
  44. });
  45. });
  46. it('nested initializer can run during construction', async function () {
  47. const mock = await ethers.deployContract('ConstructorInitializableMock');
  48. expect(await mock.initializerRan()).to.be.true;
  49. expect(await mock.onlyInitializingRan()).to.be.true;
  50. });
  51. it('multiple constructor levels can be initializers', async function () {
  52. const mock = await ethers.deployContract('ChildConstructorInitializableMock');
  53. expect(await mock.initializerRan()).to.be.true;
  54. expect(await mock.childInitializerRan()).to.be.true;
  55. expect(await mock.onlyInitializingRan()).to.be.true;
  56. });
  57. describe('reinitialization', function () {
  58. beforeEach('deploying', async function () {
  59. this.mock = await ethers.deployContract('ReinitializerMock');
  60. });
  61. it('can reinitialize', async function () {
  62. expect(await this.mock.counter()).to.equal(0n);
  63. await this.mock.initialize();
  64. expect(await this.mock.counter()).to.equal(1n);
  65. await this.mock.reinitialize(2);
  66. expect(await this.mock.counter()).to.equal(2n);
  67. await this.mock.reinitialize(3);
  68. expect(await this.mock.counter()).to.equal(3n);
  69. });
  70. it('can jump multiple steps', async function () {
  71. expect(await this.mock.counter()).to.equal(0n);
  72. await this.mock.initialize();
  73. expect(await this.mock.counter()).to.equal(1n);
  74. await this.mock.reinitialize(128);
  75. expect(await this.mock.counter()).to.equal(2n);
  76. });
  77. it('cannot nest reinitializers', async function () {
  78. expect(await this.mock.counter()).to.equal(0n);
  79. await expect(this.mock.nestedReinitialize(2, 2)).to.be.revertedWithCustomError(
  80. this.mock,
  81. 'InvalidInitialization',
  82. );
  83. await expect(this.mock.nestedReinitialize(2, 3)).to.be.revertedWithCustomError(
  84. this.mock,
  85. 'InvalidInitialization',
  86. );
  87. await expect(this.mock.nestedReinitialize(3, 2)).to.be.revertedWithCustomError(
  88. this.mock,
  89. 'InvalidInitialization',
  90. );
  91. });
  92. it('can chain reinitializers', async function () {
  93. expect(await this.mock.counter()).to.equal(0n);
  94. await this.mock.chainReinitialize(2, 3);
  95. expect(await this.mock.counter()).to.equal(2n);
  96. });
  97. it('_getInitializedVersion returns right version', async function () {
  98. await this.mock.initialize();
  99. expect(await this.mock.getInitializedVersion()).to.equal(1n);
  100. await this.mock.reinitialize(12);
  101. expect(await this.mock.getInitializedVersion()).to.equal(12n);
  102. });
  103. describe('contract locking', function () {
  104. it('prevents initialization', async function () {
  105. await this.mock.disableInitializers();
  106. await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  107. });
  108. it('prevents re-initialization', async function () {
  109. await this.mock.disableInitializers();
  110. await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  111. });
  112. it('can lock contract after initialization', async function () {
  113. await this.mock.initialize();
  114. await this.mock.disableInitializers();
  115. await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
  116. });
  117. });
  118. });
  119. describe('events', function () {
  120. it('constructor initialization emits event', async function () {
  121. const mock = await ethers.deployContract('ConstructorInitializableMock');
  122. await expect(mock.deploymentTransaction()).to.emit(mock, 'Initialized').withArgs(1n);
  123. });
  124. it('initialization emits event', async function () {
  125. const mock = await ethers.deployContract('ReinitializerMock');
  126. await expect(mock.initialize()).to.emit(mock, 'Initialized').withArgs(1n);
  127. });
  128. it('reinitialization emits event', async function () {
  129. const mock = await ethers.deployContract('ReinitializerMock');
  130. await expect(mock.reinitialize(128)).to.emit(mock, 'Initialized').withArgs(128n);
  131. });
  132. it('chained reinitialization emits multiple events', async function () {
  133. const mock = await ethers.deployContract('ReinitializerMock');
  134. await expect(mock.chainReinitialize(2, 3))
  135. .to.emit(mock, 'Initialized')
  136. .withArgs(2n)
  137. .to.emit(mock, 'Initialized')
  138. .withArgs(3n);
  139. });
  140. });
  141. describe('complex testing with inheritance', function () {
  142. const mother = 12n;
  143. const gramps = '56';
  144. const father = 34n;
  145. const child = 78n;
  146. beforeEach('deploying', async function () {
  147. this.mock = await ethers.deployContract('SampleChild');
  148. await this.mock.initialize(mother, gramps, father, child);
  149. });
  150. it('initializes human', async function () {
  151. expect(await this.mock.isHuman()).to.be.true;
  152. });
  153. it('initializes mother', async function () {
  154. expect(await this.mock.mother()).to.equal(mother);
  155. });
  156. it('initializes gramps', async function () {
  157. expect(await this.mock.gramps()).to.equal(gramps);
  158. });
  159. it('initializes father', async function () {
  160. expect(await this.mock.father()).to.equal(father);
  161. });
  162. it('initializes child', async function () {
  163. expect(await this.mock.child()).to.equal(child);
  164. });
  165. });
  166. describe('disabling initialization', function () {
  167. it('old and new patterns in bad sequence', async function () {
  168. const DisableBad1 = await ethers.getContractFactory('DisableBad1');
  169. await expect(DisableBad1.deploy()).to.be.revertedWithCustomError(DisableBad1, 'InvalidInitialization');
  170. const DisableBad2 = await ethers.getContractFactory('DisableBad2');
  171. await expect(DisableBad2.deploy()).to.be.revertedWithCustomError(DisableBad2, 'InvalidInitialization');
  172. });
  173. it('old and new patterns in good sequence', async function () {
  174. const ok = await ethers.deployContract('DisableOk');
  175. await expect(ok.deploymentTransaction())
  176. .to.emit(ok, 'Initialized')
  177. .withArgs(1n)
  178. .to.emit(ok, 'Initialized')
  179. .withArgs(MAX_UINT64);
  180. });
  181. });
  182. });