Skip to content

Commit 9af0d67

Browse files
committed
Keyring: fix API for searching
The destination keyring does not need to be the same as the one that is searched. Fix the API and remove the now-broken tests.
1 parent 7f23c16 commit 9af0d67

File tree

2 files changed

+9
-47
lines changed

2 files changed

+9
-47
lines changed

keyutils-raw/src/functions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ extern "C" {
8989
ringid: KeyringSerial,
9090
type_: *const libc::c_char,
9191
description: *const libc::c_char,
92-
destringid: KeyringSerial)
92+
destringid: Option<KeyringSerial>)
9393
-> libc::c_long;
9494
pub fn keyctl_read(
9595
id: KeyringSerial,

src/api.rs

+8-46
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@ impl Keyring {
218218
check_call(unsafe { keyctl_unlink(keyring.id, self.id) })
219219
}
220220

221-
fn search_impl<K>(&self, description: &str) -> KeyringSerial
221+
fn search_impl<K>(&self, description: &str, destination: Option<&mut Keyring>) -> KeyringSerial
222222
where
223223
K: KeyType,
224224
{
225225
let type_cstr = CString::new(K::name()).unwrap();
226226
let desc_cstr = CString::new(description).unwrap();
227227
into_serial(unsafe {
228-
keyctl_search(self.id, type_cstr.as_ptr(), desc_cstr.as_ptr(), self.id)
228+
keyctl_search(self.id, type_cstr.as_ptr(), desc_cstr.as_ptr(), destination.map(|dest| dest.id))
229229
})
230230
}
231231

@@ -234,12 +234,13 @@ impl Keyring {
234234
/// If it is found, it is attached to the keyring (if `write` permission to the keyring and
235235
/// `link` permission on the key exist) and return it. Requires the `search` permission on the
236236
/// keyring. Any children keyrings without the `search` permission are ignored.
237-
pub fn search_for_key<K, D>(&self, description: D) -> Result<Key>
237+
pub fn search_for_key<'a, K, D, DK>(&self, description: D, destination: DK) -> Result<Key>
238238
where
239239
K: KeyType,
240240
D: Borrow<K::Description>,
241+
DK: Into<Option<&'a mut Keyring>>,
241242
{
242-
check_call_key(self.search_impl::<K>(&description.borrow().description()))
243+
check_call_key(self.search_impl::<K>(&description.borrow().description(), destination.into()))
243244
}
244245

245246
/// Recursively search the keyring for a keyring with the matching description.
@@ -248,12 +249,13 @@ impl Keyring {
248249
/// `link` permission on the found keyring exist) and return it. Requires the `search`
249250
/// permission on the keyring. Any children keyrings without the `search` permission are
250251
/// ignored.
251-
pub fn search_for_keyring<D>(&self, description: D) -> Result<Self>
252+
pub fn search_for_keyring<'a, D, DK>(&self, description: D, destination: DK) -> Result<Self>
252253
where
253254
D: Borrow<<keytypes::Keyring as KeyType>::Description>,
255+
DK: Into<Option<&'a mut Keyring>>,
254256
{
255257
check_call_keyring(
256-
self.search_impl::<keytypes::Keyring>(&description.borrow().description()),
258+
self.search_impl::<keytypes::Keyring>(&description.borrow().description(), destination.into()),
257259
)
258260
}
259261

@@ -962,44 +964,4 @@ mod tests {
962964
keyring.unlink_key(&key).unwrap();
963965
keyring.invalidate().unwrap();
964966
}
965-
966-
#[test]
967-
fn test_search_key() {
968-
let mut keyring = utils::new_test_keyring();
969-
let mut new_keyring = keyring.add_keyring("new_keyring").unwrap();
970-
let mut new_inner_keyring = new_keyring.add_keyring("new_inner_keyring").unwrap();
971-
972-
// Create the key.
973-
let description = "test:rust-keyutils:search_key";
974-
let payload = "payload";
975-
let key = new_inner_keyring
976-
.add_key::<keytypes::User, _, _>(description, payload.as_bytes())
977-
.unwrap();
978-
979-
let found_key = keyring
980-
.search_for_key::<keytypes::User, _>(description)
981-
.unwrap();
982-
assert_eq!(found_key, key);
983-
984-
// Clean up.
985-
new_inner_keyring.unlink_key(&key).unwrap();
986-
new_inner_keyring.invalidate().unwrap();
987-
new_keyring.invalidate().unwrap();
988-
keyring.invalidate().unwrap();
989-
}
990-
991-
#[test]
992-
fn test_search_keyring() {
993-
let mut keyring = utils::new_test_keyring();
994-
let mut new_keyring = keyring.add_keyring("new_keyring").unwrap();
995-
let new_inner_keyring = new_keyring.add_keyring("new_inner_keyring").unwrap();
996-
997-
let found_keyring = keyring.search_for_keyring("new_inner_keyring").unwrap();
998-
assert_eq!(found_keyring, new_inner_keyring);
999-
1000-
// Clean up.
1001-
new_inner_keyring.invalidate().unwrap();
1002-
new_keyring.invalidate().unwrap();
1003-
keyring.invalidate().unwrap();
1004-
}
1005967
}

0 commit comments

Comments
 (0)