ERC165Checker.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  41. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  42. expect(supported).to.equal(false);
  43. });
  44. });
  45. context('ERC165 malicious return data', function () {
  46. beforeEach(async function () {
  47. this.target = await ERC165MaliciousData.new();
  48. });
  49. it('does not support ERC165', async function () {
  50. const supported = await this.mock.supportsERC165(this.target.address);
  51. expect(supported).to.equal(false);
  52. });
  53. it('does not support mock interface via supportsInterface', async function () {
  54. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  55. expect(supported).to.equal(false);
  56. });
  57. it('does not support mock interface via supportsAllInterfaces', async function () {
  58. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  59. expect(supported).to.equal(false);
  60. });
  61. it('does not support mock interface via getSupportedInterfaces', async function () {
  62. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  63. expect(supported.length).to.equal(1);
  64. expect(supported[0]).to.equal(false);
  65. });
  66. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  67. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  68. expect(supported).to.equal(true);
  69. });
  70. });
  71. context('ERC165 not supported', function () {
  72. beforeEach(async function () {
  73. this.target = await ERC165NotSupported.new();
  74. });
  75. it('does not support ERC165', async function () {
  76. const supported = await this.mock.supportsERC165(this.target.address);
  77. expect(supported).to.equal(false);
  78. });
  79. it('does not support mock interface via supportsInterface', async function () {
  80. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  81. expect(supported).to.equal(false);
  82. });
  83. it('does not support mock interface via supportsAllInterfaces', async function () {
  84. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  85. expect(supported).to.equal(false);
  86. });
  87. it('does not support mock interface via getSupportedInterfaces', async function () {
  88. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  89. expect(supported.length).to.equal(1);
  90. expect(supported[0]).to.equal(false);
  91. });
  92. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  93. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  94. expect(supported).to.equal(false);
  95. });
  96. });
  97. context('ERC165 supported', function () {
  98. beforeEach(async function () {
  99. this.target = await ERC165InterfacesSupported.new([]);
  100. });
  101. it('supports ERC165', async function () {
  102. const supported = await this.mock.supportsERC165(this.target.address);
  103. expect(supported).to.equal(true);
  104. });
  105. it('does not support mock interface via supportsInterface', async function () {
  106. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  107. expect(supported).to.equal(false);
  108. });
  109. it('does not support mock interface via supportsAllInterfaces', async function () {
  110. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  111. expect(supported).to.equal(false);
  112. });
  113. it('does not support mock interface via getSupportedInterfaces', async function () {
  114. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  115. expect(supported.length).to.equal(1);
  116. expect(supported[0]).to.equal(false);
  117. });
  118. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  119. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  120. expect(supported).to.equal(false);
  121. });
  122. });
  123. context('ERC165 and single interface supported', function () {
  124. beforeEach(async function () {
  125. this.target = await ERC165InterfacesSupported.new([DUMMY_ID]);
  126. });
  127. it('supports ERC165', async function () {
  128. const supported = await this.mock.supportsERC165(this.target.address);
  129. expect(supported).to.equal(true);
  130. });
  131. it('supports mock interface via supportsInterface', async function () {
  132. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  133. expect(supported).to.equal(true);
  134. });
  135. it('supports mock interface via supportsAllInterfaces', async function () {
  136. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  137. expect(supported).to.equal(true);
  138. });
  139. it('supports mock interface via getSupportedInterfaces', async function () {
  140. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  141. expect(supported.length).to.equal(1);
  142. expect(supported[0]).to.equal(true);
  143. });
  144. it('supports mock interface via supportsERC165InterfaceUnchecked', async function () {
  145. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  146. expect(supported).to.equal(true);
  147. });
  148. });
  149. context('ERC165 and many interfaces supported', function () {
  150. beforeEach(async function () {
  151. this.supportedInterfaces = [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3];
  152. this.target = await ERC165InterfacesSupported.new(this.supportedInterfaces);
  153. });
  154. it('supports ERC165', async function () {
  155. const supported = await this.mock.supportsERC165(this.target.address);
  156. expect(supported).to.equal(true);
  157. });
  158. it('supports each interfaceId via supportsInterface', async function () {
  159. for (const interfaceId of this.supportedInterfaces) {
  160. const supported = await this.mock.supportsInterface(this.target.address, interfaceId);
  161. expect(supported).to.equal(true);
  162. };
  163. });
  164. it('supports all interfaceIds via supportsAllInterfaces', async function () {
  165. const supported = await this.mock.supportsAllInterfaces(this.target.address, this.supportedInterfaces);
  166. expect(supported).to.equal(true);
  167. });
  168. it('supports none of the interfaces queried via supportsAllInterfaces', async function () {
  169. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  170. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  171. expect(supported).to.equal(false);
  172. });
  173. it('supports not all of the interfaces queried via supportsAllInterfaces', async function () {
  174. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  175. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  176. expect(supported).to.equal(false);
  177. });
  178. it('supports all interfaceIds via getSupportedInterfaces', async function () {
  179. const supported = await this.mock.getSupportedInterfaces(this.target.address, this.supportedInterfaces);
  180. expect(supported.length).to.equal(3);
  181. expect(supported[0]).to.equal(true);
  182. expect(supported[1]).to.equal(true);
  183. expect(supported[2]).to.equal(true);
  184. });
  185. it('supports none of the interfaces queried via getSupportedInterfaces', async function () {
  186. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  187. const supported = await this.mock.getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  188. expect(supported.length).to.equal(2);
  189. expect(supported[0]).to.equal(false);
  190. expect(supported[1]).to.equal(false);
  191. });
  192. it('supports not all of the interfaces queried via getSupportedInterfaces', async function () {
  193. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  194. const supported = await this.mock.getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  195. expect(supported.length).to.equal(4);
  196. expect(supported[0]).to.equal(true);
  197. expect(supported[1]).to.equal(true);
  198. expect(supported[2]).to.equal(true);
  199. expect(supported[3]).to.equal(false);
  200. });
  201. it('supports each interfaceId via supportsERC165InterfaceUnchecked', async function () {
  202. for (const interfaceId of this.supportedInterfaces) {
  203. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, interfaceId);
  204. expect(supported).to.equal(true);
  205. };
  206. });
  207. });
  208. context('account address does not support ERC165', function () {
  209. it('does not support ERC165', async function () {
  210. const supported = await this.mock.supportsERC165(DUMMY_ACCOUNT);
  211. expect(supported).to.equal(false);
  212. });
  213. it('does not support mock interface via supportsInterface', async function () {
  214. const supported = await this.mock.supportsInterface(DUMMY_ACCOUNT, DUMMY_ID);
  215. expect(supported).to.equal(false);
  216. });
  217. it('does not support mock interface via supportsAllInterfaces', async function () {
  218. const supported = await this.mock.supportsAllInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  219. expect(supported).to.equal(false);
  220. });
  221. it('does not support mock interface via getSupportedInterfaces', async function () {
  222. const supported = await this.mock.getSupportedInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  223. expect(supported.length).to.equal(1);
  224. expect(supported[0]).to.equal(false);
  225. });
  226. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  227. const supported = await this.mock.supportsERC165InterfaceUnchecked(DUMMY_ACCOUNT, DUMMY_ID);
  228. expect(supported).to.equal(false);
  229. });
  230. });
  231. it('Return bomb resistance', async function () {
  232. this.target = await ERC165ReturnBombMock.new();
  233. const tx1 = await this.mock.supportsInterface.sendTransaction(this.target.address, DUMMY_ID);
  234. expect(tx1.receipt.gasUsed).to.be.lessThan(120000); // 3*30k + 21k + some margin
  235. const tx2 = await this.mock.getSupportedInterfaces.sendTransaction(
  236. this.target.address,
  237. [
  238. DUMMY_ID,
  239. DUMMY_ID_2,
  240. DUMMY_ID_3,
  241. DUMMY_UNSUPPORTED_ID,
  242. DUMMY_UNSUPPORTED_ID_2,
  243. ],
  244. );
  245. expect(tx2.receipt.gasUsed).to.be.lessThan(250000); // (2+5)*30k + 21k + some margin
  246. });
  247. });