use super::*;
use crate::fs_btrfs_test_support::btrfs_fixture;
use crate::test_support::{fixture, remove};
use std::path::PathBuf;
#[test]
fn the_magic_numbers_map_to_their_kinds() {
assert_eq!(classify_magic(libc::BTRFS_SUPER_MAGIC as u64), FilesystemKind::Btrfs);
assert_eq!(classify_magic(libc::BTRFS_SUPER_MAGIC as u32 as u64), FilesystemKind::Btrfs);
assert_eq!(classify_magic(libc::OVERLAYFS_SUPER_MAGIC as u64), FilesystemKind::Overlayfs);
assert_eq!(classify_magic(libc::TMPFS_MAGIC as u64), FilesystemKind::Other);
assert_eq!(classify_magic(0), FilesystemKind::Other);
}
#[test]
fn a_sign_extended_f_type_still_classifies() {
let extended: u64 = u64::from(libc::BTRFS_SUPER_MAGIC as u32) | 0xffff_ffff_0000_0000;
assert_eq!(classify_magic(extended), FilesystemKind::Btrfs);
}
#[test]
fn the_kind_of_a_real_directory_is_read_and_a_missing_path_is_a_typed_error() {
let root: PathBuf = fixture("fs-kind-real");
let (_kind, free) = filesystem_facts(root.as_path()).expect("the fixture root answers");
assert!(free > 0, "a fixture root with no free byte cannot hold a fixture");
let error: PrimitiveError = filesystem_kind(root.join("absent").as_path()).expect_err("a missing path has no filesystem");
assert_eq!((error.operation, error.errno), ("statfs", libc::ENOENT));
remove(root.as_path());
}
#[test]
fn a_plain_directory_is_not_a_subvolume_root() {
let root: PathBuf = fixture("fs-kind-plain");
let plain: PathBuf = root.join("plain");
std::fs::create_dir(&plain).expect("plain directory");
assert_eq!(is_subvolume_root(plain.as_path()), Ok(false));
remove(root.as_path());
}
#[test]
fn btrfs_a_created_subvolume_is_a_root_and_a_plain_directory_beside_it_is_not() {
let Some(root) = btrfs_fixture("fs-kind-btrfs") else { return };
assert_eq!(filesystem_kind(root.as_path()), Ok(FilesystemKind::Btrfs));
let subvolume: PathBuf = root.join("sub");
crate::fs_btrfs_ioctl::subvolume_create_at(subvolume.as_path()).expect("subvolume create");
assert_eq!(is_subvolume_root(subvolume.as_path()), Ok(true));
let plain: PathBuf = root.join("plain");
std::fs::create_dir(&plain).expect("plain directory");
assert_eq!(is_subvolume_root(plain.as_path()), Ok(false));
std::fs::write(root.join("file"), b"x").expect("file");
assert_eq!(is_subvolume_root(root.join("file").as_path()), Ok(false));
remove(root.as_path());
}