| 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 |
Test case for module Log |
| 9 |
""" |
| 10 |
|
| 11 |
import logging |
| 12 |
import os |
| 13 |
import stat |
| 14 |
import sys |
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
from bleachbit.Log import init_log |
| 18 |
from tests import common |
| 19 |
|
| 20 |
|
| 21 |
class LogTestCase(common.BleachbitTestCase): |
| 22 |
"""Test case for module Log""" |
| 23 |
|
| 24 |
def setUp(self): |
| 25 |
super().setUp() |
| 26 |
self.bleachbit_logger = logging.getLogger('bleachbit') |
| 27 |
self.saved_handlers = list(self.bleachbit_logger.handlers) |
| 28 |
self.saved_level = self.bleachbit_logger.level |
| 29 |
|
| 30 |
def tearDown(self): |
| 31 |
for handler in self.bleachbit_logger.handlers: |
| 32 |
if handler not in self.saved_handlers: |
| 33 |
handler.close() |
| 34 |
self.bleachbit_logger.handlers = self.saved_handlers |
| 35 |
self.bleachbit_logger.setLevel(self.saved_level) |
| 36 |
super().tearDown() |
| 37 |
|
| 38 |
@common.skipIfWindows |
| 39 |
def test_init_log_debug_log_file_not_world_readable(self): |
| 40 |
"""The --debug-log file must not be created world-readable |
| 41 |
|
| 42 |
It can contain sensitive system info such as paths and username. |
| 43 |
""" |
| 44 |
debug_log_path = os.path.join(self.tempdir, 'debug.log') |
| 45 |
self.assertNotExists(debug_log_path) |
| 46 |
with mock.patch.object(sys, 'argv', ['bleachbit', '--debug-log', debug_log_path]): |
| 47 |
init_log() |
| 48 |
self.assertExists(debug_log_path) |
| 49 |
mode = stat.S_IMODE(os.stat(debug_log_path).st_mode) |
| 50 |
self.assertEqual(mode, 0o600) |
| 51 |
|
| 52 |
@common.skipIfWindows |
| 53 |
def test_init_log_debug_log_file_existing_untouched(self): |
| 54 |
"""init_log() must not clobber an existing debug log file's permissions""" |
| 55 |
debug_log_path = self.write_file( |
| 56 |
'existing_debug.log', text='previous content\n') |
| 57 |
# Any mode that init_log() would not pick on its own works here. |
| 58 |
# The execute bit is the harmless way to differ from its 0o600. |
| 59 |
os.chmod(debug_log_path, 0o700) |
| 60 |
with mock.patch.object(sys, 'argv', ['bleachbit', '--debug-log', debug_log_path]): |
| 61 |
init_log() |
| 62 |
mode = stat.S_IMODE(os.stat(debug_log_path).st_mode) |
| 63 |
self.assertEqual(mode, 0o700) |
| 64 |
with open(debug_log_path, encoding='utf-8') as f: |
| 65 |
self.assertIn('previous content', f.read()) |