Update gcmtest.py

This commit is contained in:
tmpz23 2022-01-20 20:35:19 +01:00 committed by GitHub
parent 0a02901f6e
commit 5bf032459c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,11 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from pathlib import Path from pathlib import Path
import hashlib
import os import os
import shutil import shutil
from time import time
__version__ = "0.0.4" __version__ = "0.0.5"
__author__ = "rigodron, algoflash, GGLinnk" __author__ = "rigodron, algoflash, GGLinnk"
__license__ = "MIT" __license__ = "MIT"
__status__ = "developpement" __status__ = "developpement"
@ -42,15 +42,26 @@ def print_paths_differences(folder1_paths:list, folder2_paths:list):
print(path) print(path)
# compare two files sha256 # compare two files
def compare_sha256(file1_path:Path, file2_path:Path): def compare_files(file1_path:Path, file2_path:Path):
return hashlib.sha256( file1_path.read_bytes() ).hexdigest() == hashlib.sha256( file2_path.read_bytes() ).hexdigest() CLUSTER_LEN = 131072
with file1_path.open("rb") as file1, file2_path.open("rb") as file2:
# Init
bytes1 = file1.read(CLUSTER_LEN)
bytes2 = file2.read(CLUSTER_LEN)
while bytes1 or bytes2: # continue if bytes1 and bytes2 have a len > 0
if bytes1 != bytes2:
return False
bytes1 = file1.read(CLUSTER_LEN)
bytes2 = file2.read(CLUSTER_LEN)
return True
# compare two GCM # compare two GCM
# -> raise an exception if there is a difference # -> raise an exception if there is a difference
def verify_GCM_sha256(folder1: Path, folder2: Path): def compare_GCM(folder1: Path, folder2: Path):
folder1_file_count = len(list(folder1.glob("**/*"))) folder1_root_paths = list((folder1 / "root").glob("**/*"))
folder1_file_count = len(folder1_root_paths)
print(f"compare \"{folder1}\" - \"{folder2}\" ({folder1_file_count} files)") print(f"compare \"{folder1}\" - \"{folder2}\" ({folder1_file_count} files)")
if folder1_file_count == 0: if folder1_file_count == 0:
raise Exception(f"ERROR - EMPTY FOLDER: {folder1}") raise Exception(f"ERROR - EMPTY FOLDER: {folder1}")
@ -58,24 +69,39 @@ def verify_GCM_sha256(folder1: Path, folder2: Path):
len1 = len(folder1.parts) len1 = len(folder1.parts)
len2 = len(folder2.parts) len2 = len(folder2.parts)
# 1. Compare names of filesystems # 1. Compare names of filesystems
folder1_paths = [Path(*path.parts[len1:]) for path in (folder1/"root").glob('**/*')] folder1_paths = [Path(*path.parts[len1:]) for path in folder1_root_paths]
folder2_paths = [Path(*path.parts[len2:]) for path in (folder2 / "root").glob('**/*')] folder2_paths = [Path(*path.parts[len2:]) for path in (folder2 / "root").glob('**/*')]
if folder1_paths != folder2_paths: if folder1_paths != folder2_paths:
print_paths_differences(folder1_paths, folder2_paths) print_paths_differences(folder1_paths, folder2_paths)
raise Exception(f"Folders \"{folder1}\" and \"{folder2}\" are different (not the same folders or files names).") raise Exception(f"Folders \"{folder1}\" and \"{folder2}\" are different (not the same folders or files names).")
# 2. Compare sys files content # 2. Compare sys files content
if not compare_sha256(folder1/"sys"/"apploader.img", folder2/"sys"/"apploader.img"): if not compare_files(folder1/"sys"/"apploader.img", folder2/"sys"/"apploader.img"):
raise Exception(f"\"{folder1}/sys/apploader.bin\" and \"{folder2}/sys/apploader.bin\" are different.") raise Exception(f"\"{folder1}/sys/apploader.bin\" and \"{folder2}/sys/apploader.bin\" are different.")
if not compare_sha256(folder1/"sys"/"boot.dol", folder2/"sys"/"boot.dol"): if not compare_files(folder1/"sys"/"boot.dol", folder2/"sys"/"boot.dol"):
raise Exception(f"\"{folder1}/sys/boot.bin\" and \"{folder2}/sys/boot.bin\" are different.") raise Exception(f"\"{folder1}/sys/boot.bin\" and \"{folder2}/sys/boot.bin\" are different.")
for path1 in (folder1/"root").glob('**/*'): for path1 in folder1_root_paths:
if path1.is_file(): if path1.is_file():
path2 = folder2/Path(*path1.parts[len1:]) path2 = folder2/Path(*path1.parts[len1:])
if not compare_sha256(path1, path2): if not compare_files(path1, path2):
raise Exception(f"\"{path1}/\" and \"{path2}\" are different.") raise Exception(f"\"{path1}/\" and \"{path2}\" are different.")
##################################################
# gcmtool.py commands wrappers
##################################################
def gcmtool_unpack(iso_path:Path, folder_path:Path):
if os.system(f"python gcmtool.py -u \"{iso_path}\" \"{folder_path}\"") != 0:
raise Exception("Error while unpacking gcm.")
def gcmtool_pack(folder_path:Path, iso_path:Path):
if os.system(f"python gcmtool.py -p \"{folder_path}\" \"{iso_path}\"") != 0:
raise Exception("Error while packing gcm.")
def gcmtool_rebuild_fst(folder_path:Path):
if os.system(f"python gcmtool.py -r \"{folder_path}\"") != 0:
raise Exception("Error while rebuilding FST.")
start = time()
print("###############################################################################") print("###############################################################################")
print("# Checking tests folder") print("# Checking tests folder")
print("###############################################################################") print("###############################################################################")
@ -91,12 +117,11 @@ print("#########################################################################
unpack_path.mkdir(parents=True) unpack_path.mkdir(parents=True)
for iso_path in roms_path.glob("*"): for iso_path in roms_path.glob("*"):
if iso_path.is_file(): if iso_path.is_file():
if os.system(f"python gcmtool.py -u \"{iso_path}\" \"{unpack_path}/{iso_path.name}\"") != 0: gcmtool_unpack(iso_path, unpack_path / iso_path.name)
raise Exception("Error while unpacking original gcm.")
# compare unpack_path dolphin_unpack_path # compare unpack_path dolphin_unpack_path
for folder_path in unpack_path.glob("*"): for folder_path in unpack_path.glob("*"):
verify_GCM_sha256(folder_path, dolphin_unpack_path / folder_path.name) compare_GCM(folder_path, dolphin_unpack_path / folder_path.name)
print("###############################################################################") print("###############################################################################")
print("# TEST 2/3") print("# TEST 2/3")
@ -104,22 +129,21 @@ print("# Comparing [unpack_path]->pack->unpack->[unpack2_path]")
print("###############################################################################") print("###############################################################################")
# repack unpack_path repack_path # repack unpack_path repack_path
repack_path.mkdir() repack_path.mkdir()
for folder_path in unpack_path.glob("*"): unpack_paths = list(unpack_path.glob("*"))
if os.system(f"python gcmtool.py -p \"{folder_path}\" \"{repack_path}/{folder_path.name}\"") != 0: for folder_path in unpack_paths:
raise Exception("Error while packing unpacked gcm.") gcmtool_pack(folder_path, repack_path / folder_path.name)
# unpack repack_path unpack2_path # unpack repack_path unpack2_path
unpack2_path.mkdir() unpack2_path.mkdir()
for iso_path in repack_path.glob("*"): for iso_path in repack_path.glob("*"):
if os.system(f"python gcmtool.py -u \"{iso_path}\" \"{unpack2_path}/{iso_path.name}\"") != 0: gcmtool_unpack(iso_path, unpack2_path / iso_path.name)
raise Exception("Error while unpacking repacked gcm.")
# remove repack_path # remove repack_path
shutil.rmtree(repack_path) shutil.rmtree(repack_path)
# compare unpack_path unpack2_path # compare unpack_path unpack2_path
for folder_path in unpack_path.glob("*"): for folder_path in unpack_paths:
verify_GCM_sha256(folder_path, unpack2_path / folder_path.name) compare_GCM(folder_path, unpack2_path / folder_path.name)
# remove unpack2_path # remove unpack2_path
shutil.rmtree(unpack2_path) shutil.rmtree(unpack2_path)
@ -129,28 +153,26 @@ print("# TEST 3/3")
print("# Comparing [unpack_path]->rebuild_fst->repack->unpack->[unpack2_path]") print("# Comparing [unpack_path]->rebuild_fst->repack->unpack->[unpack2_path]")
print("###############################################################################") print("###############################################################################")
# rebuild unpack_path FSTs # rebuild unpack_path FSTs
for folder_path in unpack_path.glob("*"): unpack_paths = list(unpack_path.glob("*"))
if os.system(f"python gcmtool.py -r \"{folder_path}\"") != 0: for folder_path in unpack_paths:
raise Exception("Error while rebuilding FST.") gcmtool_rebuild_fst(folder_path)
# repack unpack_path repack_path # repack unpack_path repack_path
repack_path.mkdir() repack_path.mkdir()
for folder_path in unpack_path.glob("*"): for folder_path in unpack_paths:
if os.system(f"python gcmtool.py -p \"{folder_path}\" \"{repack_path}/{folder_path.name}\"") != 0: gcmtool_pack(folder_path, repack_path / folder_path.name)
raise Exception("Error while packing gcm with new FST.")
# unpack repack_path unpack2_path # unpack repack_path unpack2_path
unpack2_path.mkdir() unpack2_path.mkdir()
for iso_path in repack_path.glob("*"): for iso_path in repack_path.glob("*"):
if os.system(f"python gcmtool.py -u \"{iso_path}\" \"{unpack2_path}/{iso_path.name}\"") != 0: gcmtool_unpack(iso_path, unpack2_path / iso_path.name)
raise Exception("Error while unpacking repacked gcm with new FST.")
# remove repack_path # remove repack_path
shutil.rmtree(repack_path) shutil.rmtree(repack_path)
# compare unpack_path unpack2_path # compare unpack_path unpack2_path
for folder_path in unpack_path.glob("*"): for folder_path in unpack_path.glob("*"):
verify_GCM_sha256(folder_path, unpack2_path / folder_path.name) compare_GCM(folder_path, unpack2_path / folder_path.name)
# Remove tests folders # Remove tests folders
print("###############################################################################") print("###############################################################################")
@ -159,6 +181,7 @@ print("#########################################################################
shutil.rmtree(unpack_path) shutil.rmtree(unpack_path)
shutil.rmtree(unpack2_path) shutil.rmtree(unpack2_path)
end = time()
print("###############################################################################") print("###############################################################################")
print("# All tests are OK") print(f"# All tests are OK - elapsed time : {end - start}")
print("###############################################################################") print("###############################################################################")