Skip to content

Commit

Permalink
Test for recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
poljar committed Nov 28, 2023
1 parent 3d6577e commit d48dc79
Show file tree
Hide file tree
Showing 3 changed files with 796 additions and 105 deletions.
101 changes: 101 additions & 0 deletions crates/matrix-sdk/tests/integration/encryption.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,104 @@
mod backups;
mod recovery;
mod secret_storage;
mod verification;

async fn mock_secret_store_with_backup_key(
user_id: &ruma::UserId,
key_id: &str,
server: &wiremock::MockServer,
) {
use serde_json::json;
use wiremock::{
matchers::{header, method, path},
Mock, ResponseTemplate,
};

Mock::given(method("GET"))
.and(path(format!(
"_matrix/client/r0/user/{user_id}/account_data/m.secret_storage.default_key"
)))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"key": key_id,
})))
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!(
"_matrix/client/r0/user/{user_id}/account_data/m.secret_storage.key.{key_id}"
)))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"algorithm": "m.secret_storage.v1.aes-hmac-sha2",
"iv": "1Sl4os6UhNRkVQcT6ArQ0g",
"mac": "UCZlTzqVT7mNvLkwlcCJmuq9nA27oxqpXGdLr9SxD/Y",
"name": null,
"passphrase": {
"algorithm": "m.pbkdf2",
"iterations": 1,
"salt": "ooLiz7Kz0TeWH2eYcyjP2fCegEB7PH5B"
}
})))
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!("_matrix/client/r0/user/{user_id}/account_data/m.cross_signing.master")))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(404).set_body_json(json!({
"errcode": "M_NOT_FOUND",
"error": "Account data not found"
})))
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!(
"_matrix/client/r0/user/{user_id}/account_data/m.cross_signing.user_signing"
)))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(404).set_body_json(json!({
"errcode": "M_NOT_FOUND",
"error": "Account data not found"
})))
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!(
"_matrix/client/r0/user/{user_id}/account_data/m.cross_signing.self_signing"
)))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(404).set_body_json(json!({
"errcode": "M_NOT_FOUND",
"error": "Account data not found"
})))
.mount(server)
.await;

Mock::given(method("POST"))
.and(path("_matrix/client/r0/keys/query"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"device_keys": {}
})))
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!("_matrix/client/r0/user/{user_id}/account_data/m.megolm_backup.v1")))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"encrypted": {
"yJWwBm2Ts8jHygTBslKpABFyykavhhfA": {
"ciphertext": "c39B25f6GSvW7gCUZI1OC0V821Ht2WUfxPWB43rvFSsubouHf16ImqLrwQ",
"iv": "hpyoGAElX8YRuigbqa7tfA",
"mac": "nE/RCVmFQxu+KuqxmYDDzIxf2JUlxz2oTpoJTj5pUxM"
}
}
})))
.mount(server)
.await;
}
110 changes: 5 additions & 105 deletions crates/matrix-sdk/tests/integration/encryption/backups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ use matrix_sdk::{
};
use matrix_sdk_base::SessionMeta;
use matrix_sdk_test::{async_test, JoinedRoomBuilder, SyncResponseBuilder};
use ruma::{
device_id, event_id, events::room::message::RoomMessageEvent, room_id, user_id, UserId,
};
use ruma::{device_id, event_id, events::room::message::RoomMessageEvent, room_id, user_id};
use serde_json::json;
use tempfile::tempdir;
use tokio::spawn;
Expand All @@ -38,7 +36,10 @@ use wiremock::{
Mock, ResponseTemplate,
};

use crate::{mock_sync, no_retry_test_client, test_client_builder};
use crate::{
encryption::mock_secret_store_with_backup_key, mock_sync, no_retry_test_client,
test_client_builder,
};

const ROOM_KEY: &[u8] = b"\
-----BEGIN MEGOLM SESSION DATA-----\n\
Expand Down Expand Up @@ -555,107 +556,6 @@ async fn steady_state_waiting_errors() {
task.await.unwrap();
}

async fn mock_secret_store_with_backup_key(
user_id: &UserId,
key_id: &str,
server: &wiremock::MockServer,
) {
Mock::given(method("GET"))
.and(path(format!(
"_matrix/client/r0/user/{user_id}/account_data/m.secret_storage.default_key"
)))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"key": key_id,
})))
.expect(1..)
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!(
"_matrix/client/r0/user/{user_id}/account_data/m.secret_storage.key.{key_id}"
)))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"algorithm": "m.secret_storage.v1.aes-hmac-sha2",
"iv": "1Sl4os6UhNRkVQcT6ArQ0g",
"mac": "UCZlTzqVT7mNvLkwlcCJmuq9nA27oxqpXGdLr9SxD/Y",
"name": null,
"passphrase": {
"algorithm": "m.pbkdf2",
"iterations": 1,
"salt": "ooLiz7Kz0TeWH2eYcyjP2fCegEB7PH5B"
}
})))
.expect(1..)
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!("_matrix/client/r0/user/{user_id}/account_data/m.cross_signing.master")))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(404).set_body_json(json!({
"errcode": "M_NOT_FOUND",
"error": "Account data not found"
})))
.expect(1..)
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!(
"_matrix/client/r0/user/{user_id}/account_data/m.cross_signing.user_signing"
)))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(404).set_body_json(json!({
"errcode": "M_NOT_FOUND",
"error": "Account data not found"
})))
.expect(1..)
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!(
"_matrix/client/r0/user/{user_id}/account_data/m.cross_signing.self_signing"
)))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(404).set_body_json(json!({
"errcode": "M_NOT_FOUND",
"error": "Account data not found"
})))
.expect(1..)
.mount(server)
.await;

Mock::given(method("POST"))
.and(path("_matrix/client/r0/keys/query"))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"device_keys": {}
})))
.expect(1..)
.mount(server)
.await;

Mock::given(method("GET"))
.and(path(format!("_matrix/client/r0/user/{user_id}/account_data/m.megolm_backup.v1")))
.and(header("authorization", "Bearer 1234"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"encrypted": {
"yJWwBm2Ts8jHygTBslKpABFyykavhhfA": {
"ciphertext": "c39B25f6GSvW7gCUZI1OC0V821Ht2WUfxPWB43rvFSsubouHf16ImqLrwQ",
"iv": "hpyoGAElX8YRuigbqa7tfA",
"mac": "nE/RCVmFQxu+KuqxmYDDzIxf2JUlxz2oTpoJTj5pUxM"
}
}
})))
.expect(1..)
.mount(server)
.await;
}

#[async_test]
async fn enable_from_secret_storage() {
const SECRET_STORE_KEY: &str = "mypassphrase";
Expand Down
Loading

0 comments on commit d48dc79

Please sign in to comment.