~jpastuszek/file-owner

1e02253b8b39b63a2a8486778f10fca0335b6f9c — Jakub Pastuszek 8 months ago 5f00a13
made tests portable
3 files changed, 193 insertions(+), 36 deletions(-)

M Cargo.lock
M Cargo.toml
M src/lib.rs
M Cargo.lock => Cargo.lock +120 -0
@@ 1,5 1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "bitflags"
version = "1.2.1"


@@ 23,6 25,18 @@ name = "file-owner"
version = "0.1.1-alpha.0"
dependencies = [
 "nix",
 "tempfile",
]

[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
 "cfg-if",
 "libc",
 "wasi",
]

[[package]]


@@ 42,3 56,109 @@ dependencies = [
 "cfg-if",
 "libc",
]

[[package]]
name = "ppv-lite86"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"

[[package]]
name = "rand"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
 "libc",
 "rand_chacha",
 "rand_core",
 "rand_hc",
]

[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
 "ppv-lite86",
 "rand_core",
]

[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
 "getrandom",
]

[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
 "rand_core",
]

[[package]]
name = "redox_syscall"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
dependencies = [
 "bitflags",
]

[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
 "winapi",
]

[[package]]
name = "tempfile"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
dependencies = [
 "cfg-if",
 "libc",
 "rand",
 "redox_syscall",
 "remove_dir_all",
 "winapi",
]

[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
 "winapi-i686-pc-windows-gnu",
 "winapi-x86_64-pc-windows-gnu",
]

