Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a60571c

Browse files
committedNov 18, 2024··
udp: symmetric encrypted cookie
1 parent 0e340bf commit a60571c

File tree

21 files changed

+710
-1089
lines changed

21 files changed

+710
-1089
lines changed
 

‎Cargo.lock

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ axum-extra = { version = "0", features = ["query"] }
3838
axum-server = { version = "0", features = ["tls-rustls"] }
3939
bittorrent-primitives = "0.1.0"
4040
bittorrent-tracker-client = { version = "3.0.0-develop", path = "packages/tracker-client" }
41+
blowfish = "0"
4142
camino = { version = "1", features = ["serde", "serde1"] }
4243
chrono = { version = "0", default-features = false, features = ["clock"] }
44+
cipher = "0"
4345
clap = { version = "4", features = ["derive", "env"] }
4446
crossbeam-skiplist = "0"
4547
dashmap = "6"

‎cSpell.json

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"downloadedi",
5353
"dtolnay",
5454
"elif",
55+
"endianness",
5556
"Eray",
5657
"filesd",
5758
"flamegraph",
@@ -161,6 +162,7 @@
161162
"Trackon",
162163
"typenum",
163164
"Unamed",
165+
"underflows",
164166
"untuple",
165167
"uroot",
166168
"Vagaa",

‎packages/clock/src/lib.rs

-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
pub mod clock;
2727
pub mod conv;
2828
pub mod static_time;
29-
pub mod time_extent;
3029

3130
#[macro_use]
3231
extern crate lazy_static;
@@ -41,13 +40,3 @@ pub(crate) type CurrentClock = clock::Working;
4140
#[cfg(test)]
4241
#[allow(dead_code)]
4342
pub(crate) type CurrentClock = clock::Stopped;
44-
45-
/// Working version, for production.
46-
#[cfg(not(test))]
47-
#[allow(dead_code)]
48-
pub(crate) type DefaultTimeExtentMaker = time_extent::WorkingTimeExtentMaker;
49-
50-
/// Stopped version, for testing.
51-
#[cfg(test)]
52-
#[allow(dead_code)]
53-
pub(crate) type DefaultTimeExtentMaker = time_extent::StoppedTimeExtentMaker;

‎packages/clock/src/time_extent/mod.rs

-665
This file was deleted.

‎packages/configuration/src/v2_0_0/udp_tracker.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2+
use std::time::Duration;
23

34
use serde::{Deserialize, Serialize};
45

@@ -10,11 +11,16 @@ pub struct UdpTracker {
1011
/// system to choose a random port, use port `0`.
1112
#[serde(default = "UdpTracker::default_bind_address")]
1213
pub bind_address: SocketAddr,
14+
15+
/// The lifetime of the server-generated connection cookie, that is passed
16+
/// the client as the `ConnectionId`.
17+
pub cookie_lifetime: Duration,
1318
}
1419
impl Default for UdpTracker {
1520
fn default() -> Self {
1621
Self {
1722
bind_address: Self::default_bind_address(),
23+
cookie_lifetime: Self::default_cookie_lifetime(),
1824
}
1925
}
2026
}
@@ -23,4 +29,8 @@ impl UdpTracker {
2329
fn default_bind_address() -> SocketAddr {
2430
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 6969)
2531
}
32+
33+
fn default_cookie_lifetime() -> Duration {
34+
Duration::from_secs(120)
35+
}
2636
}

‎packages/test-helpers/src/configuration.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Tracker configuration factories for testing.
22
use std::env;
33
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
4+
use std::time::Duration;
45

56
use torrust_tracker_configuration::{Configuration, HttpApi, HttpTracker, Threshold, UdpTracker};
67

@@ -47,6 +48,7 @@ pub fn ephemeral() -> Configuration {
4748
let udp_port = 0u16;
4849
config.udp_trackers = Some(vec![UdpTracker {
4950
bind_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), udp_port),
51+
cookie_lifetime: Duration::from_secs(120),
5052
}]);
5153

5254
// Ephemeral socket address for HTTP tracker

