Base64.t.sol 991 B

12345678910111213141516171819202122232425262728293031323334
  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. contract Base64Test is Test {
  6. function testEncode(bytes memory input) external pure {
  7. assertEq(Base64.encode(input), vm.toBase64(input));
  8. }
  9. function testEncodeURL(bytes memory input) external pure {
  10. assertEq(Base64.encodeURL(input), _removePadding(vm.toBase64URL(input)));
  11. }
  12. function _removePadding(string memory inputStr) internal pure returns (string memory) {
  13. bytes memory input = bytes(inputStr);
  14. bytes memory output;
  15. for (uint256 i = 0; i < input.length; ++i) {
  16. if (input[input.length - i - 1] != 0x3d) {
  17. output = new bytes(input.length - i);
  18. break;
  19. }
  20. }
  21. for (uint256 i = 0; i < output.length; ++i) {
  22. output[i] = input[i];
  23. }
  24. return string(output);
  25. }
  26. }