Fuzz Base64 and Base64URL (#4853)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
Ernesto García
2024-01-24 09:09:41 -06:00
committed by GitHub
parent e86bb45477
commit 6b30d2d8de
4 changed files with 20 additions and 49 deletions

View File

@ -6,27 +6,30 @@ import {Test} from "forge-std/Test.sol";
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
/// NOTE: This test requires `ffi` to be enabled. It does not run in the CI
/// environment given `ffi` is not recommended.
/// See: https://github.com/foundry-rs/foundry/issues/6744
contract Base64Test is Test {
function testEncode(bytes memory input) external {
string memory output = Base64.encode(input);
assertEq(output, _base64Ffi(input, "encode"));
assertEq(Base64.encode(input), vm.toBase64(input));
}
function testEncodeURL(bytes memory input) external {
string memory output = Base64.encodeURL(input);
assertEq(output, _base64Ffi(input, "encodeURL"));
assertEq(Base64.encodeURL(input), _removePadding(vm.toBase64URL(input)));
}
function _base64Ffi(bytes memory input, string memory fn) internal returns (string memory) {
string[] memory command = new string[](4);
command[0] = "bash";
command[1] = "scripts/tests/base64.sh";
command[2] = fn;
command[3] = vm.toString(input);
bytes memory retData = vm.ffi(command);
return string(retData);
function _removePadding(string memory inputStr) internal pure returns (string memory) {
bytes memory input = bytes(inputStr);
bytes memory output;
for (uint256 i = 0; i < input.length; ++i) {
if (input[input.length - i - 1] != 0x3d) {
output = new bytes(input.length - i);
break;
}
}
for (uint256 i = 0; i < output.length; ++i) {
output[i] = input[i];
}
return string(output);
}
}