use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use gunnar_grpc::{ServerConfig, AUTHENTICATION_ENABLED, DEFAULT_BIND};
#[test]
fn default_bind_is_loopback() {
let config = ServerConfig::default();
assert!(
config.is_loopback_bind(),
"gunnar-grpc's default bind is {}, which is NOT loopback. gRPC v1 is \
unauthenticated: every control-plane mutation — principals, keys, grants, branch \
protection, repo create/delete — would be served to anyone who can reach it. Bind \
127.0.0.1 until gRPC authentication ships.",
config.bind
);
assert_eq!(
config.bind,
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 50051),
"the documented default is 127.0.0.1:50051"
);
assert_eq!(config.bind, DEFAULT_BIND);
}
#[test]
fn the_default_is_not_a_wildcard_bind() {
assert!(
!DEFAULT_BIND.ip().is_unspecified(),
"DEFAULT_BIND is a wildcard address ({}), exposing the unauthenticated control plane \
to the network",
DEFAULT_BIND.ip()
);
}
#[test]
fn the_loopback_predicate_can_say_no() {
let wildcard = ServerConfig::default().with_bind("0.0.0.0:50051".parse().unwrap());
assert!(
!wildcard.is_loopback_bind(),
"0.0.0.0 must not be reported as loopback"
);
let routable = ServerConfig::default().with_bind("10.0.0.5:50051".parse().unwrap());
assert!(
!routable.is_loopback_bind(),
"a LAN address must not be reported as loopback"
);
let v6_wildcard = ServerConfig::default().with_bind("[::]:50051".parse().unwrap());
assert!(!v6_wildcard.is_loopback_bind());
let v6_loopback = ServerConfig::default().with_bind("[::1]:50051".parse().unwrap());
assert!(v6_loopback.is_loopback_bind());
}
#[test]
#[allow(clippy::assertions_on_constants)]
fn this_build_authenticates_grpc() {
assert!(
AUTHENTICATION_ENABLED,
"AUTHENTICATION_ENABLED is false, but gunnar-grpc has an auth gate. Either it was \
deleted (then delete tests/auth.rs too, and say so in the README) or the constant \
is lying to `Admin.Health`."
);
}
#[test]
fn the_default_config_requires_a_credential() {
assert!(
ServerConfig::default().authentication_enabled(),
"a freshly constructed ServerConfig serves allow-all. Deny by default is the whole \
point of closing the allow-all hole."
);
assert!(
!ServerConfig::default()
.insecure_allow_all_for_local_development()
.authentication_enabled(),
"the development escape hatch did not turn authentication off, so the assertion \
above proves nothing"
);
}
#[test]
fn event_buffer_default_is_bounded_and_nonzero() {
let config = ServerConfig::default();
assert!(
config.event_buffer > 0,
"a zero-capacity ring cannot deliver"
);
assert!(
config.event_buffer <= 65_536,
"the event buffer is a HARD bound on retained events, not a growth budget"
);
assert_eq!(ServerConfig::default().with_event_buffer(0).event_buffer, 1);
}