neels has uploaded this change for review. (
https://gerrit.osmocom.org/c/pysim/+/40207?usp=email )
Change subject: param_source: allow input val expansion like '0 * 32'
......................................................................
param_source: allow input val expansion like '0 * 32'
Working with keys, we often generate 4, 8, 16, 32 digit wide random
values. Those then typically have default input values like
00000000000000000000000000000000
it is hard for humans to count the number of digits. Much easier:
00*16
Teach the ParamSource subclasses dealing with random values to
understand an expansion like this. Any expansion is carried out before
all other input value handling.
Use this expansion also in the default_value of ConfigurableParameter
subclasses that have a default_source pointing at a ParamSource that now
understand this expansion.
Related: SYS#6768
Change-Id: Ie7171c152a7b478736f8825050305606b5af5735
---
M pySim/esim/saip/param_source.py
M pySim/esim/saip/personalization.py
2 files changed, 33 insertions(+), 8 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/07/40207/1
diff --git a/pySim/esim/saip/param_source.py b/pySim/esim/saip/param_source.py
index 407882d..7a039a3 100644
--- a/pySim/esim/saip/param_source.py
+++ b/pySim/esim/saip/param_source.py
@@ -18,6 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import random
+import re
from pySim.utils import all_subclasses_of
class ParamSourceExn(Exception):
@@ -70,7 +71,28 @@
def get_next(self, csv_row:dict=None):
return self.val
-class RandomDigitSource(ParamSource):
+class InputExpandingParamSource(ParamSource):
+
+ @classmethod
+ def expand_str(cls, s:str):
+ # user convenience syntax '0*32' becomes
'00000000000000000000000000000000'
+ if '*' not in s:
+ return s
+ tokens = re.split(r"([^ \t]+)[ \t]*\*[ \t]*([0-9]+)", s)
+ if len(tokens) < 3:
+ return s
+ parts = []
+ for unchanged, snippet, repeat_str in zip(tokens[0::3], tokens[1::3],
tokens[2::3]):
+ parts.append(unchanged)
+ repeat = int(repeat_str)
+ parts.append(snippet * repeat)
+ return ''.join(parts)
+
+ @classmethod
+ def from_str(cls, s:str):
+ return cls(cls.expand_str(s))
+
+class RandomDigitSource(InputExpandingParamSource):
'return a different sequence of random decimal digits each'
is_abstract = False
name = 'random decimal digits'
@@ -98,6 +120,8 @@
@classmethod
def from_str(cls, s:str):
+ s = cls.expand_str(s)
+
if '..' in s:
first_str, last_str = s.split('..')
first_str = first_str.strip()
@@ -110,7 +134,7 @@
last_value = int(last_str) if last_str is not None else '9' *
len(first_str)
return cls(num_digits=len(first_str), first_value=first_value,
last_value=last_value)
-class RandomHexDigitSource(ParamSource):
+class RandomHexDigitSource(InputExpandingParamSource):
'return a different sequence of random hexadecimal digits each'
is_abstract = False
name = 'random hexadecimal digits'
@@ -131,6 +155,7 @@
@classmethod
def from_str(cls, s:str):
+ s = cls.expand_str(s)
return cls(num_digits=len(s.strip()))
class IncDigitSource(RandomDigitSource):
diff --git a/pySim/esim/saip/personalization.py b/pySim/esim/saip/personalization.py
index 78ea5a8..7217af3 100644
--- a/pySim/esim/saip/personalization.py
+++ b/pySim/esim/saip/personalization.py
@@ -429,7 +429,7 @@
name = 'ICCID'
min_len = 18
max_len = 20
- default_value = '0' * 18
+ default_value = '0*18'
default_source = param_source.IncDigitSource
@classmethod
@@ -581,7 +581,7 @@
class SdKeyAes(SdKey):
key_type = KeyType.aes
allow_len = (16,24,32)
- default_value = '00' * 32
+ default_value = '00*32'
class SdKeyScp80(SdKeyAes):
@@ -868,7 +868,7 @@
allow_len = 8
rpad = 16
keyReference = None
- default_value = '0' * allow_len
+ default_value = f'0*{allow_len}'
default_source = param_source.RandomDigitSource
@classmethod
@@ -906,7 +906,7 @@
rpad = 16
min_len = 4
max_len = 8
- default_value = '0' * max_len
+ default_value = f'0*{max_len}'
default_source = param_source.RandomDigitSource
keyReference = None
@@ -947,7 +947,7 @@
class Pin1(Pin):
is_abstract = False
name = 'PIN1'
- default_value = '0' * 4 # PIN are usually 4 digits
+ default_value = '0*4' # PIN are usually 4 digits
keyReference = 0x01
class Pin2(Pin1):
@@ -1052,7 +1052,7 @@
name = 'K'
algo_config_key = 'key'
allow_len = int(128/8) # length in bytes (from BinaryParam)
- default_value = '00' * allow_len
+ default_value = f'00*{allow_len}'
default_source = param_source.RandomHexDigitSource
class Opc(K):
--
To view, visit
https://gerrit.osmocom.org/c/pysim/+/40207?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie7171c152a7b478736f8825050305606b5af5735
Gerrit-Change-Number: 40207
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>