| 1 |
# vim: ts=4:sw=4:expandtab |
| 2 |
|
| 3 |
# BleachBit |
| 4 |
# Copyright (C) 2008-2025 Andrew Ziem |
| 5 |
# https://www.bleachbit.org |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
|
| 21 |
""" |
| 22 |
Test case for module Chaff |
| 23 |
""" |
| 24 |
|
| 25 |
from unittest import mock |
| 26 |
import os |
| 27 |
from tempfile import mkdtemp |
| 28 |
from shutil import rmtree |
| 29 |
from urllib.parse import urlparse |
| 30 |
|
| 31 |
from tests import common |
| 32 |
from bleachbit.Chaff import download_models, generate_emails, generate_2600, have_models, MODEL_BASENAMES, MODEL_SHA512, DEFAULT_MODELS_DIR |
| 33 |
from bleachbit.FileUtilities import getsize |
| 34 |
|
| 35 |
|
| 36 |
class ChaffTestCase(common.BleachbitTestCase): |
| 37 |
"""Test case for module Chaff""" |
| 38 |
|
| 39 |
def test_Chaff(self): |
| 40 |
"""Unit test for class Chaff""" |
| 41 |
tmp_dir = mkdtemp(prefix='bleachbit-chaff') |
| 42 |
models_dir = os.path.join(tmp_dir, 'model') |
| 43 |
os.mkdir(models_dir) |
| 44 |
# con=Clinton content |
| 45 |
con_path = os.path.join(models_dir, 'clinton_content_model.json.bz2') |
| 46 |
# sub=Clinton subject |
| 47 |
sub_path = os.path.join(models_dir, 'clinton_subject_model.json.bz2') |
| 48 |
# ts = 2600 Magazine |
| 49 |
ts_path = os.path.join(models_dir, '2600_model.json.bz2') |
| 50 |
|
| 51 |
for _ in ('download', 'already downloaded'): |
| 52 |
ret = download_models(models_dir=models_dir) |
| 53 |
self.assertIsInstance(ret, bool) |
| 54 |
if not ret: |
| 55 |
rmtree(tmp_dir, ignore_errors=True) |
| 56 |
self.skipTest( |
| 57 |
'download_models() failed, likely a transient network issue') |
| 58 |
|
| 59 |
self.assertExists(con_path) |
| 60 |
self.assertExists(sub_path) |
| 61 |
self.assertExists(ts_path) |
| 62 |
|
| 63 |
generated_file_names = generate_emails(5, tmp_dir, models_dir) |
| 64 |
|
| 65 |
self.assertEqual(len(generated_file_names), 5) |
| 66 |
|
| 67 |
for fn in generated_file_names: |
| 68 |
self.assertExists(fn) |
| 69 |
self.assertGreater(getsize(fn), 100) |
| 70 |
with open(fn) as f: |
| 71 |
contents = f.read() |
| 72 |
self.assertIn('To: ', contents) |
| 73 |
self.assertIn('From: ', contents) |
| 74 |
self.assertIn('Sent: ', contents) |
| 75 |
self.assertIn('Subject: ', contents) |
| 76 |
self.assertNotIn('base64', contents) |
| 77 |
|
| 78 |
generate_2600(5, tmp_dir, models_dir) |
| 79 |
|
| 80 |
rmtree(tmp_dir) |
| 81 |
|
| 82 |
@mock.patch('bleachbit.Network.download_url_to_fn') |
| 83 |
def test_download_models_fallback(self, mock_download): |
| 84 |
"""Test the fallback mechanism in download_models()""" |
| 85 |
tmp_dir = mkdtemp(prefix='bleachbit-chaff') |
| 86 |
|
| 87 |
# Test when primary download mirror fails but secondary succeeds. |
| 88 |
def succeed_on_second(*args, **_kwargs): |
| 89 |
host = urlparse(args[0]).hostname or '' |
| 90 |
if host == 'sourceforge.net': |
| 91 |
return False |
| 92 |
if host == 'download.bleachbit.org': |
| 93 |
return True |
| 94 |
return None |
| 95 |
mock_download.side_effect = succeed_on_second |
| 96 |
ret = download_models(models_dir=tmp_dir) |
| 97 |
self.assertTrue( |
| 98 |
ret, "download_models() return False, meaning a download error occurred") |
| 99 |
self.assertEqual(mock_download.call_count, 6) |
| 100 |
|
| 101 |
# Test when both primary and secondary download mirrors will fail. |
| 102 |
mock_download.reset_mock() |
| 103 |
mock_download.side_effect = None |
| 104 |
mock_download.return_value = False |
| 105 |
ret = download_models(models_dir=tmp_dir) |
| 106 |
self.assertFalse( |
| 107 |
ret, "download_models() should return False when any download fails") |
| 108 |
self.assertEqual(mock_download.call_count, 2) |
| 109 |
|
| 110 |
rmtree(tmp_dir) |
| 111 |
|
| 112 |
@mock.patch('bleachbit.Network.download_url_to_fn') |
| 113 |
def test_download_models_verifies_hash(self, mock_download): |
| 114 |
"""download_models() must pass the expected SHA-512 for each model""" |
| 115 |
tmp_dir = mkdtemp(prefix='bleachbit-chaff') |
| 116 |
mock_download.return_value = True |
| 117 |
download_models(models_dir=tmp_dir) |
| 118 |
hashes_passed = {call.kwargs.get('expected_sha512') |
| 119 |
for call in mock_download.call_args_list} |
| 120 |
self.assertEqual(hashes_passed, set(MODEL_SHA512.values())) |
| 121 |
|
| 122 |
rmtree(tmp_dir) |
| 123 |
|
| 124 |
def test_make_sentence_none(self): |
| 125 |
"""Test that make_sentence() returning None does not cause TypeError""" |
| 126 |
from bleachbit.Chaff import _get_random_content, _generate_2600_file, _generate_email |
| 127 |
|
| 128 |
model = mock.MagicMock() |
| 129 |
model.make_sentence.return_value = None |
| 130 |
model.make_short_sentence.return_value = None |
| 131 |
|
| 132 |
result = _get_random_content(model, number_of_sentences=2) |
| 133 |
self.assertIsNotNone(result) |
| 134 |
|
| 135 |
result = _generate_2600_file(model, number_of_sentences=2) |
| 136 |
self.assertIsInstance(result, str) |
| 137 |
|
| 138 |
subject_model = mock.MagicMock() |
| 139 |
subject_model.make_short_sentence.return_value = None |
| 140 |
msg = _generate_email(subject_model, model, number_of_sentences=2) |
| 141 |
self.assertEqual(msg['Subject'], '') |
| 142 |
|
| 143 |
@mock.patch('bleachbit.Network.download_url_to_fn') |
| 144 |
def test_have_models(self, mock_download): |
| 145 |
"""Test for function have_models()""" |
| 146 |
def fake_download(_url, fn, **_kwargs): |
| 147 |
with open(fn, 'wb') as f: |
| 148 |
f.write(b'') |
| 149 |
return True |
| 150 |
mock_download.side_effect = fake_download |
| 151 |
download_models() |
| 152 |
rc = have_models() |
| 153 |
self.assertTrue( |
| 154 |
rc, "have_models() should return True when models are present") |
| 155 |
self.assertIsInstance(rc, bool) |
| 156 |
for basename in MODEL_BASENAMES: |
| 157 |
fn = os.path.join(DEFAULT_MODELS_DIR, basename) |
| 158 |
self.assertExists(fn) |
| 159 |
os.remove(fn) |
| 160 |
self.assertFalse( |
| 161 |
have_models(), "have_models() should return False when any model is missing") |