| 1 |
use crate::integration::common::Sq; |
| 2 |
use crate::integration::common::UserIDArg; |
| 3 |
|
| 4 |
#[test] |
| 5 |
fn list_empty() { |
| 6 |
let sq = Sq::new(); |
| 7 |
|
| 8 |
// Listing an empty key store should not be an error. |
| 9 |
sq.pki_link_list(&[]); |
| 10 |
|
| 11 |
// Listing an empty key store with a pattern (that doesn't |
| 12 |
// match anything) should be. |
| 13 |
assert!(sq.try_pki_link_list(&["not found"]).is_err()); |
| 14 |
|
| 15 |
let (cert, cert_path, _rev_path) |
| 16 |
= sq.key_generate(&[], &[ "alice" ]); |
| 17 |
sq.key_import(cert_path); |
| 18 |
|
| 19 |
// Not linked => error. |
| 20 |
assert!(sq.try_pki_link_list(&["alice"]).is_err()); |
| 21 |
|
| 22 |
// Not found => error. |
| 23 |
assert!(sq.try_pki_link_list(&["not found"]).is_err()); |
| 24 |
|
| 25 |
// Linked and found => ok. |
| 26 |
sq.pki_link_add(&[], cert.key_handle(), &["alice"]); |
| 27 |
sq.pki_link_list(&["alice"]); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn list_uncertified_cert_designator() { |
| 32 |
// Consider a certificate with the following user IDs: |
| 33 |
// |
| 34 |
// - [email protected] |
| 35 |
// - [email protected] |
| 36 |
// |
| 37 |
// The user authorizes [email protected] as a trusted introducer. |
| 38 |
// This means that [email protected] can be authenticated (trust root |
| 39 |
// -> [email protected] -> [email protected]) and can be used as a |
| 40 |
// certificate designator. Consequently, `sq pki link list |
| 41 |
// --cert-email [email protected]` should work, and the whole command |
| 42 |
// should succeed because *some* user ID has been linked. |
| 43 |
|
| 44 |
let mut sq = Sq::new(); |
| 45 |
|
| 46 |
let alice_one_email = "[email protected]"; |
| 47 |
let alice_one_userid = &format!("Alice <{}>", alice_one_email); |
| 48 |
|
| 49 |
let alice_two_email = "[email protected]"; |
| 50 |
let alice_two_userid = &format!("Alice <{}>", alice_two_email); |
| 51 |
|
| 52 |
let (alice, alice_pgp, _) = sq.key_generate( |
| 53 |
&[], &[ alice_one_userid, alice_two_userid ]); |
| 54 |
|
| 55 |
let alice_kh = &alice.key_handle().to_string(); |
| 56 |
|
| 57 |
sq.key_import(&alice_pgp); |
| 58 |
|
| 59 |
// [email protected] is not authenticated, and can't be used as a |
| 60 |
// certificate designator. |
| 61 |
assert!(sq.pki_authenticate( |
| 62 |
&[], alice_kh, UserIDArg::Email(alice_one_email)).is_err()); |
| 63 |
assert!(sq.try_pki_link_list(&["--cert-email", alice_one_email]).is_err()); |
| 64 |
|
| 65 |
// [email protected] is not authenticated, and can't be used as a |
| 66 |
// certificate designator. |
| 67 |
assert!(sq.pki_authenticate( |
| 68 |
&[], alice_kh, UserIDArg::Email(alice_two_email)).is_err()); |
| 69 |
assert!(sq.try_pki_link_list(&["--cert-email", alice_two_email]).is_err()); |
| 70 |
|
| 71 |
sq.tick(1); |
| 72 |
|
| 73 |
sq.pki_link_authorize( |
| 74 |
&["--unconstrained"], alice.key_handle(), &[ alice_one_userid ]); |
| 75 |
|
| 76 |
// [email protected] is authenticated, so we should be able to use it |
| 77 |
// to designate the certificate. |
| 78 |
assert!(sq.pki_authenticate( |
| 79 |
&[], alice_kh, UserIDArg::Email(alice_one_email)).is_ok()); |
| 80 |
sq.pki_link_list(&["--cert-email", alice_one_email]); |
| 81 |
|
| 82 |
// [email protected] is authenticated, so we should be able to use it |
| 83 |
// to designate the certificate even though it was not linked. |
| 84 |
assert!(sq.pki_authenticate( |
| 85 |
&[], alice_kh, UserIDArg::Email(alice_two_email)).is_ok()); |
| 86 |
sq.pki_link_list(&["--cert-email", alice_two_email]); |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
fn list_partially_trusted() { |
| 91 |
// If a certificate is partially trusted, then we should be able |
| 92 |
// to list it by fingerprint. |
| 93 |
|
| 94 |
let mut sq = Sq::new(); |
| 95 |
|
| 96 |
let alice_example_email = "[email protected]"; |
| 97 |
let alice_example_userid = &format!("Alice <{}>", alice_example_email); |
| 98 |
|
| 99 |
let (alice, alice_pgp, _) = sq.key_generate( |
| 100 |
&[], &[ alice_example_userid ]); |
| 101 |
|
| 102 |
let alice_kh = &alice.key_handle().to_string(); |
| 103 |
|
| 104 |
sq.key_import(&alice_pgp); |
| 105 |
|
| 106 |
// [email protected] is not authenticated, and can't be used as a |
| 107 |
// certificate designator. |
| 108 |
assert!(sq.pki_authenticate( |
| 109 |
&[], alice_kh, UserIDArg::Email(alice_example_email)).is_err()); |
| 110 |
assert!(sq.try_pki_link_list(&["--cert-email", alice_example_email]).is_err()); |
| 111 |
|
| 112 |
sq.tick(1); |
| 113 |
|
| 114 |
sq.pki_link_add( |
| 115 |
&["--amount", "40"], alice.key_handle(), &[ alice_example_userid ]); |
| 116 |
|
| 117 |
// [email protected] is NOT authenticated, so we should NOT be |
| 118 |
// able to use it to designate the certificate. |
| 119 |
assert!(sq.pki_authenticate( |
| 120 |
&[], alice_kh, UserIDArg::UserID(alice_example_userid)).is_err()); |
| 121 |
assert!(sq.try_pki_link_list(&["--cert-userid", alice_example_userid]).is_err()); |
| 122 |
|
| 123 |
// But, as always, using the fingerprint should be okay. |
| 124 |
sq.pki_link_list(&["--cert", alice_kh]); |
| 125 |
} |
| 126 |
|
| 127 |
#[test] |
| 128 |
fn list_with_unauthenticated_handle() { |
| 129 |
// Make sure it isn't possible to use --cert-email, etc. with an |
| 130 |
// unauthenticated handle. |
| 131 |
let sq = Sq::new(); |
| 132 |
|
| 133 |
let alice_name = "Alice Lovelace"; |
| 134 |
let alice_domain = "example.org"; |
| 135 |
let alice_email = &format!("alice@{}", alice_domain); |
| 136 |
let alice_userid = &format!("{} <{}>", alice_name, alice_email); |
| 137 |
|
| 138 |
let (alice_cert, alice_cert_path, _rev_path) |
| 139 |
= sq.key_generate(&[], &[ alice_userid, ]); |
| 140 |
|
| 141 |
sq.key_import(&alice_cert_path); |
| 142 |
|
| 143 |
// We need to link a user ID otherwise sq pki link list will |
| 144 |
// refuse to list the certificate. |
| 145 |
sq.pki_link_add( |
| 146 |
&["--amount", "40"], alice_cert.key_handle(), &[ alice_userid ]); |
| 147 |
|
| 148 |
let list = |args: &[&str], success: bool| { |
| 149 |
assert_eq!(sq.try_pki_link_list(args).is_ok(), success); |
| 150 |
}; |
| 151 |
|
| 152 |
list(&["--cert", &alice_cert.fingerprint().to_string()], true); |
| 153 |
list(&["--cert-userid", alice_userid], false); |
| 154 |
list(&["--cert-email", alice_email], false); |
| 155 |
list(&["--cert-domain", alice_domain], false); |
| 156 |
list(&["--cert-grep", &alice_name[1..]], false); |
| 157 |
} |
| 158 |
|