‎src/bootstrap/app.rs

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::bootstrap;
2323
use crate::core::services::tracker_factory;
2424
use crate::core::Tracker;
2525
use crate::shared::crypto::ephemeral_instance_keys;
26+
use crate::shared::crypto::keys::{self, Keeper as _};
2627

2728
/// It loads the configuration from the environment and builds the main domain [`Tracker`] struct.
2829
///
@@ -32,6 +33,9 @@ use crate::shared::crypto::ephemeral_instance_keys;
3233
#[must_use]
3334
#[instrument(skip())]
3435
pub fn setup() -> (Configuration, Arc<Tracker>) {
36+
#[cfg(not(test))]
37+
check_seed();
38+
3539
let configuration = initialize_configuration();
3640

3741
if let Err(e) = configuration.validate() {
@@ -45,6 +49,18 @@ pub fn setup() -> (Configuration, Arc<Tracker>) {
4549
(configuration, tracker)
4650
}
4751

52+
/// checks if the seed is the instance seed in production.
53+
///
54+
/// # Panics
55+
///
56+
/// It would panic if the seed is not the instance seed.
57+
pub fn check_seed() {
58+
let seed = keys::Current::get_seed();
59+
let instance = keys::Instance::get_seed();
60+
61+
assert_eq!(seed, instance, "maybe using zeroed see in production!?");
62+
}
63+
4864
/// It initializes the application with the given configuration.
4965
///
5066
/// The configuration may be obtained from the environment (via config file or env vars).
@@ -69,6 +85,12 @@ pub fn initialize_static() {
6985

7086
// Initialize the Ephemeral Instance Random Seed
7187
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED);
88+
89+
// Initialize the Ephemeral Instance Random Cipher
90+
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_CIPHER_BLOWFISH);
91+
92+
// Initialize the Zeroed Cipher
93+
lazy_static::initialize(&ephemeral_instance_keys::ZEROED_TEST_CIPHER_BLOWFISH);
7294
}
7395

7496
/// It builds the domain tracker

