File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ // SPDX-License-Identifier: Unlicense
2+ pragma solidity >= 0.8.18 ;
3+
4+ import "ds-test/test.sol " ;
5+ import "./Vm.sol " ;
6+
7+ contract TryFfiTest is DSTest {
8+ Vm constant vm = Vm (HEVM_ADDRESS);
9+
10+ function testTryFfi () public {
11+ string [] memory inputs = new string [](3 );
12+ inputs[0 ] = "bash " ;
13+ inputs[1 ] = "-c " ;
14+ inputs[2 ] =
15+ "echo -n 0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000966666920776f726b730000000000000000000000000000000000000000000000 " ;
16+
17+ Vm.FfiResult memory f = vm.tryFfi (inputs);
18+ (string memory output ) = abi.decode (f.stdout, (string ));
19+ assertEq (output, "ffi works " , "ffi failed " );
20+ assertEq (f.exit_code, 0 , "ffi failed " );
21+ }
22+
23+ function testTryFfiFail () public {
24+ string [] memory inputs = new string [](3 );
25+ inputs[0 ] = "bash " ;
26+ inputs[1 ] = "-c " ;
27+ inputs[2 ] = "quikmafs " ;
28+
29+ Vm.FfiResult memory f = vm.tryFfi (inputs);
30+ assert (f.exit_code != 0 );
31+ assertEq (string (f.stderr), string ("bash: quikmafs: command not found\n " ));
32+ }
33+ }
Original file line number Diff line number Diff line change @@ -52,6 +52,12 @@ interface Vm {
5252 uint256 privateKey;
5353 }
5454
55+ struct FfiResult {
56+ int32 exit_code;
57+ bytes stdout;
58+ bytes stderr;
59+ }
60+
5561 // Set block.timestamp (newTimestamp)
5662 function warp (uint256 ) external ;
5763
@@ -116,6 +122,9 @@ interface Vm {
116122 // Performs a foreign function call via terminal, (stringInputs) => (result)
117123 function ffi (string [] calldata ) external returns (bytes memory );
118124
125+ // Performs a foreign function call via terminal and returns the exit code, stdout, and stderr
126+ function tryFfi (string [] calldata ) external returns (FfiResult memory );
127+
119128 // Set environment variables, (name, value)
120129 function setEnv (string calldata , string calldata ) external ;
121130
You can’t perform that action at this time.
0 commit comments