Skip to content

Commit

Permalink
test: test helpers in share_strategy tests
Browse files Browse the repository at this point in the history
Split the existing `set_up_test_machine` into two parts, so we can set up
the test OlmMachine without importing data for other users
  • Loading branch information
richvdh committed Jan 29, 2025
1 parent d755a8a commit e3b3487
Showing 1 changed file with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -656,30 +656,33 @@ mod tests {
CrossSigningKeyExport, EncryptionSettings, LocalTrust, OlmError, OlmMachine,
};

async fn set_up_test_machine() -> OlmMachine {
let machine = OlmMachine::new(
KeyDistributionTestData::me_id(),
KeyDistributionTestData::me_device_id(),
)
.await;

let keys_query = KeyDistributionTestData::me_keys_query_response();
let txn_id = TransactionId::new();
machine.mark_request_as_sent(&txn_id, &keys_query).await.unwrap();
/// Returns an `OlmMachine` set up for the test user in
/// [`KeyDistributionTestData`], with cross-signing set up and the
/// private cross-signing keys imported.
async fn test_machine() -> OlmMachine {
use KeyDistributionTestData as DataSet;

// Create the local user (`@me`), and import the public identity keys
let machine = OlmMachine::new(DataSet::me_id(), DataSet::me_device_id()).await;
let keys_query = DataSet::me_keys_query_response();
machine.mark_request_as_sent(&TransactionId::new(), &keys_query).await.unwrap();

// Also import the private cross signing keys
machine
.import_cross_signing_keys(CrossSigningKeyExport {
master_key: KeyDistributionTestData::MASTER_KEY_PRIVATE_EXPORT.to_owned().into(),
self_signing_key: KeyDistributionTestData::SELF_SIGNING_KEY_PRIVATE_EXPORT
.to_owned()
.into(),
user_signing_key: KeyDistributionTestData::USER_SIGNING_KEY_PRIVATE_EXPORT
.to_owned()
.into(),
master_key: DataSet::MASTER_KEY_PRIVATE_EXPORT.to_owned().into(),
self_signing_key: DataSet::SELF_SIGNING_KEY_PRIVATE_EXPORT.to_owned().into(),
user_signing_key: DataSet::USER_SIGNING_KEY_PRIVATE_EXPORT.to_owned().into(),
})
.await
.unwrap();

machine
}

/// Import device data for `@dan`, `@dave`, and `@good`, as referenced in
/// [`KeyDistributionTestData`], into the given OlmMachine
async fn import_known_users_to_test_machine(machine: &OlmMachine) {
let keys_query = KeyDistributionTestData::dan_keys_query_response();
let txn_id = TransactionId::new();
machine.mark_request_as_sent(&txn_id, &keys_query).await.unwrap();
Expand All @@ -691,8 +694,6 @@ mod tests {
let txn_id_good = TransactionId::new();
let keys_query_good = KeyDistributionTestData::good_keys_query_response();
machine.mark_request_as_sent(&txn_id_good, &keys_query_good).await.unwrap();

machine
}

/// Assert that [`CollectStrategy::AllDevices`] retains the same
Expand Down Expand Up @@ -751,7 +752,8 @@ mod tests {

#[async_test]
async fn test_share_with_per_device_strategy_to_all() {
let machine = set_up_test_machine().await;
let machine = test_machine().await;
import_known_users_to_test_machine(&machine).await;

let encryption_settings = all_devices_strategy_settings();

Expand Down Expand Up @@ -788,7 +790,8 @@ mod tests {

#[async_test]
async fn test_share_with_only_trusted_strategy() {
let machine = set_up_test_machine().await;
let machine = test_machine().await;
import_known_users_to_test_machine(&machine).await;

let encryption_settings = EncryptionSettings {
sharing_strategy: CollectStrategy::OnlyTrustedDevices,
Expand Down Expand Up @@ -1230,7 +1233,8 @@ mod tests {

#[async_test]
async fn test_share_with_identity_strategy() {
let machine = set_up_test_machine().await;
let machine = test_machine().await;
import_known_users_to_test_machine(&machine).await;

let encryption_settings = identity_based_strategy_settings();

Expand Down Expand Up @@ -1521,7 +1525,8 @@ mod tests {

#[async_test]
async fn test_should_rotate_based_on_visibility() {
let machine = set_up_test_machine().await;
let machine = test_machine().await;
import_known_users_to_test_machine(&machine).await;

let strategy = CollectStrategy::AllDevices;

Expand Down Expand Up @@ -1566,7 +1571,8 @@ mod tests {
/// his devices.
#[async_test]
async fn test_should_rotate_based_on_device_excluded() {
let machine = set_up_test_machine().await;
let machine = test_machine().await;
import_known_users_to_test_machine(&machine).await;

let fake_room_id = room_id!("!roomid:localhost");
let encryption_settings = all_devices_strategy_settings();
Expand Down

0 comments on commit e3b3487

Please sign in to comment.