ERC165Checker.test.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ERC165CheckerMock = artifacts.require('ERC165CheckerMock');
  4. const ERC165MissingData = artifacts.require('ERC165MissingData');
  5. const ERC165MaliciousData = artifacts.require('ERC165MaliciousData');
  6. const ERC165NotSupported = artifacts.require('ERC165NotSupported');
  7. const ERC165InterfacesSupported = artifacts.require('ERC165InterfacesSupported');
  8. const DUMMY_ID = '0xdeadbeef';
  9. const DUMMY_ID_2 = '0xcafebabe';
  10. const DUMMY_ID_3 = '0xdecafbad';
  11. const DUMMY_UNSUPPORTED_ID = '0xbaddcafe';
  12. const DUMMY_UNSUPPORTED_ID_2 = '0xbaadcafe';
  13. const DUMMY_ACCOUNT = '0x1111111111111111111111111111111111111111';
  14. contract('ERC165Checker', function (accounts) {
  15. beforeEach(async function () {
  16. this.mock = await ERC165CheckerMock.new();
  17. });
  18. context('ERC165 missing return data', function () {
  19. beforeEach(async function () {
  20. this.target = await ERC165MissingData.new();
  21. });
  22. it('does not support ERC165', async function () {
  23. const supported = await this.mock.supportsERC165(this.target.address);
  24. expect(supported).to.equal(false);
  25. });
  26. it('does not support mock interface via supportsInterface', async function () {
  27. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  28. expect(supported).to.equal(false);
  29. });
  30. it('does not support mock interface via supportsAllInterfaces', async function () {
  31. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  32. expect(supported).to.equal(false);
  33. });
  34. it('does not support mock interface via getSupportedInterfaces', async function () {
  35. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  36. expect(supported.length).to.equal(1);
  37. expect(supported[0]).to.equal(false);
  38. });
  39. });
  40. context('ERC165 malicious return data', function () {
  41. beforeEach(async function () {
  42. this.target = await ERC165MaliciousData.new();
  43. });
  44. it('does not support ERC165', async function () {
  45. const supported = await this.mock.supportsERC165(this.target.address);
  46. expect(supported).to.equal(false);
  47. });
  48. it('does not support mock interface via supportsInterface', async function () {
  49. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  50. expect(supported).to.equal(false);
  51. });
  52. it('does not support mock interface via supportsAllInterfaces', async function () {
  53. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  54. expect(supported).to.equal(false);
  55. });
  56. it('does not support mock interface via getSupportedInterfaces', async function () {
  57. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  58. expect(supported.length).to.equal(1);
  59. expect(supported[0]).to.equal(false);
  60. });
  61. });
  62. context('ERC165 not supported', function () {
  63. beforeEach(async function () {
  64. this.target = await ERC165NotSupported.new();
  65. });
  66. it('does not support ERC165', async function () {
  67. const supported = await this.mock.supportsERC165(this.target.address);
  68. expect(supported).to.equal(false);
  69. });
  70. it('does not support mock interface via supportsInterface', async function () {
  71. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  72. expect(supported).to.equal(false);
  73. });
  74. it('does not support mock interface via supportsAllInterfaces', async function () {
  75. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  76. expect(supported).to.equal(false);
  77. });
  78. it('does not support mock interface via getSupportedInterfaces', async function () {
  79. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  80. expect(supported.length).to.equal(1);
  81. expect(supported[0]).to.equal(false);
  82. });
  83. });
  84. context('ERC165 supported', function () {
  85. beforeEach(async function () {
  86. this.target = await ERC165InterfacesSupported.new([]);
  87. });
  88. it('supports ERC165', async function () {
  89. const supported = await this.mock.supportsERC165(this.target.address);
  90. expect(supported).to.equal(true);
  91. });
  92. it('does not support mock interface via supportsInterface', async function () {
  93. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  94. expect(supported).to.equal(false);
  95. });
  96. it('does not support mock interface via supportsAllInterfaces', async function () {
  97. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  98. expect(supported).to.equal(false);
  99. });
  100. it('does not support mock interface via getSupportedInterfaces', async function () {
  101. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  102. expect(supported.length).to.equal(1);
  103. expect(supported[0]).to.equal(false);
  104. });
  105. });
  106. context('ERC165 and single interface supported', function () {
  107. beforeEach(async function () {
  108. this.target = await ERC165InterfacesSupported.new([DUMMY_ID]);
  109. });
  110. it('supports ERC165', async function () {
  111. const supported = await this.mock.supportsERC165(this.target.address);
  112. expect(supported).to.equal(true);
  113. });
  114. it('supports mock interface via supportsInterface', async function () {
  115. const supported = await this.mock.supportsInterface(this.target.address, DUMMY_ID);
  116. expect(supported).to.equal(true);
  117. });
  118. it('supports mock interface via supportsAllInterfaces', async function () {
  119. const supported = await this.mock.supportsAllInterfaces(this.target.address, [DUMMY_ID]);
  120. expect(supported).to.equal(true);
  121. });
  122. it('supports mock interface via getSupportedInterfaces', async function () {
  123. const supported = await this.mock.getSupportedInterfaces(this.target.address, [DUMMY_ID]);
  124. expect(supported.length).to.equal(1);
  125. expect(supported[0]).to.equal(true);
  126. });
  127. });
  128. context('ERC165 and many interfaces supported', function () {
  129. beforeEach(async function () {
  130. this.supportedInterfaces = [DUMMY_ID, DUMMY_ID_2, DUMMY_ID_3];
  131. this.target = await ERC165InterfacesSupported.new(this.supportedInterfaces);
  132. });
  133. it('supports ERC165', async function () {
  134. const supported = await this.mock.supportsERC165(this.target.address);
  135. expect(supported).to.equal(true);
  136. });
  137. it('supports each interfaceId via supportsInterface', async function () {
  138. for (const interfaceId of this.supportedInterfaces) {
  139. const supported = await this.mock.supportsInterface(this.target.address, interfaceId);
  140. expect(supported).to.equal(true);
  141. };
  142. });
  143. it('supports all interfaceIds via supportsAllInterfaces', async function () {
  144. const supported = await this.mock.supportsAllInterfaces(this.target.address, this.supportedInterfaces);
  145. expect(supported).to.equal(true);
  146. });
  147. it('supports none of the interfaces queried via supportsAllInterfaces', async function () {
  148. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  149. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  150. expect(supported).to.equal(false);
  151. });
  152. it('supports not all of the interfaces queried via supportsAllInterfaces', async function () {
  153. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  154. const supported = await this.mock.supportsAllInterfaces(this.target.address, interfaceIdsToTest);
  155. expect(supported).to.equal(false);
  156. });
  157. it('supports all interfaceIds via getSupportedInterfaces', async function () {
  158. const supported = await this.mock.getSupportedInterfaces(this.target.address, this.supportedInterfaces);
  159. expect(supported.length).to.equal(3);
  160. expect(supported[0]).to.equal(true);
  161. expect(supported[1]).to.equal(true);
  162. expect(supported[2]).to.equal(true);
  163. });
  164. it('supports none of the interfaces queried via getSupportedInterfaces', async function () {
  165. const interfaceIdsToTest = [DUMMY_UNSUPPORTED_ID, DUMMY_UNSUPPORTED_ID_2];
  166. const supported = await this.mock.getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  167. expect(supported.length).to.equal(2);
  168. expect(supported[0]).to.equal(false);
  169. expect(supported[1]).to.equal(false);
  170. });
  171. it('supports not all of the interfaces queried via getSupportedInterfaces', async function () {
  172. const interfaceIdsToTest = [...this.supportedInterfaces, DUMMY_UNSUPPORTED_ID];
  173. const supported = await this.mock.getSupportedInterfaces(this.target.address, interfaceIdsToTest);
  174. expect(supported.length).to.equal(4);
  175. expect(supported[0]).to.equal(true);
  176. expect(supported[1]).to.equal(true);
  177. expect(supported[2]).to.equal(true);
  178. expect(supported[3]).to.equal(false);
  179. });
  180. });
  181. context('account address does not support ERC165', function () {
  182. it('does not support ERC165', async function () {
  183. const supported = await this.mock.supportsERC165(DUMMY_ACCOUNT);
  184. expect(supported).to.equal(false);
  185. });
  186. it('does not support mock interface via supportsInterface', async function () {
  187. const supported = await this.mock.supportsInterface(DUMMY_ACCOUNT, DUMMY_ID);
  188. expect(supported).to.equal(false);
  189. });
  190. it('does not support mock interface via supportsAllInterfaces', async function () {
  191. const supported = await this.mock.supportsAllInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  192. expect(supported).to.equal(false);
  193. });
  194. it('does not support mock interface via getSupportedInterfaces', async function () {
  195. const supported = await this.mock.getSupportedInterfaces(DUMMY_ACCOUNT, [DUMMY_ID]);
  196. expect(supported.length).to.equal(1);
  197. expect(supported[0]).to.equal(false);
  198. });
  199. });
  200. });