1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
use keyring::{Entry, Error};
#[no_mangle]
extern "C" fn test() {
test_invalid_parameter();
test_empty_keyring();
test_empty_password_input();
test_round_trip_ascii_password();
test_round_trip_non_ascii_password();
test_update_password();
#[cfg(target_os = "ios")]
test_get_credential();
}
fn test_invalid_parameter() {
let entry = Entry::new("", "user");
assert!(
matches!(entry, Err(Error::Invalid(_, _))),
"Created entry with empty service"
);
let entry = Entry::new("service", "");
assert!(
matches!(entry, Err(Error::Invalid(_, _))),
"Created entry with empty user"
);
let entry = Entry::new_with_target("test", "service", "user");
assert!(
matches!(entry, Err(Error::Invalid(_, _))),
"Created entry with non-default target"
);
}
fn test_empty_keyring() {
let name = "test_empty_keyring".to_string();
let entry = Entry::new(&name, &name).expect("Failed to create entry");
assert!(matches!(entry.get_password(), Err(Error::NoEntry)))
}
fn test_empty_password_input() {
let name = "test_empty_password_input".to_string();
let entry = Entry::new(&name, &name).expect("Failed to create entry");
let in_pass = "";
entry
.set_password(in_pass)
.expect("Couldn't set empty password");
let out_pass = entry.get_password().expect("Couldn't get empty password");
assert_eq!(in_pass, out_pass);
entry
.delete_credential()
.expect("Couldn't delete credential with empty password");
assert!(
matches!(entry.get_password(), Err(Error::NoEntry)),
"Able to read a deleted password"
)
}
fn test_round_trip_ascii_password() {
let name = "test_round_trip_ascii_password".to_string();
let entry = Entry::new(&name, &name).expect("Failed to create entry");
let password = "test ascii password";
entry.set_password(password).unwrap();
let stored_password = entry.get_password().unwrap();
assert_eq!(stored_password, password);
entry.delete_credential().unwrap();
assert!(matches!(entry.get_password(), Err(Error::NoEntry)))
}
fn test_round_trip_non_ascii_password() {
let name = "test_round_trip_non_ascii_password".to_string();
let entry = Entry::new(&name, &name).expect("Failed to create entry");
let password = "このきれいな花は桜です";
entry.set_password(password).unwrap();
let stored_password = entry.get_password().unwrap();
assert_eq!(stored_password, password);
entry.delete_credential().unwrap();
assert!(matches!(entry.get_password(), Err(Error::NoEntry)))
}
fn test_update_password() {
let name = "test_update_password".to_string();
let entry = Entry::new(&name, &name).expect("Failed to create entry");
let password = "test ascii password";
entry.set_password(password).unwrap();
let stored_password = entry.get_password().unwrap();
assert_eq!(stored_password, password);
let password = "このきれいな花は桜です";
entry.set_password(password).unwrap();
let stored_password = entry.get_password().unwrap();
assert_eq!(stored_password, password);
entry.delete_credential().unwrap();
assert!(matches!(entry.get_password(), Err(Error::NoEntry)))
}
#[cfg(target_os = "ios")]
fn test_get_credential() {
use keyring::ios::IosCredential;
let name = "test_get_credential".to_string();
let entry = Entry::new(&name, &name).expect("Can't create entry for get_credential");
let credential: &IosCredential = entry
.get_credential()
.downcast_ref()
.expect("Not an iOS credential");
assert!(
credential.get_credential().is_err(),
"Platform credential shouldn't exist yet!"
);
entry
.set_password("test get password for get_credential")
.expect("Can't get password for get_credential");
assert!(credential.get_credential().is_ok());
entry.delete_credential().unwrap();
assert!(
matches!(entry.get_password(), Err(Error::NoEntry)),
"Platform credential exists after delete password"
)
}
|