diff options
author | Wez Furlong <wez@wezfurlong.org> | 2020-01-18 15:50:36 -0800 |
---|---|---|
committer | Wez Furlong <wez@wezfurlong.org> | 2020-01-18 15:50:36 -0800 |
commit | ebea1cce2a4c550ad11dbf10c20ab2379a37a0e2 (patch) | |
tree | 0281e763942c262b95bec85fd2b736d3cef8c342 /tests | |
parent | 4011e518e6455bb4f0a2de0aaba3763b964d0e58 (diff) | |
download | ssh2-rs-ebea1cce2a4c550ad11dbf10c20ab2379a37a0e2.zip |
Make properly Send safe
In earlier iterations I accidentally removed Send from Session and then
later restored it in an unsafe way. This commit restructures the
bindings so that each of the objects holds a reference to the
appropriate thing to keep everything alive safely, without awkward
lifetimes to deal with.
The key to this is that the underlying Session is tracked by an
Arc<Mutex<>>, with the related objects ensuring that they lock this
before they call into the underlying API.
In order to make this work, I've had to adjust the API around iterating
both known hosts and agent identities: previously these would iterate
over internal references but with this shift there isn't a reasonable
way to make that safe. The strategy is instead to return a copy of the
host/identity data and then later look up the associated raw pointer
when needed. The purist in me feels that the copy feels slightly
wasteful, but the realist justifies this with the observation that the
cardinality of both known hosts and identities is typically small enough
that the cost of this is in the noise compared to actually doing the
crypto+network ops.
I've removed a couple of error code related helpers from some of
the objects: those were really internal APIs and were redundant
with methods exported by the Error type anyway.
Fixes: https://github.com/alexcrichton/ssh2-rs/issues/154
Refs: https://github.com/alexcrichton/ssh2-rs/issues/137
Diffstat (limited to 'tests')
-rw-r--r-- | tests/all/agent.rs | 5 | ||||
-rw-r--r-- | tests/all/knownhosts.rs | 22 | ||||
-rw-r--r-- | tests/all/main.rs | 3 | ||||
-rw-r--r-- | tests/all/session.rs | 2 |
4 files changed, 18 insertions, 14 deletions
diff --git a/tests/all/agent.rs b/tests/all/agent.rs index 8feee06..b3a7963 100644 --- a/tests/all/agent.rs +++ b/tests/all/agent.rs @@ -7,9 +7,8 @@ fn smoke() { agent.connect().unwrap(); agent.list_identities().unwrap(); { - let mut a = agent.identities(); - let i1 = a.next().unwrap().unwrap(); - a.count(); + let a = agent.identities().unwrap(); + let i1 = &a[0]; assert!(agent.userauth("foo", &i1).is_err()); } agent.disconnect().unwrap(); diff --git a/tests/all/knownhosts.rs b/tests/all/knownhosts.rs index 1847797..ab83577 100644 --- a/tests/all/knownhosts.rs +++ b/tests/all/knownhosts.rs @@ -3,8 +3,9 @@ use ssh2::{KnownHostFileKind, Session}; #[test] fn smoke() { let sess = Session::new().unwrap(); - let hosts = sess.known_hosts().unwrap(); - assert_eq!(hosts.iter().count(), 0); + let known_hosts = sess.known_hosts().unwrap(); + let hosts = known_hosts.hosts().unwrap(); + assert_eq!(hosts.len(), 0); } #[test] @@ -20,11 +21,14 @@ PW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi\ /w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ== "; let sess = Session::new().unwrap(); - let mut hosts = sess.known_hosts().unwrap(); - hosts.read_str(encoded, KnownHostFileKind::OpenSSH).unwrap(); + let mut known_hosts = sess.known_hosts().unwrap(); + known_hosts + .read_str(encoded, KnownHostFileKind::OpenSSH) + .unwrap(); - assert_eq!(hosts.iter().count(), 1); - let host = hosts.iter().next().unwrap().unwrap(); + let hosts = known_hosts.hosts().unwrap(); + assert_eq!(hosts.len(), 1); + let host = &hosts[0]; assert_eq!(host.name(), None); assert_eq!( host.key(), @@ -39,10 +43,10 @@ PW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi\ ); assert_eq!( - hosts - .write_string(&host, KnownHostFileKind::OpenSSH) + known_hosts + .write_string(host, KnownHostFileKind::OpenSSH) .unwrap(), encoded ); - hosts.remove(host).unwrap(); + known_hosts.remove(host).unwrap(); } diff --git a/tests/all/main.rs b/tests/all/main.rs index f74684a..87380eb 100644 --- a/tests/all/main.rs +++ b/tests/all/main.rs @@ -36,7 +36,8 @@ pub fn authed_session() -> ssh2::Session { let mut agent = sess.agent().unwrap(); agent.connect().unwrap(); agent.list_identities().unwrap(); - let identity = agent.identities().next().unwrap().unwrap(); + let identities = agent.identities().unwrap(); + let identity = &identities[0]; agent.userauth(&user, &identity).unwrap(); } assert!(sess.authenticated()); diff --git a/tests/all/session.rs b/tests/all/session.rs index 8894081..ee3eaa0 100644 --- a/tests/all/session.rs +++ b/tests/all/session.rs @@ -51,7 +51,7 @@ fn smoke_handshake() { agent.connect().unwrap(); agent.list_identities().unwrap(); { - let identity = agent.identities().next().unwrap().unwrap(); + let identity = &agent.identities().unwrap()[0]; agent.userauth(&user, &identity).unwrap(); } assert!(sess.authenticated()); |