Skip to content

Commit

Permalink
🥳 encoding functions running
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
  • Loading branch information
pcaversaccio committed Jan 3, 2023
1 parent 30e2818 commit 118b95e
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 12 deletions.
6 changes: 2 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
url = https://github.com/GNSPS/solidity-bytes-utils.git
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts.git
[submodule "lib/prb-test"]
path = lib/prb-test
url = https://github.com/paulrberg/prb-test
[submodule "lib\\prb-test"]
branch = v0.3.1
url = https://github.com/paulrberg/prb-test.git
2 changes: 1 addition & 1 deletion lib/create-util
Submodule create-util updated 3 files
+1 −1 LICENSE
+104 −104 package-lock.json
+4 −4 package.json
2 changes: 1 addition & 1 deletion lib/forge-std
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts
2 changes: 1 addition & 1 deletion lib/prb-test
Submodule prb-test updated 1 files
+2 −2 src/Vm.sol
1 change: 1 addition & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ forge-std/=lib/forge-std/src/
create-util/=lib/create-util/contracts/
openzeppelin/=lib/openzeppelin-contracts/contracts/
solidity-bytes-utils/=lib/solidity-bytes-utils/contracts/
prb/test/=lib/prb-test/src/
95 changes: 91 additions & 4 deletions test/utils/Base64.t.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// SPDX-License-Identifier: WTFPL
pragma solidity ^0.8.17;

import {Test} from "forge-std/Test.sol";
import {PRBTest} from "prb/test/PRBTest.sol";
import {VyperDeployer} from "utils/VyperDeployer.sol";

import {IBase64} from "./interfaces/IBase64.sol";

contract Base64Test is Test {
/**
* @dev We make use of Paul's testing assertions (https://github.com/paulrberg/prb-test)
* in this test suite since it supports equality assertions for arrays.
*/
contract Base64Test is PRBTest {
VyperDeployer private vyperDeployer = new VyperDeployer();

IBase64 private base64;
Expand All @@ -15,7 +19,90 @@ contract Base64Test is Test {
base64 = IBase64(vyperDeployer.deployContract("src/utils/", "Base64"));
}

function testBase64EncodeEmptyString() public {
string[] memory output = base64.encode("", false);
function testEncodeEmptyString() public {
string[] memory emptyArray = new string[](0);
string[] memory outputStd = base64.encode("", false);
string[] memory outputUrl = base64.encode("", true);
assertEq(outputStd, emptyArray);
assertEq(outputUrl, emptyArray);
}

function testEncodeWithNoPadding() public {
string memory data = "test12";
string[] memory encoded = new string[](2);
encoded[0] = "dGVz";
encoded[1] = "dDEy";
string[] memory outputStd = base64.encode(bytes(data), false);
string[] memory outputUrl = base64.encode(bytes(data), true);
assertEq(outputStd, encoded);
assertEq(outputUrl, encoded);
}

function testEncodeWithSinglePadding() public {
string memory data = "test1";
string[] memory encoded = new string[](2);
encoded[0] = "dGVz";
encoded[1] = "dDE=";
string[] memory outputStd = base64.encode(bytes(data), false);
string[] memory outputUrl = base64.encode(bytes(data), true);
assertEq(outputStd, encoded);
assertEq(outputUrl, encoded);
}

function testEncodeWithDoublePadding() public {
string memory data = "test";
string[] memory encoded = new string[](2);
encoded[0] = "dGVz";
encoded[1] = "dA==";
string[] memory outputStd = base64.encode(bytes(data), false);
string[] memory outputUrl = base64.encode(bytes(data), true);
assertEq(outputStd, encoded);
assertEq(outputUrl, encoded);
}

function testEncodeSingleCharacter() public {
string memory data = "M";
string[] memory encoded = new string[](1);
encoded[0] = "TQ==";
string[] memory outputStd = base64.encode(bytes(data), false);
string[] memory outputUrl = base64.encode(bytes(data), true);
assertEq(outputStd, encoded);
assertEq(outputUrl, encoded);
}

function testEncodeSentence() public {
string memory data = "Snakes are great animals!";
string[] memory encoded = new string[](9);
encoded[0] = "U25h";
encoded[1] = "a2Vz";
encoded[2] = "IGFy";
encoded[3] = "ZSBn";
encoded[4] = "cmVh";
encoded[5] = "dCBh";
encoded[6] = "bmlt";
encoded[7] = "YWxz";
encoded[8] = "IQ==";
string[] memory outputStd = base64.encode(bytes(data), false);
string[] memory outputUrl = base64.encode(bytes(data), true);
assertEq(outputStd, encoded);
assertEq(outputUrl, encoded);
}

function testEncodeSafeUrl() public {
string memory data = "[]c!~?[]~~";
string[] memory encodedStd = new string[](4);
string[] memory encodedUrl = new string[](4);
encodedStd[0] = "W11j";
encodedStd[1] = "IX4/";
encodedStd[2] = "W11+";
encodedStd[3] = "fg==";
encodedUrl[0] = "W11j";
encodedUrl[1] = "IX4_";
encodedUrl[2] = "W11-";
encodedUrl[3] = "fg==";
string[] memory outputStd = base64.encode(bytes(data), false);
string[] memory outputUrl = base64.encode(bytes(data), true);
assertEq(outputStd, encodedStd);
assertEq(outputUrl, encodedUrl);
}
}

0 comments on commit 118b95e

Please sign in to comment.