Skip to content

Commit f9f97d6

Browse files
committed
test fixes
1 parent 1d7f11b commit f9f97d6

File tree

7 files changed

+218
-188
lines changed

7 files changed

+218
-188
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/test-utils/src/helpers.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::traits::Timeboxed;
5-
use rand_chacha::ChaCha20Rng;
5+
use nym_bin_common::logging::tracing_subscriber::EnvFilter;
6+
use nym_bin_common::logging::tracing_subscriber::layer::SubscriberExt;
7+
use nym_bin_common::logging::tracing_subscriber::util::SubscriberInitExt;
8+
use nym_bin_common::logging::{default_tracing_fmt_layer, tracing_subscriber};
69
use rand_chacha::rand_core::SeedableRng;
710
use std::future::Future;
811
use tokio::task::JoinHandle;
912
use tokio::time::error::Elapsed;
1013

11-
use nym_bin_common::logging::tracing_subscriber::EnvFilter;
12-
use nym_bin_common::logging::tracing_subscriber::layer::SubscriberExt;
13-
use nym_bin_common::logging::tracing_subscriber::util::SubscriberInitExt;
14-
use nym_bin_common::logging::{default_tracing_fmt_layer, tracing_subscriber};
14+
pub use rand_chacha::ChaCha20Rng as DeterministicRng;
1515
pub use rand_chacha::rand_core::{CryptoRng, RngCore};
1616

1717
pub fn leak<T>(val: T) -> &'static mut T {
@@ -26,16 +26,16 @@ where
2626
tokio::spawn(async move { fut.timeboxed().await })
2727
}
2828

