forked from googleforgames/quilkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqcmp.rs
95 lines (86 loc) · 3.11 KB
/
qcmp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use tokio::time::Duration;
use quilkin::{
codec::qcmp::Protocol,
test::{AddressType, TestHelper},
};
#[tokio::test]
#[cfg_attr(target_os = "macos", ignore)]
async fn proxy_ping() {
let mut t = TestHelper::default();
let qcmp = quilkin::net::raw_socket_with_reuse(0).unwrap();
let qcmp_port = quilkin::net::socket_port(&qcmp);
let server_proxy = quilkin::components::proxy::Proxy {
qcmp,
to: vec![(Ipv4Addr::UNSPECIFIED, 0).into()],
..<_>::default()
};
let server_config = std::sync::Arc::new(quilkin::Config::default_non_agent());
t.run_server(server_config, Some(server_proxy), None).await;
ping(qcmp_port).await;
}
#[tokio::test]
#[cfg_attr(target_os = "macos", ignore)]
async fn agent_ping() {
let qcmp_port = quilkin::test::available_addr(AddressType::Random)
.await
.port();
let agent = quilkin::cli::Agent {
qcmp_port,
..<_>::default()
};
let server_config = std::sync::Arc::new(quilkin::Config::default_agent());
let (_tx, rx) = quilkin::make_shutdown_channel(quilkin::ShutdownKind::Testing);
tokio::spawn(async move {
agent
.run(server_config, Default::default(), rx)
.await
.expect("Agent should run")
});
ping(qcmp_port).await;
}
async fn ping(port: u16) {
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
let socket = tokio::net::UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))
.await
.unwrap();
let local_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);
let ping = Protocol::ping();
let mut ping_packet = quilkin::codec::qcmp::QcmpPacket::default();
socket
.send_to(ping.encode(&mut ping_packet), &local_addr)
.await
.unwrap();
let mut buf = [0; quilkin::codec::qcmp::MAX_QCMP_PACKET_LEN];
let (size, _) = tokio::time::timeout(Duration::from_secs(1), socket.recv_from(&mut buf))
.await
.unwrap()
.unwrap();
let recv_time = quilkin::time::UtcTimestamp::now();
let reply = Protocol::parse(&buf[..size]).unwrap().unwrap();
assert_eq!(ping.nonce(), reply.nonce());
const MAX: std::time::Duration = std::time::Duration::from_millis(50);
// If it takes longer than 50 milliseconds locally, it's likely that there
// is bug.
let delay = reply.round_trip_delay(recv_time).unwrap();
assert!(
MAX > delay.duration(),
"Delay {:?} greater than {MAX:?}",
delay.duration(),
);
}