oxedyne/fe2o3/fe2o3_crypto/tests/keystore_admin_names.rs
3.9 KiB, 1 run
created by r4207234560:77720, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Admin names in a `Wallet`: a removal never leaves no entry to unlock with, whatever the |
| 2 | //! names, a second entry of a name already held is refused, and a store that already holds |
| 3 | //! one name twice still opens with every key it holds. |
| 4 | |
| 5 | use oxedyne_fe2o3_core::prelude::*; |
| 6 | use oxedyne_fe2o3_crypto::keystore::{ |
| 7 | AdminUser, |
| 8 | DEFAULT_WALLET_KDF_NAME, |
| 9 | WALLET_MASTER_KEY_LEN, |
| 10 | Wallet, |
| 11 | }; |
| 12 | use oxedyne_fe2o3_jdat::prelude::*; |
| 13 | |
| 14 | use secrecy::ExposeSecret; |
| 15 | |
| 16 | |
| 17 | const MASTER: [u8; WALLET_MASTER_KEY_LEN] = [0x5a; WALLET_MASTER_KEY_LEN]; // throwaway |
| 18 | |
| 19 | fn entry(name: &str, pass: &[u8]) -> Outcome<AdminUser> { |
| 20 | AdminUser::new(name, pass, &MASTER, DEFAULT_WALLET_KDF_NAME, vec!["*".to_string()], 0) |
| 21 | } |
| 22 | |
| 23 | /// One admin entered twice under one name, as an older store may hold it, plus `others`. |
| 24 | fn twice(others: Vec<AdminUser>) -> Outcome<Wallet> { |
| 25 | let alice = res!(entry("alice", b"alicepass")); |
| 26 | let mut admins = vec![alice.clone(), alice]; |
| 27 | admins.extend(others); |
| 28 | Ok(Wallet::new(DaticleMap::new(), admins, DaticleMap::new())) |
| 29 | } |
| 30 | |
| 31 | fn names(w: &Wallet) -> Vec<String> { |
| 32 | w.admins().iter().map(|a| a.name.clone()).collect() |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn removing_a_name_held_by_every_entry_is_refused() -> Outcome<()> { |
| 37 | // req! evaluates its arguments twice, so each removal is bound first. |
| 38 | let mut w = res!(twice(Vec::new())); |
| 39 | let refused = w.remove_by_name("alice").is_err(); |
| 40 | req!((refused, w.admins().len()), (true, 2), "remove_by_name"); |
| 41 | let refused = w.remove_admin(b"alicepass", "alice").is_err(); |
| 42 | req!((refused, w.admins().len()), (true, 2), "remove_admin"); |
| 43 | let u = res!(w.unlock(b"alicepass")); |
| 44 | req!(u.master_key.expose_secret().as_slice(), &MASTER[..]); |
| 45 | Ok(()) |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn removing_a_name_held_twice_beside_another_is_allowed() -> Outcome<()> { |
| 50 | let bob = res!(entry("bob", b"bobpass")); |
| 51 | // Every entry of the name goes, so no twin of a revoked admin stays able to unlock. |
| 52 | let mut w = res!(twice(vec![bob.clone()])); |
| 53 | res!(w.remove_by_name("alice")); |
| 54 | req!(names(&w), vec!["bob".to_string()]); |
| 55 | req!(w.unlock(b"alicepass").is_err(), true); |
| 56 | let mut w = res!(twice(vec![bob])); |
| 57 | res!(w.remove_admin(b"bobpass", "alice")); |
| 58 | req!(names(&w), vec!["bob".to_string()]); |
| 59 | // The other way round, the twin pair is what remains. |
| 60 | let mut w = res!(twice(vec![res!(entry("bob", b"bobpass"))])); |
| 61 | res!(w.remove_admin(b"alicepass", "bob")); |
| 62 | req!(names(&w), vec!["alice".to_string(), "alice".to_string()]); |
| 63 | Ok(()) |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn adding_a_name_already_held_is_refused() -> Outcome<()> { |
| 68 | let (mut w, u) = res!(Wallet::create_with_first_admin( |
| 69 | DaticleMap::new(), "alice", b"alicepass", DEFAULT_WALLET_KDF_NAME, |
| 70 | )); |
| 71 | let master = u.master_key.expose_secret().clone(); |
| 72 | match w.add_admin(b"alicepass", "alice", b"other", vec!["*".to_string()], 0, DEFAULT_WALLET_KDF_NAME) { |
| 73 | Ok(()) => return Err(err!("add_admin took a second 'alice'."; Test, Unexpected)), |
| 74 | Err(e) => req!(fmt!("{}", e).contains("'alice'"), true, "error does not name the admin"), |
| 75 | } |
| 76 | match w.enrol(&master, "alice", b"other", vec!["*".to_string()], 0, DEFAULT_WALLET_KDF_NAME) { |
| 77 | Ok(()) => return Err(err!("enrol took a second 'alice'."; Test, Unexpected)), |
| 78 | Err(e) => req!(fmt!("{}", e).contains("'alice'"), true, "error does not name the admin"), |
| 79 | } |
| 80 | req!(names(&w), vec!["alice".to_string()]); |
| 81 | Ok(()) |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn a_store_holding_a_name_twice_still_opens_with_every_key() -> Outcome<()> { |
| 86 | let alice = res!(entry("alice", b"alicepass")); |
| 87 | let twin = res!(entry("alice", b"twinpass")); |
| 88 | let w = Wallet::new(DaticleMap::new(), vec![alice, twin], DaticleMap::new()); |
| 89 | let w = res!(Wallet::from_dat(res!(w.to_dat()))); |
| 90 | req!(w.admins().len(), 2); |
| 91 | for pass in [&b"alicepass"[..], &b"twinpass"[..]] { |
| 92 | let u = res!(w.unlock(pass)); |
| 93 | req!(u.master_key.expose_secret().as_slice(), &MASTER[..]); |
| 94 | } |
| 95 | Ok(()) |
| 96 | } |