refactor(dynamic): centralize stub initialization for test cases, enhance error handling for permission issues, and expand test coverage

This commit is contained in:
elipeter 2026-05-25 09:04:04 -05:00
parent 9c323d0ed5
commit 5e1e5cbd11
6 changed files with 144 additions and 63 deletions

View file

@ -543,9 +543,19 @@ mod tests {
assert!(m.is_empty());
}
fn start_stub() -> Option<LdapStub> {
match LdapStub::start() {
Ok(stub) => Some(stub),
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => None,
Err(e) => panic!("start ldap stub: {e}"),
}
}
#[test]
fn endpoint_uses_loopback_with_assigned_port() {
let stub = LdapStub::start().unwrap();
let Some(stub) = start_stub() else {
return;
};
let ep = stub.endpoint();
assert!(ep.starts_with("127.0.0.1:"));
assert!(ep.ends_with(&stub.port().to_string()));
@ -553,7 +563,9 @@ mod tests {
#[test]
fn search_request_returns_three_for_wildcard_via_socket() {
let stub = LdapStub::start().unwrap();
let Some(stub) = start_stub() else {
return;
};
let mut s = TcpStream::connect(format!("127.0.0.1:{}", stub.port())).unwrap();
s.write_all(b"SEARCH (uid=*)\n").unwrap();
s.flush().unwrap();
@ -572,7 +584,9 @@ mod tests {
#[test]
fn search_request_returns_one_for_concrete_uid_via_socket() {
let stub = LdapStub::start().unwrap();
let Some(stub) = start_stub() else {
return;
};
let mut s = TcpStream::connect(format!("127.0.0.1:{}", stub.port())).unwrap();
s.write_all(b"SEARCH (uid=alice)\n").unwrap();
s.flush().unwrap();
@ -584,7 +598,9 @@ mod tests {
#[test]
fn record_search_helper_appends_event() {
let stub = LdapStub::start().unwrap();
let Some(stub) = start_stub() else {
return;
};
stub.record_search("(uid=*)", 3);
let events = stub.drain_events();
assert_eq!(events.len(), 1);
@ -598,7 +614,9 @@ mod tests {
#[test]
fn drop_releases_port_for_rebind() {
let port = {
let stub = LdapStub::start().unwrap();
let Some(stub) = start_stub() else {
return;
};
stub.port()
};
std::thread::sleep(Duration::from_millis(50));
@ -638,7 +656,9 @@ mod tests {
#[test]
fn ber_bind_then_search_wildcard_returns_three_entries() {
let stub = LdapStub::start().unwrap();
let Some(stub) = start_stub() else {
return;
};
let mut s = TcpStream::connect(format!("127.0.0.1:{}", stub.port())).unwrap();
let bind = build_ber_bind(1);
s.write_all(&bind).unwrap();
@ -689,7 +709,9 @@ mod tests {
#[test]
fn ber_search_concrete_uid_returns_one_entry() {
let stub = LdapStub::start().unwrap();
let Some(stub) = start_stub() else {
return;
};
let mut s = TcpStream::connect(format!("127.0.0.1:{}", stub.port())).unwrap();
s.write_all(&build_ber_bind(1)).unwrap();
let mut eq_body = Vec::new();
@ -729,7 +751,9 @@ mod tests {
// Same shape as `search_request_returns_three_for_wildcard_via_socket`
// but the leading byte is `S` (0x53), not `0x30`, so the
// accept-loop dispatches plaintext.
let stub = LdapStub::start().unwrap();
let Some(stub) = start_stub() else {
return;
};
let mut s = TcpStream::connect(format!("127.0.0.1:{}", stub.port())).unwrap();
s.write_all(b"SEARCH (uid=*)\n").unwrap();
s.flush().unwrap();