mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-16 18:38:32 +03:00
param_source: allow plugging a random implementation (for testing)
Change-Id: Idce2b18af70c17844d6f09f7704efc869456ac39
This commit is contained in:
@@ -120,15 +120,18 @@ class DecimalRangeSource(InputExpandingParamSource):
|
|||||||
last_value = int(last_str) if last_str is not None else "9" * len(first_str)
|
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)
|
return cls(num_digits=len(first_str), first_value=first_value, last_value=last_value)
|
||||||
|
|
||||||
class RandomDigitSource(DecimalRangeSource):
|
class RandomSourceMixin:
|
||||||
|
random_impl = random # TODO secure random source?
|
||||||
|
|
||||||
|
class RandomDigitSource(DecimalRangeSource, RandomSourceMixin):
|
||||||
"""return a different sequence of random decimal digits each"""
|
"""return a different sequence of random decimal digits each"""
|
||||||
name = "random decimal digits"
|
name = "random decimal digits"
|
||||||
|
|
||||||
def get_next(self, csv_row:dict=None):
|
def get_next(self, csv_row:dict=None):
|
||||||
val = random.randint(*self.val_first_last) # TODO secure random source?
|
val = self.random_impl.randint(*self.val_first_last)
|
||||||
return self.val_to_digit(val)
|
return self.val_to_digit(val)
|
||||||
|
|
||||||
class RandomHexDigitSource(InputExpandingParamSource):
|
class RandomHexDigitSource(InputExpandingParamSource, RandomSourceMixin):
|
||||||
"""return a different sequence of random hexadecimal digits each"""
|
"""return a different sequence of random hexadecimal digits each"""
|
||||||
name = "random hexadecimal digits"
|
name = "random hexadecimal digits"
|
||||||
|
|
||||||
@@ -143,7 +146,7 @@ class RandomHexDigitSource(InputExpandingParamSource):
|
|||||||
self.num_digits = num_digits
|
self.num_digits = num_digits
|
||||||
|
|
||||||
def get_next(self, csv_row:dict=None):
|
def get_next(self, csv_row:dict=None):
|
||||||
val = random.randbytes(self.num_digits // 2) # TODO secure random source?
|
val = self.random_impl.randbytes(self.num_digits // 2)
|
||||||
return b2h(val)
|
return b2h(val)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user