Strings.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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('equal', function () {
  167. it('compares two empty strings', async function () {
  168. expect(await this.mock.$equal('', '')).to.be.true;
  169. });
  170. it('compares two equal strings', async function () {
  171. expect(await this.mock.$equal('a', 'a')).to.be.true;
  172. });
  173. it('compares two different strings', async function () {
  174. expect(await this.mock.$equal('a', 'b')).to.be.false;
  175. });
  176. it('compares two different strings of different lengths', async function () {
  177. expect(await this.mock.$equal('a', 'aa')).to.be.false;
  178. expect(await this.mock.$equal('aa', 'a')).to.be.false;
  179. });
  180. it('compares two different large strings', async function () {
  181. const str1 = 'a'.repeat(201);
  182. const str2 = 'a'.repeat(200) + 'b';
  183. expect(await this.mock.$equal(str1, str2)).to.be.false;
  184. });
  185. it('compares two equal large strings', async function () {
  186. const str1 = 'a'.repeat(201);
  187. const str2 = 'a'.repeat(201);
  188. expect(await this.mock.$equal(str1, str2)).to.be.true;
  189. });
  190. });
  191. describe('Edge cases: invalid parsing', function () {
  192. it('parseUint overflow', async function () {
  193. await expect(this.mock.$parseUint((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
  194. PANIC_CODES.ARITHMETIC_OVERFLOW,
  195. );
  196. await expect(this.mock.$tryParseUint((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
  197. PANIC_CODES.ARITHMETIC_OVERFLOW,
  198. );
  199. });
  200. it('parseUint invalid character', async function () {
  201. await expect(this.mock.$parseUint('0x1')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  202. await expect(this.mock.$parseUint('1f')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  203. await expect(this.mock.$parseUint('-10')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  204. await expect(this.mock.$parseUint('1.0')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  205. await expect(this.mock.$parseUint('1 000')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  206. expect(await this.mock.$tryParseUint('0x1')).to.deep.equal([false, 0n]);
  207. expect(await this.mock.$tryParseUint('1f')).to.deep.equal([false, 0n]);
  208. expect(await this.mock.$tryParseUint('-10')).to.deep.equal([false, 0n]);
  209. expect(await this.mock.$tryParseUint('1.0')).deep.equal([false, 0n]);
  210. expect(await this.mock.$tryParseUint('1 000')).deep.equal([false, 0n]);
  211. });
  212. it('parseUint invalid range', async function () {
  213. expect(this.mock.$parseUint('12', 3, 2)).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  214. expect(await this.mock.$tryParseUint('12', 3, 2)).to.deep.equal([false, 0n]);
  215. });
  216. it('parseInt overflow', async function () {
  217. await expect(this.mock.$parseInt((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
  218. PANIC_CODES.ARITHMETIC_OVERFLOW,
  219. );
  220. await expect(this.mock.$parseInt((-ethers.MaxUint256 - 1n).toString(10))).to.be.revertedWithPanic(
  221. PANIC_CODES.ARITHMETIC_OVERFLOW,
  222. );
  223. await expect(this.mock.$tryParseInt((ethers.MaxUint256 + 1n).toString(10))).to.be.revertedWithPanic(
  224. PANIC_CODES.ARITHMETIC_OVERFLOW,
  225. );
  226. await expect(this.mock.$tryParseInt((-ethers.MaxUint256 - 1n).toString(10))).to.be.revertedWithPanic(
  227. PANIC_CODES.ARITHMETIC_OVERFLOW,
  228. );
  229. await expect(this.mock.$parseInt((ethers.MaxInt256 + 1n).toString(10))).to.be.revertedWithCustomError(
  230. this.mock,
  231. 'StringsInvalidChar',
  232. );
  233. await expect(this.mock.$parseInt((ethers.MinInt256 - 1n).toString(10))).to.be.revertedWithCustomError(
  234. this.mock,
  235. 'StringsInvalidChar',
  236. );
  237. expect(await this.mock.$tryParseInt((ethers.MaxInt256 + 1n).toString(10))).to.deep.equal([false, 0n]);
  238. expect(await this.mock.$tryParseInt((ethers.MinInt256 - 1n).toString(10))).to.deep.equal([false, 0n]);
  239. });
  240. it('parseInt invalid character', async function () {
  241. await expect(this.mock.$parseInt('0x1')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  242. await expect(this.mock.$parseInt('1f')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  243. await expect(this.mock.$parseInt('1.0')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  244. await expect(this.mock.$parseInt('1 000')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  245. expect(await this.mock.$tryParseInt('0x1')).to.deep.equal([false, 0n]);
  246. expect(await this.mock.$tryParseInt('1f')).to.deep.equal([false, 0n]);
  247. expect(await this.mock.$tryParseInt('1.0')).to.deep.equal([false, 0n]);
  248. expect(await this.mock.$tryParseInt('1 000')).to.deep.equal([false, 0n]);
  249. });
  250. it('parseInt invalid range', async function () {
  251. expect(this.mock.$parseInt('-12', 3, 2)).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  252. expect(await this.mock.$tryParseInt('-12', 3, 2)).to.deep.equal([false, 0n]);
  253. });
  254. it('parseHexUint overflow', async function () {
  255. await expect(this.mock.$parseHexUint((ethers.MaxUint256 + 1n).toString(16))).to.be.revertedWithPanic(
  256. PANIC_CODES.ARITHMETIC_OVERFLOW,
  257. );
  258. await expect(this.mock.$tryParseHexUint((ethers.MaxUint256 + 1n).toString(16))).to.be.revertedWithPanic(
  259. PANIC_CODES.ARITHMETIC_OVERFLOW,
  260. );
  261. });
  262. it('parseHexUint invalid character', async function () {
  263. await expect(this.mock.$parseHexUint('0123456789abcdefg')).to.be.revertedWithCustomError(
  264. this.mock,
  265. 'StringsInvalidChar',
  266. );
  267. await expect(this.mock.$parseHexUint('-1')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  268. await expect(this.mock.$parseHexUint('-f')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  269. await expect(this.mock.$parseHexUint('-0xf')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  270. await expect(this.mock.$parseHexUint('1.0')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  271. await expect(this.mock.$parseHexUint('1 000')).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  272. expect(await this.mock.$tryParseHexUint('0123456789abcdefg')).to.deep.equal([false, 0n]);
  273. expect(await this.mock.$tryParseHexUint('-1')).to.deep.equal([false, 0n]);
  274. expect(await this.mock.$tryParseHexUint('-f')).to.deep.equal([false, 0n]);
  275. expect(await this.mock.$tryParseHexUint('-0xf')).to.deep.equal([false, 0n]);
  276. expect(await this.mock.$tryParseHexUint('1.0')).to.deep.equal([false, 0n]);
  277. expect(await this.mock.$tryParseHexUint('1 000')).to.deep.equal([false, 0n]);
  278. });
  279. it('parseHexUint invalid begin and end', async function () {
  280. expect(this.mock.$parseHexUint('0x', 3, 2)).to.be.revertedWithCustomError(this.mock, 'StringsInvalidChar');
  281. expect(await this.mock.$tryParseHexUint('0x', 3, 2)).to.deep.equal([false, 0n]);
  282. });
  283. it('parseAddress invalid format', async function () {
  284. for (const addr of [
  285. '0x736a507fB2881d6bB62dcA54673CF5295dC07833', // valid
  286. '0x736a507fB2881d6-B62dcA54673CF5295dC07833', // invalid char
  287. '0x0736a507fB2881d6bB62dcA54673CF5295dC07833', // tooLong
  288. '0x36a507fB2881d6bB62dcA54673CF5295dC07833', // tooShort
  289. '736a507fB2881d6bB62dcA54673CF5295dC07833', // missingPrefix - supported
  290. ]) {
  291. if (ethers.isAddress(addr)) {
  292. expect(await this.mock.$parseAddress(addr)).to.equal(ethers.getAddress(addr));
  293. expect(await this.mock.$tryParseAddress(addr)).to.deep.equal([true, ethers.getAddress(addr)]);
  294. } else {
  295. await expect(this.mock.$parseAddress(addr)).to.be.revertedWithCustomError(
  296. this.mock,
  297. 'StringsInvalidAddressFormat',
  298. );
  299. expect(await this.mock.$tryParseAddress(addr)).to.deep.equal([false, ethers.ZeroAddress]);
  300. }
  301. }
  302. });
  303. });
  304. });