ERC165Checker.test.js 8.8 KB

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