Skip to content

Commit 827d922

Browse files
committed
Fix a few test failures on FreeBSD.
1 parent e4621c6 commit 827d922

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

cap-primitives/src/posish/fs/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@ fn tty_path() {
119119
#[cfg(unix)]
120120
use std::os::unix::fs::FileTypeExt;
121121

122-
for path in &["/dev/tty", "/dev/stdin", "/dev/stdout", "/dev/stderr"] {
122+
// On FreeBSD, `ttyname` doesn't seem to work on /dev/std{in,out,err}.
123+
let paths: &[&str] = if cfg!(target_os = "freebsd") {
124+
&["/dev/tty"]
125+
} else {
126+
&["/dev/tty", "/dev/stdin", "/dev/stdout", "/dev/stderr"]
127+
};
128+
129+
for path in paths {
123130
// Not all host configurations have these, so only test them if we can
124131
// open and canonicalize them, and if they're not FIFOs, which some
125132
// OS's use for stdin/stdout/stderr.

tests/fs_additional.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,23 +584,60 @@ fn dir_unsearchable_unreadable() {
584584
options.mode(0o000);
585585
check!(tmpdir.create_dir_with("dir", &options));
586586

587-
// Platforms with `O_PATH` can open a directory with no permissions.
587+
// Platforms with `O_PATH` can open a directory with no permissions. And
588+
// somehow FreeBSD can too; see `dir_unsearchable_unreadable_ambient`
589+
// below confirming this.
588590
if cfg!(any(
589-
target_os = "linux",
590591
target_os = "android",
591-
target_os = "redox"
592+
target_os = "linux",
593+
target_os = "redox",
592594
)) {
593-
check!(tmpdir.open_dir("dir"));
595+
let dir = check!(tmpdir.open_dir("dir"));
596+
assert!(dir.entries().is_err());
597+
assert!(dir.open_dir(".").is_err());
598+
} else if cfg!(target_os = "freebsd") {
599+
let dir = check!(tmpdir.open_dir("dir"));
600+
check!(dir.metadata("."));
601+
check!(dir.entries());
602+
check!(dir.open_dir("."));
594603
} else {
595604
assert!(tmpdir.open_dir("dir").is_err());
596605
}
597606
}
598607

608+
/// Like `dir_unsearchable_unreadable`, but uses ambient-authority APIs
609+
/// to test underlying host functionality.
610+
#[cfg(unix)]
611+
#[test]
612+
fn dir_unsearchable_unreadable_ambient() {
613+
use std::{fs::DirBuilder, os::unix::fs::DirBuilderExt};
614+
615+
let dir = tempfile::tempdir().unwrap();
616+
617+
let mut options = DirBuilder::new();
618+
options.mode(0o000);
619+
check!(options.create(dir.path().join("dir")));
620+
621+
// FreeBSD seems able to open directories with 0o000 permissions.
622+
if cfg!(any(
623+
target_os = "android",
624+
target_os = "linux",
625+
target_os = "redox",
626+
)) {
627+
assert!(std::fs::File::open(dir.path().join("dir")).is_err());
628+
assert!(std::fs::read_dir(dir.path().join("dir")).is_err());
629+
assert!(std::fs::File::open(dir.path().join("dir/.")).is_err());
630+
} else if cfg!(target_os = "freebsd") {
631+
check!(std::fs::File::open(dir.path().join("dir")));
632+
check!(std::fs::read_dir(dir.path().join("dir")));
633+
check!(std::fs::File::open(dir.path().join("dir/.")));
634+
}
635+
}
636+
599637
/// This test is the same as `symlink_hard_link` but uses `std::fs`'
600638
/// ambient API instead of `cap_std`. The purpose of this test is to
601639
/// confirm fundamentally OS-specific behaviors.
602640
#[test]
603-
#[cfg_attr(any(target_os = "macos", target_os = "freebsd"), ignore)] // submitted to upstream as https://github.com/rust-lang/rust/pull/78026
604641
fn symlink_hard_link_ambient() {
605642
#[cfg(unix)]
606643
use std::os::unix::fs::symlink;

tests/reopen.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(not(target_os = "freebsd"))] // FreeBSD can't reopen arbitrary files.
2+
13
#[macro_use]
24
mod sys_common;
35

@@ -26,8 +28,15 @@ fn basic_reopen() {
2628
assert!(buf.is_empty());
2729

2830
let mut ro = check!(file.reopen(OpenOptions::new().read(true)));
31+
let mut another = check!(file.reopen(OpenOptions::new().read(true)));
2932
check!(ro.read_to_string(&mut buf));
3033
assert_eq!(buf, "hello, world");
34+
buf.clear();
35+
check!(another.read_to_string(&mut buf));
36+
assert_eq!(buf, "hello, world");
37+
buf.clear();
38+
check!(another.read_to_string(&mut buf));
39+
assert!(buf.is_empty());
3140
}
3241

3342
/// Don't allow `reopen` to grant new permissions.

0 commit comments

Comments
 (0)