tests.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*! Unit tests that require access to the testing-only function
  2. BridgeLine::random() or private fields */
  3. use super::bridge_table::BridgeLine;
  4. use super::*;
  5. #[test]
  6. fn test_open_invite() {
  7. // Create a BridegDb
  8. let bdb = BridgeDb::new(20);
  9. // Create a BridgeAuth
  10. let mut ba = BridgeAuth::new(bdb.pubkey);
  11. // Make 20 buckets with one random bridge each
  12. for _ in 0..20 {
  13. let bucket: [BridgeLine; 3] =
  14. [BridgeLine::random(), Default::default(), Default::default()];
  15. ba.bridge_table.new_bucket(bucket);
  16. }
  17. // And 20 more with three random bridges each
  18. for _ in 0..20 {
  19. let bucket: [BridgeLine; 3] = [
  20. BridgeLine::random(),
  21. BridgeLine::random(),
  22. BridgeLine::random(),
  23. ];
  24. ba.bridge_table.new_bucket(bucket);
  25. }
  26. // Create the encrypted bridge table
  27. ba.bridge_table.encrypt_table();
  28. // Issue an open invitation
  29. let inv = bdb.invite();
  30. // Use it to get a Lox credential
  31. let (req, state) = open_invite::request(&inv);
  32. let resp = ba.handle_open_invite(req).unwrap();
  33. let cred = open_invite::handle_response(state, resp, &ba.lox_pub, &ba.migration_pub).unwrap();
  34. // Check that we can use the credential to read a bucket
  35. let (id, key) = bridge_table::from_scalar(cred.bucket).unwrap();
  36. let bucket = ba.bridge_table.decrypt_bucket_id(id, &key).unwrap();
  37. println!("cred = {:?}", cred);
  38. println!("bucket = {:?}", bucket);
  39. }
  40. #[test]
  41. fn test_trust_promotion() {
  42. // Create a BridegDb
  43. let bdb = BridgeDb::new(15);
  44. // Create a BridgeAuth
  45. let mut ba = BridgeAuth::new(bdb.pubkey);
  46. // Make 15 buckets with one random bridge each
  47. for _ in 0..15 {
  48. let bucket: [BridgeLine; 3] =
  49. [BridgeLine::random(), Default::default(), Default::default()];
  50. ba.bridge_table.new_bucket(bucket);
  51. }
  52. // Make 5 more buckets, each containing 3 of the previously
  53. // created bridges
  54. for i in 0u32..5 {
  55. let iusize = i as usize;
  56. let bucket: [BridgeLine; 3] = [
  57. ba.bridge_table.buckets[3 * iusize][0],
  58. ba.bridge_table.buckets[3 * iusize + 1][0],
  59. ba.bridge_table.buckets[3 * iusize + 2][0],
  60. ];
  61. ba.bridge_table.new_bucket(bucket);
  62. // Add the allowed migrations to the migration table
  63. ba.migration_table.table.push((3 * i, 15 + i));
  64. ba.migration_table.table.push((3 * i + 1, 15 + i));
  65. ba.migration_table.table.push((3 * i + 2, 15 + i));
  66. }
  67. // Create the encrypted bridge table
  68. ba.bridge_table.encrypt_table();
  69. // Issue an open invitation
  70. let inv = bdb.invite();
  71. // Use it to get a Lox credential
  72. let (req, state) = open_invite::request(&inv);
  73. let resp = ba.handle_open_invite(req).unwrap();
  74. let cred = open_invite::handle_response(state, resp, &ba.lox_pub, &ba.migration_pub).unwrap();
  75. // Time passes
  76. ba.advance_days(40);
  77. let (promreq, promstate) = trust_promotion::request(&cred, &ba.lox_pub, ba.today()).unwrap();
  78. }