Skip to content

Commit

Permalink
test: [#989] add more tests for keys
Browse files Browse the repository at this point in the history
There are four ways of adding keys to the tracker. One for each combination of:

- Expiring or permanent key.
- Pre-generated (uploaded) ot randomdly generated key.

This commit adds new tests for each case.
  • Loading branch information
josecelano committed Aug 1, 2024
1 parent 8d58882 commit 349692b
Showing 1 changed file with 187 additions and 55 deletions.
242 changes: 187 additions & 55 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,35 +1785,8 @@ mod tests {
use std::str::FromStr;
use std::time::Duration;

use torrust_tracker_clock::clock::Time;
use torrust_tracker_configuration::v2::core::PrivateMode;

use crate::core::auth::{self, Key};
use crate::core::auth::{self};
use crate::core::tests::the_tracker::private_tracker;
use crate::CurrentClock;

#[tokio::test]
async fn it_should_generate_the_expiring_authentication_keys() {
let tracker = private_tracker();

let key = tracker.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();

assert_eq!(
key.valid_until,
Some(CurrentClock::now_add(&Duration::from_secs(100)).unwrap())
);
}

#[tokio::test]
async fn it_should_authenticate_a_peer_by_using_a_key() {
let tracker = private_tracker();

let expiring_key = tracker.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();

let result = tracker.authenticate(&expiring_key.key()).await;

assert!(result.is_ok());
}

#[tokio::test]
async fn it_should_fail_authenticating_a_peer_when_it_uses_an_unregistered_key() {
Expand All @@ -1826,33 +1799,6 @@ mod tests {
assert!(result.is_err());
}

#[tokio::test]
async fn it_should_verify_a_valid_authentication_key() {
let tracker = private_tracker();

let expiring_key = tracker.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();

assert!(tracker.authenticate(&expiring_key.key()).await.is_ok());
}

#[tokio::test]
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration() {
let mut tracker = private_tracker();

tracker.config.private_mode = Some(PrivateMode {
check_keys_expiration: false,
});

let past_time = Some(Duration::ZERO);

let expiring_key = tracker
.add_auth_key(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(), past_time)
.await
.unwrap();

assert!(tracker.authenticate(&expiring_key.key()).await.is_ok());
}

#[tokio::test]
async fn it_should_fail_verifying_an_unregistered_authentication_key() {
let tracker = private_tracker();
Expand Down Expand Up @@ -1888,6 +1834,192 @@ mod tests {
assert!(result.is_ok());
assert!(tracker.verify_auth_key(&expiring_key.key()).await.is_ok());
}

mod with_expiring_and {

mod randomly_generated_keys {
use std::time::Duration;

use torrust_tracker_clock::clock::Time;
use torrust_tracker_configuration::v2::core::PrivateMode;

use crate::core::auth::Key;
use crate::core::tests::the_tracker::private_tracker;
use crate::CurrentClock;

#[tokio::test]
async fn it_should_generate_the_key() {
let tracker = private_tracker();

let peer_key = tracker.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();

assert_eq!(
peer_key.valid_until,
Some(CurrentClock::now_add(&Duration::from_secs(100)).unwrap())
);
}

#[tokio::test]
async fn it_should_authenticate_a_peer_with_the_key() {
let tracker = private_tracker();

let peer_key = tracker.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();

let result = tracker.authenticate(&peer_key.key()).await;

assert!(result.is_ok());
}

#[tokio::test]
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration() {
let mut tracker = private_tracker();

tracker.config.private_mode = Some(PrivateMode {
check_keys_expiration: false,
});

let past_timestamp = Duration::ZERO;

let peer_key = tracker
.add_auth_key(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(), Some(past_timestamp))
.await
.unwrap();

assert!(tracker.authenticate(&peer_key.key()).await.is_ok());
}
}

mod pre_generated_keys {
use std::time::Duration;

use torrust_tracker_clock::clock::Time;
use torrust_tracker_configuration::v2::core::PrivateMode;

use crate::core::auth::Key;
use crate::core::tests::the_tracker::private_tracker;
use crate::core::AddKeyRequest;
use crate::CurrentClock;

#[tokio::test]
async fn it_should_add_a_pre_generated_key() {
let tracker = private_tracker();

let peer_key = tracker
.add_peer_key(AddKeyRequest {
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
opt_seconds_valid: Some(100),
})
.await
.unwrap();

assert_eq!(
peer_key.valid_until,
Some(CurrentClock::now_add(&Duration::from_secs(100)).unwrap())
);
}

#[tokio::test]
async fn it_should_authenticate_a_peer_with_the_key() {
let tracker = private_tracker();

let peer_key = tracker
.add_peer_key(AddKeyRequest {
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
opt_seconds_valid: Some(100),
})
.await
.unwrap();

let result = tracker.authenticate(&peer_key.key()).await;

assert!(result.is_ok());
}

#[tokio::test]
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration() {
let mut tracker = private_tracker();

tracker.config.private_mode = Some(PrivateMode {
check_keys_expiration: false,
});

let peer_key = tracker
.add_peer_key(AddKeyRequest {
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
opt_seconds_valid: Some(0),
})
.await
.unwrap();

assert!(tracker.authenticate(&peer_key.key()).await.is_ok());
}
}
}

mod with_permanent_and {

mod randomly_generated_keys {
use crate::core::tests::the_tracker::private_tracker;

#[tokio::test]
async fn it_should_generate_the_key() {
let tracker = private_tracker();

let peer_key = tracker.generate_permanent_auth_key().await.unwrap();

assert_eq!(peer_key.valid_until, None);
}

#[tokio::test]
async fn it_should_authenticate_a_peer_with_the_key() {
let tracker = private_tracker();

let peer_key = tracker.generate_permanent_auth_key().await.unwrap();

let result = tracker.authenticate(&peer_key.key()).await;

assert!(result.is_ok());
}
}

mod pre_generated_keys {
use crate::core::auth::Key;
use crate::core::tests::the_tracker::private_tracker;
use crate::core::AddKeyRequest;

#[tokio::test]
async fn it_should_add_a_pre_generated_key() {
let tracker = private_tracker();

let peer_key = tracker
.add_peer_key(AddKeyRequest {
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
opt_seconds_valid: None,
})
.await
.unwrap();

assert_eq!(peer_key.valid_until, None);
}

#[tokio::test]
async fn it_should_authenticate_a_peer_with_the_key() {
let tracker = private_tracker();

let peer_key = tracker
.add_peer_key(AddKeyRequest {
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
opt_seconds_valid: None,
})
.await
.unwrap();

let result = tracker.authenticate(&peer_key.key()).await;

assert!(result.is_ok());
}
}
}
}

mod handling_an_announce_request {}
Expand Down

0 comments on commit 349692b

Please sign in to comment.