Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions src/protocol/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod tests {
use std::{time::Instant, net::{SocketAddr, IpAddr, Ipv4Addr}};

#[test]
fn connection_id_is_generated_based_on_remote_client_port_an_hours_passed_since_unix_epoch() {
fn connection_id_is_generated_based_on_remote_client_port_and_hours_passed_since_unix_epoch() {
let client_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0001);

let timestamp = 946684800u64; // GTM: Sat Jan 01 2000 00:00:00 GMT+0000
Expand All @@ -57,45 +57,45 @@ mod tests {
assert_eq!((0x_0000_0000_0004_0338u64 | 0x_0000_0010_0000_0000u64), 0x_0000_0010_0004_0338u64); // 68719739704
assert_eq!(0x_0000_0010_0004_0338u64 as i64, 68719739704); // 68719739704

let connection_id = super::get_connection_id(&client_addr, timestamp);
let connection_id = get_connection_id(&client_addr, timestamp);

assert_eq!(connection_id, ConnectionId(68719739704));
}

#[test]
fn connection_id_in_udp_tracker_should_be_the_same_for_one_client_during_one_hour() {
fn connection_id_in_udp_tracker_should_be_the_same_for_one_client_during_two_minutes() {
let client_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

let now = 946684800u64;

let connection_id = get_connection_id(&client_addr, now);

let in_one_hour = now + 3600 - 1;
let in_one_hour = now + 120 - 1;

let connection_id_after_one_hour = get_connection_id(&client_addr, in_one_hour);

assert_eq!(connection_id, connection_id_after_one_hour);
}

#[test]
fn connection_id_in_udp_tracker_should_change_for_the_same_client_and_port_after_one_hour() {
fn connection_id_in_udp_tracker_should_change_for_the_same_client_ip_and_port_after_two_minutes() {
let client_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

let now = 946684800u64;

let connection_id = get_connection_id(&client_addr, now);

let after_one_hour = now + 3600;
let after_two_minutes = now + 120;

let connection_id_after_one_hour = get_connection_id(&client_addr, after_one_hour);
let connection_id_after_two_minutes = get_connection_id(&client_addr, after_two_minutes);

assert_ne!(connection_id, connection_id_after_one_hour);
}
assert_ne!(connection_id, connection_id_after_two_minutes);
}

#[test]
fn connection_id_in_udp_tracker_should_be_different_for_each_client_at_the_same_time_if_they_use_a_different_port() {
let client_1_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0001);
let client_2_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0002);
fn connection_id_in_udp_tracker_should_be_different_for_each_client_at_the_same_time_if_they_use_a_different_ip() {
let client_1_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)), 0001);
let client_2_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0001);

let now = 946684800u64;

Expand All @@ -106,18 +106,16 @@ mod tests {
}

#[test]
fn connection_id_in_udp_tracker_should_expire_after_one_hour() {
let client_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
fn connection_id_in_udp_tracker_should_be_different_for_each_client_at_the_same_time_if_they_use_a_different_port() {
let client_1_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0001);
let client_2_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0002);

let now = 946684800u64;

let connection_id_1 = get_connection_id(&client_addr, now);

let in_one_hour = now + 3600;

let connection_id_2 = get_connection_id(&client_addr, in_one_hour);
let connection_id_for_client_1 = get_connection_id(&client_1_addr, now);
let connection_id_for_client_2 = get_connection_id(&client_2_addr, now);

assert_ne!(connection_id_1, connection_id_2);
assert_ne!(connection_id_for_client_1, connection_id_for_client_2);
}

use aquatic_udp_protocol::ConnectionId;
Expand All @@ -139,7 +137,7 @@ mod tests {

use std::{thread, time};

let t1 = time::Instant::now();
let t1 = Instant::now();

let s = S { time: t1 };

Expand All @@ -152,4 +150,4 @@ mod tests {
// Json contains time duration since t1 instant in milliseconds
assert_eq!(json_serialized_value, r#"{"time":10}"#);
}
}
}