From e91b139c99ac36a958cdd389fe29eaf04a0de1dd Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 29 Sep 2021 21:17:56 +0000 Subject: [PATCH] test.py: begin implementing TEST_SUITE_DETERMINISTIC --- mmgen/globalvars.py | 2 ++ test/include/common.py | 17 +++++++++++++---- test/test_py_d/common.py | 3 ++- test/test_py_d/ts_main.py | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/mmgen/globalvars.py b/mmgen/globalvars.py index 04ea4fb8..f4f942af 100755 --- a/mmgen/globalvars.py +++ b/mmgen/globalvars.py @@ -127,6 +127,7 @@ class GlobalContext(Lockable): debug_utf8 = False traceback = False test_suite = False + test_suite_deterministic = False test_suite_regtest = False test_suite_popen_spawn = False terminal_width = 0 @@ -204,6 +205,7 @@ class GlobalContext(Lockable): 'MMGEN_DEBUG_ALL', # special: there is no g.debug_all var 'MMGEN_TEST_SUITE', + 'MMGEN_TEST_SUITE_DETERMINISTIC', 'MMGEN_TEST_SUITE_REGTEST', 'MMGEN_TEST_SUITE_POPEN_SPAWN', 'MMGEN_TERMINAL_WIDTH', diff --git a/test/include/common.py b/test/include/common.py index 4265180e..bdd3c94c 100755 --- a/test/include/common.py +++ b/test/include/common.py @@ -66,12 +66,21 @@ sample_mn = { ref_kafile_pass = 'kafile password' ref_kafile_hash_preset = '1' -def getrandnum(n): return int(os.urandom(n).hex(),16) -def getrandhex(n): return os.urandom(n).hex() +def ts_urandom(n): + assert n < 70, f'{n} > len(sample_text) (69 bytes)' + return sample_text.encode()[:n] if g.test_suite_deterministic else os.urandom(n) + +def getrandnum(n): + return int(ts_urandom(n).hex(),16) + +def getrandhex(n): + return ts_urandom(n).hex() + def getrandnum_range(nbytes,rn_max): while True: - rn = int(os.urandom(nbytes).hex(),16) - if rn < rn_max: return rn + rn = int(ts_urandom(nbytes).hex(),16) + if rn < rn_max: + return rn def getrandstr(num_chars,no_space=False): n,m = 95,32 diff --git a/test/test_py_d/common.py b/test/test_py_d/common.py index 6b8b0bc6..5c79a354 100755 --- a/test/test_py_d/common.py +++ b/test/test_py_d/common.py @@ -145,6 +145,7 @@ def get_label(do_shuffle=False): try: return next(label_iter) except: - if do_shuffle: shuffle(labels) + if do_shuffle: + shuffle(labels) label_iter = iter(labels) return next(label_iter) diff --git a/test/test_py_d/ts_main.py b/test/test_py_d/ts_main.py index 94e3f710..63ea8267 100755 --- a/test/test_py_d/ts_main.py +++ b/test/test_py_d/ts_main.py @@ -327,7 +327,7 @@ class TestSuiteMain(TestSuiteBase,TestSuiteShared): for d in tx_data.values(): al = adata.addrlist(al_id=d['al_id']) for n,(idx,coinaddr) in enumerate(al.addrpairs()): - lbl = get_label(do_shuffle=True) + lbl = get_label(do_shuffle=not g.test_suite_deterministic) out.append(self._create_fake_unspent_entry(coinaddr,d['al_id'],idx,lbl,segwit=d['segwit'])) if n == 0: # create a duplicate address. This means addrs_per_wallet += 1 out.append(self._create_fake_unspent_entry(coinaddr,d['al_id'],idx,lbl,segwit=d['segwit']))