Skip to content

Commit

Permalink
test: test migrations for all entities
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonThormeyer committed Sep 6, 2024
1 parent 31f8f43 commit 44d6737
Showing 1 changed file with 134 additions and 1 deletion.
135 changes: 134 additions & 1 deletion keystore/tests/z_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.

#[cfg(target_family = "wasm")]
use keystore_v_1_0_0::entities::{
E2eiAcmeCA as E2eiAcmeCAV1_0_0, E2eiCrl as E2eiCrlV1_0_0, E2eiEnrollment as E2eiEnrollmentV1_0_0,
E2eiIntermediateCert as E2eiIntermediateCertV1_0_0, E2eiRefreshToken as E2eiRefreshTokenV1_0_0,
Entity as EntityV1_0_0, MlsCredential as MlsCredentialV1_0_0, MlsEncryptionKeyPair as MlsEncryptionKeyPairV1_0_0,
MlsEpochEncryptionKeyPair as MlsEpochEncryptionKeyPairV1_0_0, MlsHpkePrivateKey as MlsHpkePrivateKeyV1_0_0,
MlsKeyPackage as MlsKeyPackageV1_0_0, MlsPendingMessage as MlsPendingMessageV1_0_0,
MlsPskBundle as MlsPskBundleV1_0_0, MlsSignatureKeyPair as MlsSignatureKeyPairV1_0_0,
PersistedMlsGroup as PersistedMlsGroupV1_0_0, PersistedMlsPendingGroup as PersistedMlsPendingGroupV1_0_0,
ProteusSession as ProteusSessionV1_0_0, UniqueEntity as UniqueEntityV1_0_0,
};
pub use rstest::*;
pub use rstest_reuse::{self, *};

Expand Down Expand Up @@ -61,6 +72,73 @@ macro_rules! test_for_entity {
};
}

#[cfg(target_family = "wasm")]
macro_rules! work_for_unique_or_regular_entities {
(unique: $unique_entity_work:block, regular: $regular_entity_work:block, true) => {
$unique_entity_work
};

(unique: $unique_entity_work:block, regular: $regular_entity_work:block, ) => {
$regular_entity_work
};
}

#[cfg(target_family = "wasm")]
macro_rules! test_migration_to_db_v1_for_entity {
($test_name:ident, $entity:ty, $old_entity:ty $(, unique_entity: $unique_entity:tt)?) => {
#[wasm_bindgen_test]
async fn $test_name() {
let _ = pretty_env_logger::try_init();
let name = store_name();

let old_storage = keystore_v_1_0_0::Connection::open_with_key(&name, TEST_ENCRYPTION_KEY)
.await
.unwrap();
let old_record = <$old_entity>::random();

work_for_unique_or_regular_entities!(
unique: {
let mut connection = old_storage.borrow_conn().await.unwrap();
old_record.replace(&mut connection).await.unwrap();
drop(connection);
},
regular: {
old_storage.save(old_record.clone()).await.unwrap();
},
$(
$unique_entity
)?
);

old_storage.close().await.unwrap();

let new_storage = core_crypto_keystore::Connection::open_with_key(&name, TEST_ENCRYPTION_KEY)
.await
.unwrap();
let mut new_connection = new_storage.borrow_conn().await.unwrap();
let result;

work_for_unique_or_regular_entities!(
unique: {
result = <$entity>::find_unique(&mut new_connection).await;
assert!(result.is_ok());
},
regular: {
let string_id = StringEntityId::from(old_record.id_raw());
result = <$entity>::find_one(&mut new_connection, &string_id).await;
assert!(result.unwrap().is_some());
},
$(
$unique_entity
)?
);

drop(new_connection);
new_storage.wipe().await.unwrap();
}
};
}

