2026-11-13 11:28:11 +01:00
|
|
|
import hashlib
|
2026-11-13 11:01:16 +01:00
|
|
|
import shutil
|
|
|
|
|
import unittest
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from tempfile import TemporaryDirectory
|
|
|
|
|
|
|
|
|
|
from wallpaper_changer.argument_parser import parse_args
|
2026-11-13 11:28:11 +01:00
|
|
|
from wallpaper_changer.config import Config
|
|
|
|
|
from wallpaper_changer.image_repository import ImageRepository
|
2026-11-13 11:01:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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.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.assertTrue(
|
|
|
|
|
all(image["file"].suffix == ".jpg" for image in images)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_check_encryption_ignore_same_hash(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)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
image_repository = ImageRepository(config.img_dir)
|
|
|
|
|
image_repository.check_encryption(config.img_dir,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:
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
image_repository = ImageRepository(config.img_dir)
|
|
|
|
|
|
|
|
|
|
image_repository.check_encryption(config.img_dir,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"), 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)
|
|
|
|
|
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 / "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.img_dir, 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_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 / "Foto_Berg04.jpg", config.img_dir)
|
|
|
|
|
|
|
|
|
|
image_repository = ImageRepository(config.img_dir)
|
|
|
|
|
|
|
|
|
|
image_repository.check_encryption(config.img_dir,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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|