ERC165Checker.test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const { contract } = require('@openzeppelin/test-environment');
  2. require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const ERC165CheckerMock = contract.fromArtifact('ERC165CheckerMock');
  5. const ERC165NotSupported = contract.fromArtifact('ERC165NotSupported');
  6. const ERC165InterfacesSupported = contract.fromArtifact('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. describe('ERC165Checker', function () {
  14. beforeEach(async function () {
  15. this.mock = await ERC165CheckerMock.new();
  16. });
  17. context('ERC165 not supported', function () {
  18. beforeEach(async function () {
  19. this.target = await ERC165NotSupported.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. });
  34. context('ERC165 supported', function () {
  35. beforeEach(async function () {
  36. this.target = await ERC165InterfacesSupported.new([]);
  37. });
  38. it('supports ERC165', async function () {
  39. const supported = await this.mock.supportsERC165(this.target.address);
  40. expect(supported).to.equal(true);
  41. });
  42. it('does not support mock interface via supportsInterface', async function () {
  43. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  44. expect(supported).to.equal(false);
  45. });
  46. it('does not support mock interface via supportsAllInterfaces', async function () {
  47. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  48. expect(supported).to.equal(false);
  49. });
  50. });
  51. context('ERC165 and single interface supported', function () {
  52. beforeEach(async function () {
  53. this.target = await ERC165InterfacesSupported.new([DUMMY_ID]);
  54. });
  55. it('supports ERC165', async function () {
  56. const supported = await this.mock.supportsERC165(this.target.address);
  57. expect(supported).to.equal(true);
  58. });
  59. it('supports mock interface via supportsInterface', async function () {
  60. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  61. expect(supported).to.equal(true);
  62. });
  63. it('supports mock interface via supportsAllInterfaces', async function () {
  64. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  65. expect(supported).to.equal(true);
  66. });
  67. });
  68. context('ERC165 and many interfaces supported', function () {
  69. beforeEach(async function () {
  70. this.supportedInterfaces = [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3];
  71. this.target = await ERC165InterfacesSupported.new(this.supportedInterfaces);
  72. });
  73. it('supports ERC165', async function () {
  74. const supported = await this.mock.supportsERC165(this.target.address);
  75. expect(supported).to.equal(true);
  76. });
  77. it('supports each interfaceId via supportsInterface', async function () {
  78. for (const interfaceId of this.supportedInterfaces) {
  79. const supported = await this.mock.supportsInterface(this.target.address, interfaceId);
  80. expect(supported).to.equal(true);
  81. };
  82. });
  83. it('supports all interfaceIds via supportsAllInterfaces', async function () {
  84. const supported = await this.mock.supportsAllInterfaces(this.target.address, this.supportedInterfaces);
  85. expect(supported).to.equal(true);
  86. });
  87. it('supports none of the interfaces queried via supportsAllInterfaces', async function () {
  88. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  89. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  90. expect(supported).to.equal(false);
  91. });
  92. it('supports not all of the interfaces queried via supportsAllInterfaces', async function () {
  93. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  94. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  95. expect(supported).to.equal(false);
  96. });
  97. });
  98. context('account address does not support ERC165', function () {
  99. it('does not support ERC165', async function () {
  100. const supported = await this.mock.supportsERC165(DUMMY_ACCOUNT);
  101. expect(supported).to.equal(false);
  102. });
  103. it('does not support mock interface via supportsInterface', async function () {
  104. const supported = await this.mock.supportsInterface(DUMMY_ACCOUNT, DUMMY_ID);
  105. expect(supported).to.equal(false);
  106. });
  107. it('does not support mock interface via supportsAllInterfaces', async function () {
  108. const supported = await this.mock.supportsAllInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  109. expect(supported).to.equal(false);
  110. });
  111. });
  112. });