ERC165Checker.test.js 5.3 KB

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