test_greedy.py (2928B)
1 import logging 2 import unittest 3 import os 4 5 # local imports 6 from eth_monitor.rules import * 7 8 logging.basicConfig(level=logging.DEBUG) 9 logg = logging.getLogger() 10 11 12 class TestRule(unittest.TestCase): 13 14 def setUp(self): 15 self.alice = os.urandom(20).hex() 16 self.bob = os.urandom(20).hex() 17 self.carol = os.urandom(20).hex() 18 self.dave = os.urandom(20).hex() 19 self.x = os.urandom(20).hex() 20 self.y = os.urandom(20).hex() 21 self.hsh = os.urandom(32).hex() 22 23 24 def test_greedy_includes(self): 25 data = b'' 26 outs = [self.alice] 27 ins = [self.carol] 28 execs = [] 29 rule = RuleSimple(outs, ins, execs, match_all=True) 30 c = AddressRules() 31 c.include(rule) 32 r = c.apply_rules_addresses(self.alice, self.bob, data, self.hsh) 33 self.assertFalse(r) 34 r = c.apply_rules_addresses(self.bob, self.alice, data, self.hsh) 35 self.assertFalse(r) 36 r = c.apply_rules_addresses(self.bob, self.carol, data, self.hsh) 37 self.assertFalse(r) 38 r = c.apply_rules_addresses(self.alice, self.carol, data, self.hsh) 39 self.assertTrue(r) 40 41 rule = RuleSimple(outs, ins, execs) 42 c = AddressRules(match_all=True) 43 c.include(rule) 44 r = c.apply_rules_addresses(self.alice, self.bob, data, self.hsh) 45 self.assertTrue(r) 46 r = c.apply_rules_addresses(self.bob, self.alice, data, self.hsh) 47 self.assertFalse(r) 48 r = c.apply_rules_addresses(self.bob, self.carol, data, self.hsh) 49 self.assertTrue(r) 50 r = c.apply_rules_addresses(self.alice, self.carol, data, self.hsh) 51 self.assertTrue(r) 52 53 54 def test_greedy_data(self): 55 data = os.urandom(128).hex() 56 data_match_one = data[4:8] 57 data_match_two = data[32:42] 58 data_match_fail = os.urandom(64).hex() 59 data_match = [data_match_one] 60 61 rule = RuleData(data_match, match_all=True) 62 c = AddressRules() 63 c.include(rule) 64 r = c.apply_rules_addresses(self.alice, self.bob, data, self.hsh) 65 self.assertTrue(r) 66 67 data_match = [data_match_two] 68 rule = RuleData(data_match, match_all=True) 69 c = AddressRules() 70 c.include(rule) 71 r = c.apply_rules_addresses(self.alice, self.bob, data, self.hsh) 72 self.assertTrue(r) 73 74 data_match = [data_match_two, data_match_one] 75 rule = RuleData(data_match, match_all=True) 76 c = AddressRules() 77 c.include(rule) 78 r = c.apply_rules_addresses(self.alice, self.bob, data, self.hsh) 79 self.assertTrue(r) 80 81 data_match = [data_match_two, data_match_fail, data_match_one] 82 rule = RuleData(data_match, match_all=True) 83 c = AddressRules() 84 c.include(rule) 85 r = c.apply_rules_addresses(self.alice, self.bob, data, self.hsh) 86 self.assertFalse(r) 87 88 89 if __name__ == '__main__': 90 unittest.main()