ERC165Checker.test.js 12 KB

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