ERC1820Implementer.test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { expectRevert, singletons } = require('@openzeppelin/test-helpers');
  3. const { bufferToHex, keccak256 } = require('ethereumjs-util');
  4. const { expect } = require('chai');
  5. const ERC1820ImplementerMock = contract.fromArtifact('ERC1820ImplementerMock');
  6. describe('ERC1820Implementer', function () {
  7. const [ registryFunder, implementee, other ] = accounts;
  8. const ERC1820_ACCEPT_MAGIC = bufferToHex(keccak256('ERC1820_ACCEPT_MAGIC'));
  9. beforeEach(async function () {
  10. this.implementer = await ERC1820ImplementerMock.new();
  11. this.registry = await singletons.ERC1820Registry(registryFunder);
  12. this.interfaceA = bufferToHex(keccak256('interfaceA'));
  13. this.interfaceB = bufferToHex(keccak256('interfaceB'));
  14. });
  15. context('with no registered interfaces', function () {
  16. it('returns false when interface implementation is queried', async function () {
  17. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceA, implementee))
  18. .to.not.equal(ERC1820_ACCEPT_MAGIC);
  19. });
  20. it('reverts when attempting to set as implementer in the registry', async function () {
  21. await expectRevert(
  22. this.registry.setInterfaceImplementer(
  23. implementee, this.interfaceA, this.implementer.address, { from: implementee }
  24. ),
  25. 'Does not implement the interface'
  26. );
  27. });
  28. });
  29. context('with registered interfaces', function () {
  30. beforeEach(async function () {
  31. await this.implementer.registerInterfaceForAddress(this.interfaceA, implementee);
  32. });
  33. it('returns true when interface implementation is queried', async function () {
  34. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceA, implementee))
  35. .to.equal(ERC1820_ACCEPT_MAGIC);
  36. });
  37. it('returns false when interface implementation for non-supported interfaces is queried', async function () {
  38. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceB, implementee))
  39. .to.not.equal(ERC1820_ACCEPT_MAGIC);
  40. });
  41. it('returns false when interface implementation for non-supported addresses is queried', async function () {
  42. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceA, other))
  43. .to.not.equal(ERC1820_ACCEPT_MAGIC);
  44. });
  45. it('can be set as an implementer for supported interfaces in the registry', async function () {
  46. await this.registry.setInterfaceImplementer(
  47. implementee, this.interfaceA, this.implementer.address, { from: implementee }
  48. );
  49. expect(await this.registry.getInterfaceImplementer(implementee, this.interfaceA))
  50. .to.equal(this.implementer.address);
  51. });
  52. });
  53. });