π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Speed up paste password tests
Manav Rathiec826f7e10d1
diff --git a/rust/apps/cli/src/commands/paste.rs b/rust/apps/cli/src/commands/paste.rsindex 15a3f5fc07..aa92a6a776 100644--- a/rust/apps/cli/src/commands/paste.rs+++ b/rust/apps/cli/src/commands/paste.rs@@ -95,25 +95,22 @@ fn decrypt_password_protected_paste(payload: &PastePayload,password: PastePasswordAttempt,) -> Result<String> {- decrypt_password_protected_paste_with_prompt(- paste_key,- payload,- password,- prompt_raw_paste_password,- )+ decrypt_password_protected_paste_with_prompt(password, prompt_raw_paste_password, |password| {+ ente_paste::decrypt(payload, paste_key, Some(password))+ })}-fn decrypt_password_protected_paste_with_prompt<F>(- paste_key: &PasteKey,- payload: &PastePayload,+fn decrypt_password_protected_paste_with_prompt<F, D>(mut password: PastePasswordAttempt,mut prompt_password: F,+ mut decrypt: D,) -> Result<String>whereF: FnMut() -> Result<String>,+ D: FnMut(&str) -> ente_paste::Result<String>,{loop {- match ente_paste::decrypt(payload, paste_key, Some(password.value())) {+ match decrypt(password.value()) {Err(ente_paste::Error::IncorrectPassword) if password.can_retry() => {eprintln!("Incorrect paste password. Try again.");password = PastePasswordAttempt::Prompted(prompt_valid_paste_password(@@ -288,6 +285,13 @@ impl PastePasswordAttempt {mod tests {use super::*;+ fn test_decrypt_password_protected_paste(password: &str) -> ente_paste::Result<String> {+ match password {+ "correct horse" => Ok("protected paste".to_string()),+ _ => Err(ente_paste::Error::IncorrectPassword),+ }+ }+#[test]fn default_tty_output_escapes_controls() {let text = "ok\n\x1b]52;c;AAAA\x07\rhidden\tend\u{85}";@@ -312,14 +316,11 @@ mod tests {#[test]fn prompted_password_retry_can_recover() {- let (paste_key, payload) =- ente_paste::encrypt("protected paste", Some("correct horse")).unwrap();let mut retry_passwords = ["correct horse"].into_iter();let text = decrypt_password_protected_paste_with_prompt(- &paste_key,- &payload,PastePasswordAttempt::Prompted("wrong horse".to_string()),|| Ok::<_, Error>(retry_passwords.next().expect("retry password").to_string()),+ test_decrypt_password_protected_paste,).unwrap();@@ -329,14 +330,11 @@ mod tests {#[test]fn prompted_password_retry_ignores_empty_password() {- let (paste_key, payload) =- ente_paste::encrypt("protected paste", Some("correct horse")).unwrap();let mut retry_passwords = ["", "correct horse"].into_iter();let text = decrypt_password_protected_paste_with_prompt(- &paste_key,- &payload,PastePasswordAttempt::Prompted("wrong horse".to_string()),|| Ok::<_, Error>(retry_passwords.next().expect("retry password").to_string()),+ test_decrypt_password_protected_paste,).unwrap();@@ -346,12 +344,10 @@ mod tests {#[test]fn wrong_env_password_fails_without_retry() {- let (paste_key, payload) =- ente_paste::encrypt("protected paste", Some("correct horse")).unwrap();- let error = decrypt_password_protected_paste(- &paste_key,- &payload,+ let error = decrypt_password_protected_paste_with_prompt(PastePasswordAttempt::Env("wrong horse".to_string()),+ || panic!("environment passwords must not be retried"),+ test_decrypt_password_protected_paste,).unwrap_err();diff --git a/rust/crates/paste/src/lib.rs b/rust/crates/paste/src/lib.rsindex 3384d88fab..7694f731f9 100644--- a/rust/crates/paste/src/lib.rs+++ b/rust/crates/paste/src/lib.rs@@ -395,11 +395,9 @@ mod tests {}#[test]- fn encrypt_then_decrypt_password_protected_paste_payload() {+ fn encrypt_password_protected_paste_payload_uses_moderate_kdf() {let (paste_key, payload) = encrypt("protected paste", Some("correct horse")).unwrap();- let text = decrypt(&payload, &paste_key, Some("correct horse")).unwrap();- assert_eq!(text, "protected paste");assert!(paste_key.password_required);assert!(paste_key@@ -410,9 +408,19 @@ mod tests {assert_eq!(payload.kdf_ops_limit, argon::Params::MODERATE.ops_limit);}+ #[test]+ fn decrypt_password_protected_paste_payload() {+ let paste_key = PasteKey::parse("p-AbCd1234EfGh").unwrap();+ let payload = test_payload("protected paste", &paste_key, Some("correct horse"));+ let text = decrypt(&payload, &paste_key, Some("correct horse")).unwrap();++ assert_eq!(text, "protected paste");+ }+#[test]fn reject_wrong_paste_password() {- let (paste_key, payload) = encrypt("protected paste", Some("correct horse")).unwrap();+ let paste_key = PasteKey::parse("p-AbCd1234EfGh").unwrap();+ let payload = test_payload("protected paste", &paste_key, Some("correct horse"));let error = decrypt(&payload, &paste_key, Some("wrong horse")).unwrap_err();assert!(matches!(error, Error::IncorrectPassword));@@ -420,7 +428,8 @@ mod tests {#[test]fn structural_payload_errors_are_not_incorrect_password() {- let (paste_key, mut payload) = encrypt("protected paste", Some("correct horse")).unwrap();+ let paste_key = PasteKey::parse("p-AbCd1234EfGh").unwrap();+ let mut payload = test_payload("protected paste", &paste_key, Some("correct horse"));payload.kdf_nonce = "not base64".to_string();let error = decrypt(&payload, &paste_key, Some("correct horse")).unwrap_err();@@ -432,7 +441,8 @@ mod tests {#[test]fn wrapped_key_payload_errors_are_not_incorrect_password() {- let (paste_key, mut payload) = encrypt("protected paste", Some("correct horse")).unwrap();+ let paste_key = PasteKey::parse("p-AbCd1234EfGh").unwrap();+ let mut payload = test_payload("protected paste", &paste_key, Some("correct horse"));payload.encrypted_paste_key = "not base64".to_string();let error = decrypt(&payload, &paste_key, Some("correct horse")).unwrap_err();