ERC165Checker.test.js 11 KB

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