Skip to content

Commit 3291db8

Browse files
committed
tests/describe: port description tests from libkeyutils
1 parent 90df419 commit 3291db8

File tree

3 files changed

+139
-21
lines changed

3 files changed

+139
-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

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

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)