dapp_indexer/test/DappIndexer.t.sol.back

81 lines
3.1 KiB
Plaintext

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.23;
import { Test, console2 } from "forge-std/Test.sol";
import { DappIndexer } from "src/DappIndexer.sol";
contract DappIndexerTest is Test {
DappIndexer public dappIdxr;
string[] public dappsURI;
function setUp() public {
dappIdxr = new DappIndexer(address(this), address(this));
dappsURI = vm.envString("dapps", ' ');
}
function testAddDapp() public {
bytes32 CID1;
bytes32 CID2;
//(CID1, CID2) = stringToBytes32Pair(dappsURI[0]);
string memory dapp= "bafkreibc6p3y36yjmeqqnttqfrpb2yttxa6aonoywxwdxl7nqym4jj3jwa";
(CID1, CID2) = stringToBytes32Pair(dapp);
// Add Dapp
console2.log("Bytes32 first:");
console2.logBytes32(CID1);
console2.log("Bytes32 second:");
console2.logBytes32(CID2);
console2.log(Bytes32PairToString(CID1, CID2));
dappIdxr.addDapp(bytes32("Dapp01"), DappIndexer.PackedCID(CID1, CID2));
// retrive Dapp
DappIndexer.PackedCID memory dapp1 = dappIdxr.getCID(bytes32("Dapp01"));
assertEq(CID1, dapp1.CID1);
assertEq(CID2, dapp1.CID2);
assertEq(dappsURI[0], Bytes32PairToString(dapp1.CID1, dapp1.CID2));
}
function Bytes32PairToString(bytes32 part1, bytes32 part2) public pure returns (string memory) {
// Concatenate the two bytes32 variables
bytes memory concatenatedBytes = abi.encodePacked(part1, part2);
// Convert the concatenated bytes to a string
string memory str = string(concatenatedBytes);
return str;
}
function stringToBytes32Pair(string memory source) public pure returns (bytes32 part1, bytes32 part2) {
bytes memory sourceBytes = bytes(source);
require(sourceBytes.length == 59, "Source string must be 59 bytes");
assembly {
// Load the first 32 bytes of the string data
part1 := mload(add(sourceBytes, 32))
// Load the next 32 bytes of the string data, then shift right by 3 bytes (24 bits) to remove unwanted bytes
part2 := mload(add(sourceBytes, 64))
part2 := shl(24, part2) // Shift left to remove the last 5 bytes
part2 := shr(24, part2) // Shift right to align back to the least significant bits
}
}
//function stringToBytes32Pair(string memory source) public pure returns (bytes32 part1, bytes32 part2) {
// bytes memory sourceBytes = bytes(source);
// require(sourceBytes.length == 59, "URI string must equal to 59 bytes");
// assembly {
// // Load the first 32 bytes of the string data
// part1 := mload(add(sourceBytes, 32))
// // Load the second 32 bytes of the string data
// part2 := mload(add(sourceBytes, 58))
// }
//}
}
// function stringToBytes32Pair(string calldata str) external pure returns(bytes32 part1, bytes32 part2) {
// bytes memory sourceBytes = bytes(str);
// require(sourceBytes.length == 59, "Source string must be 59 bytes");
// part1 = bytes(str[0:32]);
// part2 = bytes(str[32:59]);
//
// return (part1, part2);
// }