Fuzz Base64 and Base64URL (#4853)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
3
.github/workflows/checks.yml
vendored
3
.github/workflows/checks.yml
vendored
@ -83,8 +83,7 @@ jobs:
|
|||||||
- name: Set up environment
|
- name: Set up environment
|
||||||
uses: ./.github/actions/setup
|
uses: ./.github/actions/setup
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
# Base64Test requires `--ffi`. See test/utils/Base64.t.sol
|
run: forge test -vv
|
||||||
run: forge test -vv --no-match-contract Base64Test
|
|
||||||
|
|
||||||
coverage:
|
coverage:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|||||||
Submodule lib/forge-std updated: c2236853aa...ae570fec08
@ -1,31 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
_encode() {
|
|
||||||
# - Print the input to stdout
|
|
||||||
# - Remove the first two characters
|
|
||||||
# - Convert from hex to binary
|
|
||||||
# - Convert from binary to base64
|
|
||||||
# - Remove newlines from `base64` output
|
|
||||||
echo -n "$1" | cut -c 3- | xxd -r -p | base64 | tr -d \\n
|
|
||||||
}
|
|
||||||
|
|
||||||
encode() {
|
|
||||||
# - Convert from base64 to hex
|
|
||||||
# - Remove newlines from `xxd` output
|
|
||||||
_encode "$1" | xxd -p | tr -d \\n
|
|
||||||
}
|
|
||||||
|
|
||||||
encodeURL() {
|
|
||||||
# - Remove padding from `base64` output
|
|
||||||
# - Replace `+` with `-`
|
|
||||||
# - Replace `/` with `_`
|
|
||||||
# - Convert from base64 to hex
|
|
||||||
# - Remove newlines from `xxd` output
|
|
||||||
_encode "$1" | sed 's/=//g' | sed 's/+/-/g' | sed 's/\//_/g' | xxd -p | tr -d \\n
|
|
||||||
}
|
|
||||||
|
|
||||||
# $1: function name
|
|
||||||
# $2: input
|
|
||||||
$1 $2
|
|
||||||
@ -6,27 +6,30 @@ import {Test} from "forge-std/Test.sol";
|
|||||||
|
|
||||||
import {Base64} from "@openzeppelin/contracts/utils/Base64.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 {
|
contract Base64Test is Test {
|
||||||
function testEncode(bytes memory input) external {
|
function testEncode(bytes memory input) external {
|
||||||
string memory output = Base64.encode(input);
|
assertEq(Base64.encode(input), vm.toBase64(input));
|
||||||
assertEq(output, _base64Ffi(input, "encode"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testEncodeURL(bytes memory input) external {
|
function testEncodeURL(bytes memory input) external {
|
||||||
string memory output = Base64.encodeURL(input);
|
assertEq(Base64.encodeURL(input), _removePadding(vm.toBase64URL(input)));
|
||||||
assertEq(output, _base64Ffi(input, "encodeURL"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function _base64Ffi(bytes memory input, string memory fn) internal returns (string memory) {
|
function _removePadding(string memory inputStr) internal pure returns (string memory) {
|
||||||
string[] memory command = new string[](4);
|
bytes memory input = bytes(inputStr);
|
||||||
command[0] = "bash";
|
bytes memory output;
|
||||||
command[1] = "scripts/tests/base64.sh";
|
|
||||||
command[2] = fn;
|
for (uint256 i = 0; i < input.length; ++i) {
|
||||||
command[3] = vm.toString(input);
|
if (input[input.length - i - 1] != 0x3d) {
|
||||||
bytes memory retData = vm.ffi(command);
|
output = new bytes(input.length - i);
|
||||||
return string(retData);
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint256 i = 0; i < output.length; ++i) {
|
||||||
|
output[i] = input[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user