|
| 1 | +// Copyright (c) 2019, Ben Boeckel |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without modification, |
| 5 | +// are permitted provided that the following conditions are met: |
| 6 | +// |
| 7 | +// * Redistributions of source code must retain the above copyright notice, |
| 8 | +// this list of conditions and the following disclaimer. |
| 9 | +// * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// * Neither the name of this project nor the names of its contributors |
| 13 | +// may be used to endorse or promote products derived from this software |
| 14 | +// without specific prior written permission. |
| 15 | +// |
| 16 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 20 | +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 23 | +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +use serial_test_derive::serial; |
| 28 | + |
| 29 | +use crate::keytypes; |
| 30 | +use crate::{KeyType, Keyring, Permission, SpecialKeyring}; |
| 31 | + |
| 32 | +use super::utils::kernel::*; |
| 33 | + |
| 34 | +#[test] |
| 35 | +#[serial(join_session)] |
| 36 | +fn join_anonymous_session() { |
| 37 | + let session_before = Keyring::attach_or_create(SpecialKeyring::Session).unwrap(); |
| 38 | + let keyring = Keyring::join_anonymous_session().unwrap(); |
| 39 | + let session_after = Keyring::attach_or_create(SpecialKeyring::Session).unwrap(); |
| 40 | + |
| 41 | + assert_ne!(session_before, keyring); |
| 42 | + assert_eq!(session_after, keyring); |
| 43 | + |
| 44 | + let desc = keyring.description().unwrap(); |
| 45 | + assert_eq!(desc.type_, keytypes::Keyring::name()); |
| 46 | + assert_eq!(desc.uid, *UID); |
| 47 | + assert_eq!(desc.gid, *GID); |
| 48 | + assert_eq!( |
| 49 | + desc.perms, |
| 50 | + Permission::POSSESSOR_ALL | Permission::USER_VIEW | Permission::USER_READ |
| 51 | + ); |
| 52 | + assert_eq!(desc.description, "_ses"); |
| 53 | + |
| 54 | + keyring.invalidate().unwrap() |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +#[serial(join_session)] |
| 59 | +fn join_new_named_session() { |
| 60 | + let session_before = Keyring::attach_or_create(SpecialKeyring::Session).unwrap(); |
| 61 | + let name = "join_new_named_session"; |
| 62 | + let keyring = Keyring::join_session(name).unwrap(); |
| 63 | + let session_after = Keyring::attach_or_create(SpecialKeyring::Session).unwrap(); |
| 64 | + |
| 65 | + assert_ne!(session_before, keyring); |
| 66 | + assert_eq!(session_after, keyring); |
| 67 | + |
| 68 | + let desc = keyring.description().unwrap(); |
| 69 | + assert_eq!(desc.type_, keytypes::Keyring::name()); |
| 70 | + assert_eq!(desc.uid, *UID); |
| 71 | + assert_eq!(desc.gid, *GID); |
| 72 | + assert_eq!( |
| 73 | + desc.perms, |
| 74 | + Permission::POSSESSOR_ALL |
| 75 | + | Permission::USER_VIEW |
| 76 | + | Permission::USER_READ |
| 77 | + | Permission::USER_LINK |
| 78 | + ); |
| 79 | + assert_eq!(desc.description, name); |
| 80 | + |
| 81 | + keyring.invalidate().unwrap() |
| 82 | +} |
| 83 | + |
| 84 | +#[test] |
| 85 | +#[serial(join_session)] |
| 86 | +fn join_existing_named_session() { |
| 87 | + let name = "join_existing_named_session"; |
| 88 | + |
| 89 | + let session_before = Keyring::attach_or_create(SpecialKeyring::Session).unwrap(); |
| 90 | + let keyring = Keyring::join_session(name).unwrap(); |
| 91 | + let session_after = Keyring::attach_or_create(SpecialKeyring::Session).unwrap(); |
| 92 | + |
| 93 | + assert_ne!(session_before, keyring); |
| 94 | + assert_eq!(session_after, keyring); |
| 95 | + |
| 96 | + let desc = keyring.description().unwrap(); |
| 97 | + assert_eq!(desc.type_, keytypes::Keyring::name()); |
| 98 | + assert_eq!(desc.uid, *UID); |
| 99 | + assert_eq!(desc.gid, *GID); |
| 100 | + assert_eq!( |
| 101 | + desc.perms, |
| 102 | + Permission::POSSESSOR_ALL |
| 103 | + | Permission::USER_VIEW |
| 104 | + | Permission::USER_READ |
| 105 | + | Permission::USER_LINK |
| 106 | + ); |
| 107 | + assert_eq!(desc.description, name); |
| 108 | + |
| 109 | + keyring.invalidate().unwrap(); |
| 110 | +} |
0 commit comments