Base64.t.sol 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Test} from "forge-std/Test.sol";
  4. import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
  5. /// NOTE: This test requires `ffi` to be enabled. It does not run in the CI
  6. /// environment given `ffi` is not recommended.
  7. /// See: https://github.com/foundry-rs/foundry/issues/6744
  8. contract Base64Test is Test {
  9. function testEncode(bytes memory input) external {
  10. string memory output = Base64.encode(input);
  11. assertEq(output, _base64Ffi(input, "encode"));
  12. }
  13. function testEncodeURL(bytes memory input) external {
  14. string memory output = Base64.encodeURL(input);
  15. assertEq(output, _base64Ffi(input, "encodeURL"));
  16. }
  17. function _base64Ffi(bytes memory input, string memory fn) internal returns (string memory) {
  18. string[] memory command = new string[](4);
  19. command[0] = "bash";
  20. command[1] = "scripts/tests/base64.sh";
  21. command[2] = fn;
  22. command[3] = vm.toString(input);
  23. bytes memory retData = vm.ffi(command);
  24. return string(retData);
  25. }
  26. }