Strings.test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  5. async function fixture() {
  6. const mock = await ethers.deployContract('$Strings');
  7. return { mock };
  8. }
  9. describe('Strings', function () {
  10. before(async function () {
  11. Object.assign(this, await loadFixture(fixture));
  12. });
  13. describe('toString', function () {
  14. const values = [
  15. 0n,
  16. 7n,
  17. 10n,
  18. 99n,
  19. 100n,
  20. 101n,
  21. 123n,
  22. 4132n,
  23. 12345n,
  24. 1234567n,
  25. 1234567890n,
  26. 123456789012345n,
  27. 12345678901234567890n,
  28. 123456789012345678901234567890n,
  29. 1234567890123456789012345678901234567890n,
  30. 12345678901234567890123456789012345678901234567890n,
  31. 123456789012345678901234567890123456789012345678901234567890n,
  32. 1234567890123456789012345678901234567890123456789012345678901234567890n,
  33. ];
  34. describe('uint256', function () {
  35. it('converts MAX_UINT256', async function () {
  36. const value = ethers.MaxUint256;
  37. expect(await this.mock.$toString(value)).to.equal(value.toString(10));
  38. expect(await this.mock.$parseUint(value.toString(10))).to.equal(value);
  39. expect(await this.mock.$tryParseUint(value.toString(10))).to.deep.equal([true, value]);
  40. });
  41. for (const value of values) {
  42. it(`converts ${value}`, async function () {
  43. expect(await this.mock.$toString(value)).to.equal(value.toString(10));
  44. expect(await this.mock.$parseUint(value.toString(10))).to.equal(value);
  45. expect(await this.mock.$tryParseUint(value.toString(10))).to.deep.equal([true, value]);
  46. });
  47. }
  48. });
  49. describe('int256', function () {
  50. it('converts MAX_INT256', async function () {
  51. const value = ethers.MaxInt256;
  52. expect(await this.mock.$toStringSigned(value)).to.equal(value.toString(10));
  53. expect(await this.mock.$parseInt(value.toString(10))).to.equal(value);
  54. expect(await this.mock.$tryParseInt(value.toString(10))).to.deep.equal([true, value]);
  55. });
  56. it('converts MIN_INT256', async function () {
  57. const value = ethers.MinInt256;
  58. expect(await this.mock.$toStringSigned(value)).to.equal(value.toString(10));
  59. expect(await this.mock.$parseInt(value.toString(10))).to.equal(value);
  60. expect(await this.mock.$tryParseInt(value.toString(10))).to.deep.equal([true, value]);
  61. });
  62. for (const value of values) {
  63. it(`convert ${value}`, async function () {
  64. expect(await this.mock.$toStringSigned(value)).to.equal(value.toString(10));
  65. expect(await this.mock.$parseInt(value.toString(10))).to.equal(value);
  66. expect(await this.mock.$tryParseInt(value.toString(10))).to.deep.equal([true, value]);
  67. });
  68. it(`convert negative ${value}`, async function () {
  69. const negated = -value;
  70. expect(await this.mock.$toStringSigned(negated)).to.equal(negated.toString(10));
  71. expect(await this.mock.$parseInt(negated.toString(10))).to.equal(negated);
  72. expect(await this.mock.$tryParseInt(negated.toString(10))).to.deep.equal([true, negated]);
  73. });
  74. }
  75. });
  76. });
  77. describe('toHexString', function () {
  78. it('converts 0', async function () {
  79. const value = 0n;
  80. const string = ethers.toBeHex(value); // 0x00
  81. expect(await this.mock.getFunction('$toHexString(uint256)')(value)).to.equal(string);
  82. expect(await this.mock.$parseHexUint(string)).to.equal(value);
  83. expect(await this.mock.$parseHexUint(string.replace(/0x/, ''))).to.equal(value);
  84. expect(await this.mock.$tryParseHexUint(string)).to.deep.equal([true, value]);
  85. expect(await this.mock.$tryParseHexUint(string.replace(/0x/, ''))).to.deep.equal([true, value]);
  86. });
  87. it('converts a positive number', async function () {
  88. const value = 0x4132n;
  89. const string = ethers.toBeHex(value);
  90. expect(await this.mock.getFunction('$toHexString(uint256)')(value)).to.equal(string);
  91. expect(await this.mock.$parseHexUint(string)).to.equal(value);
  92. expect(await this.mock.$parseHexUint(string.replace(/0x/, ''))).to.equal(value);
  93. expect(await this.mock.$tryParseHexUint(string)).to.deep.equal([true, value]);
  94. expect(await this.mock.$tryParseHexUint(string.replace(/0x/, ''))).to.deep.equal([true, value]);
  95. });
  96. it('converts MAX_UINT256', async function () {
  97. const value = ethers.MaxUint256;
  98. const string = ethers.toBeHex(value);
  99. expect(await this.mock.getFunction('$toHexString(uint256)')(value)).to.equal(string);
  100. expect(await this.mock.$parseHexUint(string)).to.equal(value);
  101. expect(await this.mock.$parseHexUint(string.replace(/0x/, ''))).to.equal(value);
  102. expect(await this.mock.$tryParseHexUint(string)).to.deep.equal([true, value]);
  103. expect(await this.mock.$tryParseHexUint(string.replace(/0x/, ''))).to.deep.equal([true, value]);
  104. });
  105. });
  106. describe('toHexString fixed', function () {
  107. it('converts a positive number (long)', async function () {
  108. expect(await this.mock.getFunction('$toHexString(uint256,uint256)')(0x4132n, 32n)).to.equal(
  109. '0x0000000000000000000000000000000000000000000000000000000000004132',
  110. );
  111. });
  112. it('converts a positive number (short)', async function () {
  113. const length = 1n;
  114. await expect(this.mock.getFunction('$toHexString(uint256,uint256)')(0x4132n, length))
  115. .to.be.revertedWithCustomError(this.mock, 'StringsInsufficientHexLength')
  116. .withArgs(0x4132, length);
  117. });
  118. it('converts MAX_UINT256', async function () {
  119. expect(await this.mock.getFunction('$toHexString(uint256,uint256)')(ethers.MaxUint256, 32n)).to.equal(
  120. ethers.toBeHex(ethers.MaxUint256),
  121. );
  122. });
  123. });
  124. describe('addresses', function () {
  125. const addresses = [
  126. '0xa9036907dccae6a1e0033479b12e837e5cf5a02f', // Random address
  127. '0x0000e0ca771e21bd00057f54a68c30d400000000', // Leading and trailing zeros
  128. // EIP-55 reference
  129. '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
  130. '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
  131. '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
  132. '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
  133. '0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359',
  134. '0x52908400098527886E0F7030069857D2E4169EE7',
  135. '0x8617E340B3D01FA5F11F306F4090FD50E238070D',
  136. '0xde709f2102306220921060314715629080e2fb77',
  137. '0x27b1fdb04752bbc536007a920d24acb045561c26',
  138. '0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed',
  139. '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
  140. '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
  141. '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
  142. ];
  143. describe('toHexString', function () {
  144. for (const addr of addresses) {
  145. it(`converts ${addr}`, async function () {
  146. expect(await this.mock.getFunction('$toHexString(address)')(addr)).to.equal(addr.toLowerCase());
  147. });
  148. }
  149. });
  150. describe('toChecksumHexString', function () {
  151. for (const addr of addresses) {
  152. it(`converts ${addr}`, async function () {
  153. expect(await this.mock.$toChecksumHexString(addr)).to.equal(ethers.getAddress(addr));
  154. });
  155. }
  156. });
  157. describe('parseAddress', function () {
  158. for (const addr of addresses) {
  159. it(`converts ${addr}`, async function () {
  160. expect(await this.mock.$parseAddress(addr)).to.equal(ethers.getAddress(addr));
  161. expect(await this.mock.$tryParseAddress(addr)).to.deep.equal([true, ethers.getAddress(addr)]);
  162. });
  163. }
  164. });
  165. });
  166. describe('bytes', function () {
  167. describe('toHexString', function () {
  168. for (const length of [0, 17, 20, 32, 42, 64, 512]) {
  169. const input = ethers.hexlify(ethers.randomBytes(length));
  170. it(`hexlify buffer of length ${length}`, async function () {
  171. expect(await this.mock.getFunction('$toHexString(bytes)')(input)).to.equal(input);
  172. });
  173. }
  174. });
  175. });
  176. describe('equal', function () {
  177. it('compares two empty strings', async function () {
  178. expect(await this.mock.$equal('', '')).to.be.true;
  179. });
  180. it('compares two equal strings', async function () {
  181. expect(await this.mock.$equal('a', 'a')).to.be.true;
  182. });
  183. it('compares two different strings', async function () {
  184. expect(await this.mock.$equal('a', 'b')).to.be.false;
  185. });
  186. it('compares two different strings of different lengths', async function () {
  187. expect(await this.mock.$equal('a', 'aa')).to.be.false;
  188. expect(await this.mock.$equal('aa', 'a')).to.be.false;
  189. });
  190. it('compares two different large strings', async function () {
  191. const str1 = 'a'.repeat(201);
  192. const str2 = 'a'.repeat(200) + 'b';
  193. expect(await this.mock.$equal(str1, str2)).to.be.false;
  194. });
  195. it('compares two equal large strings', async function () {
  196. const str1 = 'a'.repeat(201);
  197. const str2 = 'a'.repeat(201);
  198. expect(await this.mock.$equal(str1, str2)).to.be.true;
  199. });
  200. });
  201. describe('Edge cases: invalid parsing', function () {
  202. it('parseUint overflow', async function () {
  203. await expect(this.mock.$parseUint((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
  204. PANIC_CODES.ARITHMETIC_OVERFLOW,
  205. );
  206. await expect(this.mock.$tryParseUint((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
  207. PANIC_CODES.ARITHMETIC_OVERFLOW,
  208. );
  209. });
  210. it('parseUint invalid character', async function () {
  211. await expect(this.mock.$parseUint('0x1')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  212. await expect(this.mock.$parseUint('1f')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  213. await expect(this.mock.$parseUint('-10')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  214. await expect(this.mock.$parseUint('1.0')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  215. await expect(this.mock.$parseUint('1 000')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  216. expect(await this.mock.$tryParseUint('0x1')).to.deep.equal([false, 0n]);
  217. expect(await this.mock.$tryParseUint('1f')).to.deep.equal([false, 0n]);
  218. expect(await this.mock.$tryParseUint('-10')).to.deep.equal([false, 0n]);
  219. expect(await this.mock.$tryParseUint('1.0')).deep.equal([false, 0n]);
  220. expect(await this.mock.$tryParseUint('1 000')).deep.equal([false, 0n]);
  221. });
  222. it('parseUint invalid range', async function () {
  223. expect(this.mock.$parseUint('12', 3, 2)).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  224. expect(await this.mock.$tryParseUint('12', 3, 2)).to.deep.equal([false, 0n]);
  225. });
  226. it('parseInt overflow', async function () {
  227. await expect(this.mock.$parseInt((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
  228. PANIC_CODES.ARITHMETIC_OVERFLOW,
  229. );
  230. await expect(this.mock.$parseInt((-ethers.MaxUint256 - 1n).toString(10))).to.be.revertedWithPanic(
  231. PANIC_CODES.ARITHMETIC_OVERFLOW,
  232. );
  233. await expect(this.mock.$tryParseInt((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
  234. PANIC_CODES.ARITHMETIC_OVERFLOW,
  235. );
  236. await expect(this.mock.$tryParseInt((-ethers.MaxUint256 - 1n).toString(10))).to.be.revertedWithPanic(
  237. PANIC_CODES.ARITHMETIC_OVERFLOW,
  238. );
  239. await expect(this.mock.$parseInt((ethers.MaxInt256 + 1n).toString(10))).to.be.revertedWithCustomError(
  240. this.mock,
  241. 'StringsInvalidChar',
  242. );
  243. await expect(this.mock.$parseInt((ethers.MinInt256 - 1n).toString(10))).to.be.revertedWithCustomError(
  244. this.mock,
  245. 'StringsInvalidChar',
  246. );
  247. expect(await this.mock.$tryParseInt((ethers.MaxInt256 + 1n).toString(10))).to.deep.equal([false, 0n]);
  248. expect(await this.mock.$tryParseInt((ethers.MinInt256 - 1n).toString(10))).to.deep.equal([false, 0n]);
  249. });
  250. it('parseInt invalid character', async function () {
  251. await expect(this.mock.$parseInt('0x1')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  252. await expect(this.mock.$parseInt('1f')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  253. await expect(this.mock.$parseInt('1.0')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  254. await expect(this.mock.$parseInt('1 000')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  255. expect(await this.mock.$tryParseInt('0x1')).to.deep.equal([false, 0n]);
  256. expect(await this.mock.$tryParseInt('1f')).to.deep.equal([false, 0n]);
  257. expect(await this.mock.$tryParseInt('1.0')).to.deep.equal([false, 0n]);
  258. expect(await this.mock.$tryParseInt('1 000')).to.deep.equal([false, 0n]);
  259. });
  260. it('parseInt invalid range', async function () {
  261. expect(this.mock.$parseInt('-12', 3, 2)).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  262. expect(await this.mock.$tryParseInt('-12', 3, 2)).to.deep.equal([false, 0n]);
  263. });
  264. it('parseHexUint overflow', async function () {
  265. await expect(this.mock.$parseHexUint((ethers.MaxUint256 + 1n).toString(16))).to.be.revertedWithPanic(
  266. PANIC_CODES.ARITHMETIC_OVERFLOW,
  267. );
  268. await expect(this.mock.$tryParseHexUint((ethers.MaxUint256 + 1n).toString(16))).to.be.revertedWithPanic(
  269. PANIC_CODES.ARITHMETIC_OVERFLOW,
  270. );
  271. });
  272. it('parseHexUint invalid character', async function () {
  273. await expect(this.mock.$parseHexUint('0123456789abcdefg')).to.be.revertedWithCustomError(
  274. this.mock,
  275. 'StringsInvalidChar',
  276. );
  277. await expect(this.mock.$parseHexUint('-1')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  278. await expect(this.mock.$parseHexUint('-f')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  279. await expect(this.mock.$parseHexUint('-0xf')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  280. await expect(this.mock.$parseHexUint('1.0')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  281. await expect(this.mock.$parseHexUint('1 000')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  282. expect(await this.mock.$tryParseHexUint('0123456789abcdefg')).to.deep.equal([false, 0n]);
  283. expect(await this.mock.$tryParseHexUint('-1')).to.deep.equal([false, 0n]);
  284. expect(await this.mock.$tryParseHexUint('-f')).to.deep.equal([false, 0n]);
  285. expect(await this.mock.$tryParseHexUint('-0xf')).to.deep.equal([false, 0n]);
  286. expect(await this.mock.$tryParseHexUint('1.0')).to.deep.equal([false, 0n]);
  287. expect(await this.mock.$tryParseHexUint('1 000')).to.deep.equal([false, 0n]);
  288. });
  289. it('parseHexUint invalid begin and end', async function () {
  290. expect(this.mock.$parseHexUint('0x', 3, 2)).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  291. expect(await this.mock.$tryParseHexUint('0x', 3, 2)).to.deep.equal([false, 0n]);
  292. });
  293. it('parseAddress invalid format', async function () {
  294. for (const addr of [
  295. '0x736a507fB2881d6bB62dcA54673CF5295dC07833', // valid
  296. '0x736a507fB2881d6-B62dcA54673CF5295dC07833', // invalid char
  297. '0x0736a507fB2881d6bB62dcA54673CF5295dC07833', // tooLong
  298. '0x36a507fB2881d6bB62dcA54673CF5295dC07833', // tooShort
  299. '736a507fB2881d6bB62dcA54673CF5295dC07833', // missingPrefix - supported
  300. ]) {
  301. if (ethers.isAddress(addr)) {
  302. expect(await this.mock.$parseAddress(addr)).to.equal(ethers.getAddress(addr));
  303. expect(await this.mock.$tryParseAddress(addr)).to.deep.equal([true, ethers.getAddress(addr)]);
  304. } else {
  305. await expect(this.mock.$parseAddress(addr)).to.be.revertedWithCustomError(
  306. this.mock,
  307. 'StringsInvalidAddressFormat',
  308. );
  309. expect(await this.mock.$tryParseAddress(addr)).to.deep.equal([false, ethers.ZeroAddress]);
  310. }
  311. }
  312. });
  313. });
  314. describe('Escape JSON string', function () {
  315. for (const input of ['', 'a', '{"a":"b/c"}', 'a\tb\nc\\d"e\rf/g\fh\bi'])
  316. it(`escape ${JSON.stringify(input)}`, async function () {
  317. await expect(this.mock.$escapeJSON(input)).to.eventually.equal(JSON.stringify(input).slice(1, -1));
  318. });
  319. });
  320. });