diff --git a/tests/test_utils.py b/tests/test_utils.py index 12a0497..929954c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -7,6 +7,9 @@ import ctypes import string import platform +import os +import sys +from pathlib import Path from local_vault.utils import ( secure_delete, generate_random_password, validate_password_strength, @@ -39,23 +42,49 @@ def test_validate_password_strength(): assert not validate_password_strength("weakpassword") def test_is_root(monkeypatch): - monkeypatch.setattr("os.geteuid", lambda: 0) - assert is_root() - monkeypatch.setattr("os.geteuid", lambda: 1000) - assert not is_root() + if os.name == "nt": + # windows use another method + import ctypes + + monkeypatch.setattr("ctypes.windll.shell32.IsUserAnAdmin", lambda: True) + assert is_root() + + monkeypatch.setattr("ctypes.windll.shell32.IsUserAnAdmin", lambda: False) + assert not is_root() + else: + monkeypatch.setattr("os.geteuid", lambda: 0) + assert is_root() + + monkeypatch.setattr("os.geteuid", lambda: 1000) + assert not is_root() def test_ensure_directory_permissions(tmpdir): dir_path = Path(tmpdir) / "test_dir" ensure_directory_permissions(dir_path) assert dir_path.exists() - assert oct(dir_path.stat().st_mode)[-3:] == "700" + + if os.name == "nt": + # windows: just check if it is accessible (read/write/execute) + assert os.access(dir_path, os.R_OK) + assert os.access(dir_path, os.W_OK) + assert os.access(dir_path, os.X_OK) + else: + # unix: assert exact permission bits + assert oct(dir_path.stat().st_mode)[-3:] == "700" def test_secure_temporary_file(): with secure_temporary_file(mode='w+b', suffix='.txt') as f: f.write(b"Test data") f.flush() assert os.path.exists(f.name) - assert oct(os.stat(f.name).st_mode)[-3:] == "600" + + if os.name == "nt": + # windows: check if readable and writable + assert os.access(f.name, os.R_OK) + assert os.access(f.name, os.W_OK) + else: + # unix: assert strict permission bits + assert oct(os.stat(f.name).st_mode)[-3:] == "600" def test_get_system_entropy(): entropy = get_system_entropy()