FFI

FFI

Setup

Test file:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "forge-std/Test.sol";
import "forge-std/console.sol";

// forge test --match-path test/FFI.t.sol --ffi -vvvv

contract FFITest is Test {
    function testFFI() public {
        string[] memory cmds = new string[](2);
        cmds[0] = "cat";
        cmds[1] = "ffi_test.txt";
        bytes memory res = vm.ffi(cmds);
        console.log(string(res));
    }
}

vm.ffi()

vm.ffi() lets you execute arbitrary command. For example, if we want to cat a file:

string[] memory cmds = new string[](2);
cmds[0] = "cat";
cmds[1] = "ffi_test.txt";
bytes memory res = vm.ffi(cmds);
console.log(string(res));

To run FFI, we must specify it in the terminal when doing forge test:

forge test --match-path test/FFI.t.sol --ffi -vvv

Last updated