Skip to content

Commit 5b64816

Browse files
mattsu2020sylvestre
authored andcommitted
utmpx: unify canonical host API and make tests deterministic
1 parent 5685bac commit 5b64816

2 files changed

Lines changed: 72 additions & 20 deletions

File tree

‎src/uucore/src/lib/features/systemd_logind.rs‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -448,13 +448,8 @@ impl SystemdUtmpxCompat {
448448
self.record.is_user_process()
449449
}
450450

451-
/// Canonical host name
452-
pub fn canon_host(&self) -> String {
453-
self.try_canon_host().unwrap_or_else(|_| self.host())
454-
}
455-
456-
/// Canonical host name, propagating DNS errors.
457-
pub fn try_canon_host(&self) -> std::io::Result<String> {
451+
/// Canonicalize host name using DNS
452+
pub fn canon_host(&self) -> std::io::Result<String> {
458453
crate::utmpx::canonicalize_host(self.record.host.clone())
459454
}
460455
}
@@ -533,11 +528,9 @@ mod tests {
533528
use super::*;
534529

535530
#[test]
536-
fn canonical_host_preserves_display_suffix() {
537-
// localhost is resolved locally. Its canonical spelling makes returning
538-
// the unmodified host fail this regression without a DNS server.
539-
let host = "LOCALHOST:0";
540-
let expected = "localhost:0";
531+
fn canonical_host_preserves_local_display() {
532+
// A local display has no hostname to resolve.
533+
let host = ":0";
541534
let record = SystemdUtmpxCompat::new(SystemdLoginRecord {
542535
user: "user".into(),
543536
session_id: "session".into(),
@@ -549,13 +542,12 @@ mod tests {
549542
session_leader_pid: 0,
550543
record_type: SystemdRecordType::UserProcess,
551544
});
552-
assert_eq!(record.try_canon_host().unwrap(), expected);
553-
assert_eq!(record.canon_host(), expected);
545+
assert_eq!(record.canon_host().unwrap(), host);
554546
assert_eq!(
555547
crate::utmpx::UtmpxRecord::Systemd(record)
556548
.canon_host()
557549
.unwrap(),
558-
expected
550+
host
559551
);
560552
}
561553

‎src/uucore/src/lib/features/utmpx.rs‎

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,29 @@ impl Utmpx {
339339
}
340340

341341
pub(crate) fn canonicalize_host(host: String) -> IOResult<String> {
342-
let (hostname, display) = host.split_once(':').unwrap_or((&host, ""));
343-
344-
if !hostname.is_empty() {
342+
canonicalize_host_with_lookup(host, |hostname| {
345343
use dns_lookup::{AddrInfoHints, getaddrinfo};
346344

347345
const AI_CANONNAME: i32 = 0x2;
348346
let hints = AddrInfoHints {
349347
flags: AI_CANONNAME,
350348
..AddrInfoHints::default()
351349
};
352-
if let Ok(sockets) = getaddrinfo(Some(hostname), None, Some(hints)) {
350+
getaddrinfo(Some(hostname), None, Some(hints))
351+
})
352+
}
353+
354+
fn canonicalize_host_with_lookup<I>(
355+
host: String,
356+
lookup: impl FnOnce(&str) -> Result<I, dns_lookup::LookupError>,
357+
) -> IOResult<String>
358+
where
359+
I: Iterator<Item = IOResult<dns_lookup::AddrInfo>>,
360+
{
361+
let (hostname, display) = host.split_once(':').unwrap_or((&host, ""));
362+
363+
if !hostname.is_empty() {
364+
if let Ok(sockets) = lookup(hostname) {
353365
let sockets = sockets.collect::<IOResult<Vec<_>>>()?;
354366
for socket in sockets {
355367
if let Some(ai_canonname) = socket.canonname {
@@ -521,7 +533,7 @@ impl UtmpxRecord {
521533
match self {
522534
Self::Traditional(utmpx) => utmpx.canon_host(),
523535
#[cfg(all(feature = "systemd_logind", target_os = "linux"))]
524-
Self::Systemd(systemd) => systemd.try_canon_host(),
536+
Self::Systemd(systemd) => systemd.canon_host(),
525537
}
526538
}
527539
}
@@ -564,3 +576,51 @@ impl Drop for UtmpxIter {
564576
}
565577
}
566578
}
579+
580+
#[cfg(test)]
581+
mod tests {
582+
use super::*;
583+
584+
#[test]
585+
fn canonical_host_preserves_display_suffix() {
586+
let host = canonicalize_host_with_lookup("REMOTE:0.1".into(), |hostname| {
587+
assert_eq!(hostname, "REMOTE");
588+
Ok(std::iter::once(Ok(dns_lookup::AddrInfo {
589+
flags: 0,
590+
address: 0,
591+
socktype: 0,
592+
protocol: 0,
593+
sockaddr: ([192, 0, 2, 1], 0).into(),
594+
canonname: Some("remote.example".into()),
595+
})))
596+
})
597+
.unwrap();
598+
599+
assert_eq!(host, "remote.example:0.1");
600+
}
601+
602+
#[test]
603+
fn canonical_host_lookup_failure_returns_hostname() {
604+
let host = canonicalize_host_with_lookup("remote.invalid:0".into(), |_| {
605+
Err::<std::iter::Empty<IOResult<dns_lookup::AddrInfo>>, _>(
606+
std::io::Error::other("lookup failed").into(),
607+
)
608+
})
609+
.unwrap();
610+
611+
assert_eq!(host, "remote.invalid");
612+
}
613+
614+
#[test]
615+
fn canonical_host_propagates_address_errors() {
616+
let error = canonicalize_host_with_lookup("remote.example:0".into(), |_| {
617+
Ok(std::iter::once(Err(std::io::Error::new(
618+
std::io::ErrorKind::InvalidData,
619+
"invalid address",
620+
))))
621+
})
622+
.unwrap_err();
623+
624+
assert_eq!(error.kind(), std::io::ErrorKind::InvalidData);
625+
}
626+
}

0 commit comments

Comments
 (0)