[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"

[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

M Cargo.toml => Cargo.toml +3 -0
@@ 14,3 14,6 @@ edition = "2018"

[dependencies]
nix = "0.20.0"

[dev-dependencies]
tempfile = "3.2.0"

M src/lib.rs => src/lib.rs +70 -36
@@ 283,8 283,11 @@ mod tests {

    #[test]
    fn test_display() {
        assert_eq!(&Owner::from_uid(99).to_string(), "nobody");
        assert_eq!(&Group::from_gid(99).to_string(), "nogroup");
        let nobody_id = Owner::from_name("nobody").unwrap().id();
        let nogroup_id = Group::from_name("nogroup").unwrap().id();

        assert_eq!(&Owner::from_uid(nobody_id).to_string(), "nobody");
        assert_eq!(&Group::from_gid(nogroup_id).to_string(), "nogroup");

        assert_eq!(&Owner::from_uid(321321).to_string(), "321321");
        assert_eq!(&Group::from_gid(321321).to_string(), "321321");


@@ 292,60 295,91 @@ mod tests {

    #[test]
    #[ignore]
    fn test_set() {
        std::fs::write("/tmp/foo", "test").unwrap();

        set_owner("/tmp/foo", "nobody").unwrap();
        set_owner("/tmp/foo", 99).unwrap();

        set_group("/tmp/foo", "nogroup").unwrap();
        set_group("/tmp/foo", 99).unwrap();

        set_owner_group("/tmp/foo", "nobody", "nogroup").unwrap();
        set_owner_group("/tmp/foo", 99, 99).unwrap();
        set_owner_group("/tmp/foo", 99, "nogroup").unwrap();
        set_owner_group("/tmp/foo", "nobody", 99).unwrap();
    fn test_set_get() {
        let nobody_id = Owner::from_name("nobody").unwrap().id();
        let nogroup_id = Group::from_name("nogroup").unwrap().id();

        let file1 = tempfile::NamedTempFile::new().unwrap();
        let file_path1 = file1.path();
        let file2 = tempfile::NamedTempFile::new().unwrap();
        let file_path2 = file2.path();

        set_owner(file_path1, "nobody").unwrap();
        assert_eq!(owner(file_path1).unwrap().name().unwrap().as_deref(), Some("nobody"));
        set_owner(file_path2, nobody_id).unwrap();
        assert_eq!(owner(file_path2).unwrap().name().unwrap().as_deref(), Some("nobody"));

        set_group(file_path1, "nogroup").unwrap();
        assert_eq!(group(file_path1).unwrap().name().unwrap().as_deref(), Some("nogroup"));
        set_group(file_path2, nogroup_id).unwrap();
        assert_eq!(group(file_path2).unwrap().name().unwrap().as_deref(), Some("nogroup"));
    }

    #[test]
    #[ignore]
    fn test_get() {
        std::fs::write("/tmp/bar", "test").unwrap();

        set_owner("/tmp/bar", "nobody").unwrap();
        set_group("/tmp/bar", "nogroup").unwrap();
    fn test_set_get_all() {
        let nobody_id = Owner::from_name("nobody").unwrap().id();
        let nogroup_id = Group::from_name("nogroup").unwrap().id();

        let file1 = tempfile::NamedTempFile::new().unwrap();
        let file_path1 = file1.path();
        let file2 = tempfile::NamedTempFile::new().unwrap();
        let file_path2 = file2.path();
        let file3 = tempfile::NamedTempFile::new().unwrap();
        let file_path3 = file3.path();
        let file4 = tempfile::NamedTempFile::new().unwrap();
        let file_path4 = file4.path();

        set_owner_group(file_path1, "nobody", "nogroup").unwrap();
        assert_eq!(owner(file_path1).unwrap().name().unwrap().as_deref(), Some("nobody"));
        assert_eq!(group(file_path1).unwrap().name().unwrap().as_deref(), Some("nogroup"));
        set_owner_group(file_path2, nobody_id, nogroup_id).unwrap();
        assert_eq!(owner(file_path2).unwrap().name().unwrap().as_deref(), Some("nobody"));
        assert_eq!(group(file_path2).unwrap().name().unwrap().as_deref(), Some("nogroup"));

        set_owner_group(file_path3, nobody_id, "nogroup").unwrap();
        assert_eq!(owner(file_path3).unwrap().name().unwrap().as_deref(), Some("nobody"));
        assert_eq!(group(file_path3).unwrap().name().unwrap().as_deref(), Some("nogroup"));
        set_owner_group(file_path4, "nobody", nogroup_id).unwrap();
        assert_eq!(owner(file_path4).unwrap().name().unwrap().as_deref(), Some("nobody"));
        assert_eq!(group(file_path4).unwrap().name().unwrap().as_deref(), Some("nogroup"));
    }

        assert_eq!(owner("/tmp/bar").unwrap().name().unwrap().as_deref(), Some("nobody"));
        assert_eq!(group("/tmp/bar").unwrap().name().unwrap().as_deref(), Some("nogroup"));
    #[test]
    #[ignore]
    fn test_get_all() {
        let file = tempfile::NamedTempFile::new().unwrap();
        let file_path = file.path();

        set_owner_group("/tmp/bar", "nobody", "nogroup").unwrap();
        set_owner_group(file_path, "nobody", "nogroup").unwrap();

        let (o, g) = owner_group("/tmp/bar").unwrap();
        let (o, g) = owner_group(file_path).unwrap();
        assert_eq!(o.name().unwrap().as_deref(), Some("nobody"));
        assert_eq!(g.name().unwrap().as_deref(), Some("nogroup"));

        assert_eq!(o.id(), 99);
        assert_eq!(g.id(), 99);
    }

    #[test]
    #[ignore]
    fn test_ext_traits() {
        std::fs::write("/tmp/baz", "test").unwrap();
        let nobody_id = Owner::from_name("nobody").unwrap().id();
        let nogroup_id = Group::from_name("nogroup").unwrap().id();

        let file = tempfile::NamedTempFile::new().unwrap();
        let file_path = file.path();

        "/tmp/baz".set_owner("nobody").unwrap();
        "/tmp/baz".set_group("nogroup").unwrap();
        file_path.set_owner("nobody").unwrap();
        file_path.set_group("nogroup").unwrap();

        assert_eq!("/tmp/baz".owner().unwrap().name().unwrap().as_deref(), Some("nobody"));
        assert_eq!("/tmp/baz".group().unwrap().name().unwrap().as_deref(), Some("nogroup"));
        assert_eq!(file_path.owner().unwrap().name().unwrap().as_deref(), Some("nobody"));
        assert_eq!(file_path.group().unwrap().name().unwrap().as_deref(), Some("nogroup"));

        "/tmp/baz".set_owner_group("nobody", "nogroup").unwrap();
        file_path.set_owner_group("nobody", "nogroup").unwrap();

        let (o, g) = "/tmp/baz".owner_group().unwrap();
        let (o, g) = file_path.owner_group().unwrap();
        assert_eq!(o.name().unwrap().as_deref(), Some("nobody"));
        assert_eq!(g.name().unwrap().as_deref(), Some("nogroup"));

        assert_eq!(o.id(), 99);
        assert_eq!(g.id(), 99);
        assert_eq!(o.id(), nobody_id);
        assert_eq!(g.id(), nogroup_id);
    }
}