ERC165Checker.test.js 7.6 KB

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