Die XML-Dateien haben nun hashsummen in Projekt-Datei
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test-Skript für die blake2b-Hash-Implementierung.
|
||||
Testet die Hash-Berechnung für XML-Dateien.
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Füge src-Verzeichnis zum Python-Pfad hinzu
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
|
||||
|
||||
from conf import XmlFile
|
||||
|
||||
def test_blake2b_hash_calculation():
|
||||
"""Testet die blake2b-Hash-Berechnung."""
|
||||
print("=== Test: blake2b-Hash-Berechnung ===")
|
||||
|
||||
# Erstelle temporäre XML-Testdatei
|
||||
test_xml_content = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<root>
|
||||
<element>Test-Inhalt für Hash-Berechnung</element>
|
||||
<data>Weitere Testdaten</data>
|
||||
</root>"""
|
||||
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.xml', delete=False, encoding='utf-8') as f:
|
||||
f.write(test_xml_content)
|
||||
temp_xml_path = Path(f.name)
|
||||
|
||||
try:
|
||||
# Berechne Hash manuell
|
||||
with open(temp_xml_path, 'rb') as f:
|
||||
file_content = f.read()
|
||||
hash_obj = hashlib.blake2b(file_content)
|
||||
expected_hash = f"blake2b:{hash_obj.hexdigest()}"
|
||||
|
||||
print(f"Temporäre XML-Datei: {temp_xml_path}")
|
||||
print(f"Erwarteter Hash: {expected_hash}")
|
||||
|
||||
# Teste XmlFile-Objekt
|
||||
xml_file = XmlFile(xml=temp_xml_path)
|
||||
print(f"XmlFile erstellt: {xml_file.xml}")
|
||||
print(f"Initiale hashsum: {xml_file.hashsum}")
|
||||
|
||||
# Simuliere Hash-Berechnung
|
||||
xml_file.hashsum = expected_hash
|
||||
print(f"Hash gesetzt: {xml_file.hashsum}")
|
||||
|
||||
# Verifikation
|
||||
assert xml_file.hashsum == expected_hash, "Hash-Wert stimmt nicht überein!"
|
||||
assert xml_file.hashsum.startswith("blake2b:"), "Hash-Präfix fehlt!"
|
||||
|
||||
print("[OK] Test erfolgreich: Hash-Berechnung funktioniert korrekt")
|
||||
|
||||
finally:
|
||||
# Aufräumen
|
||||
if temp_xml_path.exists():
|
||||
temp_xml_path.unlink()
|
||||
|
||||
def test_xml_file_model():
|
||||
"""Testet das XmlFile-Modell."""
|
||||
print("\n=== Test: XmlFile-Modell ===")
|
||||
|
||||
# Test 1: XmlFile ohne hashsum
|
||||
xml_file1 = XmlFile(xml=Path("test.xml"))
|
||||
print(f"XmlFile ohne hashsum: {xml_file1}")
|
||||
print(f"hashsum ist None: {xml_file1.hashsum is None}")
|
||||
|
||||
# Test 2: XmlFile mit hashsum
|
||||
test_hash = "blake2b:1234567890abcdef"
|
||||
xml_file2 = XmlFile(xml=Path("test2.xml"), hashsum=test_hash)
|
||||
print(f"XmlFile mit hashsum: {xml_file2}")
|
||||
print(f"hashsum: {xml_file2.hashsum}")
|
||||
|
||||
# Verifikation
|
||||
assert xml_file1.hashsum is None, "Initiale hashsum sollte None sein"
|
||||
assert xml_file2.hashsum == test_hash, "Gesetzte hashsum stimmt nicht überein"
|
||||
|
||||
print("[OK] Test erfolgreich: XmlFile-Modell funktioniert korrekt")
|
||||
|
||||
def test_hash_consistency():
|
||||
"""Testet die Konsistenz der Hash-Berechnung."""
|
||||
print("\n=== Test: Hash-Konsistenz ===")
|
||||
|
||||
test_content = b"<xml>Test content for consistency check</xml>"
|
||||
|
||||
# Berechne Hash mehrmals
|
||||
hash1 = hashlib.blake2b(test_content).hexdigest()
|
||||
hash2 = hashlib.blake2b(test_content).hexdigest()
|
||||
hash3 = hashlib.blake2b(test_content).hexdigest()
|
||||
|
||||
print(f"Hash 1: {hash1}")
|
||||
print(f"Hash 2: {hash2}")
|
||||
print(f"Hash 3: {hash3}")
|
||||
|
||||
# Verifikation
|
||||
assert hash1 == hash2 == hash3, "Hash-Werte sind nicht konsistent!"
|
||||
|
||||
# Test mit unterschiedlichem Inhalt
|
||||
different_content = b"<xml>Different content</xml>"
|
||||
hash_different = hashlib.blake2b(different_content).hexdigest()
|
||||
|
||||
print(f"Hash für anderen Inhalt: {hash_different}")
|
||||
assert hash1 != hash_different, "Verschiedene Inhalte sollten verschiedene Hashes haben!"
|
||||
|
||||
print("[OK] Test erfolgreich: Hash-Berechnung ist konsistent")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Starte Tests für blake2b-Hash-Implementierung...\n")
|
||||
|
||||
try:
|
||||
test_blake2b_hash_calculation()
|
||||
test_xml_file_model()
|
||||
test_hash_consistency()
|
||||
|
||||
print("\n[SUCCESS] Alle Tests erfolgreich abgeschlossen!")
|
||||
print("\nDie blake2b-Hash-Implementierung ist bereit für den Einsatz.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n[ERROR] Test fehlgeschlagen: {e}")
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user