Skip to content

Commit

Permalink
test(rust): fix Identity comparison test
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjoDeundiak committed Aug 24, 2023
1 parent 540c7d8 commit 8ca9734
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ impl IdentitiesCreation {

/// Create an `Identity` with a key previously created in the Vault and store it
pub async fn create_identity_with_existing_key(&self, key_id: &KeyId) -> Result<Identity> {
// TODO: Consider creating PurposeKeys by default
self.make_and_persist_identity(Some(key_id)).await
}

Expand Down
88 changes: 76 additions & 12 deletions implementations/rust/ockam/ockam_identity/src/identity/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ impl Display for Identity {
mod tests {
use super::super::super::identities;
use super::*;
use crate::Identities;
use core::str::FromStr;
use ockam_core::compat::rand::RngCore;
use ockam_vault::{Secret, SecretAttributes, SoftwareSigningVault, Vault};
use rand::thread_rng;

#[tokio::test]
async fn test_display() {
Expand All @@ -200,41 +204,101 @@ Change history: 81a201583ba20101025835a4028201815820bd144a3f6472ba2215b6b86b2820

#[tokio::test]
async fn test_compare() -> Result<()> {
let identities = identities();
let identity1 = identities.identities_creation().create_identity().await?;
let signing_vault0 = SoftwareSigningVault::create();
let signing_vault01 = SoftwareSigningVault::create();
let signing_vault02 = SoftwareSigningVault::create();

let identity2 = identities
let mut key0_bin = [0u8; 32];
thread_rng().fill_bytes(&mut key0_bin);

let key0 = signing_vault0
.import_key(Secret::new(key0_bin.to_vec()), SecretAttributes::Ed25519)
.await?;
let key01 = signing_vault01
.import_key(Secret::new(key0_bin.to_vec()), SecretAttributes::Ed25519)
.await?;
let key02 = signing_vault02
.import_key(Secret::new(key0_bin.to_vec()), SecretAttributes::Ed25519)
.await?;

let identities0 = Identities::builder()
.with_vault(Vault::new(
signing_vault0,
Vault::create_verifying_vault(),
Vault::create_secure_channel_vault(),
))
.build();

let identity0 = identities0
.identities_creation()
.create_identity_with_existing_key(&key0)
.await?;
let identity0_bin = identity0.export()?;

let identities01 = Identities::builder()
.with_vault(Vault::new(
signing_vault01,
Vault::create_verifying_vault(),
Vault::create_secure_channel_vault(),
))
.build();
let identities02 = Identities::builder()
.with_vault(Vault::new(
signing_vault02,
Vault::create_verifying_vault(),
Vault::create_secure_channel_vault(),
))
.build();

let identity01 = identities01
.identities_creation()
.import_private_identity(&identity0_bin, &key01)
.await?;
let identity02 = identities02
.identities_creation()
.import_private_identity(&identity0_bin, &key02)
.await?;

let identity01 = identities01
.identities_keys()
.rotate_key(identity1.clone())
.rotate_key(identity01.clone())
.await?;

let identity3 = identities
let identity02 = identities02
.identities_keys()
.rotate_key(identity1.clone())
.rotate_key(identity02.clone())
.await?;

assert_eq!(
identity1.compare(&identity1),
identity0.compare(&identity0),
IdentityHistoryComparison::Equal
);
assert_eq!(
identity2.compare(&identity2),
identity01.compare(&identity01),
IdentityHistoryComparison::Equal
);
assert_eq!(
identity3.compare(&identity3),
identity02.compare(&identity02),
IdentityHistoryComparison::Equal
);
assert_eq!(
identity1.compare(&identity2),
identity0.compare(&identity01),
IdentityHistoryComparison::Older
);
assert_eq!(
identity2.compare(&identity1),
identity0.compare(&identity02),
IdentityHistoryComparison::Older
);
assert_eq!(
identity01.compare(&identity0),
IdentityHistoryComparison::Newer
);
assert_eq!(
identity02.compare(&identity0),
IdentityHistoryComparison::Newer
);
assert_eq!(
identity2.compare(&identity3),
identity01.compare(&identity02),
IdentityHistoryComparison::Conflict
);

Expand Down

0 comments on commit 8ca9734

Please sign in to comment.