Skip to content

Commit 4ba9f5e

Browse files
committed
Add test + refactor
1 parent 398a26d commit 4ba9f5e

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

neptun/src/noise/timers.rs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ pub enum TimerName {
6767
}
6868

6969
impl TimerName {
70-
pub const VALUES: [Self; TimerName::Top as usize] = [
71-
Self::TimeCurrent,
70+
pub const VALUES: [Self; TimerName::Top as usize - 1] = [
7271
Self::TimeSessionEstablished,
7372
Self::TimeLastHandshakeStarted,
7473
Self::TimeLastPacketReceived,
@@ -215,6 +214,14 @@ impl Tunn {
215214
}
216215
}
217216

217+
fn tick_marked_timers(&mut self, timer_mask: u16) {
218+
for timer_name in TimerName::VALUES {
219+
if (timer_mask & (1 << (timer_name as u16))) != 0 {
220+
self.timer_tick(timer_name);
221+
}
222+
}
223+
}
224+
218225
pub fn update_timers<'a>(&mut self, dst: &'a mut [u8]) -> TunnResult<'a> {
219226
let mut handshake_initiation_required = false;
220227
let mut keepalive_required = false;
@@ -234,16 +241,8 @@ impl Tunn {
234241
let timer_mask = self
235242
.timers
236243
.timers_to_update_mask
237-
.load(std::sync::atomic::Ordering::Relaxed);
238-
for timer_name in TimerName::VALUES {
239-
if (timer_mask & (1 << (timer_name as u16))) != 0 {
240-
self.timer_tick(timer_name);
241-
}
242-
}
243-
// Reset all marked bits
244-
self.timers
245-
.timers_to_update_mask
246-
.store(0, std::sync::atomic::Ordering::Relaxed);
244+
.swap(0, std::sync::atomic::Ordering::Relaxed);
245+
self.tick_marked_timers(timer_mask);
247246

248247
self.update_session_timers(now);
249248

@@ -429,7 +428,7 @@ mod tests {
429428
use super::TimerName;
430429

431430
#[test]
432-
fn create_two_tuns() {
431+
fn test_update_marked_timers() {
433432
let my_secret_key = x25519_dalek::StaticSecret::random_from_rng(OsRng);
434433
let my_idx = OsRng.next_u32();
435434

@@ -469,4 +468,41 @@ mod tests {
469468
assert!(my_tun.timers[TimerName::TimeLastDataPacketReceived].is_zero());
470469
assert!(my_tun.timers[TimerName::TimePersistentKeepalive].is_zero());
471470
}
471+
472+
#[test]
473+
fn test_mark_timers_during_update() {
474+
let my_secret_key = x25519_dalek::StaticSecret::random_from_rng(OsRng);
475+
let my_idx = OsRng.next_u32();
476+
477+
let their_secret_key = x25519_dalek::StaticSecret::random_from_rng(OsRng);
478+
let their_public_key = x25519_dalek::PublicKey::from(&their_secret_key);
479+
480+
let mut my_tun =
481+
Tunn::new(my_secret_key, their_public_key, None, None, my_idx, None).unwrap();
482+
483+
// Mark timers to update
484+
my_tun.mark_timer_to_update(super::TimerName::TimeLastDataPacketSent);
485+
486+
let timer_mask = my_tun
487+
.timers
488+
.timers_to_update_mask
489+
.swap(0, std::sync::atomic::Ordering::Relaxed);
490+
491+
my_tun.mark_timer_to_update(super::TimerName::TimeLastDataPacketReceived);
492+
493+
my_tun.tick_marked_timers(timer_mask);
494+
495+
// Only those timers marked should be udpated
496+
assert!(!my_tun.timers[TimerName::TimeLastDataPacketSent].is_zero());
497+
assert!(my_tun.timers[TimerName::TimeLastDataPacketReceived].is_zero());
498+
499+
// Reset the timers
500+
my_tun.timers[TimerName::TimeLastDataPacketSent] = SafeDuration::from_millis(0);
501+
502+
my_tun.update_timers(&mut [0]);
503+
504+
// Now the timers should not update
505+
assert!(my_tun.timers[TimerName::TimeLastDataPacketSent].is_zero());
506+
assert!(!my_tun.timers[TimerName::TimeLastDataPacketReceived].is_zero());
507+
}
472508
}

0 commit comments

Comments
 (0)