tests.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*! Unit tests. Note that these require
  2. https://gitlab.torproject.org/vecna/lox-rs/-/tree/main/crates/lox-distributor
  3. to be running. That fork adds an endpoint which allows for artificially
  4. increasing the number of days that have passed, which allows us to test
  5. trust migration and level up functions. */
  6. // TODO:
  7. // - unit test file save/read functions
  8. // - unit test migration when possible
  9. // Note: We can't run multiple time-changing tests simultaneously because
  10. // they will invalidate invites that haven't been redeemed yet.
  11. use super::client_lib::*;
  12. use super::client_net::HyperNet;
  13. use lox_library::bridge_table::BridgeLine;
  14. use lox_library::proto::level_up::{LEVEL_INTERVAL, LEVEL_INVITATIONS};
  15. use lox_library::proto::trust_promotion::UNTRUSTED_INTERVAL;
  16. use lox_library::scalar_u32;
  17. use std::cmp::min;
  18. // These are all combined into the same test because otherwise we run into
  19. // issues with server state due to asynchronicity.
  20. #[tokio::test]
  21. async fn test_credential_operations() {
  22. let net = HyperNet {
  23. hostname: "http://localhost:8001".to_string(),
  24. };
  25. let la_pubkeys = get_lox_auth_keys(&net).await;
  26. // Get new Lox credential
  27. let open_inv = get_open_invitation(&net).await;
  28. let (mut cred, bridgeline) =
  29. get_lox_credential(&net, &open_inv, get_lox_pub(&la_pubkeys)).await;
  30. let bucket = get_bucket(&net, &cred).await;
  31. assert_eq!(bucket[0], bridgeline);
  32. assert_eq!(bucket[1], BridgeLine::default());
  33. assert_eq!(bucket[2], BridgeLine::default());
  34. // Level up Lox Credential
  35. assert_eq!(scalar_u32(&cred.trust_level).unwrap(), 0);
  36. // Advance server time and trust migrate
  37. advance_days(&net, u16::try_from(UNTRUSTED_INTERVAL).unwrap()).await;
  38. assert!(eligible_for_trust_promotion(&net, &cred).await);
  39. let migration_cred = trust_promotion(&net, &cred, get_lox_pub(&la_pubkeys)).await;
  40. cred = trust_migration(
  41. &net,
  42. &cred,
  43. &migration_cred,
  44. get_lox_pub(&la_pubkeys),
  45. get_migration_pub(&la_pubkeys),
  46. )
  47. .await;
  48. assert_eq!(scalar_u32(&cred.trust_level).unwrap(), 1);
  49. // Advance server time and level up
  50. for i in 1..LEVEL_INTERVAL.len() {
  51. assert_eq!(
  52. scalar_u32(&cred.trust_level).unwrap(),
  53. u32::try_from(i).unwrap()
  54. );
  55. advance_days(&net, u16::try_from(LEVEL_INTERVAL[i]).unwrap()).await;
  56. assert!(eligible_for_level_up(&net, &cred).await);
  57. let encbuckets = get_reachability_credential(&net).await;
  58. cred = level_up(
  59. &net,
  60. &cred,
  61. &encbuckets,
  62. get_lox_pub(&la_pubkeys),
  63. get_reachability_pub(&la_pubkeys),
  64. )
  65. .await
  66. .0;
  67. // Assert that we increased level by 1 or stayed at 4
  68. assert_eq!(
  69. scalar_u32(&cred.trust_level).unwrap(),
  70. u32::try_from(min(i + 1, LEVEL_INTERVAL.len() - 1)).unwrap()
  71. );
  72. assert_eq!(
  73. scalar_u32(&cred.invites_remaining).unwrap(),
  74. LEVEL_INVITATIONS[i]
  75. );
  76. // Invite as many friends as possible
  77. for j in 0..LEVEL_INVITATIONS[i] {
  78. let encbuckets = get_reachability_credential(&net).await;
  79. let (new_cred, invite) = issue_invite(
  80. &net,
  81. &cred,
  82. &encbuckets,
  83. get_lox_pub(&la_pubkeys),
  84. get_reachability_pub(&la_pubkeys),
  85. get_invitation_pub(&la_pubkeys),
  86. )
  87. .await;
  88. let (friend_cred, friend_bucket) = redeem_invite(
  89. &net,
  90. &invite,
  91. get_lox_pub(&la_pubkeys),
  92. get_invitation_pub(&la_pubkeys),
  93. )
  94. .await;
  95. cred = new_cred;
  96. assert_eq!(
  97. scalar_u32(&cred.invites_remaining).unwrap(),
  98. LEVEL_INVITATIONS[i] - j - 1
  99. );
  100. // TODO: Where is this defined? Should I use the library constant?
  101. assert_eq!(scalar_u32(&friend_cred.trust_level).unwrap(), 1);
  102. assert_eq!(&cred.bucket, &friend_cred.bucket);
  103. }
  104. }
  105. }