| 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 RecognizeCleanerML |
| 9 |
""" |
| 10 |
|
| 11 |
import os |
| 12 |
from unittest import mock |
| 13 |
|
| 14 |
import bleachbit |
| 15 |
from tests import common |
| 16 |
from bleachbit.Options import options, path_to_option |
| 17 |
from bleachbit.RecognizeCleanerML import hashdigest, RecognizeCleanerML |
| 18 |
|
| 19 |
|
| 20 |
class RecognizeCleanerMLTestCase(common.BleachbitTestCase): |
| 21 |
"""Test case for RecognizeCleanerML""" |
| 22 |
|
| 23 |
def test_hash(self): |
| 24 |
"""Unit test for hash()""" |
| 25 |
digest = hashdigest('bleachbit') |
| 26 |
self.assertEqual(len(digest), 128) |
| 27 |
self.assertEqual(digest[1:10], '6382c203e') |
| 28 |
|
| 29 |
def test_init_without_salt(self): |
| 30 |
"""Unit test for __init__ when hashsalt is not set in options""" |
| 31 |
self.assertFalse(options.has_option('hashsalt')) |
| 32 |
# Create a CleanerML file so list_cleanerml_files() returns a file, |
| 33 |
# which exercises __recognized(). |
| 34 |
cleaner_dir = bleachbit.personal_cleaners_dir |
| 35 |
os.makedirs(cleaner_dir, exist_ok=True) |
| 36 |
cleaner_file = os.path.join(cleaner_dir, 'test_recognize.xml') |
| 37 |
try: |
| 38 |
with open(cleaner_file, 'w', encoding='utf-8') as f: |
| 39 |
f.write( |
| 40 |
'<?xml version="1.0"?><cleaner id="test"><label>Test</label></cleaner>') |
| 41 |
with mock.patch('bleachbit.RecognizeCleanerML.cleaner_change_dialog'): |
| 42 |
rcm = RecognizeCleanerML(parent_window=None) |
| 43 |
self.assertIsNotNone(rcm.salt) |
| 44 |
self.assertIsInstance(rcm.salt, str) |
| 45 |
self.assertEqual(len(rcm.salt), 128) |
| 46 |
# Verify __recognized() was exercised by checking the hash was stored. |
| 47 |
self.assertTrue(options.has_option( |
| 48 |
path_to_option(cleaner_file), 'hashpath')) |
| 49 |
finally: |
| 50 |
if os.path.exists(cleaner_file): |
| 51 |
os.remove(cleaner_file) |