#[cfg(test)]
mod tests_impl {
use super::common::*;
Expand Down Expand Up @@ -151,6 +229,9 @@ mod tests_impl {

#[cfg(test)]
mod tests {
#[cfg(target_family = "wasm")]
// Use V1_0_0 entities
use super::*;
use crate::common::*;
use crate::utils::EntityRandomExt;
use crate::utils::EntityRandomUpdateExt;
Expand All @@ -176,13 +257,39 @@ mod tests {
test_for_entity!(test_e2ei_intermediate_cert, E2eiIntermediateCert);
test_for_entity!(test_e2ei_crl, E2eiCrl);
test_for_entity!(test_e2ei_enrollment, E2eiEnrollment ignore_update:true);

cfg_if::cfg_if! {
if #[cfg(target_family = "wasm")] {
test_migration_to_db_v1_for_entity!(test_mls_group_migration, PersistedMlsGroup, PersistedMlsGroupV1_0_0);
test_migration_to_db_v1_for_entity!(test_mls_pending_group_migration, PersistedMlsPendingGroup, PersistedMlsPendingGroupV1_0_0);
test_migration_to_db_v1_for_entity!(test_mls_pending_message_migration, MlsPendingMessage, MlsPendingMessageV1_0_0);
test_migration_to_db_v1_for_entity!(test_mls_credential_migration, MlsCredential, MlsCredentialV1_0_0);
test_migration_to_db_v1_for_entity!(test_mls_keypackage_migration, MlsKeyPackage, MlsKeyPackageV1_0_0);
test_migration_to_db_v1_for_entity!(test_mls_signature_keypair_migration, MlsSignatureKeyPair, MlsSignatureKeyPairV1_0_0);
test_migration_to_db_v1_for_entity!(test_mls_psk_bundle_migration, MlsPskBundle, MlsPskBundleV1_0_0);
test_migration_to_db_v1_for_entity!(test_mls_encryption_keypair_migration, MlsEncryptionKeyPair, MlsEncryptionKeyPairV1_0_0);
test_migration_to_db_v1_for_entity!(test_mls_epoch_encryption_keypair_migration, MlsEpochEncryptionKeyPair, MlsEpochEncryptionKeyPairV1_0_0);
test_migration_to_db_v1_for_entity!(test_mls_hpke_private_key_migration, MlsHpkePrivateKey, MlsHpkePrivateKeyV1_0_0);
test_migration_to_db_v1_for_entity!(test_e2ei_intermediate_cert_migration, E2eiIntermediateCert, E2eiIntermediateCertV1_0_0);
test_migration_to_db_v1_for_entity!(test_e2ei_crl_migration, E2eiCrl, E2eiCrlV1_0_0);
test_migration_to_db_v1_for_entity!(test_e2ei_enrollment_migration, E2eiEnrollment, E2eiEnrollmentV1_0_0);
test_migration_to_db_v1_for_entity!(test_e2ei_ca_migration, E2eiAcmeCA, E2eiAcmeCAV1_0_0, unique_entity:true);
test_migration_to_db_v1_for_entity!(test_e2ei_token_migration, E2eiRefreshToken, E2eiRefreshTokenV1_0_0, unique_entity:true);
}
}

}
}
cfg_if::cfg_if! {
if #[cfg(feature = "proteus-keystore")] {
test_for_entity!(test_proteus_identity, ProteusIdentity ignore_entity_count:true ignore_update:true);
test_for_entity!(test_proteus_prekey, ProteusPrekey);
test_for_entity!(test_proteus_session, ProteusSession);
cfg_if::cfg_if! {
if #[cfg(target_family = "wasm")] {
test_migration_to_db_v1_for_entity!(test_proteus_session_migration, ProteusSession, ProteusSessionV1_0_0);
}
}
}
}
#[apply(all_storage_types)]
Expand Down Expand Up @@ -328,7 +435,22 @@ pub mod utils {
impl_entity_random_update_ext!(PersistedMlsGroup, id_field=id, blob_fields=[state,], additional_fields=[(parent_id: None),]);
impl_entity_random_update_ext!(PersistedMlsPendingGroup, id_field=id, blob_fields=[state,custom_configuration,], additional_fields=[(parent_id: None),]);
impl_entity_random_update_ext!(MlsPendingMessage, id_field=id, blob_fields=[message,]);

cfg_if::cfg_if! {
if #[cfg(target_family = "wasm")] {
impl_entity_random_ext!(MlsKeyPackageV1_0_0, blob_fields=[keypackage,], additional_fields=[(keypackage_ref: uuid::Uuid::new_v4().hyphenated().to_string().into()),]);
impl_entity_random_ext!(MlsCredentialV1_0_0, id_field=id, blob_fields=[credential,], additional_fields=[(created_at: 0),]);
impl_entity_random_ext!(MlsSignatureKeyPairV1_0_0, blob_fields=[pk,keypair,credential_id,], additional_fields=[(signature_scheme: rand::random()),]);
impl_entity_random_ext!(MlsHpkePrivateKeyV1_0_0, blob_fields=[pk,sk,]);
impl_entity_random_ext!(MlsEncryptionKeyPairV1_0_0, blob_fields=[pk,sk,]);
impl_entity_random_ext!(MlsPskBundleV1_0_0, blob_fields=[psk,psk_id,]);
impl_entity_random_ext!(PersistedMlsGroupV1_0_0, id_field=id, blob_fields=[state,], additional_fields=[(parent_id: None),]);
impl_entity_random_ext!(PersistedMlsPendingGroupV1_0_0, id_field=id, blob_fields=[state,custom_configuration,], additional_fields=[(parent_id: None),]);
impl_entity_random_ext!(MlsPendingMessageV1_0_0, id_field=id, blob_fields=[message,]);

impl_entity_random_ext!(E2eiCrlV1_0_0, blob_fields=[content,], additional_fields=[(distribution_point: "some-distribution-point".into()),]);
impl_entity_random_ext!(E2eiIntermediateCertV1_0_0, blob_fields=[content,], additional_fields=[(ski_aki_pair: "some-key-pair".into()),]);
impl_entity_random_ext!(E2eiAcmeCAV1_0_0, blob_fields=[content,]);
impl_entity_random_ext!(E2eiRefreshTokenV1_0_0, blob_fields=[content,]);
}
}
}
Expand All @@ -338,6 +460,11 @@ pub mod utils {
if #[cfg(feature = "proteus-keystore")] {

impl_entity_random_update_ext!(ProteusSession, blob_fields=[session,], additional_fields=[(id: uuid::Uuid::new_v4().hyphenated().to_string()),]);
cfg_if::cfg_if! {
if #[cfg(target_family = "wasm")] {
impl_entity_random_ext!(ProteusSessionV1_0_0, blob_fields=[session,], additional_fields=[(id: uuid::Uuid::new_v4().hyphenated().to_string()),]);
}
}

impl EntityRandomExt for core_crypto_keystore::entities::ProteusPrekey {
fn random() -> Self {
Expand Down Expand Up @@ -391,6 +518,12 @@ pub mod utils {

impl_entity_random_update_ext!(E2eiEnrollment, id_field=id, blob_fields=[content,]);
impl_entity_random_update_ext!(MlsEpochEncryptionKeyPair, id_field=id, blob_fields=[keypairs,]);
cfg_if::cfg_if! {
if #[cfg(target_family = "wasm")] {
impl_entity_random_ext!(E2eiEnrollmentV1_0_0, id_field=id, blob_fields=[content,]);
impl_entity_random_ext!(MlsEpochEncryptionKeyPairV1_0_0, id_field=id, blob_fields=[keypairs,]);
}
}


impl EntityRandomExt for core_crypto_keystore::entities::E2eiIntermediateCert {
Expand Down

0 comments on commit 44d6737

Please sign in to comment.