2019-10-19 15:22:30 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
#
|
|
|
|
|
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
|
2023-01-03 10:36:07 +00:00
|
|
|
# Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
|
2019-10-19 15:22:30 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
"""
|
2022-11-14 09:54:07 +00:00
|
|
|
test/objattrtest.py: Test immutable attributes of MMGen data objects
|
2019-10-19 15:22:30 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# TODO: test 'typeconv' during instance creation
|
|
|
|
|
|
2023-10-03 14:27:55 +00:00
|
|
|
from collections import namedtuple
|
2019-10-19 15:22:30 +00:00
|
|
|
|
2023-09-26 09:40:12 +00:00
|
|
|
import include.test_init
|
2019-10-19 15:22:30 +00:00
|
|
|
|
2023-10-03 14:27:55 +00:00
|
|
|
from mmgen.cfg import Config
|
|
|
|
|
from mmgen.util import msg,msg_r,gmsg,die
|
|
|
|
|
from mmgen.color import red,yellow,green,blue,purple,nocolor
|
2023-10-11 12:58:51 +00:00
|
|
|
from mmgen.obj import ListItemAttr
|
2019-10-19 15:22:30 +00:00
|
|
|
|
|
|
|
|
opts_data = {
|
|
|
|
|
'sets': [
|
|
|
|
|
('show_descriptor_type', True, 'verbose', True),
|
|
|
|
|
],
|
|
|
|
|
'text': {
|
|
|
|
|
'desc': 'Test immutable attributes of MMGen data objects',
|
|
|
|
|
'usage':'[options] [object]',
|
|
|
|
|
'options': """
|
|
|
|
|
-h, --help Print this help message
|
|
|
|
|
--, --longhelp Print help message for long options (common options)
|
|
|
|
|
-d, --show-descriptor-type Display the attribute's descriptor type
|
|
|
|
|
-v, --verbose Produce more verbose output
|
|
|
|
|
"""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 16:04:10 +00:00
|
|
|
cfg = Config(opts_data=opts_data)
|
2023-03-28 18:14:37 +00:00
|
|
|
|
|
|
|
|
from test.include.common import set_globals
|
|
|
|
|
set_globals(cfg)
|
|
|
|
|
|
2023-10-03 14:27:55 +00:00
|
|
|
from test.objattrtest_py_d.oat_common import sample_objs
|
2019-10-19 15:22:30 +00:00
|
|
|
|
2023-10-03 14:27:55 +00:00
|
|
|
pd = namedtuple('attr_bits', ['read_ok','delete_ok','reassign_ok','typeconv','set_none_ok'])
|
|
|
|
|
perm_bits = ('read_ok','delete_ok','reassign_ok')
|
|
|
|
|
attr_dfls = {
|
|
|
|
|
'reassign_ok': False,
|
|
|
|
|
'delete_ok': False,
|
|
|
|
|
'typeconv': True,
|
|
|
|
|
'set_none_ok': False,
|
|
|
|
|
}
|
2019-10-19 15:22:30 +00:00
|
|
|
|
2023-10-03 14:27:55 +00:00
|
|
|
def parse_attrbits(bits):
|
2019-10-19 15:22:30 +00:00
|
|
|
return pd(
|
2023-10-03 14:27:55 +00:00
|
|
|
bool(0b00001 & bits), # read
|
|
|
|
|
bool(0b00010 & bits), # delete
|
|
|
|
|
bool(0b00100 & bits), # reassign
|
|
|
|
|
bool(0b01000 & bits), # typeconv
|
|
|
|
|
bool(0b10000 & bits), # set_none
|
2019-10-19 15:22:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def get_descriptor_obj(objclass,attrname):
|
|
|
|
|
for o in (objclass,objclass.__bases__[0]): # assume there's only one base class
|
|
|
|
|
if attrname in o.__dict__:
|
|
|
|
|
return o.__dict__[attrname]
|
2022-02-05 13:32:56 +00:00
|
|
|
die(4,f'unable to find descriptor {objclass.__name__}.{attrname}')
|
2019-10-19 15:22:30 +00:00
|
|
|
|
|
|
|
|
def test_attr_perm(obj,attrname,perm_name,perm_value,dobj,attrval_type):
|
|
|
|
|
|
2023-10-11 12:58:51 +00:00
|
|
|
class SampleObjError(Exception):
|
|
|
|
|
pass
|
2019-10-19 15:22:30 +00:00
|
|
|
|
|
|
|
|
pname = perm_name.replace('_ok','')
|
|
|
|
|
pstem = pname.rstrip('e')
|
|
|
|
|
|
|
|
|
|
try:
|
2023-10-03 14:27:55 +00:00
|
|
|
if perm_name == 'read_ok': # non-existent perm
|
2019-10-19 15:22:30 +00:00
|
|
|
getattr(obj,attrname)
|
|
|
|
|
elif perm_name == 'reassign_ok':
|
|
|
|
|
try:
|
|
|
|
|
so = sample_objs[attrval_type.__name__]
|
|
|
|
|
except:
|
2023-10-03 14:27:55 +00:00
|
|
|
raise SampleObjError(f'unable to find sample object of type {attrval_type.__name__!r}')
|
2020-04-08 14:13:13 +00:00
|
|
|
# ListItemAttr allows setting an attribute if its value is None
|
|
|
|
|
if type(dobj) == ListItemAttr and getattr(obj,attrname) == None:
|
2019-10-19 15:22:30 +00:00
|
|
|
setattr(obj,attrname,so)
|
|
|
|
|
setattr(obj,attrname,so)
|
|
|
|
|
elif perm_name == 'delete_ok':
|
|
|
|
|
delattr(obj,attrname)
|
|
|
|
|
except SampleObjError as e:
|
2022-02-05 13:32:56 +00:00
|
|
|
die(4,f'Test script error ({e})')
|
2019-10-19 15:22:30 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
if perm_value == True:
|
|
|
|
|
fs = '{!r}: unable to {} attribute {!r}, though {}ing is allowed ({})'
|
2022-02-05 13:32:56 +00:00
|
|
|
die(4,fs.format(type(obj).__name__,pname,attrname,pstem,e))
|
2019-10-19 15:22:30 +00:00
|
|
|
else:
|
|
|
|
|
if perm_value == False:
|
|
|
|
|
fs = '{!r}: attribute {!r} is {n}able, though {n}ing is forbidden'
|
2022-02-05 13:32:56 +00:00
|
|
|
die(4,fs.format(type(obj).__name__,attrname,n=pstem))
|
2019-10-19 15:22:30 +00:00
|
|
|
|
|
|
|
|
def test_attr(data,obj,attrname,dobj,bits,attrval_type):
|
|
|
|
|
if hasattr(obj,attrname): # TODO
|
|
|
|
|
td_attrval_type = data.attrs[attrname][1]
|
|
|
|
|
|
|
|
|
|
if attrval_type not in (td_attrval_type,type(None)):
|
|
|
|
|
fs = '\nattribute {!r} of {!r} instance has incorrect type {!r} (should be {!r})'
|
2022-02-05 13:32:56 +00:00
|
|
|
die(4,fs.format(attrname,type(obj).__name__,attrval_type.__name__,td_attrval_type.__name__))
|
2019-10-19 15:22:30 +00:00
|
|
|
|
|
|
|
|
if hasattr(dobj,'__dict__'):
|
|
|
|
|
d = dobj.__dict__
|
|
|
|
|
bits = bits._asdict()
|
2023-10-03 14:27:55 +00:00
|
|
|
colors = {
|
|
|
|
|
'reassign_ok': purple,
|
|
|
|
|
'delete_ok': red,
|
|
|
|
|
'typeconv': green,
|
|
|
|
|
'set_none_ok': yellow,
|
|
|
|
|
}
|
|
|
|
|
for k in bits:
|
2019-10-19 15:22:30 +00:00
|
|
|
if k in d:
|
|
|
|
|
if d[k] != bits[k]:
|
|
|
|
|
fs = 'init value {iv}={a} for attr {n!r} does not match test data ({iv}={b})'
|
2022-02-05 13:32:56 +00:00
|
|
|
die(4,fs.format(iv=k,n=attrname,a=d[k],b=bits[k]))
|
2023-10-03 14:27:55 +00:00
|
|
|
if cfg.verbose and d[k] != attr_dfls[k]:
|
|
|
|
|
msg_r(colors[k](f' {k}={d[k]!r}'))
|
2019-10-19 15:22:30 +00:00
|
|
|
|
2023-10-03 14:27:55 +00:00
|
|
|
def test_object(mod,test_data,objname):
|
2019-10-19 15:22:30 +00:00
|
|
|
|
|
|
|
|
if '.' in objname:
|
|
|
|
|
on1,on2 = objname.split('.')
|
2023-10-03 14:27:55 +00:00
|
|
|
cls = getattr(getattr(mod,on1),on2)
|
2019-10-19 15:22:30 +00:00
|
|
|
else:
|
2023-10-03 14:27:55 +00:00
|
|
|
cls = getattr(mod,objname)
|
2019-10-19 15:22:30 +00:00
|
|
|
|
2023-03-28 18:14:37 +00:00
|
|
|
fs = 'Testing attribute ' + ('{!r:<15}{dt:13}' if cfg.show_descriptor_type else '{!r}')
|
2019-10-19 15:22:30 +00:00
|
|
|
data = test_data[objname]
|
|
|
|
|
obj = cls(*data.args,**data.kwargs)
|
|
|
|
|
|
|
|
|
|
for attrname,adata in data.attrs.items():
|
|
|
|
|
dobj = get_descriptor_obj(type(obj),attrname)
|
2023-03-28 18:14:37 +00:00
|
|
|
if cfg.verbose:
|
2019-10-19 15:22:30 +00:00
|
|
|
msg_r(fs.format(attrname,dt=type(dobj).__name__.replace('MMGen','')))
|
2023-10-03 14:27:55 +00:00
|
|
|
bits = parse_attrbits(adata[0])
|
2019-10-19 15:22:30 +00:00
|
|
|
test_attr(data,obj,attrname,dobj,bits,adata[1])
|
2023-10-03 14:27:55 +00:00
|
|
|
for bit_name,bit_value in bits._asdict().items():
|
|
|
|
|
if bit_name in perm_bits:
|
|
|
|
|
test_attr_perm(obj,attrname,bit_name,bit_value,dobj,adata[1])
|
2023-03-28 18:14:37 +00:00
|
|
|
cfg._util.vmsg('')
|
2019-10-19 15:22:30 +00:00
|
|
|
|
|
|
|
|
def do_loop():
|
|
|
|
|
import importlib
|
2020-05-28 09:53:34 +00:00
|
|
|
modname = f'test.objattrtest_py_d.oat_{proto.coin.lower()}_{proto.network}'
|
2023-10-03 14:27:55 +00:00
|
|
|
mod = importlib.import_module(modname)
|
|
|
|
|
test_data = getattr(mod,'tests')
|
2020-05-28 09:53:34 +00:00
|
|
|
gmsg(f'Running immutable attribute tests for {proto.coin} {proto.network}')
|
2019-10-19 15:22:30 +00:00
|
|
|
|
2023-03-28 18:14:37 +00:00
|
|
|
utests = cfg._args
|
2019-10-19 15:22:30 +00:00
|
|
|
for obj in test_data:
|
2023-10-11 12:58:51 +00:00
|
|
|
if utests and obj not in utests:
|
|
|
|
|
continue
|
2023-03-28 18:14:37 +00:00
|
|
|
msg((blue if cfg.verbose else nocolor)(f'Testing {obj}'))
|
2023-10-03 14:27:55 +00:00
|
|
|
test_object(mod,test_data,obj)
|
2019-10-19 15:22:30 +00:00
|
|
|
|
2023-03-28 18:14:37 +00:00
|
|
|
proto = cfg._proto
|
|
|
|
|
|
2019-10-19 15:22:30 +00:00
|
|
|
do_loop()
|