|
| 1 | +// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS |
| 2 | + |
| 3 | +use libparsec_tests_fixtures::prelude::*; |
| 4 | +use libparsec_types::prelude::*; |
| 5 | + |
| 6 | +use crate::{client::pki_enrollment_info::PkiEnrollmentInfoError, Client}; |
| 7 | + |
| 8 | +use super::utils::client_factory; |
| 9 | + |
| 10 | +#[parsec_test(testbed = "minimal")] |
| 11 | +#[cfg(target_os = "windows")] // because we are signing data with a cert, it needs to be on windows |
| 12 | +async fn ok(env: &TestbedEnv) { |
| 13 | + use libparsec_client_connection::test_register_send_hook; |
| 14 | + use libparsec_platform_pki::sign_message; |
| 15 | + use libparsec_protocol::anonymous_cmds::{ |
| 16 | + self, v5::pki_enrollment_info::PkiEnrollmentInfoStatus, |
| 17 | + }; |
| 18 | + |
| 19 | + use crate::pki_enrollment_info::PKIInfoItem; |
| 20 | + let alice_device = env.local_device("alice@dev1"); |
| 21 | + |
| 22 | + let alice_client = client_factory(&env.discriminant_dir, alice_device.clone()).await; |
| 23 | + |
| 24 | + let expected_accepted_on = DateTime::from_timestamp_micros(1668594983390001).unwrap(); |
| 25 | + let expected_submitted_on = DateTime::from_timestamp_micros(1668594983390002).unwrap(); |
| 26 | + let enrollment_id = PKIEnrollmentID::from_hex("e1fe88bd0f054261887a6c8039710b40").unwrap(); |
| 27 | + let expected_answer = PkiEnrollmentAnswerPayload { |
| 28 | + user_id: UserID::from_hex("9268b5acc07711f0ae7c2394da79527f").unwrap(), |
| 29 | + device_id: DeviceID::from_hex("a46105b6c07711f09e41f70f2e4e5650").unwrap(), |
| 30 | + device_label: DeviceLabel::try_from("new pki device").unwrap(), |
| 31 | + human_handle: HumanHandle::new(EmailAddress::try_from("[email protected]").unwrap(), "pki") |
| 32 | + .unwrap(), |
| 33 | + profile: UserProfile::Standard, |
| 34 | + root_verify_key: alice_device.root_verify_key().clone(), |
| 35 | + }; |
| 36 | + |
| 37 | + let cert_hash = X509CertificateHash::fake_sha256(); |
| 38 | + let cert_ref = cert_hash.clone().into(); |
| 39 | + let signed = sign_message(&expected_answer.dump(), &cert_ref) |
| 40 | + .context("Failed to sign message") |
| 41 | + .unwrap(); |
| 42 | + |
| 43 | + let pki_enrollment_item = PkiEnrollmentInfoStatus::Accepted { |
| 44 | + accepter_der_x509_certificate: cert_hash.to_string().into(), |
| 45 | + accept_payload: expected_answer.dump().into(), |
| 46 | + accept_payload_signature: signed.signature, |
| 47 | + accept_payload_signature_algorithm: signed.algo, |
| 48 | + submitted_on: expected_submitted_on, |
| 49 | + accepted_on: expected_accepted_on, |
| 50 | + }; |
| 51 | + |
| 52 | + test_register_send_hook(&env.discriminant_dir, { |
| 53 | + move |_req: anonymous_cmds::latest::pki_enrollment_info::Req| { |
| 54 | + anonymous_cmds::latest::pki_enrollment_info::Rep::Ok(pki_enrollment_item) |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + let enrollment_info = Client::pki_enrollment_info( |
| 59 | + alice_client.config.clone(), |
| 60 | + ParsecPkiEnrollmentAddr::new( |
| 61 | + alice_client.organization_addr(), |
| 62 | + alice_client.organization_id().clone(), |
| 63 | + ), |
| 64 | + enrollment_id, |
| 65 | + ) |
| 66 | + .await |
| 67 | + .unwrap(); |
| 68 | + match enrollment_info { |
| 69 | + PKIInfoItem::Accepted { |
| 70 | + answer, |
| 71 | + accepted_on, |
| 72 | + submitted_on, |
| 73 | + } => { |
| 74 | + assert_eq!(answer, expected_answer); |
| 75 | + assert_eq!(accepted_on, expected_accepted_on); |
| 76 | + assert_eq!(submitted_on, expected_submitted_on); |
| 77 | + } |
| 78 | + _ => panic!("unexpected answer"), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[parsec_test(testbed = "coolorg")] |
| 83 | +async fn enrollment_not_found(env: &TestbedEnv) { |
| 84 | + let mallory_device = env.local_device("mallory@dev1"); |
| 85 | + let mallory_client = client_factory(&env.discriminant_dir, mallory_device).await; |
| 86 | + let rep = Client::pki_enrollment_info( |
| 87 | + mallory_client.config.clone(), |
| 88 | + ParsecPkiEnrollmentAddr::new( |
| 89 | + mallory_client.organization_addr(), |
| 90 | + mallory_client.organization_id().clone(), |
| 91 | + ), |
| 92 | + PKIEnrollmentID::default(), |
| 93 | + ) |
| 94 | + .await; |
| 95 | + p_assert_matches!(rep.unwrap_err(), PkiEnrollmentInfoError::EnrollmentNotFound) |
| 96 | +} |
0 commit comments