objattrtest.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test/objattrtest.py: Test immutable attributes of MMGen data objects
  20. """
  21. # TODO: test 'typeconv' during instance creation
  22. import sys,os
  23. pn = os.path.dirname(sys.argv[0])
  24. os.chdir(os.path.join(pn,os.pardir))
  25. sys.path.__setitem__(0,os.path.abspath(os.curdir))
  26. os.environ['MMGEN_TEST_SUITE'] = '1'
  27. # Import these _after_ local path's been added to sys.path
  28. from test.objattrtest_py_d.oat_common import *
  29. from mmgen.common import *
  30. from mmgen.addrlist import *
  31. from mmgen.passwdlist import *
  32. from mmgen.tx.base import Base
  33. from mmgen.proto.btc.tw.unspent import BitcoinTwUnspentOutputs
  34. opts_data = {
  35. 'sets': [
  36. ('show_nonstandard_init', True, 'verbose', True),
  37. ('show_descriptor_type', True, 'verbose', True),
  38. ],
  39. 'text': {
  40. 'desc': 'Test immutable attributes of MMGen data objects',
  41. 'usage':'[options] [object]',
  42. 'options': """
  43. -h, --help Print this help message
  44. --, --longhelp Print help message for long options (common options)
  45. -i, --show-nonstandard-init Display non-standard attribute initialization info
  46. -d, --show-descriptor-type Display the attribute's descriptor type
  47. -v, --verbose Produce more verbose output
  48. """
  49. }
  50. }
  51. cmd_args = opts.init(opts_data)
  52. pd = namedtuple('permission_bits', ['read_ok','delete_ok','reassign_ok'])
  53. def parse_permbits(bits):
  54. return pd(
  55. bool(0b001 & bits), # read
  56. bool(0b010 & bits), # delete
  57. bool(0b100 & bits), # reassign
  58. )
  59. def get_descriptor_obj(objclass,attrname):
  60. for o in (objclass,objclass.__bases__[0]): # assume there's only one base class
  61. if attrname in o.__dict__:
  62. return o.__dict__[attrname]
  63. die(4,f'unable to find descriptor {objclass.__name__}.{attrname}')
  64. def test_attr_perm(obj,attrname,perm_name,perm_value,dobj,attrval_type):
  65. class SampleObjError(Exception): pass
  66. pname = perm_name.replace('_ok','')
  67. pstem = pname.rstrip('e')
  68. try:
  69. if perm_name == 'read_ok':
  70. getattr(obj,attrname)
  71. elif perm_name == 'reassign_ok':
  72. try:
  73. so = sample_objs[attrval_type.__name__]
  74. except:
  75. die( 'SampleObjError', f'unable to find sample object of type {attrval_type.__name__!r}' )
  76. # ListItemAttr allows setting an attribute if its value is None
  77. if type(dobj) == ListItemAttr and getattr(obj,attrname) == None:
  78. setattr(obj,attrname,so)
  79. setattr(obj,attrname,so)
  80. elif perm_name == 'delete_ok':
  81. delattr(obj,attrname)
  82. except SampleObjError as e:
  83. die(4,f'Test script error ({e})')
  84. except Exception as e:
  85. if perm_value == True:
  86. fs = '{!r}: unable to {} attribute {!r}, though {}ing is allowed ({})'
  87. die(4,fs.format(type(obj).__name__,pname,attrname,pstem,e))
  88. else:
  89. if perm_value == False:
  90. fs = '{!r}: attribute {!r} is {n}able, though {n}ing is forbidden'
  91. die(4,fs.format(type(obj).__name__,attrname,n=pstem))
  92. def test_attr(data,obj,attrname,dobj,bits,attrval_type):
  93. if hasattr(obj,attrname): # TODO
  94. td_attrval_type = data.attrs[attrname][1]
  95. if attrval_type not in (td_attrval_type,type(None)):
  96. fs = '\nattribute {!r} of {!r} instance has incorrect type {!r} (should be {!r})'
  97. die(4,fs.format(attrname,type(obj).__name__,attrval_type.__name__,td_attrval_type.__name__))
  98. if hasattr(dobj,'__dict__'):
  99. d = dobj.__dict__
  100. bits = bits._asdict()
  101. for k in ('reassign_ok','delete_ok'):
  102. if k in d:
  103. if d[k] != bits[k]:
  104. fs = 'init value {iv}={a} for attr {n!r} does not match test data ({iv}={b})'
  105. die(4,fs.format(iv=k,n=attrname,a=d[k],b=bits[k]))
  106. if opt.verbose and d[k] == True:
  107. msg_r(f' {k}={d[k]!r}')
  108. if opt.show_nonstandard_init:
  109. for k,v in (('typeconv',False),('set_none_ok',True)):
  110. if d[k] == v:
  111. msg_r(f' {k}={v}')
  112. def test_object(test_data,objname):
  113. if '.' in objname:
  114. on1,on2 = objname.split('.')
  115. cls = getattr(globals()[on1],on2)
  116. else:
  117. cls = globals()[objname]
  118. fs = 'Testing attribute ' + ('{!r:<15}{dt:13}' if opt.show_descriptor_type else '{!r}')
  119. data = test_data[objname]
  120. obj = cls(*data.args,**data.kwargs)
  121. for attrname,adata in data.attrs.items():
  122. dobj = get_descriptor_obj(type(obj),attrname)
  123. if opt.verbose:
  124. msg_r(fs.format(attrname,dt=type(dobj).__name__.replace('MMGen','')))
  125. bits = parse_permbits(adata[0])
  126. test_attr(data,obj,attrname,dobj,bits,adata[1])
  127. for perm_name,perm_value in bits._asdict().items():
  128. test_attr_perm(obj,attrname,perm_name,perm_value,dobj,adata[1])
  129. vmsg('')
  130. def do_loop():
  131. import importlib
  132. modname = f'test.objattrtest_py_d.oat_{proto.coin.lower()}_{proto.network}'
  133. test_data = importlib.import_module(modname).tests
  134. gmsg(f'Running immutable attribute tests for {proto.coin} {proto.network}')
  135. utests = cmd_args
  136. for obj in test_data:
  137. if utests and obj not in utests: continue
  138. msg((blue if opt.verbose else nocolor)(f'Testing {obj}'))
  139. test_object(test_data,obj)
  140. from mmgen.protocol import init_proto_from_opts
  141. proto = init_proto_from_opts(need_amt=True)
  142. do_loop()