objattrtest.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. from collections import namedtuple
  23. try:
  24. from include import test_init
  25. except ImportError:
  26. from test.include import test_init
  27. from mmgen.cfg import Config
  28. from mmgen.util import msg,msg_r,gmsg,die
  29. from mmgen.color import red,yellow,green,blue,purple,nocolor
  30. from mmgen.obj import ListItemAttr
  31. opts_data = {
  32. 'sets': [
  33. ('show_descriptor_type', True, 'verbose', True),
  34. ],
  35. 'text': {
  36. 'desc': 'Test immutable attributes of MMGen data objects',
  37. 'usage':'[options] [object]',
  38. 'options': """
  39. -h, --help Print this help message
  40. --, --longhelp Print help message for long options (common options)
  41. -d, --show-descriptor-type Display the attribute's descriptor type
  42. -v, --verbose Produce more verbose output
  43. """
  44. }
  45. }
  46. cfg = Config(opts_data=opts_data)
  47. from test.include.common import set_globals
  48. set_globals(cfg)
  49. from test.objattrtest_py_d.oat_common import sample_objs
  50. pd = namedtuple('attr_bits', ['read_ok','delete_ok','reassign_ok','typeconv','set_none_ok'])
  51. perm_bits = ('read_ok','delete_ok','reassign_ok')
  52. attr_dfls = {
  53. 'reassign_ok': False,
  54. 'delete_ok': False,
  55. 'typeconv': True,
  56. 'set_none_ok': False,
  57. }
  58. def parse_attrbits(bits):
  59. return pd(
  60. bool(0b00001 & bits), # read
  61. bool(0b00010 & bits), # delete
  62. bool(0b00100 & bits), # reassign
  63. bool(0b01000 & bits), # typeconv
  64. bool(0b10000 & bits), # set_none
  65. )
  66. def get_descriptor_obj(objclass,attrname):
  67. for o in (objclass,objclass.__bases__[0]): # assume there's only one base class
  68. if attrname in o.__dict__:
  69. return o.__dict__[attrname]
  70. die(4,f'unable to find descriptor {objclass.__name__}.{attrname}')
  71. def test_attr_perm(obj,attrname,perm_name,perm_value,dobj,attrval_type):
  72. class SampleObjError(Exception):
  73. pass
  74. pname = perm_name.replace('_ok','')
  75. pstem = pname.rstrip('e')
  76. try:
  77. if perm_name == 'read_ok': # non-existent perm
  78. getattr(obj,attrname)
  79. elif perm_name == 'reassign_ok':
  80. try:
  81. so = sample_objs[attrval_type.__name__]
  82. except Exception as e:
  83. raise SampleObjError(f'unable to find sample object of type {attrval_type.__name__!r}') from e
  84. # ListItemAttr allows setting an attribute if its value is None
  85. if type(dobj) is ListItemAttr and getattr(obj,attrname) is None:
  86. setattr(obj,attrname,so)
  87. setattr(obj,attrname,so)
  88. elif perm_name == 'delete_ok':
  89. delattr(obj,attrname)
  90. except SampleObjError as e:
  91. die(4,f'Test script error ({e})')
  92. except Exception as e:
  93. if perm_value is True:
  94. fs = '{!r}: unable to {} attribute {!r}, though {}ing is allowed ({})'
  95. die(4,fs.format(type(obj).__name__,pname,attrname,pstem,e))
  96. else:
  97. if perm_value is False:
  98. fs = '{!r}: attribute {!r} is {n}able, though {n}ing is forbidden'
  99. die(4,fs.format(type(obj).__name__,attrname,n=pstem))
  100. def test_attr(data,obj,attrname,dobj,bits,attrval_type):
  101. if hasattr(obj,attrname): # TODO
  102. td_attrval_type = data.attrs[attrname][1]
  103. if attrval_type not in (td_attrval_type,type(None)):
  104. fs = '\nattribute {!r} of {!r} instance has incorrect type {!r} (should be {!r})'
  105. die(4,fs.format(attrname,type(obj).__name__,attrval_type.__name__,td_attrval_type.__name__))
  106. if hasattr(dobj,'__dict__'):
  107. d = dobj.__dict__
  108. bits = bits._asdict()
  109. colors = {
  110. 'reassign_ok': purple,
  111. 'delete_ok': red,
  112. 'typeconv': green,
  113. 'set_none_ok': yellow,
  114. }
  115. for k in bits:
  116. if k in d:
  117. if d[k] != bits[k]:
  118. fs = 'init value {iv}={a} for attr {n!r} does not match test data ({iv}={b})'
  119. die(4,fs.format(iv=k,n=attrname,a=d[k],b=bits[k]))
  120. if cfg.verbose and d[k] != attr_dfls[k]:
  121. msg_r(colors[k](f' {k}={d[k]!r}'))
  122. def test_object(mod,test_data,objname):
  123. if '.' in objname:
  124. on1,on2 = objname.split('.')
  125. cls = getattr(getattr(mod,on1),on2)
  126. else:
  127. cls = getattr(mod,objname)
  128. fs = 'Testing attribute ' + ('{!r:<15}{dt:13}' if cfg.show_descriptor_type else '{!r}')
  129. data = test_data[objname]
  130. obj = cls(*data.args,**data.kwargs)
  131. for attrname,adata in data.attrs.items():
  132. dobj = get_descriptor_obj(type(obj),attrname)
  133. if cfg.verbose:
  134. msg_r(fs.format(attrname,dt=type(dobj).__name__.replace('MMGen','')))
  135. bits = parse_attrbits(adata[0])
  136. test_attr(data,obj,attrname,dobj,bits,adata[1])
  137. for bit_name,bit_value in bits._asdict().items():
  138. if bit_name in perm_bits:
  139. test_attr_perm(obj,attrname,bit_name,bit_value,dobj,adata[1])
  140. cfg._util.vmsg('')
  141. def do_loop():
  142. import importlib
  143. modname = f'test.objattrtest_py_d.oat_{proto.coin.lower()}_{proto.network}'
  144. mod = importlib.import_module(modname)
  145. test_data = getattr(mod,'tests')
  146. gmsg(f'Running immutable attribute tests for {proto.coin} {proto.network}')
  147. utests = cfg._args
  148. for obj in test_data:
  149. if utests and obj not in utests:
  150. continue
  151. msg((blue if cfg.verbose else nocolor)(f'Testing {obj}'))
  152. test_object(mod,test_data,obj)
  153. proto = cfg._proto
  154. if __name__ == '__main__':
  155. do_loop()