ERC165Checker.test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  39. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  40. expect(supported).to.equal(false);
  41. });
  42. });
  43. context('ERC165 not supported', function () {
  44. beforeEach(async function () {
  45. this.target = await ERC165NotSupported.new();
  46. });
  47. it('does not support ERC165', async function () {
  48. const supported = await this.mock.supportsERC165(this.target.address);
  49. expect(supported).to.equal(false);
  50. });
  51. it('does not support mock interface via supportsInterface', async function () {
  52. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  53. expect(supported).to.equal(false);
  54. });
  55. it('does not support mock interface via supportsAllInterfaces', async function () {
  56. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  57. expect(supported).to.equal(false);
  58. });
  59. it('does not support mock interface via getSupportedInterfaces', async function () {
  60. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  61. expect(supported.length).to.equal(1);
  62. expect(supported[0]).to.equal(false);
  63. });
  64. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  65. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  66. expect(supported).to.equal(false);
  67. });
  68. });
  69. context('ERC165 supported', function () {
  70. beforeEach(async function () {
  71. this.target = await ERC165InterfacesSupported.new([]);
  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('does not support mock interface via supportsInterface', async function () {
  78. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  79. expect(supported).to.equal(false);
  80. });
  81. it('does not support mock interface via supportsAllInterfaces', async function () {
  82. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  83. expect(supported).to.equal(false);
  84. });
  85. it('does not support mock interface via getSupportedInterfaces', async function () {
  86. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  87. expect(supported.length).to.equal(1);
  88. expect(supported[0]).to.equal(false);
  89. });
  90. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  91. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  92. expect(supported).to.equal(false);
  93. });
  94. });
  95. context('ERC165 and single interface supported', function () {
  96. beforeEach(async function () {
  97. this.target = await ERC165InterfacesSupported.new([DUMMY_ID]);
  98. });
  99. it('supports ERC165', async function () {
  100. const supported = await this.mock.supportsERC165(this.target.address);
  101. expect(supported).to.equal(true);
  102. });
  103. it('supports mock interface via supportsInterface', async function () {
  104. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  105. expect(supported).to.equal(true);
  106. });
  107. it('supports mock interface via supportsAllInterfaces', async function () {
  108. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  109. expect(supported).to.equal(true);
  110. });
  111. it('supports mock interface via getSupportedInterfaces', async function () {
  112. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  113. expect(supported.length).to.equal(1);
  114. expect(supported[0]).to.equal(true);
  115. });
  116. it('supports mock interface via supportsERC165InterfaceUnchecked', async function () {
  117. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, DUMMY_ID);
  118. expect(supported).to.equal(true);
  119. });
  120. });
  121. context('ERC165 and many interfaces supported', function () {
  122. beforeEach(async function () {
  123. this.supportedInterfaces = [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3];
  124. this.target = await ERC165InterfacesSupported.new(this.supportedInterfaces);
  125. });
  126. it('supports ERC165', async function () {
  127. const supported = await this.mock.supportsERC165(this.target.address);
  128. expect(supported).to.equal(true);
  129. });
  130. it('supports each interfaceId via supportsInterface', async function () {
  131. for (const interfaceId of this.supportedInterfaces) {
  132. const supported = await this.mock.supportsInterface(this.target.address, interfaceId);
  133. expect(supported).to.equal(true);
  134. };
  135. });
  136. it('supports all interfaceIds via supportsAllInterfaces', async function () {
  137. const supported = await this.mock.supportsAllInterfaces(this.target.address, this.supportedInterfaces);
  138. expect(supported).to.equal(true);
  139. });
  140. it('supports none of the interfaces queried via supportsAllInterfaces', async function () {
  141. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  142. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  143. expect(supported).to.equal(false);
  144. });
  145. it('supports not all of the interfaces queried via supportsAllInterfaces', async function () {
  146. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  147. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  148. expect(supported).to.equal(false);
  149. });
  150. it('supports all interfaceIds via getSupportedInterfaces', async function () {
  151. const supported = await this.mock.getSupportedInterfaces(this.target.address, this.supportedInterfaces);
  152. expect(supported.length).to.equal(3);
  153. expect(supported[0]).to.equal(true);
  154. expect(supported[1]).to.equal(true);
  155. expect(supported[2]).to.equal(true);
  156. });
  157. it('supports none of the interfaces queried via getSupportedInterfaces', async function () {
  158. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  159. const supported = await this.mock.getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  160. expect(supported.length).to.equal(2);
  161. expect(supported[0]).to.equal(false);
  162. expect(supported[1]).to.equal(false);
  163. });
  164. it('supports not all of the interfaces queried via getSupportedInterfaces', async function () {
  165. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  166. const supported = await this.mock.getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  167. expect(supported.length).to.equal(4);
  168. expect(supported[0]).to.equal(true);
  169. expect(supported[1]).to.equal(true);
  170. expect(supported[2]).to.equal(true);
  171. expect(supported[3]).to.equal(false);
  172. });
  173. it('supports each interfaceId via supportsERC165InterfaceUnchecked', async function () {
  174. for (const interfaceId of this.supportedInterfaces) {
  175. const supported = await this.mock.supportsERC165InterfaceUnchecked(this.target.address, interfaceId);
  176. expect(supported).to.equal(true);
  177. };
  178. });
  179. });
  180. context('account address does not support ERC165', function () {
  181. it('does not support ERC165', async function () {
  182. const supported = await this.mock.supportsERC165(DUMMY_ACCOUNT);
  183. expect(supported).to.equal(false);
  184. });
  185. it('does not support mock interface via supportsInterface', async function () {
  186. const supported = await this.mock.supportsInterface(DUMMY_ACCOUNT, DUMMY_ID);
  187. expect(supported).to.equal(false);
  188. });
  189. it('does not support mock interface via supportsAllInterfaces', async function () {
  190. const supported = await this.mock.supportsAllInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  191. expect(supported).to.equal(false);
  192. });
  193. it('does not support mock interface via getSupportedInterfaces', async function () {
  194. const supported = await this.mock.getSupportedInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  195. expect(supported.length).to.equal(1);
  196. expect(supported[0]).to.equal(false);
  197. });
  198. it('does not support mock interface via supportsERC165InterfaceUnchecked', async function () {
  199. const supported = await this.mock.supportsERC165InterfaceUnchecked(DUMMY_ACCOUNT, DUMMY_ID);
  200. expect(supported).to.equal(false);
  201. });
  202. });
  203. });