mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-16 18:38:32 +03:00
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
This commit is contained in:
@@ -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 @@ class ConstantSource(ParamSource):
|
||||
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'
|
||||
@@ -100,6 +122,8 @@ class RandomDigitSource(ParamSource):
|
||||
|
||||
@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()
|
||||
@@ -112,7 +136,7 @@ class RandomDigitSource(ParamSource):
|
||||
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'
|
||||
@@ -133,6 +157,7 @@ class RandomHexDigitSource(ParamSource):
|
||||
|
||||
@classmethod
|
||||
def from_str(cls, s:str):
|
||||
s = cls.expand_str(s)
|
||||
return cls(num_digits=len(s.strip()))
|
||||
|
||||
class IncDigitSource(RandomDigitSource):
|
||||
|
||||
@@ -430,7 +430,7 @@ class Iccid(DecimalParam):
|
||||
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 SdKey(BinaryParam):
|
||||
class SdKeyAes(SdKey):
|
||||
key_type = KeyType.aes
|
||||
allow_len = (16,24,32)
|
||||
default_value = '00' * 32
|
||||
default_value = '00*32'
|
||||
|
||||
|
||||
class SdKeyScp80(SdKeyAes):
|
||||
@@ -873,7 +873,7 @@ class Puk(DecimalHexParam):
|
||||
allow_len = 8
|
||||
rpad = 16
|
||||
keyReference = None
|
||||
default_value = '0' * allow_len
|
||||
default_value = f'0*{allow_len}'
|
||||
default_source = param_source.RandomDigitSource
|
||||
|
||||
@classmethod
|
||||
@@ -911,7 +911,7 @@ class Pin(DecimalHexParam):
|
||||
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
|
||||
|
||||
@@ -952,7 +952,7 @@ class Pin(DecimalHexParam):
|
||||
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):
|
||||
@@ -1057,7 +1057,7 @@ class K(BinaryParam, AlgoConfig):
|
||||
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):
|
||||
|
||||
Reference in New Issue
Block a user