use super::*;
#[cfg(unix)]
#[test]
fn sock_decoding_matches_chronys_layout() {
let mut buf = [0u8; sock::SAMPLE_SIZE];
let sec: i64 = 1_756_224_000;
let usec: i64 = 250_000;
let offset: f64 = -0.001_5;
buf[0..8].copy_from_slice(&sec.to_ne_bytes());
buf[8..16].copy_from_slice(&usec.to_ne_bytes());
buf[16..24].copy_from_slice(&offset.to_ne_bytes());
buf[24..28].copy_from_slice(&1i32.to_ne_bytes()); buf[28..32].copy_from_slice(&0i32.to_ne_bytes()); buf[36..40].copy_from_slice(&sock::SOCK_MAGIC.to_ne_bytes());
let s = sock::decode_sample(&buf).expect("should decode");
assert!((s.local_s - 1_756_224_000.25).abs() < 1e-9);
assert_eq!(
s.offset_s(),
offset,
"the offset must survive decoding exactly, not approximately — rounding it away is what caps a refclock at ~100 ns"
);
assert_eq!(s.leap, LeapWarning::None);
}
#[cfg(unix)]
#[test]
fn a_datagram_without_the_magic_is_not_a_time_sample() {
let mut buf = [0u8; sock::SAMPLE_SIZE];
buf[0..8].copy_from_slice(&1_756_224_000i64.to_ne_bytes());
buf[36..40].copy_from_slice(&0xDEAD_BEEFu32.to_ne_bytes());
assert!(sock::decode_sample(&buf).is_none());
assert!(sock::decode_sample(&buf[..20]).is_none());
assert!(sock::decode_sample(&[]).is_none());
}
#[cfg(unix)]
#[test]
fn sock_leap_field_is_honoured() {
let mut buf = [0u8; sock::SAMPLE_SIZE];
buf[0..8].copy_from_slice(&1_756_224_000i64.to_ne_bytes());
buf[36..40].copy_from_slice(&sock::SOCK_MAGIC.to_ne_bytes());
buf[28..32].copy_from_slice(&3i32.to_ne_bytes()); let s = sock::decode_sample(&buf).expect("decode");
assert_eq!(s.leap, LeapWarning::NotSynchronized);
assert!(s.validate(None).is_err());
}
#[cfg(unix)]
#[test]
fn a_sock_endpoint_binds_and_receives_a_real_datagram() {
use std::os::unix::net::UnixDatagram;
let path = std::env::temp_dir().join(format!("rusty_time_sock_test_{}", std::process::id()));
let path = path.to_string_lossy().into_owned();
let listener = sock::SockRefclock::bind(&path).expect("bind");
assert!(listener.try_sample().is_none(), "nothing sent yet");
let producer = UnixDatagram::unbound().expect("producer");
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock");
let mut buf = [0u8; sock::SAMPLE_SIZE];
buf[0..8].copy_from_slice(&(now.as_secs() as i64).to_ne_bytes());
buf[8..16].copy_from_slice(&(now.subsec_micros() as i64).to_ne_bytes());
buf[16..24].copy_from_slice(&0.000_25f64.to_ne_bytes());
buf[36..40].copy_from_slice(&sock::SOCK_MAGIC.to_ne_bytes());
producer.send_to(&buf, &path).expect("send");
let mut got = None;
for _ in 0..50 {
if let Some(s) = listener.try_sample() {
got = Some(s);
break;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
let sample = got.expect("a sent sample should arrive");
assert_eq!(sample.offset_s(), 0.000_25, "offset must be exact");
assert!(
sample.validate(None).is_ok(),
"a fresh sample must validate"
);
let _ = std::fs::remove_file(&path);
}
#[cfg(target_os = "linux")]
#[test]
fn kernel_receive_timestamps_are_produced_when_the_platform_supplies_them() {
use std::net::UdpSocket;
let server = UdpSocket::bind("127.0.0.1:0").expect("bind");
let addr = server.local_addr().expect("addr");
if crate::net::enable_rx_timestamps(&server).is_err() {
return;
}
let client = UdpSocket::bind("127.0.0.1:0").expect("client");
let before = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock")
.as_secs_f64();
client.send_to(&[0u8; 48], addr).expect("send");
let mut bufs = [[0u8; 1024]; 4];
let mut out = Vec::new();
let mut received = None;
for _ in 0..50 {
if crate::net::wait_readable(&server, std::time::Duration::from_millis(100))
.unwrap_or(false)
&& crate::net::recv_batch(
&server,
&mut bufs,
&mut crate::net::BatchScratch::new(),
&mut out,
)
.unwrap_or(0)
> 0
{
received = out.first().copied();
break;
}
}
let received = received.expect("datagram should arrive");
let after = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock")
.as_secs_f64();
match received.kernel_rx_ns {
Some(stamp) => {
let stamp_s = stamp as f64 * 1e-9;
assert!(
stamp_s >= before - 1.0 && stamp_s <= after + 1.0,
"kernel timestamp {stamp_s} is outside the window [{before}, {after}]"
);
}
None => {
eprintln!("note: SO_TIMESTAMPING enabled but no timestamp was supplied");
}
}
}