29-
pub fn deterministic_rng() -> ChaCha20Rng {
29+
pub fn deterministic_rng() -> DeterministicRng {
3030
seeded_rng([42u8; 32])
3131
}
3232

33-
pub fn seeded_rng(seed: [u8; 32]) -> ChaCha20Rng {
34-
ChaCha20Rng::from_seed(seed)
33+
pub fn seeded_rng(seed: [u8; 32]) -> DeterministicRng {
34+
DeterministicRng::from_seed(seed)
3535
}
3636

37-
pub fn u64_seeded_rng(seed: u64) -> ChaCha20Rng {
38-
ChaCha20Rng::seed_from_u64(seed)
37+
pub fn u64_seeded_rng(seed: u64) -> DeterministicRng {
38+
DeterministicRng::seed_from_u64(seed)
3939
}
4040

4141
// test logger to use during debugging

common/wireguard/src/ip_pool/mod.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ impl IpPool {
240240
.count()
241241
}
242242

243+
/// Get the number of allocated IPs in the pool
244+
pub fn pre_allocated_count(&self) -> usize {
245+
self.allocations
246+
.iter()
247+
.filter(|(_, state)| matches!(state, AllocationState::PreAllocated { .. }))
248+
.count()
249+
}
250+
243251
/// Get the number of allocated IPs in the pool
244252
pub fn allocated_count(&self) -> usize {
245253
self.allocations
@@ -261,7 +269,6 @@ impl IpPool {
261269
let mut freed = 0;
262270

263271
for state in self.allocations.values_mut() {
264-
println!("entry: {state:?}");
265272
if let AllocationState::PreAllocated { allocated_at, .. } = state {
266273
let age = now.duration_since(*allocated_at);
267274
if age > max_age {
@@ -373,19 +380,25 @@ mod tests {
373380
};
374381

375382
assert_eq!(pool.free_count(), 3);
376-
assert_eq!(pool.allocated_count(), 0);
383+
assert_eq!(pool.pre_allocated_count(), 0);
377384

378385
let allocation1 = pool.pre_allocate()?;
379386
assert_eq!(pool.free_count(), 2);
380-
assert_eq!(pool.allocated_count(), 1);
387+
assert_eq!(pool.pre_allocated_count(), 1);
381388

382389
let allocation2 = pool.pre_allocate()?;
383390
assert_eq!(pool.free_count(), 1);
384-
assert_eq!(pool.allocated_count(), 2);
391+
assert_eq!(pool.pre_allocated_count(), 2);
392+
393+
pool.confirm_allocation(allocation1)?;
394+
assert_eq!(pool.free_count(), 1);
395+
assert_eq!(pool.pre_allocated_count(), 1);
396+
assert_eq!(pool.allocated_count(), 1);
385397

386398
let allocation3 = pool.pre_allocate()?;
387399
assert_eq!(pool.free_count(), 0);
388-
assert_eq!(pool.allocated_count(), 3);
400+
assert_eq!(pool.pre_allocated_count(), 2);
401+
assert_eq!(pool.allocated_count(), 1);
389402

390403
// make sure each was unique and different from the gateway
391404
ensure_different_allocation(allocation1, allocation2)?;
@@ -402,13 +415,15 @@ mod tests {
402415
// if pair gets released, it's eligible for allocation again
403416
pool.release(allocation2);
404417
assert_eq!(pool.free_count(), 1);
405-
assert_eq!(pool.allocated_count(), 2);
418+
assert_eq!(pool.pre_allocated_count(), 1);
419+
assert_eq!(pool.allocated_count(), 1);
406420

407421
let reallocation = pool.pre_allocate()?;
408422
assert_eq!(reallocation, allocation2);
409423

410424
assert_eq!(pool.free_count(), 0);
411-
assert_eq!(pool.allocated_count(), 3);
425+
assert_eq!(pool.pre_allocated_count(), 2);
426+
assert_eq!(pool.allocated_count(), 1);
412427

413428
Ok(())
414429
}

common/wireguard/src/peer_controller/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ impl PeerController {
269269
"could not determine ip pair allocated to the peer".to_string(),
270270
));
271271
};
272-
self.ip_pool.confirm_allocation(ip_pair)?;
273272

274273
// Try to configure WireGuard peer
275274
if let Err(e) = self.wg_api.configure_peer(peer) {
@@ -302,6 +301,9 @@ impl PeerController {
302301
*self.host_information.write().await = host_information;
303302
}
304303
let public_key = peer.public_key.clone();
304+
305+
self.ip_pool.confirm_allocation(ip_pair)?;
306+
305307
tokio::spawn(async move {
306308
handle.run().await;
307309
debug!("Peer handle shut down for {public_key}");

gateway/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ bytes = { workspace = true }
8989
defguard_wireguard_rs = { workspace = true }
9090

9191
[dev-dependencies]
92+
anyhow = { workspace = true }
93+
nym-test-utils = { workspace = true }
9294
nym-gateway-storage = { workspace = true, features = ["mock"] }
9395
nym-wireguard = { workspace = true, features = ["mock"] }
94-
mock_instant = "0.6.0"
96+
mock_instant = { workspace = true }
9597
time = { workspace = true }
9698

9799
[lints]

gateway/src/node/internal_service_providers/authenticator/seen_credential_cache.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,30 +156,9 @@ mod test {
156156
cache.remove_stale();
157157
assert!(cache.get_peer_pub_key(&credential).is_some());
158158

159-
MockClock::advance_system_time(SEEN_CREDENTIAL_CACHE_TIME * 2);
159+
MockClock::advance(SEEN_CREDENTIAL_CACHE_TIME * 2);
160160

161161
cache.remove_stale();
162162
assert!(cache.get_peer_pub_key(&credential).is_none());
163163
}
164-
165-
#[test]
166-
fn invalid_time() {
167-
assert!(MockClock::is_thread_local());
168-
assert!(Instant::now().is_thread_local());
169-
170-
let mut cache = SeenCredentialCache::new();
171-
let credential = CredentialSpendingData::try_from_bytes(&CREDENTIAL_BYTES).unwrap();
172-
let peer_pub_key = PeerPublicKey::from_str(PUB_KEY).unwrap();
173-
174-
// set some value for time
175-
MockClock::set_system_time(Duration::from_secs(10));
176-
cache.insert_credential(credential.clone(), peer_pub_key);
177-
178-
// then set the time in the past
179-
MockClock::set_system_time(Duration::ZERO);
180-
cache.remove_stale();
181-
182-
// invalid time should remove the credential, just in case
183-
assert!(cache.get_peer_pub_key(&credential).is_none());
184-
}
185164
}

0 commit comments

Comments
 (0)