ERC165Checker.test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const DUMMY_ID = '0xdeadbeef';
  5. const DUMMY_ID_2 = '0xcafebabe';
  6. const DUMMY_ID_3 = '0xdecafbad';
  7. const DUMMY_UNSUPPORTED_ID = '0xbaddcafe';
  8. const DUMMY_UNSUPPORTED_ID_2 = '0xbaadcafe';
  9. const DUMMY_ACCOUNT = '0x1111111111111111111111111111111111111111';
  10. async function fixture() {
  11. return { mock: await ethers.deployContract('$ERC165Checker') };
  12. }
  13. describe('ERC165Checker', function () {
  14. beforeEach(async function () {
  15. Object.assign(this, await loadFixture(fixture));
  16. });
  17. describe('ERC165 missing return data', function () {
  18. before(async function () {
  19. this.target = await ethers.deployContract('ERC165MissingData');
  20. });
  21. it('does not support ERC165', async function () {
  22. expect(await this.mock.$supportsERC165(this.target)).to.be.false;
  23. });
  24. it('does not support mock interface via supportsInterface', async function () {
  25. expect(await this.mock.$supportsInterface(this.target, DUMMY_ID)).to.be.false;
  26. });
  27. it('does not support mock interface via supportsAllInterfaces', async function () {
  28. expect(await this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.be.false;
  29. });
  30. it('does not support mock interface via getSupportedInterfaces', async function () {
  31. expect(await this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.deep.equal([false]);
  32. });
  33. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  34. expect(await this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.be.false;
  35. });
  36. });
  37. describe('ERC165 malicious return data', function () {
  38. beforeEach(async function () {
  39. this.target = await ethers.deployContract('ERC165MaliciousData');
  40. });
  41. it('does not support ERC165', async function () {
  42. expect(await this.mock.$supportsERC165(this.target)).to.be.false;
  43. });
  44. it('does not support mock interface via supportsInterface', async function () {
  45. expect(await this.mock.$supportsInterface(this.target, DUMMY_ID)).to.be.false;
  46. });
  47. it('does not support mock interface via supportsAllInterfaces', async function () {
  48. expect(await this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.be.false;
  49. });
  50. it('does not support mock interface via getSupportedInterfaces', async function () {
  51. expect(await this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.deep.equal([false]);
  52. });
  53. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  54. expect(await this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.be.true;
  55. });
  56. });
  57. describe('ERC165 not supported', function () {
  58. beforeEach(async function () {
  59. this.target = await ethers.deployContract('ERC165NotSupported');
  60. });
  61. it('does not support ERC165', async function () {
  62. expect(await this.mock.$supportsERC165(this.target)).to.be.false;
  63. });
  64. it('does not support mock interface via supportsInterface', async function () {
  65. expect(await this.mock.$supportsInterface(this.target, DUMMY_ID)).to.be.false;
  66. });
  67. it('does not support mock interface via supportsAllInterfaces', async function () {
  68. expect(await this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.be.false;
  69. });
  70. it('does not support mock interface via getSupportedInterfaces', async function () {
  71. expect(await this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.deep.equal([false]);
  72. });
  73. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  74. expect(await this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.be.false;
  75. });
  76. });
  77. describe('ERC165 supported', function () {
  78. beforeEach(async function () {
  79. this.target = await ethers.deployContract('ERC165InterfacesSupported', [[]]);
  80. });
  81. it('supports ERC165', async function () {
  82. expect(await this.mock.$supportsERC165(this.target)).to.be.true;
  83. });
  84. it('does not support mock interface via supportsInterface', async function () {
  85. expect(await this.mock.$supportsInterface(this.target, DUMMY_ID)).to.be.false;
  86. });
  87. it('does not support mock interface via supportsAllInterfaces', async function () {
  88. expect(await this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.be.false;
  89. });
  90. it('does not support mock interface via getSupportedInterfaces', async function () {
  91. expect(await this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.deep.equal([false]);
  92. });
  93. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  94. expect(await this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.be.false;
  95. });
  96. });
  97. describe('ERC165 and single interface supported', function () {
  98. beforeEach(async function () {
  99. this.target = await ethers.deployContract('ERC165InterfacesSupported', [[DUMMY_ID]]);
  100. });
  101. it('supports ERC165', async function () {
  102. expect(await this.mock.$supportsERC165(this.target)).to.be.true;
  103. });
  104. it('supports mock interface via supportsInterface', async function () {
  105. expect(await this.mock.$supportsInterface(this.target, DUMMY_ID)).to.be.true;
  106. });
  107. it('supports mock interface via supportsAllInterfaces', async function () {
  108. expect(await this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.be.true;
  109. });
  110. it('supports mock interface via getSupportedInterfaces', async function () {
  111. expect(await this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.deep.equal([true]);
  112. });
  113. it('supports mock interface via supportsERC165InterfaceUnchecked', async function () {
  114. expect(await this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.be.true;
  115. });
  116. });
  117. describe('ERC165 and many interfaces supported', function () {
  118. const supportedInterfaces = [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3];
  119. beforeEach(async function () {
  120. this.target = await ethers.deployContract('ERC165InterfacesSupported', [supportedInterfaces]);
  121. });
  122. it('supports ERC165', async function () {
  123. expect(await this.mock.$supportsERC165(this.target)).to.be.true;
  124. });
  125. it('supports each interfaceId via supportsInterface', async function () {
  126. for (const interfaceId of supportedInterfaces) {
  127. expect(await this.mock.$supportsInterface(this.target, interfaceId)).to.be.true;
  128. }
  129. });
  130. it('supports all interfaceIds via supportsAllInterfaces', async function () {
  131. expect(await this.mock.$supportsAllInterfaces(this.target, supportedInterfaces)).to.be.true;
  132. });
  133. it('supports none of the interfaces queried via supportsAllInterfaces', async function () {
  134. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  135. expect(await this.mock.$supportsAllInterfaces(this.target, interfaceIdsToTest)).to.be.false;
  136. });
  137. it('supports not all of the interfaces queried via supportsAllInterfaces', async function () {
  138. const interfaceIdsToTest = [...supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  139. expect(await this.mock.$supportsAllInterfaces(this.target, interfaceIdsToTest)).to.be.false;
  140. });
  141. it('supports all interfaceIds via getSupportedInterfaces', async function () {
  142. expect(await this.mock.$getSupportedInterfaces(this.target, supportedInterfaces)).to.deep.equal(
  143. supportedInterfaces.map(i => supportedInterfaces.includes(i)),
  144. );
  145. });
  146. it('supports none of the interfaces queried via getSupportedInterfaces', async function () {
  147. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  148. expect(await this.mock.$getSupportedInterfaces(this.target, interfaceIdsToTest)).to.deep.equal(
  149. interfaceIdsToTest.map(i => supportedInterfaces.includes(i)),
  150. );
  151. });
  152. it('supports not all of the interfaces queried via getSupportedInterfaces', async function () {
  153. const interfaceIdsToTest = [...supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  154. expect(await this.mock.$getSupportedInterfaces(this.target, interfaceIdsToTest)).to.deep.equal(
  155. interfaceIdsToTest.map(i => supportedInterfaces.includes(i)),
  156. );
  157. });
  158. it('supports each interfaceId via supportsERC165InterfaceUnchecked', async function () {
  159. for (const interfaceId of supportedInterfaces) {
  160. expect(await this.mock.$supportsERC165InterfaceUnchecked(this.target, interfaceId)).to.be.true;
  161. }
  162. });
  163. });
  164. describe('account address does not support ERC165', function () {
  165. it('does not support ERC165', async function () {
  166. expect(await this.mock.$supportsERC165(DUMMY_ACCOUNT)).to.be.false;
  167. });
  168. it('does not support mock interface via supportsInterface', async function () {
  169. expect(await this.mock.$supportsInterface(DUMMY_ACCOUNT, DUMMY_ID)).to.be.false;
  170. });
  171. it('does not support mock interface via supportsAllInterfaces', async function () {
  172. expect(await this.mock.$supportsAllInterfaces(DUMMY_ACCOUNT, [DUMMY_ID])).to.be.false;
  173. });
  174. it('does not support mock interface via getSupportedInterfaces', async function () {
  175. expect(await this.mock.$getSupportedInterfaces(DUMMY_ACCOUNT, [DUMMY_ID])).to.deep.equal([false]);
  176. });
  177. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  178. expect(await this.mock.$supportsERC165InterfaceUnchecked(DUMMY_ACCOUNT, DUMMY_ID)).to.be.false;
  179. });
  180. });
  181. it('Return bomb resistance', async function () {
  182. this.target = await ethers.deployContract('ERC165ReturnBombMock');
  183. const { gasUsed: gasUsed1 } = await this.mock.$supportsInterface.send(this.target, DUMMY_ID).then(tx => tx.wait());
  184. expect(gasUsed1).to.be.lessThan(120_000n); // 3*30k + 21k + some margin
  185. const { gasUsed: gasUsed2 } = await this.mock.$getSupportedInterfaces
  186. .send(this.target, [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3, DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2])
  187. .then(tx => tx.wait());
  188. expect(gasUsed2).to.be.lessThan(250_000n); // (2+5)*30k + 21k + some margin
  189. });
  190. });