diff --git a/src/node.rs b/src/node.rs index 7d185d8..3e33a32 100644 --- a/src/node.rs +++ b/src/node.rs @@ -3074,12 +3074,16 @@ pub(crate) mod tests { /// A stand-in for `monero-wallet-rpc`: it records its argv, then does what /// the test needs. The wallet child spawns a real process, so this is the /// only honest way to drive the start path offline. + /// + /// The argv is `mv`d into place in one step rather than truncated and + /// appended: a poller must never read half of it. It did — the port was + /// parsed while it was still being written, so `42765` read as `427`, and + /// the mock was bound on a port nothing dialled. fn fake_child_bin(dir: &Path, name: &str, log: &Path, body: &str) -> PathBuf { std::fs::create_dir_all(dir).expect("fake bin dir"); let path = dir.join(name); let script = format!( - "#!/bin/sh\n: > '{}'\nprintf '%s\\n' \"$0\" >> '{}'\nfor a in \"$@\"; do printf '%s\\n' \"$a\" >> '{}'; done\n{body}\n", - log.display(), + "#!/bin/sh\ntmp='{}'.$$\n: > \"$tmp\"\nprintf '%s\\n' \"$0\" >> \"$tmp\"\nfor a in \"$@\"; do printf '%s\\n' \"$a\" >> \"$tmp\"; done\nmv -f \"$tmp\" '{}'\n{body}\n", log.display(), log.display() ); diff --git a/src/wallet_child.rs b/src/wallet_child.rs index d07b3fb..13357de 100644 --- a/src/wallet_child.rs +++ b/src/wallet_child.rs @@ -1233,17 +1233,24 @@ mod tests { } /// A stand-in for monerod / monero-wallet-rpc: records its argv, then parks. + /// + /// The argv lands in `` with one `mv`, not a truncate-then-append: a + /// reader must never see half an argv. It did — the poll below could catch + /// `--rpc-bind-port` while the port was still being written and parse + /// `427` out of `42765`, a valid-but-wrong port, and the test then bound a + /// mock nobody dialled. fn fake_bin(dir: &Path, name: &str, log: &Path) -> PathBuf { let path = dir.join(name); let mut f = fs::File::create(&path).unwrap(); writeln!(f, "#!/bin/sh").unwrap(); - writeln!(f, ": > '{}'", log.display()).unwrap(); + writeln!(f, "tmp='{}'.$$", log.display()).unwrap(); + writeln!(f, ": > \"$tmp\"").unwrap(); writeln!( f, - "for a in \"$@\"; do printf '%s\\n' \"$a\" >> '{}'; done", - log.display() + "for a in \"$@\"; do printf '%s\\n' \"$a\" >> \"$tmp\"; done" ) .unwrap(); + writeln!(f, "mv -f \"$tmp\" '{}'", log.display()).unwrap(); writeln!(f, "sleep 30").unwrap(); drop(f); let mut perms = fs::metadata(&path).unwrap().permissions();