Skip to content

Commit 3e95087

Browse files
committed
tests/describe: port description tests from libkeyutils
1 parent 73dfe83 commit 3e95087

File tree

3 files changed

+134
-21
lines changed

3 files changed

+134
-21
lines changed

src/api.rs

-21
Original file line numberDiff line numberDiff line change
@@ -824,27 +824,6 @@ mod tests {
824824
use super::*;
825825
use crate::tests::utils;
826826

827-
#[test]
828-
fn test_describe_key() {
829-
let mut keyring = utils::new_test_keyring();
830-
831-
// Create the key.
832-
let desc = "test:rust-keyutils:describe_key";
833-
let payload = "payload";
834-
let key = keyring
835-
.add_key::<keytypes::User, _, _>(desc, payload.as_bytes())
836-
.unwrap();
837-
838-
// Check its description.
839-
let description = key.description().unwrap();
840-
assert_eq!(&description.type_, keytypes::User::name());
841-
assert_eq!(&description.description, desc);
842-
843-
// Clean up.
844-
keyring.unlink_key(&key).unwrap();
845-
keyring.invalidate().unwrap();
846-
}
847-
848827
#[test]
849828
fn test_invalidate_key() {
850829
let mut keyring = utils::new_test_keyring();

src/tests/describe.rs

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 crate::keytypes::{Keyring, User};
28+
use crate::{Key, KeyType, Permission};
29+
30+
use super::utils::kernel::*;
31+
use super::utils;
32+
33+
#[test]
34+
fn invalid_key() {
35+
let key = utils::invalid_key();
36+
let err = key.description().unwrap_err();
37+
assert_eq!(err, errno::Errno(libc::EINVAL));
38+
}
39+
40+
#[test]
41+
fn non_existent_key() {
42+
let mut keyring = utils::new_test_keyring();
43+
let key = keyring.add_key::<User, _, _>("non_existent_key", "payload".as_bytes()).unwrap();
44+
45+
keyring.unlink_key(&key).unwrap();
46+
47+
// Keys are deleted asynchronously; describing the key succeeds until it has been garbage
48+
// collected.
49+
loop {
50+
match key.description() {
51+
Ok(_) => (),
52+
Err(errno::Errno(libc::ENOKEY)) => break,
53+
e@Err(_) => {
54+
e.unwrap();
55+
unreachable!()
56+
},
57+
}
58+
}
59+
60+
keyring.invalidate().unwrap()
61+
}
62+
63+
#[test]
64+
fn describe_keyring() {
65+
let mut keyring = utils::new_test_keyring();
66+
let description = "describe_keyring";
67+
let keyring = keyring.add_keyring(description).unwrap();
68+
69+
let perms = Permission::POSSESSOR_ALL
70+
| Permission::USER_VIEW;
71+
72+
let desc = keyring.description().unwrap();
73+
assert_eq!(desc.type_, Keyring::name());
74+
assert_eq!(desc.uid, *UID);
75+
assert_eq!(desc.gid, *GID);
76+
assert_eq!(desc.perms, perms);
77+
assert_eq!(desc.description, description);
78+
79+
keyring.invalidate().unwrap()
80+
}
81+
82+
#[test]
83+
fn describe_key() {
84+
let mut keyring = utils::new_test_keyring();
85+
let description = "describe_key";
86+
let key = keyring.add_key::<User, _, _>(description, "payload".as_bytes()).unwrap();
87+
88+
let perms = Permission::POSSESSOR_ALL
89+
| Permission::USER_VIEW;
90+
91+
let desc = key.description().unwrap();
92+
assert_eq!(desc.type_, User::name());
93+
assert_eq!(desc.uid, *UID);
94+
assert_eq!(desc.gid, *GID);
95+
assert_eq!(desc.perms, perms);
96+
assert_eq!(desc.description, description);
97+
}
98+
99+
#[test]
100+
fn describe_key_no_perm() {
101+
let mut keyring = utils::new_test_keyring();
102+
let description = "describe_key_no_perm";
103+
let mut key = keyring.add_key::<User, _, _>(description, "payload".as_bytes()).unwrap();
104+
105+
let old_perms = key.description().unwrap().perms;
106+
let perms = {
107+
let mut perms = old_perms;
108+
let view_bits = Permission::POSSESSOR_VIEW
109+
| Permission::USER_VIEW;
110+
perms.remove(view_bits);
111+
perms
112+
};
113+
key.set_permissions(perms).unwrap();
114+
115+
let err = key.description().unwrap_err();
116+
assert_eq!(err, errno::Errno(libc::EACCES));
117+
118+
keyring.invalidate().unwrap()
119+
}
120+
121+
#[test]
122+
fn describe_revoked_key() {
123+
let mut keyring = utils::new_test_keyring();
124+
let key = keyring.add_key::<User, _, _>("describe_revoked_key", "payload".as_bytes()).unwrap();
125+
126+
let key_mirror = unsafe { Key::new(key.serial()) };
127+
key.revoke().unwrap();
128+
129+
let err = key_mirror.description().unwrap_err();
130+
assert_eq!(err, errno::Errno(libc::EKEYREVOKED));
131+
132+
keyring.invalidate().unwrap()
133+
}

src/tests/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ pub(crate) mod utils;
3030

3131
mod add;
3232
mod clear;
33+
mod describe;

0 commit comments

Comments
 (0)