Skip to content

A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.

Rust

Cover session cleanup after ADB forward removal fails (#140)

Vishnu Srivatsavaae667e9e93ba
diff --git a/src-tauri/src/session.rs b/src-tauri/src/session.rs
index 8f91970..fd19d7b 100644
--- a/src-tauri/src/session.rs
+++ b/src-tauri/src/session.rs
@@ -435,10 +435,6 @@ mod tests {
use std::os::unix::fs::PermissionsExt;
use std::sync::OnceLock;
- fn quote(path: &Path) -> String {
- format!("'{}'", path.to_string_lossy().replace('\'', "'\\''"))
- }
-
fn daemon() -> &'static Path {
static BINARY: OnceLock<PathBuf> = OnceLock::new();
BINARY.get_or_init(|| {
@@ -505,16 +501,9 @@ mod tests {
.unwrap()
.join("tests/fake_session_adb.py");
let executable = base.join("fake-adb");
- fs::write(
- &executable,
- format!(
- "#!/bin/sh\nexec python3 {} {} \"$@\"\n",
- quote(&helper),
- quote(&file)
- ),
- )
- .unwrap();
- fs::set_permissions(&executable, fs::Permissions::from_mode(0o700)).unwrap();
+ // Use a checked-in executable. A concurrent fork can inherit
+ // an open writer for a freshly generated script on Linux.
+ std::os::unix::fs::symlink(helper, &executable).unwrap();
executable
}
fn rows(&self) -> Vec<Vec<String>> {
@@ -784,6 +773,27 @@ mod tests {
);
}
+ #[test]
+ fn failed_forward_removal_still_stops_the_owned_daemon_and_removes_its_files() {
+ let device = Device::new();
+ let adb = device.client("remove-failure", "same");
+ let (mut session, _) =
+ Session::start(adb.clone(), daemon(), "same".into(), false).unwrap();
+ let address = session.connection.address;
+ let forwards = device.rows();
+ fail(&adb, "remove-forward");
+
+ let errors = session.cleanup();
+ assert_eq!(errors.len(), 1, "{errors:?}");
+ assert!(errors[0].contains("Injected remove-forward failure"), "{errors:?}");
+ assert_eq!(device.rows(), forwards);
+ assert_eq!(device.directories(), 0);
+ assert!(
+ std::net::TcpStream::connect_timeout(&address, Duration::from_millis(200)).is_err()
+ );
+ assert!(session.cleanup().is_empty());
+ }
+
#[test]
fn cleanup_preserves_a_forward_that_was_rebound_to_a_foreign_endpoint() {
let device = Device::new();
@@ -796,13 +806,15 @@ mod tests {
.unwrap();
let mut rows = device.rows();
let local = format!("tcp:{}", session.connection.address.port());
- rows.iter_mut().find(|row| row[1] == local).unwrap()[2] = "tcp:9999".into();
+ let foreign_port = if session.device_port == Some(9999) { 9998 } else { 9999 };
+ rows.iter_mut().find(|row| row[1] == local).unwrap()[2] = format!("tcp:{foreign_port}");
fs::write(
device.root.join("forwards.json"),
serde_json::to_vec(&rows).unwrap(),
)
.unwrap();
- assert!(session.cleanup().is_empty());
+ let errors = session.cleanup();
+ assert!(errors.is_empty(), "{errors:?}");
assert_eq!(device.rows(), rows);
assert_eq!(device.directories(), 0);
}
diff --git a/tests/fake_session_adb.py b/tests/fake_session_adb.py
old mode 100644
new mode 100755
index 4092c24..ae49b24
--- a/tests/fake_session_adb.py
+++ b/tests/fake_session_adb.py
@@ -14,8 +14,8 @@ import subprocess
import sys
import time
-config = json.loads(Path(sys.argv[1]).read_text())
-args = sys.argv[2:]
+config = json.loads(Path(sys.argv[0]).with_name("config.json").read_text())
+args = sys.argv[1:]
if args not in (["devices", "-l"], ["version"]):
assert args[:2] == ["-s", config["serial"]], args
args = args[2:]
@@ -89,6 +89,7 @@ elif args[0] == "forward":
for row in rows:
print(" ".join(row))
elif args[1] == "--remove":
+ fail("remove-forward")
rows = [row for row in rows if not (row[0] == config["serial"] and row[1] == args[2])]
registry.write_text(json.dumps(rows))
else: