test(wallpaper_changer): test behaviour for duplicate hashs
This commit is contained in:
1 parent
1cd2dde037
commit
ed812c354f
1 file changed
+117
-29
@@ -1,6 +1,8 @@
|
||||
import hashlib
|
||||
import re
|
||||
import shutil
|
||||
import unittest
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
@@ -88,19 +90,19 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
config = Config.load(args)
|
||||
config.img_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
for file in assets.glob("Foto_Berg01.*"):
|
||||
for file in (assets/"images").glob("Foto_Berg01.*"):
|
||||
shutil.copy(file, config.img_dir)
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.convert_to_jpg()
|
||||
images = image_repository.scan(config.private_key)
|
||||
|
||||
self.assertEqual(len(images), 4)
|
||||
self.assertEqual(len(images), 5)
|
||||
self.assertTrue(
|
||||
all(image["file"].suffix == ".jpg" for image in images)
|
||||
)
|
||||
|
||||
def test_check_encryption_ignore_same_hash(self) -> None:
|
||||
def test_check_encryption_ignore_same_hash_shorter_name(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[1] / "assets"
|
||||
@@ -108,48 +110,38 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
config = Config.load(args)
|
||||
new_image_path = assets / "Foto_Berg_Schneeberg01.jpg"
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
shutil.copy(new_image_path, config.img_dir)
|
||||
shutil.copy(assets / "Desktop1920x1080" / "Foto_Berg01.jpg", config.img_dir)
|
||||
shutil.copy(assets / "images" / "Foto_Berg_Schneeberg01.jpg", config.img_dir)
|
||||
shutil.copy(assets / "images"/ "Foto_Berg01.jpg", config.img_dir / "Foto01.jpg")
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.check_encryption(config.img_dir,config.private_key)
|
||||
image_repository.check_encryption(config.private_key)
|
||||
images = image_repository.scan(config.private_key)
|
||||
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 1)
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg_enc"), 9)
|
||||
|
||||
def test_check_encryption_replace_same_hash(self) -> None:
|
||||
def test_check_encryption_ignore_same_hash_equal_length_name(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[1] / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
config = Config.load(args)
|
||||
|
||||
new_image_name = "Foto_Berg_Schneeberg01.jpg"
|
||||
new_image_path = assets / new_image_name
|
||||
new_image_hash = hashlib.sha256(new_image_path.read_bytes()).hexdigest()
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
shutil.copy(new_image_path, config.img_dir)
|
||||
shutil.copy(assets / "Desktop1920x1080" / "Foto_Berg01.jpg", config.img_dir)
|
||||
shutil.copy(assets / "images" / "Foto_Berg_Schneeberg01.jpg", config.img_dir)
|
||||
shutil.copy(assets / "images"/ "Foto_Berg01.jpg", config.img_dir)
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
|
||||
image_repository.check_encryption(config.img_dir,config.private_key,replace=True)
|
||||
image_repository.check_encryption(config.private_key)
|
||||
images = image_repository.scan(config.private_key)
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 0)
|
||||
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 1)
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg_enc"), 9)
|
||||
|
||||
image_hash = next(
|
||||
item["hash"] for item in images if item["name"] == new_image_name
|
||||
)
|
||||
self.assertEqual(image_hash, new_image_hash)
|
||||
|
||||
def test_check_encryption_replace_same_name(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
@@ -159,7 +151,7 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
config = Config.load(args)
|
||||
|
||||
image_path = assets / "Foto_Berg04.jpg"
|
||||
image_path = assets / "images"/ "Foto_Berg04.jpg"
|
||||
new_image_name = "Foto_Berg03.jpg"
|
||||
new_image_hash = hashlib.sha256(image_path.read_bytes()).hexdigest()
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
@@ -172,7 +164,7 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
item["hash"] for item in images if item["name"] == new_image_name
|
||||
)
|
||||
|
||||
image_repository.check_encryption(config.img_dir, config.private_key, replace=True)
|
||||
image_repository.check_encryption( config.private_key, replace=True)
|
||||
images = image_repository.scan(config.private_key)
|
||||
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 0)
|
||||
@@ -183,6 +175,32 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
self.assertEqual(image_hash, new_image_hash)
|
||||
self.assertNotEqual(image_hash, old_image_hash)
|
||||
|
||||
def test_check_encryption_replace_same_hash_shorter_name(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[1] / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
config = Config.load(args)
|
||||
|
||||
image_path = assets / "images"/ "Foto_Berg01.jpg"
|
||||
new_image_name = "Foto_Berg_NiceBerg03.jpg"
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
shutil.copy(image_path, config.img_dir / new_image_name)
|
||||
new_image_hash = hashlib.sha256(((config.img_dir / new_image_name)).read_bytes()).hexdigest()
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
images = image_repository.scan(config.private_key)
|
||||
|
||||
image_repository.check_encryption(config.private_key)
|
||||
images = image_repository.scan(config.private_key)
|
||||
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 1)
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg_enc"), 8)
|
||||
self.assertTrue([image["name"] for image in images if image["hash"] == new_image_hash] == [new_image_name])
|
||||
|
||||
def test_check_encryption_set_unique_name(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
@@ -193,11 +211,11 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
shutil.copy(assets / "Foto_Berg04.jpg", config.img_dir)
|
||||
shutil.copy(assets / "images" / "Foto_Berg04.jpg", config.img_dir)
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
|
||||
image_repository.check_encryption(config.img_dir,config.private_key)
|
||||
image_repository.check_encryption(config.private_key)
|
||||
images = image_repository.scan(config.private_key)
|
||||
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 0)
|
||||
@@ -263,6 +281,76 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
images = ImageRepository(image_root/"New").scan(config.private_key)
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 2)
|
||||
|
||||
def test_check_consecutive_filenames(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[1] / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.check_consecutive_filenames(correct_wrong_numbers=True)
|
||||
images = image_repository.scan(config.private_key)
|
||||
groups = defaultdict(list)
|
||||
|
||||
for item in images:
|
||||
match = re.match(r"^(.*?)(\d+)$", item["file"].stem)
|
||||
if match:
|
||||
stem, number = match.groups()
|
||||
groups[stem].append(int(number))
|
||||
|
||||
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 8)
|
||||
self.assertEqual( [max(groups[key]) for key in groups],
|
||||
[len(groups[key]) for key in groups] )
|
||||
|
||||
class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
def test_scan_skips_unsupported_and_corrupt_files(self) -> None:
|
||||
"""Verify scanning ignores unsupported files and handles corrupt image files."""
|
||||
self.skipTest("TODO: add supported, unsupported, and corrupt file fixtures")
|
||||
|
||||
def test_scan_maps_nested_encrypted_images_from_naming_table(self) -> None:
|
||||
"""Verify nested encrypted files are matched to their naming-table entries."""
|
||||
self.skipTest("TODO: cover nested image paths and naming-table mapping")
|
||||
|
||||
def test_scan_with_encryption_disabled_includes_plain_jpeg_payloads_only(self) -> None:
|
||||
"""Verify plaintext JPEG payloads with an encrypted suffix are scanned safely."""
|
||||
self.skipTest("TODO: cover valid and encrypted .jpg_enc payloads")
|
||||
|
||||
def test_convert_to_jpg_handles_collisions_and_transparency(self) -> None:
|
||||
"""Verify conversion avoids overwrites and composites transparent pixels."""
|
||||
self.skipTest("TODO: cover destination collisions and alpha handling")
|
||||
|
||||
def test_convert_to_jpg_applies_exif_orientation(self) -> None:
|
||||
"""Verify converted images respect their EXIF orientation metadata."""
|
||||
self.skipTest("TODO: add an oriented image fixture and assert output dimensions")
|
||||
|
||||
def test_check_encryption_handles_missing_and_empty_new_folders(self) -> None:
|
||||
"""Verify encryption checking is a no-op for missing or empty input folders."""
|
||||
self.skipTest("TODO: cover missing and empty input folders")
|
||||
|
||||
def test_check_encryption_preserves_plaintext_on_encryption_failure(self) -> None:
|
||||
"""Verify failed encryption does not delete the original image."""
|
||||
self.skipTest("TODO: simulate encryption failure and assert source preservation")
|
||||
|
||||
def test_decode_image_rejects_hash_mismatch(self) -> None:
|
||||
"""Verify decrypted content with an unexpected hash is rejected."""
|
||||
self.skipTest("TODO: assert hash mismatch behavior")
|
||||
|
||||
def test_decode_all_handles_unmapped_encrypted_files(self) -> None:
|
||||
"""Verify decode-all behavior when an encrypted image has no table entry."""
|
||||
self.skipTest("TODO: cover missing naming-table entries")
|
||||
|
||||
def test_decode_operations_avoid_existing_destination_names(self) -> None:
|
||||
"""Verify decoded images do not overwrite existing files with the same name."""
|
||||
self.skipTest("TODO: cover destination name collisions")
|
||||
|
||||
def test_decode_all_preserves_inputs_when_decryption_fails(self) -> None:
|
||||
"""Verify failed decode-all leaves encrypted sources and the naming table intact."""
|
||||
self.skipTest("TODO: simulate decryption failure and assert source preservation")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user