From 59a59ac5bf9cc9be2698eb35376c3b58fd483252 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Fri, 15 Jan 2021 09:35:04 +0100 Subject: net: Decouple from core. - Move core::NetworkPolicy to net::Policy, update all code accordingly. --- store/src/backend/mod.rs | 68 +++++++++--------- store/src/lib.rs | 173 +++++++++++++++++++++++----------------------- store/tests/ipc-policy.rs | 17 +++-- 3 files changed, 128 insertions(+), 130 deletions(-) (limited to 'store') diff --git a/store/src/backend/mod.rs b/store/src/backend/mod.rs index 9947b28c..8e913436 100644 --- a/store/src/backend/mod.rs +++ b/store/src/backend/mod.rs @@ -285,10 +285,10 @@ impl MappingServer { MappingServer{c: c, id: id} } - fn open(c: Rc, realm: &str, policy: core::NetworkPolicy, name: &str) + fn open(c: Rc, realm: &str, policy: net::Policy, name: &str) -> Result { // We cannot implement ToSql and friends for - // core::NetworkPolicy, hence we need to do it by foot. + // net::Policy, hence we need to do it by foot. let p: u8 = (&policy).into(); c.execute( @@ -300,14 +300,14 @@ impl MappingServer { |row| Ok((row.get(0)?, row.get(1)?)))?; // We cannot implement FromSql and friends for - // core::NetworkPolicy, hence we need to do it by foot. + // net::Policy, hence we need to do it by foot. if mapping_policy < 0 || mapping_policy > 3 { return Err(super::Error::ProtocolError.into()); } - let mapping_policy = core::NetworkPolicy::from(mapping_policy as u8); + let mapping_policy = net::Policy::from(mapping_policy as u8); if mapping_policy != policy { - return Err(core::Error::NetworkPolicyViolation(mapping_policy) + return Err(net::Error::PolicyViolation(mapping_policy) .into()); } @@ -839,7 +839,7 @@ impl KeyServer { } /// Returns when the next key using the given policy should be updated. - fn next_update_at(c: &Rc, network_policy: core::NetworkPolicy) + fn next_update_at(c: &Rc, network_policy: net::Policy) -> Option { let network_policy_u8 = u8::from(&network_policy); @@ -856,7 +856,7 @@ impl KeyServer { } /// Returns the number of keys using the given policy. - fn need_update(c: &Rc, network_policy: core::NetworkPolicy) + fn need_update(c: &Rc, network_policy: net::Policy) -> Result { let network_policy_u8 = u8::from(&network_policy); @@ -872,11 +872,11 @@ impl KeyServer { /// Helper for `update`. fn update_helper(c: &Rc, - network_policy: core::NetworkPolicy) + network_policy: net::Policy) -> Result<(KeyServer, openpgp::KeyID, net::KeyServer)> { - assert!(network_policy != core::NetworkPolicy::Offline); + assert!(network_policy != net::Policy::Offline); let network_policy_u8 = u8::from(&network_policy); // Select the key that was updated least recently. @@ -892,9 +892,7 @@ impl KeyServer { let fingerprint = fingerprint.parse::() .map_err(|_| node::Error::SystemError)?; - let ctx = core::Context::configure() - .network_policy(network_policy).build()?; - let keyserver = net::KeyServer::keys_openpgp_org(&ctx)?; + let keyserver = net::KeyServer::keys_openpgp_org(network_policy)?; Ok((KeyServer::new(c.clone(), id), fingerprint.into(), @@ -903,7 +901,7 @@ impl KeyServer { /// Updates the key that was least recently updated. async fn update(c: &Rc, - network_policy: core::NetworkPolicy) + network_policy: net::Policy) -> Result { let (key, id, mut keyserver) = Self::update_helper(c, network_policy)?; @@ -938,7 +936,7 @@ impl KeyServer { /// Perform periodic housekeeping. async fn start_housekeeping(c: Rc) { loop { - let duration = Self::update(&c, core::NetworkPolicy::Encrypted).await; + let duration = Self::update(&c, net::Policy::Encrypted).await; let duration = duration.unwrap_or(min_sleep_time()); tokio::time::delay_for(random_duration(duration)).await; @@ -1110,11 +1108,11 @@ impl node::mapping_iter::Server for MappingIterServer { |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)))); // We cannot implement FromSql and friends for - // core::NetworkPolicy, hence we need to do it by foot. + // net::Policy, hence we need to do it by foot. if network_policy < 0 || network_policy > 3 { fail!(node::Error::SystemError); } - let network_policy = core::NetworkPolicy::from(network_policy as u8); + let network_policy = net::Policy::from(network_policy as u8); let mut entry = pry!(results.get().get_result()).init_ok(); entry.set_realm(&realm); @@ -1255,17 +1253,17 @@ impl From for node::Error { } } - if let Some(e) = e.downcast_ref::() { + if let Some(e) = e.downcast_ref::() { return match e { - &core::Error::NetworkPolicyViolation(p) => + &net::Error::PolicyViolation(p) => match p { - core::NetworkPolicy::Offline => + net::Policy::Offline => node::Error::NetworkPolicyViolationOffline, - core::NetworkPolicy::Anonymized => + net::Policy::Anonymized => node::Error::NetworkPolicyViolationAnonymized, - core::NetworkPolicy::Encrypted => + net::Policy::Encrypted => node::Error::NetworkPolicyViolationEncrypted, - core::NetworkPolicy::Insecure => + net::Policy::Insecure => node::Error::NetworkPolicyViolationInsecure, }, _ => unreachable!(), @@ -1399,30 +1397,30 @@ CREATE TABLE log ( /* Miscellaneous. */ -impl<'a> From<&'a core::NetworkPolicy> for node::NetworkPolicy { - fn from(policy: &core::NetworkPolicy) -> Self { +impl<'a> From<&'a net::Policy> for node::NetworkPolicy { + fn from(policy: &net::Policy) -> Self { match policy { - &core::NetworkPolicy::Offline => node::NetworkPolicy::Offline, - &core::NetworkPolicy::Anonymized => node::NetworkPolicy::Anonymized, - &core::NetworkPolicy::Encrypted => node::NetworkPolicy::Encrypted, - &core::NetworkPolicy::Insecure => node::NetworkPolicy::Insecure, + &net::Policy::Offline => node::NetworkPolicy::Offline, + &net::Policy::Anonymized => node::NetworkPolicy::Anonymized, + &net::Policy::Encrypted => node::NetworkPolicy::Encrypted, + &net::Policy::Insecure => node::NetworkPolicy::Insecure, } } } -impl From for node::NetworkPolicy { - fn from(policy: core::NetworkPolicy) -> Self { +impl From for node::NetworkPolicy { + fn from(policy: net::Policy) -> Self { (&policy).into() } } -impl From for core::NetworkPolicy { +impl From for net::Policy { fn from(policy: node::NetworkPolicy) -> Self { match policy { - node::NetworkPolicy::Offline => core::NetworkPolicy::Offline, - node::NetworkPolicy::Anonymized => core::NetworkPolicy::Anonymized, - node::NetworkPolicy::Encrypted => core::NetworkPolicy::Encrypted, - node::NetworkPolicy::Insecure => core::NetworkPolicy::Insecure, + node::NetworkPolicy::Offline => net::Policy::Offline, + node::NetworkPolicy::Anonymized => net::Policy::Anonymized, + node::NetworkPolicy::Encrypted => net::Policy::Encrypted, + node::NetworkPolicy::Insecure => net::Policy::Insecure, } } } diff --git a/store/src/lib.rs b/store/src/lib.rs index d91bd59c..93b6402c 100644 --- a/store/src/lib.rs +++ b/store/src/lib.rs @@ -14,22 +14,23 @@ //! Sequoia updates keys in compliance with the [network policy] used //! to create the mapping. //! -//! [network policy]: ../sequoia_core/enum.NetworkPolicy.html +//! [network policy]: ../sequoia_net/enum.Policy.html //! //! # Examples //! //! ``` //! # use sequoia_openpgp as openpgp; //! # use openpgp::Fingerprint; -//! # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; +//! # use sequoia_core::{Context, IPCPolicy}; +//! # use sequoia_net as net; //! # use sequoia_store::*; //! # fn main() { f().unwrap(); } //! # fn f() -> Result<()> { //! # let ctx = Context::configure() -//! # .network_policy(NetworkPolicy::Offline) //! # .ipc_policy(IPCPolicy::Internal) //! # .ephemeral().build()?; -//! let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; +//! let mapping = Mapping::open(&ctx, net::Policy::Offline, +//! REALM_CONTACTS, "default")?; //! //! let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); //! let binding = mapping.add("Mister B.", &fp)?; @@ -63,7 +64,7 @@ use capnp_rpc::rpc_twoparty_capnp::Side; use sequoia_openpgp as openpgp; #[allow(unused_imports)] -use sequoia_core; +use sequoia_core as core; use sequoia_ipc; use crate::openpgp::Fingerprint; @@ -71,9 +72,9 @@ use crate::openpgp::KeyID; use crate::openpgp::Cert; use crate::openpgp::parse::Parse; use crate::openpgp::serialize::Serialize; -use sequoia_core as core; use sequoia_core::Context; use sequoia_ipc as ipc; +use sequoia_net as net; #[allow(dead_code)] mod store_protocol_capnp; use crate::store_protocol_capnp::node; @@ -151,12 +152,11 @@ impl Store { /// # use sequoia_openpgp as openpgp; /// # use openpgp::Cert; /// # use openpgp::parse::Parse; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; /// # use sequoia_store::{Store, Result}; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let cert = Cert::from_bytes( @@ -185,12 +185,11 @@ impl Store { /// # use sequoia_openpgp as openpgp; /// # use openpgp::Cert; /// # use openpgp::parse::Parse; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; /// # use sequoia_store::{Store, Result}; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let cert = Cert::from_bytes( @@ -218,12 +217,11 @@ impl Store { /// # use sequoia_openpgp as openpgp; /// # use openpgp::Cert; /// # use openpgp::parse::Parse; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; /// # use sequoia_store::{Store, Result}; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let cert = Cert::from_bytes( @@ -252,12 +250,11 @@ impl Store { /// # use sequoia_openpgp as openpgp; /// # use openpgp::{Cert, KeyID}; /// # use openpgp::parse::Parse; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; /// # use sequoia_store::{Store, Result}; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let cert = Cert::from_bytes( @@ -333,12 +330,13 @@ impl Mapping { /// of the context that created the mapping in the first place. /// Opening the mapping with a different network policy is /// forbidden. - pub fn open(c: &Context, realm: &str, name: &str) -> Result { + pub fn open(c: &Context, p: net::Policy, realm: &str, name: &str) + -> Result { let (mut core, client) = Store::connect(c)?; let mut request = client.open_request(); request.get().set_realm(realm); - request.get().set_network_policy(c.network_policy().into()); + request.get().set_network_policy(p.into()); request.get().set_ephemeral(c.ephemeral()); request.get().set_name(name); @@ -366,15 +364,16 @@ impl Mapping { /// ``` /// # use sequoia_openpgp as openpgp; /// # use openpgp::Fingerprint; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); /// mapping.add("Mister B.", &fp)?; /// # Ok(()) @@ -396,17 +395,18 @@ impl Mapping { /// # use sequoia_openpgp as openpgp; /// # use openpgp::Cert; /// # use openpgp::parse::Parse; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let cert = Cert::from_bytes( /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// mapping.import("Testy McTestface", &cert)?; /// # Ok(()) /// # } @@ -428,20 +428,22 @@ impl Mapping { /// ``` /// # use sequoia_openpgp as openpgp; /// # use openpgp::Fingerprint; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); /// mapping.add("Mister B.", &fp)?; /// drop(mapping); /// // ... - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// let binding = mapping.lookup("Mister B.")?; /// # Ok(()) /// # } @@ -463,18 +465,19 @@ impl Mapping { /// # use sequoia_openpgp as openpgp; /// # use openpgp::{Cert, KeyID}; /// # use openpgp::parse::Parse; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let cert = Cert::from_bytes( /// # &include_bytes!("../../openpgp/tests/data/keys/emmelie-dorothea-dina-samantha-awina-ed25519.pgp")[..]) /// # .unwrap(); - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// mapping.import("Emmelie", &cert)?; /// /// // Lookup by the primary key's KeyID. @@ -506,20 +509,22 @@ impl Mapping { /// # use sequoia_openpgp as openpgp; /// # #[macro_use] use sequoia_core; /// # use openpgp::Fingerprint; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); /// mapping.add("Mister B.", &fp)?; /// mapping.delete()?; /// // ... - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// let binding = mapping.lookup("Mister B."); /// assert!(binding.is_err()); // not found /// # Ok(()) @@ -596,15 +601,16 @@ impl Binding { /// ``` /// # use sequoia_openpgp as openpgp; /// # use openpgp::Fingerprint; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); /// let binding = mapping.add("Mister B.", &fp)?; @@ -659,19 +665,20 @@ impl Binding { /// # #[macro_use] use sequoia_core; /// # use openpgp::Cert; /// # use openpgp::parse::Parse; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let old = Cert::from_bytes( /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// # let new = Cert::from_bytes( /// # &include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")[..]).unwrap(); - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// mapping.import("Testy McTestface", &old)?; /// // later... /// let binding = mapping.lookup("Testy McTestface")?; @@ -713,19 +720,20 @@ impl Binding { /// # #[macro_use] use sequoia_core; /// # use openpgp::Cert; /// # use openpgp::parse::Parse; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let old = Cert::from_bytes( /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// # let new = Cert::from_bytes( /// # &include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")[..]).unwrap(); - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// mapping.import("Testy McTestface", &old)?; /// // later... /// let binding = mapping.lookup("Testy McTestface")?; @@ -756,15 +764,16 @@ impl Binding { /// # use sequoia_openpgp as openpgp; /// # #[macro_use] use sequoia_core; /// # use openpgp::Fingerprint; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); /// let binding = mapping.add("Mister B.", &fp)?; /// binding.delete()?; @@ -863,19 +872,20 @@ impl Key { /// # use openpgp::Fingerprint; /// # use openpgp::Cert; /// # use openpgp::parse::Parse; - /// # use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; + /// # use sequoia_core::{Context, IPCPolicy}; + /// # use sequoia_net as net; /// # use sequoia_store::*; /// # fn main() { f().unwrap(); } /// # fn f() -> Result<()> { /// # let ctx = Context::configure() - /// # .network_policy(NetworkPolicy::Offline) /// # .ipc_policy(IPCPolicy::Internal) /// # .ephemeral().build()?; /// # let old = Cert::from_bytes( /// # &include_bytes!("../../openpgp/tests/data/keys/testy.pgp")[..]).unwrap(); /// # let new = Cert::from_bytes( /// # &include_bytes!("../../openpgp/tests/data/keys/testy-new.pgp")[..]).unwrap(); - /// let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default")?; + /// let mapping = Mapping::open(&ctx, net::Policy::Offline, + /// REALM_CONTACTS, "default")?; /// let fp = "3E8877C877274692975189F5D03F6F865226FE8B".parse().unwrap(); /// let binding = mapping.add("Testy McTestface", &fp)?; /// let key = binding.key()?; @@ -1036,7 +1046,7 @@ pub struct MappingIter { } impl Iterator for MappingIter { - type Item = (String, String, core::NetworkPolicy, Mapping); + type Item = (String, String, net::Policy, Mapping); fn next(&mut self) -> Option { let request = self.iter.next_request(); @@ -1152,13 +1162,13 @@ impl From for anyhow::Error { node::Error::MalformedFingerprint => Error::MalformedFingerprint.into(), node::Error::NetworkPolicyViolationOffline => - core::Error::NetworkPolicyViolation(core::NetworkPolicy::Offline).into(), + net::Error::PolicyViolation(net::Policy::Offline).into(), node::Error::NetworkPolicyViolationAnonymized => - core::Error::NetworkPolicyViolation(core::NetworkPolicy::Anonymized).into(), + net::Error::PolicyViolation(net::Policy::Anonymized).into(), node::Error::NetworkPolicyViolationEncrypted => - core::Error::NetworkPolicyViolation(core::NetworkPolicy::Encrypted).into(), + net::Error::PolicyViolation(net::Policy::Encrypted).into(), node::Error::NetworkPolicyViolationInsecure => - core::Error::NetworkPolicyViolation(core::NetworkPolicy::Insecure).into(), + net::Error::PolicyViolation(net::Policy::Insecure).into(), } } } @@ -1232,34 +1242,34 @@ mod test { ( $x:expr ) => { include_bytes!(concat!("../../openpgp/tests/data/keys/", $x)) }; } + const P: net::Policy = net::Policy::Offline; + #[test] fn mapping_network_policy_mismatch() { let ctx = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); // Create mapping. - Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let ctx2 = core::Context::configure() .home(ctx.home()) - .network_policy(core::NetworkPolicy::Encrypted) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let mapping = Mapping::open(&ctx2, REALM_CONTACTS, "default"); - assert_match!(core::Error::NetworkPolicyViolation(_) - = mapping.err().unwrap().downcast::().unwrap()); + let mapping = Mapping::open(&ctx2, net::Policy::Encrypted, + REALM_CONTACTS, "default"); + assert_match!(net::Error::PolicyViolation(_) + = mapping.err().unwrap().downcast::().unwrap()); } #[test] fn import_key() { let ctx = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let cert = Cert::from_bytes(&bytes!("testy.pgp")[..]).unwrap(); mapping.import("Mr. McTestface", &cert).unwrap(); let binding = mapping.lookup("Mr. McTestface").unwrap(); @@ -1271,10 +1281,9 @@ mod test { fn key_not_found() { let ctx = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let r = mapping.lookup("I do not exist"); assert_match!(Error::NotFound = r.err().unwrap().downcast::().unwrap()); @@ -1284,10 +1293,9 @@ mod test { fn add_then_import_wrong_key() { let ctx = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let cert = Cert::from_bytes(&bytes!("testy.pgp")[..]).unwrap(); let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); let binding = mapping.add("Mister B.", &fp).unwrap(); @@ -1300,10 +1308,9 @@ mod test { fn add_then_add_different_key() { let ctx = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let b = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); mapping.add("Mister B.", &b).unwrap(); let c = Fingerprint::from_bytes(b"cccccccccccccccccccc"); @@ -1316,11 +1323,10 @@ mod test { fn delete_mapping_twice() { let ctx = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let s0 = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); - let s1 = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let s0 = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); + let s1 = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); s0.delete().unwrap(); s1.delete().unwrap(); } @@ -1329,11 +1335,10 @@ mod test { fn delete_mapping_then_use() { let ctx = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let s0 = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); - let s1 = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let s0 = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); + let s1 = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); s0.delete().unwrap(); let binding = s1.lookup("Foobarbaz"); assert_match!(Error::NotFound @@ -1348,10 +1353,9 @@ mod test { fn delete_binding_twice() { let ctx = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); let b0 = mapping.add("Mister B.", &fp).unwrap(); let b1 = mapping.lookup("Mister B.").unwrap(); @@ -1363,10 +1367,9 @@ mod test { fn delete_binding_then_use() { let ctx = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); let b0 = mapping.add("Mister B.", &fp).unwrap(); let b1 = mapping.lookup("Mister B.").unwrap(); @@ -1380,23 +1383,21 @@ mod test { fn make_some_mappings() -> core::Context { let ctx0 = core::Context::configure() .ephemeral() - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); - let mapping = Mapping::open(&ctx0, REALM_CONTACTS, "default").unwrap(); + let mapping = Mapping::open(&ctx0, P, REALM_CONTACTS, "default").unwrap(); let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); mapping.add("Mister B.", &fp).unwrap(); mapping.add("B4", &fp).unwrap(); - Mapping::open(&ctx0, REALM_CONTACTS, "another mapping").unwrap(); + Mapping::open(&ctx0, P, REALM_CONTACTS, "another mapping").unwrap(); let ctx1 = core::Context::configure() .home(ctx0.home()) - .network_policy(core::NetworkPolicy::Offline) .ipc_policy(core::IPCPolicy::Internal) .build().unwrap(); let mapping = - Mapping::open(&ctx1, REALM_SOFTWARE_UPDATES, "default").unwrap(); + Mapping::open(&ctx1, P, REALM_SOFTWARE_UPDATES, "default").unwrap(); let fp = Fingerprint::from_bytes(b"cccccccccccccccccccc"); mapping.add("Mister C.", &fp).unwrap(); @@ -1406,7 +1407,7 @@ mod test { #[test] fn stats() { let ctx = make_some_mappings(); - let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); let binding = mapping.add("Mister B.", &fp).unwrap(); @@ -1446,13 +1447,13 @@ mod test { let (realm, name, network_policy, mapping) = iter.next().unwrap(); assert_eq!(realm, REALM_CONTACTS); assert_eq!(name, "default"); - assert_eq!(network_policy, core::NetworkPolicy::Offline); + assert_eq!(network_policy, net::Policy::Offline); let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); mapping.add("Mister B.", &fp).unwrap(); let (realm, name, network_policy, mapping) = iter.next().unwrap(); assert_eq!(realm, REALM_CONTACTS); assert_eq!(name, "another mapping"); - assert_eq!(network_policy, core::NetworkPolicy::Offline); + assert_eq!(network_policy, net::Policy::Offline); mapping.add("Mister B.", &fp).unwrap(); assert!(iter.next().is_none()); } @@ -1460,7 +1461,7 @@ mod test { #[test] fn binding_iterator() { let ctx = make_some_mappings(); - let mapping = Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + let mapping = Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let mut iter = mapping.iter().unwrap(); let (label, fingerprint, binding) = iter.next().unwrap(); let fp = Fingerprint::from_bytes(b"bbbbbbbbbbbbbbbbbbbb"); diff --git a/store/tests/ipc-policy.rs b/store/tests/ipc-policy.rs index ccac8369..e744b036 100644 --- a/store/tests/ipc-policy.rs +++ b/store/tests/ipc-policy.rs @@ -4,18 +4,20 @@ use sequoia_store; use std::env::current_exe; use std::path::PathBuf; -use sequoia_core::{Context, NetworkPolicy, IPCPolicy}; +use sequoia_core::{Context, IPCPolicy}; use sequoia_store::{Mapping, REALM_CONTACTS}; +use sequoia_net as net; + +const P: net::Policy = net::Policy::Offline; #[test] fn ipc_policy_external() { let ctx = Context::configure() .ephemeral() .lib(current_exe().unwrap().parent().unwrap().parent().unwrap()) - .network_policy(NetworkPolicy::Offline) .ipc_policy(IPCPolicy::External) .build().unwrap(); - Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); } #[test] @@ -23,10 +25,9 @@ fn ipc_policy_internal() { let ctx = Context::configure() .ephemeral() .lib(PathBuf::from("/i/do/not/exist")) - .network_policy(NetworkPolicy::Offline) .ipc_policy(IPCPolicy::Internal) .build().unwrap(); - Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); } #[test] @@ -34,16 +35,14 @@ fn ipc_policy_robust() { let ctx = Context::configure() .ephemeral() .lib(current_exe().unwrap().parent().unwrap().parent().unwrap()) - .network_policy(NetworkPolicy::Offline) .ipc_policy(IPCPolicy::Robust) .build().unwrap(); - Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); let ctx = Context::configure() .ephemeral() .lib(PathBuf::from("/i/do/not/exist")) - .network_policy(NetworkPolicy::Offline) .ipc_policy(IPCPolicy::Robust) .build().unwrap(); - Mapping::open(&ctx, REALM_CONTACTS, "default").unwrap(); + Mapping::open(&ctx, P, REALM_CONTACTS, "default").unwrap(); } -- cgit v1.2.3