| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (c) 2008-2026 Andrew Ziem. |
| 3 |
# |
| 4 |
# This work is licensed under the terms of the GNU GPL, version 3 or |
| 5 |
# later. See the COPYING file in the top-level directory. |
| 6 |
|
| 7 |
|
| 8 |
""" |
| 9 |
Test cases for __init__ |
| 10 |
""" |
| 11 |
|
| 12 |
import os |
| 13 |
import shutil |
| 14 |
import tempfile |
| 15 |
import unittest |
| 16 |
|
| 17 |
import bleachbit |
| 18 |
from bleachbit import IS_MAC, IS_POSIX, IS_WINDOWS, get_share_dirs, get_share_path |
| 19 |
from tests import common |
| 20 |
|
| 21 |
|
| 22 |
class InitTestCase(common.BleachbitTestCase): |
| 23 |
|
| 24 |
"""Test cases for __init__""" |
| 25 |
|
| 26 |
def test_expanduser(self): |
| 27 |
"""Unit test for function expanduser()""" |
| 28 |
# already absolute |
| 29 |
test_input = '/home/user/foo' |
| 30 |
test_output = os.path.expanduser(test_input) |
| 31 |
self.assertEqual(test_input, test_output) |
| 32 |
|
| 33 |
# tilde not at beginning |
| 34 |
test_input = '/home/user/~' |
| 35 |
test_output = os.path.expanduser(test_input) |
| 36 |
self.assertEqual(test_input, test_output) |
| 37 |
|
| 38 |
# should be expanded |
| 39 |
test_inputs = () |
| 40 |
if IS_WINDOWS: |
| 41 |
test_inputs = ('~', r'~\ntuser.dat') |
| 42 |
elif IS_MAC: |
| 43 |
# macOS defaults to zsh |
| 44 |
test_inputs = ('~', '~/.zshrc') |
| 45 |
elif IS_POSIX: |
| 46 |
test_inputs = ('~', '~/.profile') |
| 47 |
for test_input in test_inputs: |
| 48 |
test_output = os.path.expanduser(test_input) |
| 49 |
self.assertNotEqual(test_input, test_output) |
| 50 |
msg = None |
| 51 |
if not os.path.exists(test_output) and not test_input == '~': |
| 52 |
msg = f"contents of home directory: {os.listdir(os.path.expanduser('~'))}" |
| 53 |
self.assertExists(test_output, msg) |
| 54 |
if IS_POSIX: |
| 55 |
self.assertTrue(os.path.samefile( |
| 56 |
test_output, os.path.expanduser(test_input))) |
| 57 |
|
| 58 |
def test_get_share_dirs(self): |
| 59 |
"""Unit test for get_share_dirs()""" |
| 60 |
got_shared_dirs = get_share_dirs() |
| 61 |
# There must be at least one shared directory. |
| 62 |
self.assertGreater(len(got_shared_dirs), 0) |
| 63 |
existing = [d for d in got_shared_dirs if os.path.isdir(d)] |
| 64 |
# At least one of the shared directories must exist. |
| 65 |
self.assertGreater(len(existing), 0, |
| 66 |
f'No share dir exists among: {got_shared_dirs}') |
| 67 |
|
| 68 |
def test_get_share_path(self): |
| 69 |
"""Unit test for get_share_path()""" |
| 70 |
for fn in ('app-menu.ui', 'protected_path.xml'): |
| 71 |
self.assertExists(get_share_path(fn)) |
| 72 |
self.assertIsNone(get_share_path('nonexistent')) |
| 73 |
|
| 74 |
@common.skipUnlessWindows |
| 75 |
def test_harden_dll_search_path(self): |
| 76 |
"""The current directory is not on the DLL search path""" |
| 77 |
import ctypes |
| 78 |
from ctypes import wintypes |
| 79 |
ERROR_MOD_NOT_FOUND = 126 |
| 80 |
|
| 81 |
src = os.path.join(os.environ['SystemRoot'], 'System32', 'version.dll') |
| 82 |
tmpdir = tempfile.mkdtemp() |
| 83 |
planted = os.path.join(tmpdir, 'bb_dll_preload_test.dll') |
| 84 |
shutil.copy(src, planted) |
| 85 |
|
| 86 |
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) |
| 87 |
load = kernel32.LoadLibraryW |
| 88 |
load.argtypes = [wintypes.LPCWSTR] |
| 89 |
load.restype = wintypes.HMODULE |
| 90 |
free = kernel32.FreeLibrary |
| 91 |
free.argtypes = [wintypes.HMODULE] |
| 92 |
|
| 93 |
orig_cwd = os.getcwd() |
| 94 |
try: |
| 95 |
os.chdir(tmpdir) |
| 96 |
bleachbit._harden_dll_search_path() |
| 97 |
|
| 98 |
# By bare name the DLL must not be found in the current directory |
| 99 |
ctypes.set_last_error(0) |
| 100 |
handle = load('bb_dll_preload_test.dll') |
| 101 |
if handle: |
| 102 |
free(handle) |
| 103 |
self.assertFalse( |
| 104 |
handle, 'current directory still on DLL search path') |
| 105 |
self.assertEqual(ctypes.get_last_error(), ERROR_MOD_NOT_FOUND) |
| 106 |
|
| 107 |
# A full path still loads |
| 108 |
handle = load(planted) |
| 109 |
self.assertTrue(handle) |
| 110 |
free(handle) |
| 111 |
finally: |
| 112 |
os.chdir(orig_cwd) |
| 113 |
shutil.rmtree(tmpdir, ignore_errors=True) |
| 114 |
|
| 115 |
|
| 116 |
def suite(): |
| 117 |
"""Return a test suite""" |
| 118 |
return unittest.TestLoader().loadTestsFromTestCase(InitTestCase) |