/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Dia-Files

Copyright (C) 2019-2024  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2019-2024".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

#![cfg(test)]

use {
    std::fs,
    crate::Result,
    super::{FilePermissions, Permissions, RawPermission},
};

#[cfg(unix)]
use std::os::unix::prelude::PermissionsExt;

#[cfg(unix)]
#[test]
fn permissions() {
    assert_eq!(RawPermission::try_from(Permissions::None as isize).unwrap(), 0);
    assert_eq!(RawPermission::try_from(Permissions::Read as isize).unwrap(), 4);
    assert_eq!(RawPermission::try_from(Permissions::Write as isize).unwrap(), 2);
    assert_eq!(RawPermission::try_from(Permissions::Execute as isize).unwrap(), 1);

    assert_eq!(Permissions::ReadWrite as RawPermission, Permissions::Read as RawPermission | Permissions::Write as RawPermission);
    assert_eq!(Permissions::ReadExecute as RawPermission, Permissions::Read as RawPermission | Permissions::Execute as RawPermission);
    assert_eq!(Permissions::WriteExecute as RawPermission, Permissions::Write as RawPermission | Permissions::Execute as RawPermission);

    assert_eq!(
        Permissions::ReadWriteExecute as RawPermission,
        Permissions::Read as RawPermission | Permissions::Write as RawPermission | Permissions::Execute as RawPermission,
    );
}

#[cfg(unix)]
#[test]
fn file_permissions() -> Result<()> {
    let file_permissions = FilePermissions::new(Permissions::Read, Permissions::ReadWrite, Permissions::Write);

    assert_eq!(file_permissions.user(), &Permissions::Read);
    assert_eq!(file_permissions.group(), &Permissions::ReadWrite);
    assert_eq!(file_permissions.others(), &Permissions::Write);

    assert_ne!(file_permissions.user(), file_permissions.group());
    assert_ne!(file_permissions.user(), file_permissions.others());
    assert_ne!(file_permissions.group(), file_permissions.others());

    {
        let permissions = fs::Permissions::from(&file_permissions);
        assert_eq!(permissions.mode() << 23 >> 23, 0o462);
        assert_eq!(FilePermissions::try_from(permissions)?, file_permissions);
    }

    Ok(())
}
