356 lines
18 KiB
Python
356 lines
18 KiB
Python
import hashlib
|
|
import re
|
|
import shutil
|
|
import unittest
|
|
from collections import defaultdict
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from wallpaper_changer.argument_parser import parse_args
|
|
from wallpaper_changer.config import Config
|
|
from wallpaper_changer.image_repository import ImageRepository
|
|
|
|
|
|
class ImageRepositoryTests(unittest.TestCase):
|
|
def parse_args(self, *arguments: str):
|
|
return parse_args(list(arguments))
|
|
|
|
def test_scan_plaintext_folder_desktop(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,
|
|
)
|
|
images = ImageRepository(config.img_dir).scan(config.private_key,
|
|
disable_encryption=True)
|
|
|
|
self.assertEqual(len(images), 8)
|
|
self.assertTrue(
|
|
all(image["file"].parent.name == "Desktop1920x1080" for image in images)
|
|
)
|
|
self.assertTrue(
|
|
all(image["file"].suffix == ".jpg" for image in images)
|
|
)
|
|
|
|
def test_scan_plaintext_folder_corrupt(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)
|
|
shutil.copytree(assets / "Desktop1920x1080", image_root / "corrupt")
|
|
|
|
images = ImageRepository(config.img_dir).scan(config.private_key, disable_encryption=True)
|
|
self.assertEqual(len(images), 8)
|
|
self.assertTrue(
|
|
all(image["file"].suffix == ".jpg" for image in images)
|
|
)
|
|
self.assertTrue(
|
|
all(image["file"].parent.name == "corrupt" for image in images)
|
|
)
|
|
|
|
def test_scan_enc_folder_corrupt(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)
|
|
|
|
shutil.copytree(assets / "corrupt", image_root / "corrupt")
|
|
images = ImageRepository(config.img_dir).scan(config.private_key, disable_encryption=False)
|
|
self.assertEqual(len(images), 8)
|
|
self.assertTrue(
|
|
all(image["file"].suffix == ".jpg_enc" for image in images)
|
|
)
|
|
self.assertTrue(
|
|
all(image["hash"] != None for image in images)
|
|
)
|
|
self.assertTrue(
|
|
all(image["file"].parent.name == "corrupt" for image in images)
|
|
)
|
|
|
|
def test_convert_to_jpg(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)
|
|
config.img_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
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), 5)
|
|
self.assertTrue(
|
|
all(image["file"].suffix == ".jpg" for image in images)
|
|
)
|
|
|
|
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"
|
|
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)
|
|
|
|
shutil.copytree(assets / "corrupt", 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.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_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"))
|
|
config = Config.load(args)
|
|
|
|
shutil.copytree(assets / "corrupt", 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.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_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_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)
|
|
shutil.copy(image_path, config.img_dir / new_image_name)
|
|
|
|
image_repository = ImageRepository(config.img_dir)
|
|
images = image_repository.scan(config.private_key)
|
|
|
|
old_image_hash = next(
|
|
item["hash"] for item in images if item["name"] == new_image_name
|
|
)
|
|
|
|
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)
|
|
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg_enc"), 8)
|
|
image_hash = next(
|
|
item["hash"] for item in images if item["name"] == new_image_name
|
|
)
|
|
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)
|
|
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)
|
|
|
|
shutil.copytree(assets / "corrupt", config.img_dir)
|
|
shutil.copy(assets / "images" / "Foto_Berg04.jpg", config.img_dir)
|
|
|
|
image_repository = ImageRepository(config.img_dir)
|
|
|
|
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_enc"), 9)
|
|
self.assertTrue( image["name"] == "Foto_Berg05.jpg" for image in images if image["file"].name == "00009.jpg_enc")
|
|
|
|
def test_decode_all(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)
|
|
|
|
shutil.copytree(assets / "corrupt", config.img_dir)
|
|
|
|
image_repository = ImageRepository(config.img_dir)
|
|
image_repository.decode_all(config.private_key)
|
|
images = image_repository.scan(config.private_key)
|
|
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 8)
|
|
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg_enc"), 0)
|
|
|
|
def test_decode_argument(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)
|
|
|
|
shutil.copytree(assets / "corrupt", config.img_dir)
|
|
|
|
image_repository = ImageRepository(config.img_dir)
|
|
image_repository.decode_images(pattern="Kirchberg",
|
|
conf_dir=config.conf_dir,
|
|
private_key=config.private_key)
|
|
images = image_repository.scan(config.private_key)
|
|
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg_enc"), 8)
|
|
images = image_repository.scan(config.private_key,
|
|
img_dir=image_root/"New")
|
|
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg"), 2)
|
|
|
|
def test_decode_argument_remove(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)
|
|
|
|
shutil.copytree(assets / "corrupt", config.img_dir)
|
|
|
|
image_repository = ImageRepository(config.img_dir)
|
|
image_repository.decode_images(pattern="Kirchberg",
|
|
conf_dir=config.conf_dir,
|
|
private_key=config.private_key,
|
|
remove=True)
|
|
images = image_repository.scan(config.private_key)
|
|
self.assertEqual( sum(1 for image in images if image["file"].suffix == ".jpg_enc"), 6)
|
|
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() |