Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down