Browse Source

WIP blockage_migration test

onyinyang 1 month ago
parent
commit
10b410b9c1
1 changed files with 31 additions and 21 deletions
  1. 31 21
      src/proto/blockage_migration.rs

+ 31 - 21
src/proto/blockage_migration.rs

@@ -35,7 +35,7 @@ and a new Lox credential to be issued:
 use super::super::dup_filter::SeenType;
 #[cfg(feature = "bridgeauth")]
 use super::super::BridgeAuth;
-use super::super::{scalar_u32, G};
+use super::super::{scalar_u32, Scalar, G};
 use super::check_blockage::MIN_TRUST_LEVEL;
 use super::errors::CredentialError;
 use super::level_up::LEVEL_INVITATIONS;
@@ -49,21 +49,21 @@ use sha2::Sha512;
 
 const SESSION_ID: &[u8] = b"blockage_migration";
 
-muCMZProtocol! { blockage_migration<min_trust_level>,
+muCMZProtocol! { blockage_migration,
     [ L: Lox { id: R, bucket: H, trust_level: R, level_since: H, invites_remaining: H, blockages: H },
     M: Migration { lox_id: R, from_bucket: H, to_bucket: H } ],
-    N: Lox {id: J, bucket: H, trust_level: R, level_since: S, invites_remaining: I, blockages: H },
+    N: Lox {id: J, bucket: H, trust_level: I, level_since: S, invites_remaining: I, blockages: H },
     L.bucket = M.from_bucket,
     N.bucket = M.to_bucket,
     N.blockages = L.blockages + 1,
 }
 
 pub fn request(
+    rng: &mut (impl CryptoRng + RngCore),
     L: Lox,
     M: Migration,
     pubkeys: CMZPubkey<G>,
 ) -> Result<(blockage_migration::Request, blockage_migration::ClientState), CredentialError> {
-    let mut rng = rand::thread_rng();
     cmz_group_init(G::hash_from_bytes::<Sha512>(b"CMZ Generator A"));
 
     // Ensure that the credenials can be correctly shown; that is, the
@@ -95,18 +95,15 @@ pub fn request(
     }
 
     let mut N = Lox::using_pubkey(&pubkeys);
+    N.bucket = M.to_bucket;
     N.trust_level = Some((level - 2).into());
     // The invites remaining is the appropriate number for the new
     // level (note that LEVEL_INVITATIONS[i] is the number of
     // invitations for moving from level i to level i+1)
     N.invites_remaining = Some(LEVEL_INVITATIONS[(level - 3) as usize].into());
-    let params = blockage_migration::Params {
-        // Since we do not yet (and may not?) have PartialOrd for
-        // protocols, we will subtract and extra value and check strictly gt
-        min_trust_level: (MIN_TRUST_LEVEL - 1).into(),
-    };
+    N.blockages = Some(L.blockages.unwrap() + Scalar::ONE);
 
-    match blockage_migration::prepare(&mut rng, SESSION_ID, &L, &M, N, &params) {
+    match blockage_migration::prepare(rng, SESSION_ID, &L, &M, N) {
         Ok(req_state) => Ok(req_state),
         Err(e) => Err(CredentialError::CMZError(e)),
     }
@@ -131,10 +128,8 @@ impl BridgeAuth {
                 let level: u32 = match scalar_u32(&L.trust_level.unwrap()) {
                     Some(v) if v >= MIN_TRUST_LEVEL && v as usize <= MAX_LEVEL => v,
                     _ => {
-                        // This error should be improved i.e., InvalidAttr and the type
-                        // with a description
                         return Err(CMZError::RevealAttrMissing(
-                            "blockage_migration",
+                            "level",
                             "Could not be converted to u32 or value not in range",
                         ));
                     }
@@ -142,20 +137,13 @@ impl BridgeAuth {
                 if L.id.is_some_and(|b| b != M.lox_id.unwrap()) {
                     return Err(CMZError::IssProofFailed);
                 }
-                if L.trust_level
-                    .is_some_and(|b| scalar_u32(&b).unwrap() < MIN_TRUST_LEVEL)
-                {
-                    return Err(CMZError::IssProofFailed);
-                }
                 L.set_privkey(&self.lox_priv);
                 M.set_privkey(&self.migration_priv);
                 N.set_privkey(&self.lox_priv);
                 N.trust_level = Some((level - 2).into());
                 N.level_since = Some(today.into());
                 N.invites_remaining = Some(LEVEL_INVITATIONS[(level - 3) as usize].into());
-                Ok(blockage_migration::Params {
-                    min_trust_level: (MIN_TRUST_LEVEL - 1).into(),
-                })
+                Ok(())
             },
             |L: &Lox, _M: &Migration, _N: &Lox| {
                 if self.id_filter.filter(&L.id.unwrap()) == SeenType::Seen {
@@ -181,3 +169,25 @@ pub fn handle_response(
         Err(_e) => Err(CMZError::Unknown),
     }
 }
+
+#[cfg(all(test, feature = "bridgeauth"))]
+mod tests {
+    use crate::mock_auth::TestHarness;
+
+    #[test]
+    fn test_blockage_migration() {
+        let mut th = TestHarness::new();
+        let rng = &mut rand::thread_rng();
+        let invite = th.bdb.invite().unwrap();
+        let mut lox_cred = th.open_invite(rng, &invite);
+        let mut mig_cred = th.trust_promotion(rng, lox_cred.clone());
+        lox_cred = th.migration(rng, lox_cred.clone(), mig_cred.clone());
+        let lox_cred_1 = th.level_up(rng, lox_cred.clone());
+        let lox_cred_2 = th.level_up(rng, lox_cred_1.clone());
+        let lox_cred_3 = th.level_up(rng, lox_cred_2.clone());
+        th.block_bridges(lox_cred_3.clone());
+        mig_cred = th.check_blockage(rng, lox_cred_3.clone());
+        lox_cred = th.blockage_migration(rng, lox_cred_3.clone(), mig_cred.clone());
+        th.verify_lox(&lox_cred);
+    }
+}