|
| 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 std::iter; |
| 28 | + |
| 29 | +use super::utils; |
| 30 | +use super::utils::kernel::*; |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn invalid_keyring() { |
| 34 | + let mut keyring = utils::invalid_keyring(); |
| 35 | + let err = keyring.add_keyring("").unwrap_err(); |
| 36 | + assert_eq!(err, errno::Errno(libc::EINVAL)); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn empty_keyring_description() { |
| 41 | + let mut keyring = utils::new_test_keyring(); |
| 42 | + let err = keyring.add_keyring("").unwrap_err(); |
| 43 | + assert_eq!(err, errno::Errno(libc::EINVAL)); |
| 44 | + |
| 45 | + keyring.invalidate().unwrap() |
| 46 | +} |
| 47 | + |
| 48 | +#[test] |
| 49 | +fn max_keyring_description() { |
| 50 | + let mut keyring = utils::new_test_keyring(); |
| 51 | + // Subtract one because the NUL is added in the kernel API. |
| 52 | + let maxdesc: String = iter::repeat('a').take(*PAGE_SIZE - 1).collect(); |
| 53 | + let res = keyring.add_keyring(maxdesc.as_ref()); |
| 54 | + // If the user's quota is smaller than this, it's an error. |
| 55 | + if KEY_INFO.maxbytes < *PAGE_SIZE { |
| 56 | + assert_eq!(res.unwrap_err(), errno::Errno(libc::EDQUOT)); |
| 57 | + } else { |
| 58 | + let keyring = res.unwrap(); |
| 59 | + assert_eq!(keyring.description().unwrap().description, maxdesc); |
| 60 | + keyring.invalidate().unwrap(); |
| 61 | + } |
| 62 | + |
| 63 | + keyring.invalidate().unwrap() |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn overlong_keyring_description() { |
| 68 | + let mut keyring = utils::new_test_keyring(); |
| 69 | + // On MIPS with < 3.19, there is a bug where this is allowed. 3.19 was released in Feb 2015, |
| 70 | + // so this is being ignored here. |
| 71 | + let maxdesc: String = iter::repeat('a').take(*PAGE_SIZE).collect(); |
| 72 | + let err = keyring.add_keyring(maxdesc.as_ref()).unwrap_err(); |
| 73 | + assert_eq!(err, errno::Errno(libc::EINVAL)); |
| 74 | + |
| 75 | + keyring.invalidate().unwrap() |
| 76 | +} |
| 77 | + |
| 78 | +#[test] |
| 79 | +fn new_keyring() { |
| 80 | + let mut keyring = utils::new_test_keyring(); |
| 81 | + let new_keyring = keyring.add_keyring("new_keyring").unwrap(); |
| 82 | + |
| 83 | + let (keys, keyrings) = keyring.read().unwrap(); |
| 84 | + assert_eq!(keys.len(), 0); |
| 85 | + assert_eq!(keyrings.len(), 1); |
| 86 | + assert_eq!(keyrings[0], new_keyring); |
| 87 | + |
| 88 | + keyring.invalidate().unwrap(); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn duplicate_keyring_names() { |
| 93 | + let mut keyring = utils::new_test_keyring(); |
| 94 | + let new_keyring1 = keyring.add_keyring("duplicate_keyring_names").unwrap(); |
| 95 | + let new_keyring2 = keyring.add_keyring("duplicate_keyring_names").unwrap(); |
| 96 | + |
| 97 | + // The keyring should have been displaced. |
| 98 | + assert_ne!(new_keyring1, new_keyring2); |
| 99 | + |
| 100 | + // The original keyring should not be in the parent keyring. |
| 101 | + let (keys, keyrings) = keyring.read().unwrap(); |
| 102 | + assert!(keys.is_empty()); |
| 103 | + assert_eq!(1, keyrings.len()); |
| 104 | + assert_eq!(new_keyring2, keyrings[0]); |
| 105 | + |
| 106 | + utils::wait_for_keyring_gc_description(&new_keyring1); |
| 107 | + |
| 108 | + // It should be inaccessible. |
| 109 | + let err = new_keyring1.description().unwrap_err(); |
| 110 | + assert_eq!(err, errno::Errno(libc::ENOKEY)); |
| 111 | + |
| 112 | + keyring.invalidate().unwrap(); |
| 113 | +} |
0 commit comments