-
-
Notifications
You must be signed in to change notification settings - Fork 0
🧹 Refactor build_ncch_args_contentid to reduce nesting #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,67 @@ | ||||||||||||||||||||||||||||
| #!/usr/bin/env python3 | ||||||||||||||||||||||||||||
| import importlib.util | ||||||||||||||||||||||||||||
| import unittest | ||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||
| import tempfile | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Dynamically import cia_3ds_decryptor.py | ||||||||||||||||||||||||||||
| file_path = Path(__file__).parent / "cia_3ds_decryptor.py" | ||||||||||||||||||||||||||||
| spec = importlib.util.spec_from_file_location("cia_3ds_decryptor", str(file_path)) | ||||||||||||||||||||||||||||
| if spec is None: | ||||||||||||||||||||||||||||
| raise ImportError(f"Could not load {file_path}") | ||||||||||||||||||||||||||||
| decryptor = importlib.util.module_from_spec(spec) | ||||||||||||||||||||||||||||
| sys.modules["cia_3ds_decryptor"] = decryptor | ||||||||||||||||||||||||||||
| spec.loader.exec_module(decryptor) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class TestDecryptorContentId(unittest.TestCase): | ||||||||||||||||||||||||||||
| def setUp(self): | ||||||||||||||||||||||||||||
| self.temp_dir = tempfile.TemporaryDirectory() | ||||||||||||||||||||||||||||
| self.bin_dir = Path(self.temp_dir.name) | ||||||||||||||||||||||||||||
| self.content_txt = self.bin_dir / "content.txt" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def tearDown(self): | ||||||||||||||||||||||||||||
| self.temp_dir.cleanup() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_extract_content_ids_no_file(self): | ||||||||||||||||||||||||||||
|
Check notice on line 26 in Cachyos/Scripts/WIP/emu/test_decryptor_contentid.py
|
||||||||||||||||||||||||||||
| self.assertEqual(decryptor._extract_content_ids(self.content_txt), []) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_extract_content_ids_valid_file(self): | ||||||||||||||||||||||||||||
|
Check notice on line 29 in Cachyos/Scripts/WIP/emu/test_decryptor_contentid.py
|
||||||||||||||||||||||||||||
| content = ( | ||||||||||||||||||||||||||||
| "ContentId: 00000001\n" | ||||||||||||||||||||||||||||
| "Some other line\n" | ||||||||||||||||||||||||||||
| "ContentId: 00000002 \n" | ||||||||||||||||||||||||||||
| "ContentId:\n" # Invalid, empty after split | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good set of test cases. To make it even more robust, I'd suggest adding a case with a malformed hex value for the ContentID. This would ensure the parser can gracefully handle such errors without crashing, which it currently does not. This test will fail until the corresponding error handling is added to
Suggested change
|
||||||||||||||||||||||||||||
| self.content_txt.write_text(content) | ||||||||||||||||||||||||||||
| expected = [1, 2] | ||||||||||||||||||||||||||||
| self.assertEqual(decryptor._extract_content_ids(self.content_txt), expected) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_build_ncch_args_contentid(self): | ||||||||||||||||||||||||||||
| content = ( | ||||||||||||||||||||||||||||
| "ContentId: 0000000A\n" | ||||||||||||||||||||||||||||
| "ContentId: 0000000B\n" | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| self.content_txt.write_text(content) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Create some fake ncch files | ||||||||||||||||||||||||||||
| (self.bin_dir / "tmp.0.ncch").touch() | ||||||||||||||||||||||||||||
| (self.bin_dir / "tmp.1.ncch").touch() | ||||||||||||||||||||||||||||
| (self.bin_dir / "tmp.2.ncch").touch() # More files than IDs | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ncch0 = self.bin_dir / "tmp.0.ncch" | ||||||||||||||||||||||||||||
| ncch1 = self.bin_dir / "tmp.1.ncch" | ||||||||||||||||||||||||||||
| ncch2 = self.bin_dir / "tmp.2.ncch" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| args = decryptor.build_ncch_args_contentid(self.bin_dir, self.content_txt) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Expect content ids 10 and 11, and fallback to 2 for the last one | ||||||||||||||||||||||||||||
| expected_parts = [ | ||||||||||||||||||||||||||||
| f'-i "{ncch0}:0:10"', | ||||||||||||||||||||||||||||
| f'-i "{ncch1}:1:11"', | ||||||||||||||||||||||||||||
| f'-i "{ncch2}:2:2"', | ||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||
| self.assertEqual(args, " ".join(expected_parts)) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||||||||
| unittest.main() | ||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The call to
int(cid, 16)is not wrapped in atry...exceptblock. Ifcontent.txtcontains a malformed ContentID (e.g.,ContentId: not-a-hex-value), this will raise aValueErrorand crash the script. It's safer to handle this potential error to make the parsing more robust.