‎src/bootstrap/jobs/udp_tracker.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ use crate::servers::udp::UDP_TRACKER_LOG_TARGET;
3232
#[instrument(skip(config, tracker, form))]
3333
pub async fn start_job(config: &UdpTracker, tracker: Arc<core::Tracker>, form: ServiceRegistrationForm) -> JoinHandle<()> {
3434
let bind_to = config.bind_address;
35+
let cookie_lifetime = config.cookie_lifetime;
3536

3637
let server = Server::new(Spawner::new(bind_to))
37-
.start(tracker, form)
38+
.start(tracker, form, cookie_lifetime)
3839
.await
3940
.expect("it should be able to start the udp tracker");
4041

‎src/lib.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@
488488
//! In addition to the production code documentation you can find a lot of
489489
//! examples on the integration and unit tests.
490490
491-
use torrust_tracker_clock::{clock, time_extent};
491+
use torrust_tracker_clock::clock;
492492

493493
pub mod app;
494494
pub mod bootstrap;
@@ -510,13 +510,3 @@ pub(crate) type CurrentClock = clock::Working;
510510
#[cfg(test)]
511511
#[allow(dead_code)]
512512
pub(crate) type CurrentClock = clock::Stopped;
513-
514-
/// Working version, for production.
515-
#[cfg(not(test))]
516-
#[allow(dead_code)]
517-
pub(crate) type DefaultTimeExtentMaker = time_extent::WorkingTimeExtentMaker;
518-
519-
/// Stopped version, for testing.
520-
#[cfg(test)]
521-
#[allow(dead_code)]
522-
pub(crate) type DefaultTimeExtentMaker = time_extent::StoppedTimeExtentMaker;

‎src/servers/udp/connection_cookie.rs

+218-250
Large diffs are not rendered by default.

‎src/servers/udp/error.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
//! Error types for the UDP server.
22
use std::panic::Location;
33

4+
use aquatic_udp_protocol::ConnectionId;
5+
use derive_more::derive::Display;
46
use thiserror::Error;
57
use torrust_tracker_located_error::LocatedError;
68

9+
#[derive(Display, Debug)]
10+
#[display(":?")]
11+
pub struct ConnectionCookie(pub ConnectionId);
12+
713
/// Error returned by the UDP server.
814
#[derive(Error, Debug)]
915
pub enum Error {
16+
#[error("the issue time should be a normal floating point number")]
17+
InvalidCookieIssueTime { invalid_value: f64 },
18+
19+
#[error("connection id was decoded, but could not be understood")]
20+
InvalidConnectionId { bad_id: ConnectionCookie },
21+
22+
#[error("connection id was decoded, but was expired (too old)")]
23+
ConnectionIdExpired { bad_age: f64, min_age: f64 },
24+
25+
#[error("connection id was decoded, but was invalid (from future)")]
26+
ConnectionIdFromFuture { future_age: f64, max_age: f64 },
27+
1028
/// Error returned when the domain tracker returns an error.
1129
#[error("tracker server error: {source}")]
1230
TrackerError {
@@ -20,10 +38,6 @@ pub enum Error {
2038
message: String,
2139
},
2240

23-
/// Error returned when the connection id could not be verified.
24-
#[error("connection id could not be verified")]
25-
InvalidConnectionId { location: &'static Location<'static> },
26-
2741
/// Error returned when the request is invalid.
2842
#[error("bad request: {source}")]
2943
BadRequest {

‎src/servers/udp/handlers.rs

+225-61
Large diffs are not rendered by default.

‎src/servers/udp/server/launcher.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl Launcher {
3535
pub async fn run_with_graceful_shutdown(
3636
tracker: Arc<Tracker>,
3737
bind_to: SocketAddr,
38+
cookie_lifetime: Duration,
3839
tx_start: oneshot::Sender<Started>,
3940
rx_halt: oneshot::Receiver<Halted>,
4041
) {
@@ -65,7 +66,7 @@ impl Launcher {
6566
let local_addr = local_udp_url.clone();
6667
tokio::task::spawn(async move {
6768
tracing::debug!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_with_graceful_shutdown::task (listening...)");
68-
let () = Self::run_udp_server_main(receiver, tracker.clone()).await;
69+
let () = Self::run_udp_server_main(receiver, tracker.clone(), cookie_lifetime).await;
6970
})
7071
};
7172

@@ -103,14 +104,16 @@ impl Launcher {
103104
}
104105

105106
#[instrument(skip(receiver, tracker))]
106-
async fn run_udp_server_main(mut receiver: Receiver, tracker: Arc<Tracker>) {
107+
async fn run_udp_server_main(mut receiver: Receiver, tracker: Arc<Tracker>, cookie_lifetime: Duration) {
107108
let active_requests = &mut ActiveRequests::default();
108109

109110
let addr = receiver.bound_socket_address();
110111
let local_addr = format!("udp://{addr}");
111112

113+
let cookie_lifetime = cookie_lifetime.as_secs_f64();
114+
112115
loop {
113-
let processor = Processor::new(receiver.socket.clone(), tracker.clone());
116+
let processor = Processor::new(receiver.socket.clone(), tracker.clone(), cookie_lifetime);
114117

115118
if let Some(req) = {
116119
tracing::trace!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_udp_server (wait for request)");

‎src/servers/udp/server/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod tests {
7676
let stopped = Server::new(Spawner::new(bind_to));
7777

7878
let started = stopped
79-
.start(tracker, register.give_form())
79+
.start(tracker, register.give_form(), config.cookie_lifetime)
8080
.await
8181
.expect("it should start the server");
8282

@@ -98,7 +98,7 @@ mod tests {
9898
let stopped = Server::new(Spawner::new(bind_to));
9999

100100
let started = stopped
101-
.start(tracker, register.give_form())
101+
.start(tracker, register.give_form(), config.cookie_lifetime)
102102
.await
103103
.expect("it should start the server");
104104

‎src/servers/udp/server/processor.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,45 @@ use std::net::SocketAddr;
33
use std::sync::Arc;
44

55
use aquatic_udp_protocol::Response;
6+
use torrust_tracker_clock::clock::Time as _;
67
use tracing::{instrument, Level};
78

89
use super::bound_socket::BoundSocket;
910
use crate::core::Tracker;
1011
use crate::servers::udp::{handlers, RawRequest};
12+
use crate::CurrentClock;
1113

1214
pub struct Processor {
1315
socket: Arc<BoundSocket>,
1416
tracker: Arc<Tracker>,
17+
cookie_lifetime: f64,
1518
}
1619

1720
impl Processor {
18-
pub fn new(socket: Arc<BoundSocket>, tracker: Arc<Tracker>) -> Self {
19-
Self { socket, tracker }
21+
pub fn new(socket: Arc<BoundSocket>, tracker: Arc<Tracker>, cookie_lifetime: f64) -> Self {
22+
Self {
23+
socket,
24+
tracker,
25+
cookie_lifetime,
26+
}
2027
}
2128

2229
#[instrument(skip(self, request))]
2330
pub async fn process_request(self, request: RawRequest) {
31+
let cookie_issue_time = CurrentClock::now().as_secs_f64();
32+
let cookie_expiry_time = cookie_issue_time - self.cookie_lifetime - 1.0;
33+
let cookie_tolerance_max_time = cookie_issue_time + 1.0;
34+
2435
let from = request.from;
25-
let response = handlers::handle_packet(request, &self.tracker, self.socket.address()).await;
36+
let response = handlers::handle_packet(
37+
request,
38+
&self.tracker,
39+
self.socket.address(),
40+
cookie_issue_time,
41+
cookie_expiry_time,
42+
cookie_tolerance_max_time,
43+
)
44+
.await;
2645
self.send_response(from, response).await;
2746
}
2847

‎src/servers/udp/server/spawner.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A thin wrapper for tokio spawn to launch the UDP server launcher as a new task.
22
use std::net::SocketAddr;
33
use std::sync::Arc;
4+
use std::time::Duration;
45

56
use derive_more::derive::Display;
67
use derive_more::Constructor;
@@ -27,13 +28,14 @@ impl Spawner {
2728
pub fn spawn_launcher(
2829
&self,
2930
tracker: Arc<Tracker>,
31+
cookie_lifetime: Duration,
3032
tx_start: oneshot::Sender<Started>,
3133
rx_halt: oneshot::Receiver<Halted>,
3234
) -> JoinHandle<Spawner> {
3335
let spawner = Self::new(self.bind_to);
3436

3537
tokio::spawn(async move {
36-
Launcher::run_with_graceful_shutdown(tracker, spawner.bind_to, tx_start, rx_halt).await;
38+
Launcher::run_with_graceful_shutdown(tracker, spawner.bind_to, cookie_lifetime, tx_start, rx_halt).await;
3739
spawner
3840
})
3941
}

‎src/servers/udp/server/states.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt::Debug;
22
use std::net::SocketAddr;
33
use std::sync::Arc;
4+
use std::time::Duration;
45

56
use derive_more::derive::Display;
67
use derive_more::Constructor;
@@ -62,14 +63,19 @@ impl Server<Stopped> {
6263
/// It panics if unable to receive the bound socket address from service.
6364
///
6465
#[instrument(skip(self, tracker, form), err, ret(Display, level = Level::INFO))]
65-
pub async fn start(self, tracker: Arc<Tracker>, form: ServiceRegistrationForm) -> Result<Server<Running>, std::io::Error> {
66+
pub async fn start(
67+
self,
68+
tracker: Arc<Tracker>,
69+
form: ServiceRegistrationForm,
70+
cookie_lifetime: Duration,
71+
) -> Result<Server<Running>, std::io::Error> {
6672
let (tx_start, rx_start) = tokio::sync::oneshot::channel::<Started>();
6773
let (tx_halt, rx_halt) = tokio::sync::oneshot::channel::<Halted>();
6874

6975
assert!(!tx_halt.is_closed(), "Halt channel for UDP tracker should be open");
7076

7177
// May need to wrap in a task to about a tokio bug.
72-
let task = self.state.spawner.spawn_launcher(tracker, tx_start, rx_halt);
78+
let task = self.state.spawner.spawn_launcher(tracker, cookie_lifetime, tx_start, rx_halt);
7379

7480
let local_addr = rx_start.await.expect("it should be able to start the service").address;
7581

‎src/shared/crypto/ephemeral_instance_keys.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22
//!
33
//! They are ephemeral because they are generated at runtime when the
44
//! application starts and are not persisted anywhere.
5+
6+
use blowfish::BlowfishLE;
7+
use cipher::generic_array::GenericArray;
8+
use cipher::{BlockSizeUser, KeyInit};
59
use rand::rngs::ThreadRng;
610
use rand::Rng;
711

812
pub type Seed = [u8; 32];
13+
pub type CipherBlowfish = BlowfishLE;
14+
pub type CipherArrayBlowfish = GenericArray<u8, <CipherBlowfish as BlockSizeUser>::BlockSize>;
915

1016
lazy_static! {
1117
/// The random static seed.
1218
pub static ref RANDOM_SEED: Seed = Rng::gen(&mut ThreadRng::default());
19+
20+
/// The random cipher from the seed.
21+
pub static ref RANDOM_CIPHER_BLOWFISH: CipherBlowfish = CipherBlowfish::new_from_slice(&Rng::gen::<Seed>(&mut ThreadRng::default())).expect("it could not generate key");
22+
23+
/// The constant cipher for testing.
24+
pub static ref ZEROED_TEST_CIPHER_BLOWFISH: CipherBlowfish = CipherBlowfish::new_from_slice(&[0u8; 32]).expect("it could not generate key");
1325
}

‎src/shared/crypto/keys.rs

+118-74
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,154 @@
11
//! This module contains logic related to cryptographic keys.
2-
pub mod seeds {
3-
//! This module contains logic related to cryptographic seeds.
4-
//!
5-
//! Specifically, it contains the logic for storing the seed and providing
6-
//! it to other modules.
7-
//!
8-
//! A **seed** is a pseudo-random number that is used as a secret key for
9-
//! cryptographic operations.
10-
use self::detail::CURRENT_SEED;
11-
use crate::shared::crypto::ephemeral_instance_keys::{Seed, RANDOM_SEED};
12-
13-
/// This trait is for structures that can keep and provide a seed.
14-
pub trait Keeper {
15-
type Seed: Sized + Default + AsMut<[u8]>;
16-
17-
/// It returns a reference to the seed that is keeping.
18-
fn get_seed() -> &'static Self::Seed;
2+
//!
3+
//! Specifically, it contains the logic for storing the seed and providing
4+
//! it to other modules.
5+
//!
6+
//! It also provides the logic for the cipher for encryption and decryption.
7+
8+
use self::detail_cipher::CURRENT_CIPHER;
9+
use self::detail_seed::CURRENT_SEED;
10+
pub use crate::shared::crypto::ephemeral_instance_keys::CipherArrayBlowfish;
11+
use crate::shared::crypto::ephemeral_instance_keys::{CipherBlowfish, Seed, RANDOM_CIPHER_BLOWFISH, RANDOM_SEED};
12+
13+
/// This trait is for structures that can keep and provide a seed.
14+
pub trait Keeper {
15+
type Seed: Sized + Default + AsMut<[u8]>;
16+
type Cipher: cipher::BlockCipher;
17+
18+
/// It returns a reference to the seed that is keeping.
19+
fn get_seed() -> &'static Self::Seed;
20+
fn get_cipher_blowfish() -> &'static Self::Cipher;
21+
}
22+
23+
/// The keeper for the instance. When the application is running
24+
/// in production, this will be the seed keeper that is used.
25+
pub struct Instance;
26+
27+
/// The keeper for the current execution. It's a facade at compilation
28+
/// time that will either be the instance seed keeper (with a randomly
29+
/// generated key for production) or the zeroed seed keeper.
30+
pub struct Current;
31+
32+
impl Keeper for Instance {
33+
type Seed = Seed;
34+
type Cipher = CipherBlowfish;
35+
36+
fn get_seed() -> &'static Self::Seed {
37+
&RANDOM_SEED
1938
}
2039

21-
/// The seed keeper for the instance. When the application is running
22-
/// in production, this will be the seed keeper that is used.
23-
pub struct Instance;
40+
fn get_cipher_blowfish() -> &'static Self::Cipher {
41+
&RANDOM_CIPHER_BLOWFISH
42+
}
43+
}
2444

25-
/// The seed keeper for the current execution. It's a facade at compilation
26-
/// time that will either be the instance seed keeper (with a randomly
27-
/// generated key for production) or the zeroed seed keeper.
28-
pub struct Current;
45+
impl Keeper for Current {
46+
type Seed = Seed;
47+
type Cipher = CipherBlowfish;
2948

30-
impl Keeper for Instance {
31-
type Seed = Seed;
49+
#[allow(clippy::needless_borrow)]
50+
fn get_seed() -> &'static Self::Seed {
51+
&CURRENT_SEED
52+
}
3253

33-
fn get_seed() -> &'static Self::Seed {
34-
&RANDOM_SEED
35-
}
54+
fn get_cipher_blowfish() -> &'static Self::Cipher {
55+
&CURRENT_CIPHER
3656
}
57+
}
58+
59+
#[cfg(test)]
60+
mod tests {
61+
62+
use super::detail_seed::ZEROED_TEST_SEED;
63+
use super::{Current, Instance, Keeper};
64+
use crate::shared::crypto::ephemeral_instance_keys::{CipherBlowfish, Seed, ZEROED_TEST_CIPHER_BLOWFISH};
3765

38-
impl Keeper for Current {
66+
pub struct ZeroedTest;
67+
68+
impl Keeper for ZeroedTest {
3969
type Seed = Seed;
70+
type Cipher = CipherBlowfish;
4071

4172
#[allow(clippy::needless_borrow)]
4273
fn get_seed() -> &'static Self::Seed {
43-
&CURRENT_SEED
74+
&ZEROED_TEST_SEED
75+
}
76+
77+
fn get_cipher_blowfish() -> &'static Self::Cipher {
78+
&ZEROED_TEST_CIPHER_BLOWFISH
4479
}
4580
}
4681

82+
#[test]
83+
fn the_default_seed_and_the_zeroed_seed_should_be_the_same_when_testing() {
84+
assert_eq!(Current::get_seed(), ZeroedTest::get_seed());
85+
}
86+
87+
#[test]
88+
fn the_default_seed_and_the_instance_seed_should_be_different_when_testing() {
89+
assert_ne!(Current::get_seed(), Instance::get_seed());
90+
}
91+
}
92+
93+
mod detail_seed {
94+
use crate::shared::crypto::ephemeral_instance_keys::Seed;
95+
96+
#[allow(dead_code)]
97+
pub const ZEROED_TEST_SEED: Seed = [0u8; 32];
98+
4799
#[cfg(test)]
48-
mod tests {
49-
use super::detail::ZEROED_TEST_SEED;
50-
use super::{Current, Instance, Keeper};
51-
use crate::shared::crypto::ephemeral_instance_keys::Seed;
100+
pub use ZEROED_TEST_SEED as CURRENT_SEED;
52101

53-
pub struct ZeroedTestSeed;
102+
#[cfg(not(test))]
103+
pub use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED as CURRENT_SEED;
54104

55-
impl Keeper for ZeroedTestSeed {
56-
type Seed = Seed;
105+
#[cfg(test)]
106+
mod tests {
107+
use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED;
108+
use crate::shared::crypto::keys::detail_seed::ZEROED_TEST_SEED;
109+
use crate::shared::crypto::keys::CURRENT_SEED;
57110

58-
#[allow(clippy::needless_borrow)]
59-
fn get_seed() -> &'static Self::Seed {
60-
&ZEROED_TEST_SEED
61-
}
111+
#[test]
112+
fn it_should_have_a_zero_test_seed() {
113+
assert_eq!(ZEROED_TEST_SEED, [0u8; 32]);
62114
}
63115

64116
#[test]
65-
fn the_default_seed_and_the_zeroed_seed_should_be_the_same_when_testing() {
66-
assert_eq!(Current::get_seed(), ZeroedTestSeed::get_seed());
117+
fn it_should_default_to_zeroed_seed_when_testing() {
118+
assert_eq!(CURRENT_SEED, ZEROED_TEST_SEED);
67119
}
68120

69121
#[test]
70-
fn the_default_seed_and_the_instance_seed_should_be_different_when_testing() {
71-
assert_ne!(Current::get_seed(), Instance::get_seed());
122+
fn it_should_have_a_large_random_seed() {
123+
assert!(u128::from_ne_bytes((*RANDOM_SEED)[..16].try_into().unwrap()) > u128::from(u64::MAX));
124+
assert!(u128::from_ne_bytes((*RANDOM_SEED)[16..].try_into().unwrap()) > u128::from(u64::MAX));
72125
}
73126
}
127+
}
74128

75-
mod detail {
76-
use crate::shared::crypto::ephemeral_instance_keys::Seed;
77-
78-
#[allow(dead_code)]
79-
pub const ZEROED_TEST_SEED: &Seed = &[0u8; 32];
80-
81-
#[cfg(test)]
82-
pub use ZEROED_TEST_SEED as CURRENT_SEED;
129+
mod detail_cipher {
130+
#[allow(unused_imports)]
131+
#[cfg(not(test))]
132+
pub use crate::shared::crypto::ephemeral_instance_keys::RANDOM_CIPHER_BLOWFISH as CURRENT_CIPHER;
133+
#[cfg(test)]
134+
pub use crate::shared::crypto::ephemeral_instance_keys::ZEROED_TEST_CIPHER_BLOWFISH as CURRENT_CIPHER;
83135

84-
#[cfg(not(test))]
85-
pub use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED as CURRENT_SEED;
136+
#[cfg(test)]
137+
mod tests {
138+
use cipher::BlockEncrypt;
86139

87-
#[cfg(test)]
88-
mod tests {
89-
use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED;
90-
use crate::shared::crypto::keys::seeds::detail::ZEROED_TEST_SEED;
91-
use crate::shared::crypto::keys::seeds::CURRENT_SEED;
140+
use crate::shared::crypto::ephemeral_instance_keys::{CipherArrayBlowfish, ZEROED_TEST_CIPHER_BLOWFISH};
141+
use crate::shared::crypto::keys::detail_cipher::CURRENT_CIPHER;
92142

93-
#[test]
94-
fn it_should_have_a_zero_test_seed() {
95-
assert_eq!(*ZEROED_TEST_SEED, [0u8; 32]);
96-
}
143+
#[test]
144+
fn it_should_default_to_zeroed_seed_when_testing() {
145+
let mut data: cipher::generic_array::GenericArray<u8, _> = CipherArrayBlowfish::from([0u8; 8]);
146+
let mut data_2 = CipherArrayBlowfish::from([0u8; 8]);
97147

98-
#[test]
99-
fn it_should_default_to_zeroed_seed_when_testing() {
100-
assert_eq!(*CURRENT_SEED, *ZEROED_TEST_SEED);
101-
}
148+
CURRENT_CIPHER.encrypt_block(&mut data);
149+
ZEROED_TEST_CIPHER_BLOWFISH.encrypt_block(&mut data_2);
102150

103-
#[test]
104-
fn it_should_have_a_large_random_seed() {
105-
assert!(u128::from_ne_bytes((*RANDOM_SEED)[..16].try_into().unwrap()) > u128::from(u64::MAX));
106-
assert!(u128::from_ne_bytes((*RANDOM_SEED)[16..].try_into().unwrap()) > u128::from(u64::MAX));
107-
}
151+
assert_eq!(data, data_2);
108152
}
109153
}
110154
}

‎tests/servers/udp/environment.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ impl Environment<Stopped> {
5555

5656
#[allow(dead_code)]
5757
pub async fn start(self) -> Environment<Running> {
58+
let cookie_lifetime = self.config.cookie_lifetime;
5859
Environment {
5960
config: self.config,
6061
tracker: self.tracker.clone(),
6162
registar: self.registar.clone(),
62-
server: self.server.start(self.tracker, self.registar.give_form()).await.unwrap(),
63+
server: self
64+
.server
65+
.start(self.tracker, self.registar.give_form(), cookie_lifetime)
66+
.await
67+
.unwrap(),
6368
}
6469
}
6570
}

0 commit comments

Comments
 (0)
Please sign in to comment.