From 9aeac8f5df6b9253f703f76dfb7bdadb7443479b Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Sun, 15 Mar 2026 15:57:12 +0700 Subject: [PATCH] global_platform: make --install-parameters-* independently optional gen_install_parameters() had contradictory logic: the outer guard required all three arguments to be non-None/non-empty (making them mutually inclusive), while the inner checks then treated each one as optional. Make each parameter independently optional (defaulting to None) and remove the all-or-nothing guard from both the function and its caller. Simplify the function body to a straightforward single-pass construction of system_specific_params. Change-Id: I8756fb38016cdf0527fe2e21edb44381d1dc557f --- pySim/global_platform/__init__.py | 8 ++------ pySim/global_platform/install_param.py | 28 +++++++++++++------------- tests/unittests/test_globalplatform.py | 2 +- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/pySim/global_platform/__init__.py b/pySim/global_platform/__init__.py index 1c0d5219..8ef4092a 100644 --- a/pySim/global_platform/__init__.py +++ b/pySim/global_platform/__init__.py @@ -864,7 +864,7 @@ class ADF_SD(CardADF): help='JAVA-CARD CAP file to install') # Ideally, the parser should enforce that: # * either the `--install-parameters` is given alone, - # * or all `--install-parameters-*` are given together. + # * or distinct `--install-parameters-*` are given instead. # We tried to achieve this using mutually exclusive groups (add_mutually_exclusive_group). # However, group nesting was never supported, often failed to work correctly, and was unintentionally # exposed through inheritance. It has been deprecated since version 3.11, removed in version 3.14. @@ -898,11 +898,7 @@ class ADF_SD(CardADF): if opts.install_parameters is not None: install_parameters = opts.install_parameters; else: - # `--install-parameters-*` are mutually inclusive - if opts.install_parameters_non_volatile_memory_quota is None or \ - opts.install_parameters_volatile_memory_quota is None or \ - opts.install_parameters_stk is None: - raise ValueError("Either --install-parameters alone, or all --install-parameters-* must be specified") + # `--install-parameters-*` are all optional install_parameters = gen_install_parameters(opts.install_parameters_non_volatile_memory_quota, opts.install_parameters_volatile_memory_quota, opts.install_parameters_stk) diff --git a/pySim/global_platform/install_param.py b/pySim/global_platform/install_param.py index 89e6c7f0..fc96792c 100644 --- a/pySim/global_platform/install_param.py +++ b/pySim/global_platform/install_param.py @@ -17,6 +17,8 @@ # along with this program. If not, see . # +from typing import Optional + from osmocom.construct import * from osmocom.utils import * from osmocom.tlv import * @@ -46,7 +48,9 @@ class InstallParams(TLV_IE_Collection, nested=[AppSpecificParams, SystemSpecific # GPD_SPE_013, table 11-49 pass -def gen_install_parameters(non_volatile_memory_quota:int, volatile_memory_quota:int, stk_parameter:str): +def gen_install_parameters(non_volatile_memory_quota: Optional[int] = None, + volatile_memory_quota: Optional[int] = None, + stk_parameter: Optional[str] = None): # GPD_SPE_013, table 11-49 @@ -54,19 +58,15 @@ def gen_install_parameters(non_volatile_memory_quota:int, volatile_memory_quota: install_params = InstallParams() install_params_dict = [{'app_specific_params': None}] - #Conditional - if non_volatile_memory_quota and volatile_memory_quota and stk_parameter: - system_specific_params = [] - #Optional - if non_volatile_memory_quota: - system_specific_params += [{'non_volatile_memory_quota': non_volatile_memory_quota}] - #Optional - if volatile_memory_quota: - system_specific_params += [{'volatile_memory_quota': volatile_memory_quota}] - #Optional - if stk_parameter: - system_specific_params += [{'stk_parameter': stk_parameter}] - install_params_dict += [{'system_specific_params': system_specific_params}] + system_specific_params = [] + if non_volatile_memory_quota is not None: + system_specific_params.append({'non_volatile_memory_quota': non_volatile_memory_quota}) + if volatile_memory_quota is not None: + system_specific_params.append({'volatile_memory_quota': volatile_memory_quota}) + if stk_parameter is not None: + system_specific_params.append({'stk_parameter': stk_parameter}) + if system_specific_params: + install_params_dict.append({'system_specific_params': system_specific_params}) install_params.from_dict(install_params_dict) return b2h(install_params.to_bytes()) diff --git a/tests/unittests/test_globalplatform.py b/tests/unittests/test_globalplatform.py index 069886cf..8698470d 100644 --- a/tests/unittests/test_globalplatform.py +++ b/tests/unittests/test_globalplatform.py @@ -295,7 +295,7 @@ class Install_param_Test(unittest.TestCase): load_parameters = gen_install_parameters(256, 256, '010001001505000000000000000000000000') self.assertEqual(load_parameters, 'c900ef1cc8020100c7020100ca12010001001505000000000000000000000000') - load_parameters = gen_install_parameters(None, None, '') + load_parameters = gen_install_parameters() self.assertEqual(load_parameters, 'c900') if __name__ == "__main__":