laforge submitted this change.

View Change

Approvals: Jenkins Builder: Verified laforge: Looks good to me, approved
global_platform: refactor gen_install_parameters()

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 check. Simplify the function body to a
straightforward single-pass construction of system_specific_params.

Change-Id: I8756fb38016cdf0527fe2e21edb44381d1dc557f
---
M pySim/global_platform/install_param.py
M tests/unittests/test_globalplatform.py
2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/pySim/global_platform/install_param.py b/pySim/global_platform/install_param.py
index 89e6c7f..70c63b8 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 <http://www.gnu.org/licenses/>.
#

+from typing import Optional
+
from osmocom.construct import *
from osmocom.utils import *
from osmocom.tlv import *
@@ -46,7 +48,9 @@
# 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,17 @@
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}]
+ # Collect system specific parameters (optional)
+ 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})
+ # Add system specific parameters to the install parameters, if any
+ 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 069886c..8698470 100644
--- a/tests/unittests/test_globalplatform.py
+++ b/tests/unittests/test_globalplatform.py
@@ -295,7 +295,7 @@
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__":

To view, visit change 42378. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I8756fb38016cdf0527fe2e21edb44381d1dc557f
Gerrit-Change-Number: 42378
Gerrit-PatchSet: 6
Gerrit-Owner: fixeria <vyanitskiy@sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier@sysmocom.de>
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-Reviewer: osmith <osmith@sysmocom.de>