Add Base64Url encoding (#4822)
Co-authored-by: Ernesto García <ernestognw@gmail.com>
This commit is contained in:
32
test/utils/Base64.t.sol
Normal file
32
test/utils/Base64.t.sol
Normal file
@ -0,0 +1,32 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
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"));
|
||||
}
|
||||
|
||||
function testEncodeURL(bytes memory input) external {
|
||||
string memory output = Base64.encodeURL(input);
|
||||
assertEq(output, _base64Ffi(input, "encodeURL"));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,10 @@ const { ethers } = require('hardhat');
|
||||
const { expect } = require('chai');
|
||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
// Replace "+/" with "-_" in the char table, and remove the padding
|
||||
// see https://datatracker.ietf.org/doc/html/rfc4648#section-5
|
||||
const base64toBase64Url = str => str.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');
|
||||
|
||||
async function fixture() {
|
||||
const mock = await ethers.deployContract('$Base64');
|
||||
return { mock };
|
||||
@ -12,18 +16,35 @@ describe('Strings', function () {
|
||||
Object.assign(this, await loadFixture(fixture));
|
||||
});
|
||||
|
||||
describe('from bytes - base64', function () {
|
||||
describe('base64', function () {
|
||||
for (const { title, input, expected } of [
|
||||
{ title: 'converts to base64 encoded string with double padding', input: 'test', expected: 'dGVzdA==' },
|
||||
{ title: 'converts to base64 encoded string with single padding', input: 'test1', expected: 'dGVzdDE=' },
|
||||
{ title: 'converts to base64 encoded string without padding', input: 'test12', expected: 'dGVzdDEy' },
|
||||
{ title: 'empty bytes', input: '0x', expected: '' },
|
||||
{ title: 'converts to base64 encoded string (/ case)', input: 'où', expected: 'b/k=' },
|
||||
{ title: 'converts to base64 encoded string (+ case)', input: 'zs~1t8', expected: 'enN+MXQ4' },
|
||||
{ title: 'empty bytes', input: '', expected: '' },
|
||||
])
|
||||
it(title, async function () {
|
||||
const raw = ethers.isBytesLike(input) ? input : ethers.toUtf8Bytes(input);
|
||||
const buffer = Buffer.from(input, 'ascii');
|
||||
expect(await this.mock.$encode(buffer)).to.equal(ethers.encodeBase64(buffer));
|
||||
expect(await this.mock.$encode(buffer)).to.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
expect(await this.mock.$encode(raw)).to.equal(ethers.encodeBase64(raw));
|
||||
expect(await this.mock.$encode(raw)).to.equal(expected);
|
||||
describe('base64url', function () {
|
||||
for (const { title, input, expected } of [
|
||||
{ title: 'converts to base64url encoded string with double padding', input: 'test', expected: 'dGVzdA' },
|
||||
{ title: 'converts to base64url encoded string with single padding', input: 'test1', expected: 'dGVzdDE' },
|
||||
{ title: 'converts to base64url encoded string without padding', input: 'test12', expected: 'dGVzdDEy' },
|
||||
{ title: 'converts to base64url encoded string (_ case)', input: 'où', expected: 'b_k' },
|
||||
{ title: 'converts to base64url encoded string (- case)', input: 'zs~1t8', expected: 'enN-MXQ4' },
|
||||
{ title: 'empty bytes', input: '', expected: '' },
|
||||
])
|
||||
it(title, async function () {
|
||||
const buffer = Buffer.from(input, 'ascii');
|
||||
expect(await this.mock.$encodeURL(buffer)).to.equal(base64toBase64Url(ethers.encodeBase64(buffer)));
|
||||
expect(await this.mock.$encodeURL(buffer)).to.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user