use super::*;
#[test]
fn default_matches_adb_desktop_behaviour() {
let c = ConnectionConfig::new();
assert_eq!(c.max_payload(), MAX_PAYLOAD);
assert_eq!(c.initial_ack_bytes(), INITIAL_DELAYED_ACK_BYTES);
assert_eq!(c.max_rx_per_channel(), usize::MAX);
assert_eq!(c.max_channels(), 32);
assert_eq!(c.max_banner_properties(), 64);
assert_eq!(ConnectionConfig::default(), c);
}
#[test]
fn embedded_preset_is_bounded() {
let c = ConnectionConfig::embedded();
assert_eq!(c.max_payload(), 8 * 1024);
assert_eq!(c.initial_ack_bytes(), 32 * 1024);
assert_eq!(c.max_rx_per_channel(), 64 * 1024);
assert_eq!(c.max_channels(), 32);
assert_eq!(c.max_banner_properties(), 64);
}
#[test]
fn max_channels_is_clamped_to_one() {
assert_eq!(
ConnectionConfig::new().with_max_channels(0).max_channels(),
1
);
assert_eq!(
ConnectionConfig::new().with_max_channels(1).max_channels(),
1
);
assert_eq!(
ConnectionConfig::new().with_max_channels(5).max_channels(),
5
);
}
#[test]
fn max_channels_is_clamped_to_the_cap() {
let cap = ConnectionConfig::MAX_CHANNELS;
let clamped = |n| ConnectionConfig::new().with_max_channels(n).max_channels();
assert_eq!(clamped(usize::MAX), cap);
assert_eq!(clamped(cap + 1), cap);
assert_eq!(clamped(cap), cap);
}
#[test]
fn max_channels_clamp_works_in_const_context() {
const FLOOR: ConnectionConfig = ConnectionConfig::new().with_max_channels(0);
const CAP: ConnectionConfig = ConnectionConfig::new().with_max_channels(usize::MAX);
assert_eq!(FLOOR.max_channels(), 1);
assert_eq!(CAP.max_channels(), ConnectionConfig::MAX_CHANNELS);
}
#[test]
fn initial_ack_bytes_is_clamped_to_one() {
let clamped = |n| {
ConnectionConfig::new()
.with_initial_ack_bytes(n)
.initial_ack_bytes()
};
assert_eq!(clamped(0), 1);
assert_eq!(clamped(1), 1);
assert_eq!(clamped(4096), 4096);
}
#[test]
fn initial_ack_bytes_is_clamped_to_i32_max() {
let clamped = |n| {
ConnectionConfig::new()
.with_initial_ack_bytes(n)
.initial_ack_bytes()
};
assert_eq!(clamped(u32::MAX), MOST);
assert_eq!(clamped(MOST + 1), MOST);
assert_eq!(clamped(MOST), MOST);
}
#[test]
fn max_banner_properties_is_kept_verbatim() {
let c = ConnectionConfig::new().with_max_banner_properties(0);
assert_eq!(c.max_banner_properties(), 0);
let c = ConnectionConfig::new().with_max_banner_properties(3);
assert_eq!(c.max_banner_properties(), 3);
}
#[test]
fn advertised_credit_never_exceeds_the_enforced_cap() {
let c = ConnectionConfig::new()
.with_max_payload(8 * 1024)
.with_initial_ack_bytes(32 * 1024 * 1024)
.with_max_rx_per_channel(1024 * 1024);
assert_eq!(c.advertised_ack_bytes(), 1024 * 1024 - 8 * 1024 + 1);
let unbounded = ConnectionConfig::new().with_initial_ack_bytes(32 * 1024 * 1024);
assert_eq!(unbounded.advertised_ack_bytes(), 32 * 1024 * 1024);
}
const KIB: u32 = 1024;
const MIB: u32 = 1024 * KIB;
const MOST: u32 = i32::MAX as u32;
const TOP: usize = u32::MAX as usize - 1023;
const LIMITS: [(u32, u32, usize, usize, u32); 13] = [
(KIB, 32 * MIB, 1, 1, 1),
(KIB, 32 * MIB, 1024, 1, 1),
(MIB, 32 * MIB, 1024 * 1024, 1, 1),
(KIB, 32 * MIB, 1025, 2, 2),
(KIB, 32 * MIB, 2048, 1025, 1025),
(KIB, 1024, 2048, 1024, 1024),
(KIB, 1025, 2048, 1025, 1025),
(KIB, 1026, 2048, 1025, 1025),
(4 * KIB, KIB, 64 * 1024, 4096, 1024),
(MIB, 32 * MIB, usize::MAX, 32 << 20, 32 * MIB),
(KIB, TOP as u32, usize::MAX, MOST as usize, MOST),
(KIB, u32::MAX, u32::MAX as usize, MOST as usize, MOST),
(
KIB,
u32::MAX,
MOST as usize + 1022,
MOST as usize - 1,
MOST - 1,
),
];
fn config(max_payload: u32, credit: u32, cap: usize) -> ConnectionConfig {
ConnectionConfig::new()
.with_max_payload(max_payload)
.with_initial_ack_bytes(credit)
.with_max_rx_per_channel(cap)
}
#[test]
fn both_limits_stop_one_packet_short_of_the_cap() {
for (max_payload, credit, cap, watermark, advertised) in LIMITS {
let c = config(max_payload, credit, cap);
let row = (max_payload, credit, cap);
assert_eq!(c.rx_watermark(), watermark, "{row:?}");
assert_eq!(c.advertised_ack_bytes(), advertised, "{row:?}");
}
}
#[test]
fn neither_limit_lets_a_full_packet_past_the_cap() {
for (max_payload, credit, cap, _, _) in LIMITS {
let c = config(max_payload, credit, cap);
let row = (max_payload, credit, cap);
let room = c.rx_cap() - max_payload as usize;
assert!(c.rx_watermark() >= 1, "{row:?}");
assert!(c.advertised_ack_bytes() >= 1, "{row:?}");
assert!(c.rx_watermark() - 1 <= room, "{row:?}");
assert!(c.advertised_ack_bytes() as usize - 1 <= room, "{row:?}");
}
}
#[test]
fn the_presets_keep_their_limits() {
let desktop = ConnectionConfig::new();
assert_eq!(desktop.rx_watermark(), 32 * 1024 * 1024);
assert_eq!(desktop.advertised_ack_bytes(), 32 * 1024 * 1024);
let embedded = ConnectionConfig::embedded();
assert_eq!(embedded.rx_watermark(), 32 * 1024);
assert_eq!(embedded.advertised_ack_bytes(), 32 * 1024);
}
#[test]
fn the_limits_work_in_const_context_with_no_cap_at_all() {
const WIDEST: ConnectionConfig = ConnectionConfig::new()
.with_max_payload(KIB)
.with_initial_ack_bytes(u32::MAX)
.with_max_rx_per_channel(usize::MAX);
const WATERMARK: usize = WIDEST.rx_watermark();
const CREDIT: u32 = WIDEST.advertised_ack_bytes();
assert_eq!(WATERMARK, MOST as usize);
assert_eq!(CREDIT, MOST);
}
#[test]
fn max_payload_is_clamped_to_the_protocol_maximum() {
let c = ConnectionConfig::new().with_max_payload(MAX_PAYLOAD * 4);
assert_eq!(c.max_payload(), MAX_PAYLOAD);
}
#[test]
fn max_payload_is_clamped_to_the_minimum() {
let c = ConnectionConfig::new().with_max_payload(0);
assert_eq!(c.max_payload(), MIN_MAX_PAYLOAD);
}
#[test]
fn max_payload_in_range_is_kept_verbatim() {
let c = ConnectionConfig::new().with_max_payload(8 * 1024);
assert_eq!(c.max_payload(), 8 * 1024);
}
#[test]
fn rx_cap_never_falls_below_one_payload() {
let c = ConnectionConfig::new()
.with_max_payload(8 * 1024)
.with_max_rx_per_channel(1024);
assert_eq!(c.rx_cap(), 8 * 1024);
}
#[test]
fn rx_cap_uses_the_configured_value_when_larger() {
let c = ConnectionConfig::new()
.with_max_payload(8 * 1024)
.with_max_rx_per_channel(64 * 1024);
assert_eq!(c.rx_cap(), 64 * 1024);
}
#[test]
fn builder_is_usable_in_const_context() {
const C: ConnectionConfig = ConnectionConfig::embedded()
.with_max_payload(4 * 1024)
.with_max_channels(4)
.with_max_banner_properties(8);
assert_eq!(C.max_payload(), 4 * 1024);
assert_eq!(C.initial_ack_bytes(), 32 * 1024);
assert_eq!(C.max_channels(), 4);
assert_eq!(C.max_banner_properties(), 8);
}