use pretty_assertions::assert_eq;
use super::*;
#[cfg(windows)]
#[test]
fn atomic_persist_retries_windows_sharing_violations() {
assert!(should_retry_atomic_persist(
&std::io::Error::from_raw_os_error(32)
));
}
fn utf16le(s: &str) -> Vec<u8> {
let mut bytes = vec![0xff, 0xfe];
bytes.extend(s.encode_utf16().flat_map(u16::to_le_bytes));
bytes
}
#[test]
fn test_decode_text_honours_a_byte_order_mark() {
assert_eq!(decode_text(&utf16le("abc\n")).unwrap(), "abc\n");
let mut be = vec![0xfe, 0xff];
be.extend("abc\n".encode_utf16().flat_map(u16::to_be_bytes));
assert_eq!(decode_text(&be).unwrap(), "abc\n");
assert_eq!(decode_text(b"\xef\xbb\xbfabc\n").unwrap(), "abc\n");
assert_eq!(decode_text(b"abc\n").unwrap(), "abc\n");
assert_eq!(decode_text(b"").unwrap(), "");
}
#[test]
fn test_decode_text_rejects_what_it_cannot_decode() {
let err = decode_text(b"\xff\x00abc").unwrap_err().to_string();
assert!(err.contains("byte-order mark"), "{err}");
let mut truncated = utf16le("abc");
truncated.pop();
let err = decode_text(&truncated).unwrap_err().to_string();
assert!(err.contains("truncated UTF-16LE"), "{err}");
let err = decode_text(&[0xff, 0xfe, 0x00, 0xd8])
.unwrap_err()
.to_string();
assert!(err.contains("invalid UTF-16LE"), "{err}");
}
#[test]
fn test_read_to_string_bom_decodes_a_utf16_file() {
let tmp = tempfile::TempDir::new().unwrap();
let path = tmp.path().join("hashes.sha256");
fs::write(&path, utf16le("deadbeef *tool.tar.gz\n")).unwrap();
assert_eq!(
read_to_string_bom(&path).unwrap(),
"deadbeef *tool.tar.gz\n"
);
assert!(read_to_string(&path).is_err());
}
#[test]
fn test_is_plain_file_name() {
for ok in ["tool", "my-tool.exe", "tool.tar.gz", "..hidden", "a b"] {
assert!(is_plain_file_name(ok), "should accept {ok:?}");
}
for bad in [
"",
".",
"..",
"../tool",
"a/b",
"/abs/tool",
"..\\tool",
"a\\b",
"C:\\tool",
] {
assert!(!is_plain_file_name(bad), "should reject {bad:?}");
}
}
#[test]
fn test_is_safe_relative_path() {
for ok in ["tool", "bin/tool", "nested/path/tool.exe", "a b/tool"] {
assert!(is_safe_relative_path(ok), "should accept {ok:?}");
}
for bad in [
"",
".",
"..",
"../tool",
"bin/../../tool",
"/abs/tool",
"..\\tool",
"C:\\tool",
"\\\\server\\share\\tool",
] {
assert!(!is_safe_relative_path(bad), "should reject {bad:?}");
}
}
#[tokio::test]
async fn test_run_blocking_current_thread_runtime() {
assert_eq!(run_blocking(|| 42), 42);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_run_blocking_multi_thread_runtime() {
assert_eq!(run_blocking(|| 42), 42);
}
#[cfg(unix)]
#[test]
fn open_regular_file_refuses_a_pipe_without_blocking() {
let tmp = tempfile::tempdir().unwrap();
let fifo = tmp.path().join("mise.toml");
nix::unistd::mkfifo(
&fifo,
nix::sys::stat::Mode::S_IRUSR | nix::sys::stat::Mode::S_IWUSR,
)
.unwrap();
let (tx, rx) = std::sync::mpsc::channel();
let probe = fifo.clone();
std::thread::spawn(move || {
let _ = tx.send(read_config_to_string(&probe).is_err());
});
let refused = rx
.recv_timeout(std::time::Duration::from_secs(10))
.expect("opening a pipe blocked");
assert!(refused);
let dir = tmp.path().join("dir.toml");
std::fs::create_dir(&dir).unwrap();
assert!(open_regular_file(&dir).is_err());
}
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread")]
async fn read_config_to_string_async_refuses_a_pipe_without_blocking() {
let tmp = tempfile::tempdir().unwrap();
let fifo = tmp.path().join("tasks.toml");
nix::unistd::mkfifo(
&fifo,
nix::sys::stat::Mode::S_IRUSR | nix::sys::stat::Mode::S_IWUSR,
)
.unwrap();
let result = tokio::time::timeout(
std::time::Duration::from_secs(10),
read_config_to_string_async(&fifo),
)
.await
.expect("reading a pipe blocked");
let err = result.unwrap_err();
assert!(format!("{err:#}").contains("not a regular file"), "{err:#}");
}
#[test]
fn config_files_over_the_limit_are_refused() {
let tmp = tempfile::tempdir().unwrap();
let small = tmp.path().join("mise.toml");
std::fs::write(&small, "[tools]\n").unwrap();
assert_eq!(read_config_to_string(&small).unwrap(), "[tools]\n");
ensure_config_file_size(&small).unwrap();
let at_limit = tmp.path().join("at_limit.toml");
File::create(&at_limit)
.unwrap()
.set_len(CONFIG_FILE_LIMIT)
.unwrap();
assert_eq!(
read_config_to_string(&at_limit).unwrap().len() as u64,
CONFIG_FILE_LIMIT
);
let large = tmp.path().join("large.toml");
File::create(&large)
.unwrap()
.set_len(CONFIG_FILE_LIMIT + 1)
.unwrap();
let err = read_config_to_string(&large).unwrap_err().to_string();
assert!(err.contains("larger than 16 MiB"), "{err}");
assert!(ensure_config_file_size(&large).is_err());
}
#[test]
fn read_config_to_string_if_exists_treats_missing_as_none() {
let tmp = tempfile::tempdir().unwrap();
let missing = tmp.path().join("miserc.toml");
assert_eq!(read_config_to_string_if_exists(&missing).unwrap(), None);
std::fs::write(&missing, "env = []\n").unwrap();
assert_eq!(
read_config_to_string_if_exists(&missing)
.unwrap()
.as_deref(),
Some("env = []\n")
);
let large = tmp.path().join("large.toml");
File::create(&large)
.unwrap()
.set_len(CONFIG_FILE_LIMIT + 1)
.unwrap();
assert!(read_config_to_string_if_exists(&large).is_err());
assert!(read_config_to_string_if_exists(tmp.path()).is_err());
}