Skip to content

Commit

Permalink
test: use builder pattern for device options
Browse files Browse the repository at this point in the history
Use the builder pattern for `KeyQueryResponseTemplate::with_device`. I'm going
to be adding at least one more option to this and it's already a bit opaque.
  • Loading branch information
richvdh committed Jan 16, 2025
1 parent 6a0333e commit 2cd9dc0
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions testing/matrix-sdk-test/src/test_json/keys_query_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
/// ```
/// # use ruma::{device_id, owned_user_id};
/// # use vodozemac::{Curve25519PublicKey, Ed25519SecretKey};
/// # use matrix_sdk_test::test_json::keys_query_sets::KeyQueryResponseTemplate;
/// # use matrix_sdk_test::test_json::keys_query_sets::{KeyQueryResponseTemplate, KeyQueryResponseTemplateDeviceOptions};
///
/// let pub_curve_key = "PBo2nKbink/HxgzMrBftGPogsD0d47LlIMsViTpCRn4";
/// let ed25519_key = "yzj53Kccfqx2yx9lcTwaRfPZX+7jU19harsDWWu5YnM";
Expand All @@ -39,7 +39,7 @@ use crate::{
/// device_id!("TESTDEVICE"),
/// &Curve25519PublicKey::from_base64(pub_curve_key).unwrap(),
/// &Ed25519SecretKey::from_base64(ed25519_key).unwrap(),
/// false,
/// KeyQueryResponseTemplateDeviceOptions::new(),
/// );
///
/// let response = builder.build_response();
Expand All @@ -51,7 +51,7 @@ use crate::{
/// ```
/// # use ruma::{device_id, owned_user_id, user_id};
/// # use vodozemac::{Curve25519PublicKey, Ed25519SecretKey};
/// # use matrix_sdk_test::test_json::keys_query_sets::KeyQueryResponseTemplate;
/// # use matrix_sdk_test::test_json::keys_query_sets::{KeyQueryResponseTemplate, KeyQueryResponseTemplateDeviceOptions};
///
/// // Private cross-signing keys
/// let master_key = "QGZo39k199RM0NYvPvFNXBspc5llftHWKKHqEi25q0U";
Expand Down Expand Up @@ -81,7 +81,7 @@ use crate::{
/// device_id!("SECUREDEVICE"),
/// &Curve25519PublicKey::from_base64(pub_curve_key).unwrap(),
/// &Ed25519SecretKey::from_base64(ed25519_key).unwrap(),
/// true,
/// KeyQueryResponseTemplateDeviceOptions::new().verified(true),
/// );
///
/// let response = builder.build_response();
Expand Down Expand Up @@ -159,14 +159,15 @@ impl KeyQueryResponseTemplate {
/// Ed25519 device key must be provided so that the signature can be
/// calculated.
///
/// The device can optionally be signed by the self-signing key by setting
/// `cross_signed` to `true`.
/// The device can optionally be signed by the self-signing key by calling
/// [`KeyResponseTemplateDeviceOptions::verified(true)`] on the `options`
/// object.
pub fn with_device(
mut self,
device_id: &DeviceId,
curve25519_public_key: &Curve25519PublicKey,
ed25519_secret_key: &Ed25519SecretKey,
cross_signed: bool,
options: KeyQueryResponseTemplateDeviceOptions,
) -> Self {
let mut device_keys = json!({
"algorithms": [
Expand All @@ -183,7 +184,7 @@ impl KeyQueryResponseTemplate {
});

sign_json(&mut device_keys, ed25519_secret_key, &self.user_id, device_id.as_str());
if cross_signed {
if options.verified {
let ssk = self
.self_signing_key
.as_ref()
Expand Down Expand Up @@ -283,6 +284,28 @@ impl KeyQueryResponseTemplate {
}
}

/// Options which control the addition of a device to a
/// [`KeyQueryResponseTemplate`], via [`KeyQueryResponseTemplate::with_device`].
pub struct KeyQueryResponseTemplateDeviceOptions {
verified: bool,
}

impl KeyQueryResponseTemplateDeviceOptions {
/// Creates a blank new set of options ready for configuration.
///
/// All options are initially set to `false`.
pub fn new() -> Self {
KeyQueryResponseTemplateDeviceOptions { verified: false, dehydrated: false }
}

/// Sets the option for whether the device will be verified (i.e., signed by
/// the self-signing key).
pub fn verified(mut self, verified: bool) -> Self {
self.verified = verified;
self
}
}

/// This set of keys/query response was generated using a local synapse.
///
/// The current user is `@me:localhost`, the private part of the
Expand Down Expand Up @@ -404,7 +427,7 @@ impl KeyDistributionTestData {
&Curve25519PublicKey::from_base64("PBo2nKbink/HxgzMrBftGPogsD0d47LlIMsViTpCRn4")
.unwrap(),
&Ed25519SecretKey::from_base64("yzj53Kccfqx2yx9lcTwaRfPZX+7jU19harsDWWu5YnM").unwrap(),
true,
KeyQueryResponseTemplateDeviceOptions::new().verified(true),
);

// Add unsigned device FRGNMZVOKA
Expand All @@ -413,7 +436,7 @@ impl KeyDistributionTestData {
&Curve25519PublicKey::from_base64("Hc/BC/xyQIEnScyZkEk+ilDMfOARxHMFoEcggPqqRw4")
.unwrap(),
&Ed25519SecretKey::from_base64("/SlFtNKxTPN+i4pHzSPWZ1Oc6ymMB33sS32GXZkaLos").unwrap(),
false,
KeyQueryResponseTemplateDeviceOptions::new(),
);

builder.build_response()
Expand Down

0 comments on commit 2cd9dc0

Please sign in to comment.