ERC165Checker.test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. });
  40. context('ERC165 malicious return data', function () {
  41. beforeEach(async function () {
  42. this.target = await ERC165MaliciousData.new();
  43. });
  44. it('does not support ERC165', async function () {
  45. const supported = await this.mock.supportsERC165(this.target.address);
  46. expect(supported).to.equal(false);
  47. });
  48. it('does not support mock interface via supportsInterface', async function () {
  49. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  50. expect(supported).to.equal(false);
  51. });
  52. it('does not support mock interface via supportsAllInterfaces', async function () {
  53. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  54. expect(supported).to.equal(false);
  55. });
  56. it('does not support mock interface via getSupportedInterfaces', async function () {
  57. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  58. expect(supported.length).to.equal(1);
  59. expect(supported[0]).to.equal(false);
  60. });
  61. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  62. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  63. expect(supported).to.equal(true);
  64. });
  65. });
  66. context('ERC165 not supported', function () {
  67. beforeEach(async function () {
  68. this.target = await ERC165NotSupported.new();
  69. });
  70. it('does not support ERC165', async function () {
  71. const supported = await this.mock.supportsERC165(this.target.address);
  72. expect(supported).to.equal(false);
  73. });
  74. it('does not support mock interface via supportsInterface', async function () {
  75. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  76. expect(supported).to.equal(false);
  77. });
  78. it('does not support mock interface via supportsAllInterfaces', async function () {
  79. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  80. expect(supported).to.equal(false);
  81. });
  82. it('does not support mock interface via getSupportedInterfaces', async function () {
  83. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  84. expect(supported.length).to.equal(1);
  85. expect(supported[0]).to.equal(false);
  86. });
  87. });
  88. context('ERC165 supported', function () {
  89. beforeEach(async function () {
  90. this.target = await ERC165InterfacesSupported.new([]);
  91. });
  92. it('supports ERC165', async function () {
  93. const supported = await this.mock.supportsERC165(this.target.address);
  94. expect(supported).to.equal(true);
  95. });
  96. it('does not support mock interface via supportsInterface', async function () {
  97. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  98. expect(supported).to.equal(false);
  99. });
  100. it('does not support mock interface via supportsAllInterfaces', async function () {
  101. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  102. expect(supported).to.equal(false);
  103. });
  104. it('does not support mock interface via getSupportedInterfaces', async function () {
  105. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  106. expect(supported.length).to.equal(1);
  107. expect(supported[0]).to.equal(false);
  108. });
  109. });
  110. context('ERC165 and single interface supported', function () {
  111. beforeEach(async function () {
  112. this.target = await ERC165InterfacesSupported.new([DUMMY_ID]);
  113. });
  114. it('supports ERC165', async function () {
  115. const supported = await this.mock.supportsERC165(this.target.address);
  116. expect(supported).to.equal(true);
  117. });
  118. it('supports mock interface via supportsInterface', async function () {
  119. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  120. expect(supported).to.equal(true);
  121. });
  122. it('supports mock interface via supportsAllInterfaces', async function () {
  123. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  124. expect(supported).to.equal(true);
  125. });
  126. it('supports mock interface via getSupportedInterfaces', async function () {
  127. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  128. expect(supported.length).to.equal(1);
  129. expect(supported[0]).to.equal(true);
  130. });
  131. });
  132. context('ERC165 and many interfaces supported', function () {
  133. beforeEach(async function () {
  134. this.supportedInterfaces = [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3];
  135. this.target = await ERC165InterfacesSupported.new(this.supportedInterfaces);
  136. });
  137. it('supports ERC165', async function () {
  138. const supported = await this.mock.supportsERC165(this.target.address);
  139. expect(supported).to.equal(true);
  140. });
  141. it('supports each interfaceId via supportsInterface', async function () {
  142. for (const interfaceId of this.supportedInterfaces) {
  143. const supported = await this.mock.supportsInterface(this.target.address, interfaceId);
  144. expect(supported).to.equal(true);
  145. };
  146. });
  147. it('supports all interfaceIds via supportsAllInterfaces', async function () {
  148. const supported = await this.mock.supportsAllInterfaces(this.target.address, this.supportedInterfaces);
  149. expect(supported).to.equal(true);
  150. });
  151. it('supports none of the interfaces queried via supportsAllInterfaces', async function () {
  152. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  153. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  154. expect(supported).to.equal(false);
  155. });
  156. it('supports not all of the interfaces queried via supportsAllInterfaces', async function () {
  157. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  158. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  159. expect(supported).to.equal(false);
  160. });
  161. it('supports all interfaceIds via getSupportedInterfaces', async function () {
  162. const supported = await this.mock.getSupportedInterfaces(this.target.address, this.supportedInterfaces);
  163. expect(supported.length).to.equal(3);
  164. expect(supported[0]).to.equal(true);
  165. expect(supported[1]).to.equal(true);
  166. expect(supported[2]).to.equal(true);
  167. });
  168. it('supports none of the interfaces queried via getSupportedInterfaces', async function () {
  169. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  170. const supported = await this.mock.getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  171. expect(supported.length).to.equal(2);
  172. expect(supported[0]).to.equal(false);
  173. expect(supported[1]).to.equal(false);
  174. });
  175. it('supports not all of the interfaces queried via getSupportedInterfaces', async function () {
  176. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  177. const supported = await this.mock.getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  178. expect(supported.length).to.equal(4);
  179. expect(supported[0]).to.equal(true);
  180. expect(supported[1]).to.equal(true);
  181. expect(supported[2]).to.equal(true);
  182. expect(supported[3]).to.equal(false);
  183. });
  184. });
  185. context('account address does not support ERC165', function () {
  186. it('does not support ERC165', async function () {
  187. const supported = await this.mock.supportsERC165(DUMMY_ACCOUNT);
  188. expect(supported).to.equal(false);
  189. });
  190. it('does not support mock interface via supportsInterface', async function () {
  191. const supported = await this.mock.supportsInterface(DUMMY_ACCOUNT, DUMMY_ID);
  192. expect(supported).to.equal(false);
  193. });
  194. it('does not support mock interface via supportsAllInterfaces', async function () {
  195. const supported = await this.mock.supportsAllInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  196. expect(supported).to.equal(false);
  197. });
  198. it('does not support mock interface via getSupportedInterfaces', async function () {
  199. const supported = await this.mock.getSupportedInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  200. expect(supported.length).to.equal(1);
  201. expect(supported[0]).to.equal(false);
  202. });
  203. });
  204. });