tests.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*! Unit tests. Note that these require
  2. https://gitlab.torproject.org/vecna/lox/-/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. use tokio::spawn;
  19. // These are all combined into the same test because otherwise we run into
  20. // issues with server state due to asynchronicity.
  21. #[tokio::test]
  22. async fn test_credential_operations() {
  23. let net = HyperNet {
  24. hostname: "http://localhost:8001".to_string(),
  25. };
  26. let net_test = HyperNet {
  27. hostname: "http://localhost:8005".to_string(),
  28. };
  29. let la_pubkeys = get_lox_auth_keys(&net).await;
  30. // Get new Lox credential
  31. let open_inv = get_open_invitation(&net).await;
  32. let (mut cred, bridgeline) =
  33. get_lox_credential(&net, &open_inv, get_lox_pub(&la_pubkeys)).await;
  34. let bucket = get_bucket(&net, &cred).await;
  35. assert_eq!(bucket[0], bridgeline);
  36. assert_eq!(bucket[1], BridgeLine::default());
  37. assert_eq!(bucket[2], BridgeLine::default());
  38. // Level up Lox Credential
  39. assert_eq!(scalar_u32(&cred.trust_level).unwrap(), 0);
  40. // Advance server time and trust migrate
  41. advance_days(&net_test, u16::try_from(UNTRUSTED_INTERVAL).unwrap()).await;
  42. assert!(eligible_for_trust_promotion(&net, &cred).await);
  43. let migration_cred = trust_promotion(&net, &cred, get_lox_pub(&la_pubkeys)).await;
  44. cred = trust_migration(
  45. &net,
  46. &cred,
  47. &migration_cred,
  48. get_lox_pub(&la_pubkeys),
  49. get_migration_pub(&la_pubkeys),
  50. )
  51. .await;
  52. assert_eq!(scalar_u32(&cred.trust_level).unwrap(), 1);
  53. // Advance server time and level up
  54. for i in 1..LEVEL_INTERVAL.len() {
  55. assert_eq!(
  56. scalar_u32(&cred.trust_level).unwrap(),
  57. u32::try_from(i).unwrap()
  58. );
  59. advance_days(&net_test, u16::try_from(LEVEL_INTERVAL[i]).unwrap()).await;
  60. assert!(eligible_for_level_up(&net, &cred).await);
  61. let encbuckets = get_reachability_credential(&net).await;
  62. cred = level_up(
  63. &net,
  64. &cred,
  65. &encbuckets,
  66. get_lox_pub(&la_pubkeys),
  67. get_reachability_pub(&la_pubkeys),
  68. )
  69. .await
  70. .0;
  71. // Assert that we increased level by 1 or stayed at 4
  72. assert_eq!(
  73. scalar_u32(&cred.trust_level).unwrap(),
  74. u32::try_from(min(i + 1, LEVEL_INTERVAL.len() - 1)).unwrap()
  75. );
  76. assert_eq!(
  77. scalar_u32(&cred.invites_remaining).unwrap(),
  78. LEVEL_INVITATIONS[i]
  79. );
  80. // Invite as many friends as possible
  81. for j in 0..LEVEL_INVITATIONS[i] {
  82. let encbuckets = get_reachability_credential(&net).await;
  83. let (new_cred, invite) = issue_invite(
  84. &net,
  85. &cred,
  86. &encbuckets,
  87. get_lox_pub(&la_pubkeys),
  88. get_reachability_pub(&la_pubkeys),
  89. get_invitation_pub(&la_pubkeys),
  90. )
  91. .await;
  92. let (friend_cred, friend_bucket) = redeem_invite(
  93. &net,
  94. &invite,
  95. get_lox_pub(&la_pubkeys),
  96. get_invitation_pub(&la_pubkeys),
  97. )
  98. .await;
  99. cred = new_cred;
  100. assert_eq!(
  101. scalar_u32(&cred.invites_remaining).unwrap(),
  102. LEVEL_INVITATIONS[i] - j - 1
  103. );
  104. // TODO: Where is this defined? Should I use the library constant?
  105. assert_eq!(scalar_u32(&friend_cred.trust_level).unwrap(), 1);
  106. assert_eq!(&cred.bucket, &friend_cred.bucket);
  107. }
  108. }
  109. }