ERC165Checker.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ERC165Checker = artifacts.require('$ERC165Checker');
  4. const ERC165Storage = artifacts.require('$ERC165Storage');
  5. const ERC165MissingData = artifacts.require('ERC165MissingData');
  6. const ERC165MaliciousData = artifacts.require('ERC165MaliciousData');
  7. const ERC165NotSupported = artifacts.require('ERC165NotSupported');
  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 () {
  16. beforeEach(async function () {
  17. this.mock = await ERC165Checker.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 ERC165Storage.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 ERC165Storage.new();
  126. await this.target.$_registerInterface(DUMMY_ID);
  127. });
  128. it('supports ERC165', async function () {
  129. const supported = await this.mock.$supportsERC165(this.target.address);
  130. expect(supported).to.equal(true);
  131. });
  132. it('supports mock interface via supportsInterface', async function () {
  133. const supported = await this.mock.$supportsInterface(this.target.address, DUMMY_ID);
  134. expect(supported).to.equal(true);
  135. });
  136. it('supports mock interface via supportsAllInterfaces', async function () {
  137. const supported = await this.mock.$supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  138. expect(supported).to.equal(true);
  139. });
  140. it('supports mock interface via getSupportedInterfaces', async function () {
  141. const supported = await this.mock.$getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  142. expect(supported.length).to.equal(1);
  143. expect(supported[0]).to.equal(true);
  144. });
  145. it('supports mock interface via supportsERC165InterfaceUnchecked', async function () {
  146. const supported = await this.mock.$supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  147. expect(supported).to.equal(true);
  148. });
  149. });
  150. context('ERC165 and many interfaces supported', function () {
  151. beforeEach(async function () {
  152. this.supportedInterfaces = [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3];
  153. this.target = await ERC165Storage.new();
  154. await Promise.all(this.supportedInterfaces.map(interfaceId => this.target.$_registerInterface(interfaceId)));
  155. });
  156. it('supports ERC165', async function () {
  157. const supported = await this.mock.$supportsERC165(this.target.address);
  158. expect(supported).to.equal(true);
  159. });
  160. it('supports each interfaceId via supportsInterface', async function () {
  161. for (const interfaceId of this.supportedInterfaces) {
  162. const supported = await this.mock.$supportsInterface(this.target.address, interfaceId);
  163. expect(supported).to.equal(true);
  164. }
  165. });
  166. it('supports all interfaceIds via supportsAllInterfaces', async function () {
  167. const supported = await this.mock.$supportsAllInterfaces(this.target.address, this.supportedInterfaces);
  168. expect(supported).to.equal(true);
  169. });
  170. it('supports none of the interfaces queried via supportsAllInterfaces', async function () {
  171. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  172. const supported = await this.mock.$supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  173. expect(supported).to.equal(false);
  174. });
  175. it('supports not all of the interfaces queried via supportsAllInterfaces', async function () {
  176. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  177. const supported = await this.mock.$supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  178. expect(supported).to.equal(false);
  179. });
  180. it('supports all interfaceIds via getSupportedInterfaces', async function () {
  181. const supported = await this.mock.$getSupportedInterfaces(this.target.address, this.supportedInterfaces);
  182. expect(supported.length).to.equal(3);
  183. expect(supported[0]).to.equal(true);
  184. expect(supported[1]).to.equal(true);
  185. expect(supported[2]).to.equal(true);
  186. });
  187. it('supports none of the interfaces queried via getSupportedInterfaces', async function () {
  188. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  189. const supported = await this.mock.$getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  190. expect(supported.length).to.equal(2);
  191. expect(supported[0]).to.equal(false);
  192. expect(supported[1]).to.equal(false);
  193. });
  194. it('supports not all of the interfaces queried via getSupportedInterfaces', async function () {
  195. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  196. const supported = await this.mock.$getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  197. expect(supported.length).to.equal(4);
  198. expect(supported[0]).to.equal(true);
  199. expect(supported[1]).to.equal(true);
  200. expect(supported[2]).to.equal(true);
  201. expect(supported[3]).to.equal(false);
  202. });
  203. it('supports each interfaceId via supportsERC165InterfaceUnchecked', async function () {
  204. for (const interfaceId of this.supportedInterfaces) {
  205. const supported = await this.mock.$supportsERC165InterfaceUnchecked(this.target.address, interfaceId);
  206. expect(supported).to.equal(true);
  207. }
  208. });
  209. });
  210. context('account address does not support ERC165', function () {
  211. it('does not support ERC165', async function () {
  212. const supported = await this.mock.$supportsERC165(DUMMY_ACCOUNT);
  213. expect(supported).to.equal(false);
  214. });
  215. it('does not support mock interface via supportsInterface', async function () {
  216. const supported = await this.mock.$supportsInterface(DUMMY_ACCOUNT, DUMMY_ID);
  217. expect(supported).to.equal(false);
  218. });
  219. it('does not support mock interface via supportsAllInterfaces', async function () {
  220. const supported = await this.mock.$supportsAllInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  221. expect(supported).to.equal(false);
  222. });
  223. it('does not support mock interface via getSupportedInterfaces', async function () {
  224. const supported = await this.mock.$getSupportedInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  225. expect(supported.length).to.equal(1);
  226. expect(supported[0]).to.equal(false);
  227. });
  228. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  229. const supported = await this.mock.$supportsERC165InterfaceUnchecked(DUMMY_ACCOUNT, DUMMY_ID);
  230. expect(supported).to.equal(false);
  231. });
  232. });
  233. it('Return bomb resistance', async function () {
  234. this.target = await ERC165ReturnBombMock.new();
  235. const tx1 = await this.mock.$supportsInterface.sendTransaction(this.target.address, DUMMY_ID);
  236. expect(tx1.receipt.gasUsed).to.be.lessThan(120000); // 3*30k + 21k + some margin
  237. const tx2 = await this.mock.$getSupportedInterfaces.sendTransaction(this.target.address, [
  238. DUMMY_ID,
  239. DUMMY_ID_2,
  240. DUMMY_ID_3,
  241. DUMMY_UNSUPPORTED_ID,
  242. DUMMY_UNSUPPORTED_ID_2,
  243. ]);
  244. expect(tx2.receipt.gasUsed).to.be.lessThan(250000); // (2+5)*30k + 21k + some margin
  245. });
  246. });