tests.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 crate::{networking::*, *};
  12. use lox_library::{
  13. bridge_table::{self, from_scalar, BridgeLine, BridgeTable},
  14. proto::{
  15. level_up::{LEVEL_INTERVAL, LEVEL_INVITATIONS},
  16. trust_promotion::UNTRUSTED_INTERVAL,
  17. },
  18. scalar_u32,
  19. };
  20. use hyper::StatusCode;
  21. use std::{
  22. cmp::min,
  23. collections::{HashMap, HashSet},
  24. };
  25. use tokio::spawn;
  26. // Advance days on server
  27. pub async fn advance_days(net: &dyn Networking, days: u16) -> u32 {
  28. let resp = net
  29. .request(
  30. "/advancedays".to_string(),
  31. serde_json::to_vec(&days).unwrap(),
  32. )
  33. .await;
  34. let today: u32 = serde_json::from_slice(&resp).unwrap();
  35. today
  36. }
  37. // These are all combined into the same test because otherwise we run into
  38. // issues with server state due to asynchronicity.
  39. #[tokio::test]
  40. async fn test_credential_operations() {
  41. let net = HyperNet {
  42. hostname: "http://localhost:8001".to_string(),
  43. };
  44. let net_test = HyperNet {
  45. hostname: "http://localhost:8005".to_string(),
  46. };
  47. let la_pubkeys = get_lox_auth_keys(&net).await;
  48. // Get new Lox credential
  49. let open_inv = get_open_invitation(&net).await;
  50. let (mut cred, bridgeline) =
  51. get_lox_credential(&net, &open_inv, get_lox_pub(&la_pubkeys)).await;
  52. let bucket = get_bucket(&net, &cred).await;
  53. assert_eq!(bucket[0], bridgeline);
  54. assert_eq!(bucket[1], BridgeLine::default());
  55. assert_eq!(bucket[2], BridgeLine::default());
  56. // Level up Lox Credential
  57. assert_eq!(scalar_u32(&cred.trust_level).unwrap(), 0);
  58. // Advance server time and trust migrate
  59. advance_days(&net_test, u16::try_from(UNTRUSTED_INTERVAL).unwrap()).await;
  60. assert!(eligible_for_trust_promotion(&net, &cred).await);
  61. let migration_cred = trust_promotion(&net, &cred, get_lox_pub(&la_pubkeys)).await;
  62. cred = trust_migration(
  63. &net,
  64. &cred,
  65. &migration_cred,
  66. get_lox_pub(&la_pubkeys),
  67. get_migration_pub(&la_pubkeys),
  68. )
  69. .await;
  70. assert_eq!(scalar_u32(&cred.trust_level).unwrap(), 1);
  71. // Advance server time and level up
  72. for i in 1..LEVEL_INTERVAL.len() {
  73. assert_eq!(
  74. scalar_u32(&cred.trust_level).unwrap(),
  75. u32::try_from(i).unwrap()
  76. );
  77. advance_days(&net_test, u16::try_from(LEVEL_INTERVAL[i]).unwrap()).await;
  78. assert!(eligible_for_level_up(&net, &cred).await);
  79. let encbuckets = get_reachability_credential(&net).await;
  80. cred = level_up(
  81. &net,
  82. &cred,
  83. &encbuckets,
  84. get_lox_pub(&la_pubkeys),
  85. get_reachability_pub(&la_pubkeys),
  86. )
  87. .await
  88. .0;
  89. // Assert that we increased level by 1 or stayed at 4
  90. assert_eq!(
  91. scalar_u32(&cred.trust_level).unwrap(),
  92. u32::try_from(min(i + 1, LEVEL_INTERVAL.len() - 1)).unwrap()
  93. );
  94. assert_eq!(
  95. scalar_u32(&cred.invites_remaining).unwrap(),
  96. LEVEL_INVITATIONS[i]
  97. );
  98. // Invite as many friends as possible
  99. for j in 0..LEVEL_INVITATIONS[i] {
  100. let encbuckets = get_reachability_credential(&net).await;
  101. let (new_cred, invite) = issue_invite(
  102. &net,
  103. &cred,
  104. &encbuckets,
  105. get_lox_pub(&la_pubkeys),
  106. get_reachability_pub(&la_pubkeys),
  107. get_invitation_pub(&la_pubkeys),
  108. )
  109. .await;
  110. let (friend_cred, friend_bucket) = redeem_invite(
  111. &net,
  112. &invite,
  113. get_lox_pub(&la_pubkeys),
  114. get_invitation_pub(&la_pubkeys),
  115. )
  116. .await;
  117. cred = new_cred;
  118. assert_eq!(
  119. scalar_u32(&cred.invites_remaining).unwrap(),
  120. LEVEL_INVITATIONS[i] - j - 1
  121. );
  122. assert_eq!(scalar_u32(&friend_cred.trust_level).unwrap(), 1);
  123. assert_eq!(&cred.bucket, &friend_cred.bucket);
  124. }
  125. }
  126. }