ERC165Checker.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. await expect(this.mock.$supportsERC165(this.target)).to.eventually.be.false;
  23. });
  24. it('does not support mock interface via supportsInterface', async function () {
  25. await expect(this.mock.$supportsInterface(this.target, DUMMY_ID)).to.eventually.be.false;
  26. });
  27. it('does not support mock interface via supportsAllInterfaces', async function () {
  28. await expect(this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.eventually.be.false;
  29. });
  30. it('does not support mock interface via getSupportedInterfaces', async function () {
  31. await expect(this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.eventually.deep.equal([false]);
  32. });
  33. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  34. await expect(this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.eventually.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. await expect(this.mock.$supportsERC165(this.target)).to.eventually.be.false;
  43. });
  44. it('does not support mock interface via supportsInterface', async function () {
  45. await expect(this.mock.$supportsInterface(this.target, DUMMY_ID)).to.eventually.be.false;
  46. });
  47. it('does not support mock interface via supportsAllInterfaces', async function () {
  48. await expect(this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.eventually.be.false;
  49. });
  50. it('does not support mock interface via getSupportedInterfaces', async function () {
  51. await expect(this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.eventually.deep.equal([false]);
  52. });
  53. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  54. await expect(this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.eventually.be.true;
  55. });
  56. });
  57. describe('ERC165 revert on invalid interface', function () {
  58. beforeEach(async function () {
  59. this.target = await ethers.deployContract('ERC165RevertInvalid', [[DUMMY_ID]]);
  60. });
  61. it('does not support ERC165', async function () {
  62. await expect(this.mock.$supportsERC165(this.target)).to.eventually.be.false;
  63. });
  64. it('does not support mock interface via supportsInterface', async function () {
  65. await expect(this.mock.$supportsInterface(this.target, DUMMY_ID)).to.eventually.be.false;
  66. });
  67. it('does not support mock interface via supportsAllInterfaces', async function () {
  68. await expect(this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.eventually.be.false;
  69. });
  70. it('does not support mock interface via getSupportedInterfaces', async function () {
  71. await expect(this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.eventually.deep.equal([false]);
  72. });
  73. it('support mock interface via supportsERC165InterfaceUnchecked', async function () {
  74. await expect(this.mock.$supportsERC165InterfaceUnchecked(this.target, '0xffffffff')).to.eventually.be.false;
  75. await expect(this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.eventually.be.true;
  76. });
  77. });
  78. describe('ERC165 not supported', function () {
  79. beforeEach(async function () {
  80. this.target = await ethers.deployContract('ERC165NotSupported');
  81. });
  82. it('does not support ERC165', async function () {
  83. await expect(this.mock.$supportsERC165(this.target)).to.eventually.be.false;
  84. });
  85. it('does not support mock interface via supportsInterface', async function () {
  86. await expect(this.mock.$supportsInterface(this.target, DUMMY_ID)).to.eventually.be.false;
  87. });
  88. it('does not support mock interface via supportsAllInterfaces', async function () {
  89. await expect(this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.eventually.be.false;
  90. });
  91. it('does not support mock interface via getSupportedInterfaces', async function () {
  92. await expect(this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.eventually.deep.equal([false]);
  93. });
  94. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  95. await expect(this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.eventually.be.false;
  96. });
  97. });
  98. describe('ERC165 supported', function () {
  99. beforeEach(async function () {
  100. this.target = await ethers.deployContract('ERC165InterfacesSupported', [[]]);
  101. });
  102. it('supports ERC165', async function () {
  103. await expect(this.mock.$supportsERC165(this.target)).to.eventually.be.true;
  104. });
  105. it('does not support mock interface via supportsInterface', async function () {
  106. await expect(this.mock.$supportsInterface(this.target, DUMMY_ID)).to.eventually.be.false;
  107. });
  108. it('does not support mock interface via supportsAllInterfaces', async function () {
  109. await expect(this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.eventually.be.false;
  110. });
  111. it('does not support mock interface via getSupportedInterfaces', async function () {
  112. await expect(this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.eventually.deep.equal([false]);
  113. });
  114. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  115. await expect(this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.eventually.be.false;
  116. });
  117. });
  118. describe('ERC165 and single interface supported', function () {
  119. beforeEach(async function () {
  120. this.target = await ethers.deployContract('ERC165InterfacesSupported', [[DUMMY_ID]]);
  121. });
  122. it('supports ERC165', async function () {
  123. await expect(this.mock.$supportsERC165(this.target)).to.eventually.be.true;
  124. });
  125. it('supports mock interface via supportsInterface', async function () {
  126. await expect(this.mock.$supportsInterface(this.target, DUMMY_ID)).to.eventually.be.true;
  127. });
  128. it('supports mock interface via supportsAllInterfaces', async function () {
  129. await expect(this.mock.$supportsAllInterfaces(this.target, [DUMMY_ID])).to.eventually.be.true;
  130. });
  131. it('supports mock interface via getSupportedInterfaces', async function () {
  132. await expect(this.mock.$getSupportedInterfaces(this.target, [DUMMY_ID])).to.eventually.deep.equal([true]);
  133. });
  134. it('supports mock interface via supportsERC165InterfaceUnchecked', async function () {
  135. await expect(this.mock.$supportsERC165InterfaceUnchecked(this.target, DUMMY_ID)).to.eventually.be.true;
  136. });
  137. });
  138. describe('ERC165 and many interfaces supported', function () {
  139. const supportedInterfaces = [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3];
  140. beforeEach(async function () {
  141. this.target = await ethers.deployContract('ERC165InterfacesSupported', [supportedInterfaces]);
  142. });
  143. it('supports ERC165', async function () {
  144. await expect(this.mock.$supportsERC165(this.target)).to.eventually.be.true;
  145. });
  146. it('supports each interfaceId via supportsInterface', async function () {
  147. for (const interfaceId of supportedInterfaces) {
  148. await expect(this.mock.$supportsInterface(this.target, interfaceId)).to.eventually.be.true;
  149. }
  150. });
  151. it('supports all interfaceIds via supportsAllInterfaces', async function () {
  152. await expect(this.mock.$supportsAllInterfaces(this.target, supportedInterfaces)).to.eventually.be.true;
  153. });
  154. it('supports none of the interfaces queried via supportsAllInterfaces', async function () {
  155. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  156. await expect(this.mock.$supportsAllInterfaces(this.target, interfaceIdsToTest)).to.eventually.be.false;
  157. });
  158. it('supports not all of the interfaces queried via supportsAllInterfaces', async function () {
  159. const interfaceIdsToTest = [...supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  160. await expect(this.mock.$supportsAllInterfaces(this.target, interfaceIdsToTest)).to.eventually.be.false;
  161. });
  162. it('supports all interfaceIds via getSupportedInterfaces', async function () {
  163. await expect(this.mock.$getSupportedInterfaces(this.target, supportedInterfaces)).to.eventually.deep.equal(
  164. supportedInterfaces.map(i => supportedInterfaces.includes(i)),
  165. );
  166. });
  167. it('supports none of the interfaces queried via getSupportedInterfaces', async function () {
  168. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  169. await expect(this.mock.$getSupportedInterfaces(this.target, interfaceIdsToTest)).to.eventually.deep.equal(
  170. interfaceIdsToTest.map(i => supportedInterfaces.includes(i)),
  171. );
  172. });
  173. it('supports not all of the interfaces queried via getSupportedInterfaces', async function () {
  174. const interfaceIdsToTest = [...supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  175. await expect(this.mock.$getSupportedInterfaces(this.target, interfaceIdsToTest)).to.eventually.deep.equal(
  176. interfaceIdsToTest.map(i => supportedInterfaces.includes(i)),
  177. );
  178. });
  179. it('supports each interfaceId via supportsERC165InterfaceUnchecked', async function () {
  180. for (const interfaceId of supportedInterfaces) {
  181. await expect(this.mock.$supportsERC165InterfaceUnchecked(this.target, interfaceId)).to.eventually.be.true;
  182. }
  183. });
  184. });
  185. describe('account address does not support ERC165', function () {
  186. it('does not support ERC165', async function () {
  187. await expect(this.mock.$supportsERC165(DUMMY_ACCOUNT)).to.eventually.be.false;
  188. });
  189. it('does not support mock interface via supportsInterface', async function () {
  190. await expect(this.mock.$supportsInterface(DUMMY_ACCOUNT, DUMMY_ID)).to.eventually.be.false;
  191. });
  192. it('does not support mock interface via supportsAllInterfaces', async function () {
  193. await expect(this.mock.$supportsAllInterfaces(DUMMY_ACCOUNT, [DUMMY_ID])).to.eventually.be.false;
  194. });
  195. it('does not support mock interface via getSupportedInterfaces', async function () {
  196. await expect(this.mock.$getSupportedInterfaces(DUMMY_ACCOUNT, [DUMMY_ID])).to.eventually.deep.equal([false]);
  197. });
  198. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  199. await expect(this.mock.$supportsERC165InterfaceUnchecked(DUMMY_ACCOUNT, DUMMY_ID)).to.eventually.be.false;
  200. });
  201. });
  202. it('Return bomb resistance', async function () {
  203. this.target = await ethers.deployContract('ERC165ReturnBombMock');
  204. const { gasUsed: gasUsed1 } = await this.mock.$supportsInterface.send(this.target, DUMMY_ID).then(tx => tx.wait());
  205. expect(gasUsed1).to.be.lessThan(120_000n); // 3*30k + 21k + some margin
  206. const { gasUsed: gasUsed2 } = await this.mock.$getSupportedInterfaces
  207. .send(this.target, [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3, DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2])
  208. .then(tx => tx.wait());
  209. expect(gasUsed2).to.be.lessThan(250_000n); // (2+5)*30k + 21k + some margin
  210. });
  211. });