mirror of
git://holbrook.no/eth-monitor.git
synced 2024-11-23 12:26:45 +01:00
Start filter test writing
This commit is contained in:
parent
e0b6e2c14b
commit
fa694c957b
@ -71,16 +71,19 @@ class RuleMethod:
|
|||||||
|
|
||||||
class RuleSimple:
|
class RuleSimple:
|
||||||
|
|
||||||
def __init__(self, outputs, inputs, executables, description=None):
|
def __init__(self, outputs, inputs, executables, description=None, match_all=False):
|
||||||
self.description = description
|
self.description = description
|
||||||
if self.description == None:
|
if self.description == None:
|
||||||
self.description = str(uuid.uuid4())
|
self.description = str(uuid.uuid4())
|
||||||
self.outputs = outputs
|
self.outputs = outputs
|
||||||
self.inputs = inputs
|
self.inputs = inputs
|
||||||
self.executables = executables
|
self.executables = executables
|
||||||
|
self.match_all = match_all
|
||||||
|
|
||||||
|
|
||||||
def check(self, sender, recipient, data, tx_hash):
|
def check(self, sender, recipient, data, tx_hash):
|
||||||
|
have_fail = False
|
||||||
|
have_match = False
|
||||||
for rule in self.outputs:
|
for rule in self.outputs:
|
||||||
if rule != None and is_same_address(sender, rule):
|
if rule != None and is_same_address(sender, rule):
|
||||||
logg.debug('tx {} rule {} match in SENDER {}'.format(tx_hash, self.description, sender))
|
logg.debug('tx {} rule {} match in SENDER {}'.format(tx_hash, self.description, sender))
|
||||||
@ -107,10 +110,11 @@ class RuleSimple:
|
|||||||
|
|
||||||
class AddressRules:
|
class AddressRules:
|
||||||
|
|
||||||
def __init__(self, include_by_default=False):
|
def __init__(self, include_by_default=False, match_all=False):
|
||||||
self.excludes = []
|
self.excludes = []
|
||||||
self.includes = []
|
self.includes = []
|
||||||
self.include_by_default = include_by_default
|
self.include_by_default = include_by_default
|
||||||
|
self.match_all = match_all
|
||||||
|
|
||||||
|
|
||||||
def exclude(self, rule):
|
def exclude(self, rule):
|
||||||
@ -130,17 +134,27 @@ class AddressRules:
|
|||||||
# TODO: rename
|
# TODO: rename
|
||||||
def apply_rules_addresses(self, sender, recipient, data, tx_hash):
|
def apply_rules_addresses(self, sender, recipient, data, tx_hash):
|
||||||
v = self.include_by_default
|
v = self.include_by_default
|
||||||
|
have_fail = False
|
||||||
|
have_match = False
|
||||||
|
|
||||||
for rule in self.includes:
|
for rule in self.includes:
|
||||||
if rule.check(sender, recipient, data, tx_hash):
|
if rule.check(sender, recipient, data, tx_hash):
|
||||||
v = True
|
v = True
|
||||||
logg.info('match in includes rule: {}'.format(rule))
|
logg.info('match in includes rule: {}'.format(rule))
|
||||||
|
if not self.match_all:
|
||||||
|
break
|
||||||
|
elif self.match_all:
|
||||||
|
v = False
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if not v:
|
||||||
|
return v
|
||||||
|
|
||||||
for rule in self.excludes:
|
for rule in self.excludes:
|
||||||
if rule.check(sender, recipient, data, tx_hash):
|
if rule.check(sender, recipient, data, tx_hash):
|
||||||
v = False
|
v = False
|
||||||
logg.info('match in excludes rule: {}'.format(rule))
|
logg.info('match in excludes rule: {}'.format(rule))
|
||||||
break
|
if not self.match_all:
|
||||||
|
break
|
||||||
|
|
||||||
return v
|
return v
|
||||||
|
40
tests/test_filter.py
Normal file
40
tests/test_filter.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
# standard imports
|
||||||
|
import logging
|
||||||
|
import unittest
|
||||||
|
import os
|
||||||
|
|
||||||
|
# local imports
|
||||||
|
from eth_monitor.rules import *
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
logg = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
|
class TestRule(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.alice = os.urandom(20).hex()
|
||||||
|
self.bob = os.urandom(20).hex()
|
||||||
|
self.carol = os.urandom(20).hex()
|
||||||
|
self.dave = os.urandom(20).hex()
|
||||||
|
self.x = os.urandom(20).hex()
|
||||||
|
self.y = os.urandom(20).hex()
|
||||||
|
self.hsh = os.urandom(32).hex()
|
||||||
|
|
||||||
|
|
||||||
|
def test_address_include(self):
|
||||||
|
outs = [self.alice]
|
||||||
|
ins = []
|
||||||
|
execs = []
|
||||||
|
rule = RuleSimple(outs, ins, execs)
|
||||||
|
c = AddressRules()
|
||||||
|
c.include(rule)
|
||||||
|
data = b''
|
||||||
|
r = c.apply_rules_addresses(self.alice, self.bob, data, self.hsh)
|
||||||
|
self.assertTrue(r)
|
||||||
|
r = c.apply_rules_addresses(self.bob, self.alice, data, self.hsh)
|
||||||
|
self.assertFalse(r)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
Loading…
Reference in New Issue
Block a user