ShortStrings.t.sol 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Test} from "forge-std/Test.sol";
  4. import {SymTest} from "halmos-cheatcodes/SymTest.sol";
  5. import {ShortStrings, ShortString} from "@openzeppelin/contracts/utils/ShortStrings.sol";
  6. contract ShortStringsTest is Test, SymTest {
  7. string _fallback;
  8. function testRoundtripShort(string memory input) external {
  9. vm.assume(_isShort(input));
  10. _assertRoundtripShort(input);
  11. }
  12. function symbolicRoundtripShort() external {
  13. string memory input = svm.createString(31, "RoundtripShortInput");
  14. _assertRoundtripShort(input);
  15. }
  16. function testRoundtripWithFallback(string memory input, string memory fallbackInitial) external {
  17. _assertRoundtripWithFallback(input, fallbackInitial);
  18. }
  19. function symbolicRoundtripWithFallbackLong() external {
  20. string memory input = svm.createString(256, "RoundtripWithFallbackInput");
  21. string memory fallbackInitial = svm.createString(256, "RoundtripWithFallbackFallbackInitial");
  22. _assertRoundtripWithFallback(input, fallbackInitial);
  23. }
  24. function symbolicRoundtripWithFallbackShort() external {
  25. string memory input = svm.createString(31, "RoundtripWithFallbackInput");
  26. string memory fallbackInitial = svm.createString(31, "RoundtripWithFallbackFallbackInitial");
  27. _assertRoundtripWithFallback(input, fallbackInitial);
  28. }
  29. function testRevertLong(string memory input) external {
  30. vm.assume(!_isShort(input));
  31. _assertRevertLong(input);
  32. }
  33. function testLengthShort(string memory input) external {
  34. vm.assume(_isShort(input));
  35. _assertLengthShort(input);
  36. }
  37. function symbolicLengthShort() external {
  38. string memory input = svm.createString(31, "LengthShortInput");
  39. _assertLengthShort(input);
  40. }
  41. function testLengthWithFallback(string memory input, string memory fallbackInitial) external {
  42. _fallback = fallbackInitial;
  43. _assertLengthWithFallback(input);
  44. }
  45. function symbolicLengthWithFallback() external {
  46. uint256 length = 256;
  47. string memory input = svm.createString(length, "LengthWithFallbackInput");
  48. string memory fallbackInitial = svm.createString(length, "LengthWithFallbackFallbackInitial");
  49. _fallback = fallbackInitial;
  50. _assertLengthWithFallback(input);
  51. }
  52. /// Assertions
  53. function _assertRoundtripShort(string memory input) internal {
  54. ShortString short = ShortStrings.toShortString(input);
  55. string memory output = ShortStrings.toString(short);
  56. assertEq(input, output);
  57. }
  58. function _assertRoundtripWithFallback(string memory input, string memory fallbackInitial) internal {
  59. _fallback = fallbackInitial; // Make sure that the initial value has no effect
  60. ShortString short = ShortStrings.toShortStringWithFallback(input, _fallback);
  61. string memory output = ShortStrings.toStringWithFallback(short, _fallback);
  62. assertEq(input, output);
  63. }
  64. function _assertRevertLong(string memory input) internal {
  65. vm.expectRevert(abi.encodeWithSelector(ShortStrings.StringTooLong.selector, input));
  66. this.toShortString(input);
  67. }
  68. function _assertLengthShort(string memory input) internal {
  69. ShortString short = ShortStrings.toShortString(input);
  70. uint256 shortLength = ShortStrings.byteLength(short);
  71. uint256 inputLength = bytes(input).length;
  72. assertEq(inputLength, shortLength);
  73. }
  74. function _assertLengthWithFallback(string memory input) internal {
  75. uint256 inputLength = bytes(input).length;
  76. ShortString short = ShortStrings.toShortStringWithFallback(input, _fallback);
  77. uint256 shortLength = ShortStrings.byteLengthWithFallback(short, _fallback);
  78. assertEq(inputLength, shortLength);
  79. }
  80. /// Helpers
  81. function toShortString(string memory input) external pure returns (ShortString) {
  82. return ShortStrings.toShortString(input);
  83. }
  84. function _isShort(string memory input) internal pure returns (bool) {
  85. return bytes(input).length < 32;
  86. }
  87. }