Files
openzeppelin-contracts/test/utils/Strings.t.sol
Hadrien Croubois e5e9ff72f0 Release v5.2 audit fixes (#5330)
Signed-off-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: Sam Bugs <101145325+0xsambugs@users.noreply.github.com>
Co-authored-by: Ernesto García <ernestognw@gmail.com>
Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com>
Co-authored-by: wizard <112275929+famouswizard@users.noreply.github.com>
Co-authored-by: leopardracer <136604165+leopardracer@users.noreply.github.com>
Co-authored-by: cairo <cairoeth@protonmail.com>
2024-12-04 17:37:13 +01:00

51 lines
1.6 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Test} from "forge-std/Test.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
contract StringsTest is Test {
using Strings for *;
function testParse(uint256 value) external {
assertEq(value, value.toString().parseUint());
}
function testParseSigned(int256 value) external {
assertEq(value, value.toStringSigned().parseInt());
}
function testParseHex(uint256 value) external {
assertEq(value, value.toHexString().parseHexUint());
}
function testParseChecksumHex(address value) external {
assertEq(value, value.toChecksumHexString().parseAddress());
}
function testTryParseHexUintExtendedEnd(string memory random) external pure {
uint256 length = bytes(random).length;
assembly ("memory-safe") {
mstore(add(add(random, 0x20), length), 0x3030303030303030303030303030303030303030303030303030303030303030)
}
(bool success, ) = random.tryParseHexUint(1, length + 1);
assertFalse(success);
}
function testTryParseAddressExtendedEnd(address random, uint256 begin) external pure {
begin = bound(begin, 3, 43);
string memory input = random.toHexString();
uint256 length = bytes(input).length;
assembly ("memory-safe") {
mstore(add(add(input, 0x20), length), 0x3030303030303030303030303030303030303030303030303030303030303030)
}
(bool success, ) = input.tryParseAddress(begin, begin + 40);
assertFalse(success);
}
}