diff --git a/crates/lox-library/src/lib.rs b/crates/lox-library/src/lib.rs index 52159df..ab1d9e4 100644 --- a/crates/lox-library/src/lib.rs +++ b/crates/lox-library/src/lib.rs @@ -257,95 +257,6 @@ impl BridgeAuth { // The protocol modules pub mod open_invite; -// Unit tests that require access to the testing-only function -// BridgeLine::random() +// Unit tests #[cfg(test)] -mod tests { - use super::bridge_table::BridgeLine; - use super::*; - - #[test] - fn test_open_invite() { - // Create a BridegDb - let bdb = BridgeDb::new(20); - // Create a BridgeAuth - let mut ba = BridgeAuth::new(bdb.pubkey); - - // Make 20 buckets with one random bridge each - for _ in 0..20 { - let bucket: [BridgeLine; 3] = - [BridgeLine::random(), Default::default(), Default::default()]; - ba.bridge_table.new_bucket(bucket); - } - // And 20 more with three random bridges each - for _ in 0..20 { - let bucket: [BridgeLine; 3] = [ - BridgeLine::random(), - BridgeLine::random(), - BridgeLine::random(), - ]; - ba.bridge_table.new_bucket(bucket); - } - // Create the encrypted bridge table - ba.bridge_table.encrypt_table(); - - // Issue an open invitation - let inv = bdb.invite(); - - // Use it to get a Lox credential - let (req, state) = open_invite::request(&inv); - let resp = ba.handle_open_invite(req).unwrap(); - let cred = - open_invite::handle_response(state, resp, &ba.lox_pub, &ba.migration_pub).unwrap(); - - // Check that we can use the credential to read a bucket - let (id, key) = bridge_table::from_scalar(cred.bucket).unwrap(); - let bucket = ba.bridge_table.decrypt_bucket_id(id, &key).unwrap(); - println!("cred = {:?}", cred); - println!("bucket = {:?}", bucket); - } - - #[test] - fn test_trust_promotion() { - // Create a BridegDb - let bdb = BridgeDb::new(15); - // Create a BridgeAuth - let mut ba = BridgeAuth::new(bdb.pubkey); - - // Make 15 buckets with one random bridge each - for _ in 0..15 { - let bucket: [BridgeLine; 3] = - [BridgeLine::random(), Default::default(), Default::default()]; - ba.bridge_table.new_bucket(bucket); - } - // Make 5 more buckets, each containing 3 of the previously - // created bridges - for i in 0u32..5 { - let iusize = i as usize; - let bucket: [BridgeLine; 3] = [ - ba.bridge_table.buckets[3 * iusize][0], - ba.bridge_table.buckets[3 * iusize + 1][0], - ba.bridge_table.buckets[3 * iusize + 2][0], - ]; - ba.bridge_table.new_bucket(bucket); - // Add the allowed migrations to the migration table - ba.migration_table.table.push((3 * i, 15 + i)); - ba.migration_table.table.push((3 * i + 1, 15 + i)); - ba.migration_table.table.push((3 * i + 2, 15 + i)); - } - // Create the encrypted bridge table - ba.bridge_table.encrypt_table(); - - // Issue an open invitation - let inv = bdb.invite(); - - // Use it to get a Lox credential - let (req, state) = open_invite::request(&inv); - let resp = ba.handle_open_invite(req).unwrap(); - let cred = - open_invite::handle_response(state, resp, &ba.lox_pub, &ba.migration_pub).unwrap(); - - // Time passes - ba.advance_days(40); - } -} +mod tests; diff --git a/crates/lox-library/src/tests.rs b/crates/lox-library/src/tests.rs new file mode 100644 index 0000000..4cdb51d --- /dev/null +++ b/crates/lox-library/src/tests.rs @@ -0,0 +1,90 @@ +/*! Unit tests that require access to the testing-only function +BridgeLine::random() or private fields */ + +use super::bridge_table::BridgeLine; +use super::*; + +#[test] +fn test_open_invite() { + // Create a BridegDb + let bdb = BridgeDb::new(20); + // Create a BridgeAuth + let mut ba = BridgeAuth::new(bdb.pubkey); + + // Make 20 buckets with one random bridge each + for _ in 0..20 { + let bucket: [BridgeLine; 3] = + [BridgeLine::random(), Default::default(), Default::default()]; + ba.bridge_table.new_bucket(bucket); + } + // And 20 more with three random bridges each + for _ in 0..20 { + let bucket: [BridgeLine; 3] = [ + BridgeLine::random(), + BridgeLine::random(), + BridgeLine::random(), + ]; + ba.bridge_table.new_bucket(bucket); + } + // Create the encrypted bridge table + ba.bridge_table.encrypt_table(); + + // Issue an open invitation + let inv = bdb.invite(); + + // Use it to get a Lox credential + let (req, state) = open_invite::request(&inv); + let resp = ba.handle_open_invite(req).unwrap(); + let cred = + open_invite::handle_response(state, resp, &ba.lox_pub, &ba.migration_pub).unwrap(); + + // Check that we can use the credential to read a bucket + let (id, key) = bridge_table::from_scalar(cred.bucket).unwrap(); + let bucket = ba.bridge_table.decrypt_bucket_id(id, &key).unwrap(); + println!("cred = {:?}", cred); + println!("bucket = {:?}", bucket); +} + +#[test] +fn test_trust_promotion() { + // Create a BridegDb + let bdb = BridgeDb::new(15); + // Create a BridgeAuth + let mut ba = BridgeAuth::new(bdb.pubkey); + + // Make 15 buckets with one random bridge each + for _ in 0..15 { + let bucket: [BridgeLine; 3] = + [BridgeLine::random(), Default::default(), Default::default()]; + ba.bridge_table.new_bucket(bucket); + } + // Make 5 more buckets, each containing 3 of the previously + // created bridges + for i in 0u32..5 { + let iusize = i as usize; + let bucket: [BridgeLine; 3] = [ + ba.bridge_table.buckets[3 * iusize][0], + ba.bridge_table.buckets[3 * iusize + 1][0], + ba.bridge_table.buckets[3 * iusize + 2][0], + ]; + ba.bridge_table.new_bucket(bucket); + // Add the allowed migrations to the migration table + ba.migration_table.table.push((3 * i, 15 + i)); + ba.migration_table.table.push((3 * i + 1, 15 + i)); + ba.migration_table.table.push((3 * i + 2, 15 + i)); + } + // Create the encrypted bridge table + ba.bridge_table.encrypt_table(); + + // Issue an open invitation + let inv = bdb.invite(); + + // Use it to get a Lox credential + let (req, state) = open_invite::request(&inv); + let resp = ba.handle_open_invite(req).unwrap(); + let cred = + open_invite::handle_response(state, resp, &ba.lox_pub, &ba.migration_pub).unwrap(); + + // Time passes + ba.advance_days(40); +}