def test_setMatch(self): # Case 1 - Empty rules watcherInstance = logwatch_manager.LogWatch(0, "Test Watcher", None) watcherInstance.setMatch(("IP", "GT", "0.0.0.0", "False", "False"), ()) self.assertDictEqual({"value": ("IP", "GT", "0.0.0.0", "False", "False"), "left": None, "right": None}, watcherInstance.rules) # Case 2 - Single element of rule tree watcherInstance = logwatch_manager.LogWatch(0, "Test Watcher", None) watcherInstance.rules = createNode(("IP", "GT", "0.0.0.0", "False", "False")) watcherInstance.setMatch(("IP", "LT", "255.255.255.255", "False", "False"), ()) self.assertDictEqual({"value": ("IP", "LT", "255.255.255.255", "False", "False"), "left": None, "right": None}, watcherInstance.rules) # Case 3 - Leaf of rule tree rule1 = ("IP", "GT", "0.0.0.0", "False", "False") rule2 = ("IP", "LT", "255.255.255.255", "False", "False") rule3 = ("IP", "EQ", "10.0.0.1", "False", "False") rule4 = ("IP", "EQ", "10.0.0.2", "False", "False") watcherInstance = logwatch_manager.LogWatch(0, "Test Watcher", None) watcherInstance.rules["value"] = "AND" watcherInstance.rules["left"] = createNode(rule1) watcherInstance.rules["right"] = createNode(rule2) watcherInstance.setMatch(rule3, (0,)) watcherInstance.setMatch(rule4, (1,)) self.assertDictEqual({"value": "AND", "left": {"value": rule3, "left": None, "right": None}, "right": {"value": rule4, "left": None, "right": None}}, watcherInstance.rules)
def combineMatch(self, match, connector, address): node = getNode(self.rules, address) if node["left"] is not None or node["right"] is not None: raise Exception( "Cant combine rule at LogWatch {} since address {} is not a leaf" .format(self.lwID, address)) node["left"] = createNode(node["value"]) node["value"] = connector node["right"] = createNode(match)
def test_delMatch(self): # Case 1 - Single element of rule tree watcherInstance = logwatch_manager.LogWatch(0, "Test Watcher", None) watcherInstance.rules = createNode(("IP", "GT", "0.0.0.0", "False", "False")) watcherInstance.delMatch(()) self.assertDictEqual({"value": (), "left": None, "right": None}, watcherInstance.rules) # Case 2 - Internal element of rule tree watcherInstance = logwatch_manager.LogWatch(0, "Test Watcher", None) watcherInstance.rules["value"] = "AND" watcherInstance.rules["left"] = createNode(("IP", "GT", "0.0.0.0", "False", "False")) watcherInstance.rules["right"] = createNode(("IP", "LT", "255.255.255.255", "False", "False")) watcherInstance.delMatch((0,)) self.assertDictEqual({"value": ("IP", "LT", "255.255.255.255", "False", "False"), "left": None, "right": None}, watcherInstance.rules)
def __init__(self, lwID, name, pipe): super(LogWatch, self).__init__(daemon=True) self.lwID = lwID self.name = name self.pipe = pipe self.rules = createNode() self.logCV = threading.Condition() self.logs = []
def test_createNode(self): node1 = createNode("val1", {"left"}, {"right"}) test1 = {'value': 'val1', 'left': {'left'}, 'right': {'right'}} self.assertDictEqual(node1, test1) node2 = createNode("val2", test1, test1) test2 = { 'value': 'val2', 'left': { 'value': 'val1', 'left': {'left'}, 'right': {'right'} }, 'right': { 'value': 'val1', 'left': {'left'}, 'right': {'right'} } } self.assertDictEqual(node2, test2)
def test_combineMatch(self): # Case 1 - Single element rule tree watcherInstance = logwatch_manager.LogWatch(0, "Test Watcher", None) watcherInstance.rules = createNode(("OLD_MATCH",)) watcherInstance.combineMatch(("NEW_MATCH",), "AND", ()) self.assertDictEqual({"value": "AND", "left": {"value": ("OLD_MATCH",), "left": None, "right": None}, "right": {"value": ("NEW_MATCH",), "left": None, "right": None}}, watcherInstance.rules) # Case 2 - Leaf of rule tree watcherInstance.rules["value"] = "AND" watcherInstance.rules["left"] = createNode(("IP", "GT", "0.0.0.0", "False", "False")) watcherInstance.rules["right"] = createNode(("IP", "LT", "255.255.255.255", "False", "False")) watcherInstance.combineMatch(("NEW_MATCH",), "OR", (0,)) self.assertDictEqual({"value": "AND", "left": {"value": "OR", "left": {"value": ("IP", "GT", "0.0.0.0", "False", "False"), "left": None, "right": None}, "right": {"value": ("NEW_MATCH",), "left": None, "right": None}}, "right": {"value": ("IP", "LT", "255.255.255.255", "False", "False"), "left": None, "right": None}}, watcherInstance.rules)
def delMatch(self, address: tuple): node = getNode(self.rules, address) if node["left"] is not None or node["right"] is not None: raise Exception( "Cant delete rule at LogWatch {} since address {} is not a leaf" .format(self.lwID, address)) if address == (): self.rules = createNode() else: parentNode = getNode(self.rules, address[:-1]) if address[-1] == 0: survivorNode = parentNode["right"] else: survivorNode = parentNode["left"] parentNode["value"] = survivorNode["value"] parentNode["left"] = survivorNode["left"] parentNode["right"] = survivorNode["right"]
def createWatcher(self, lwID=None, name=None, soft=False): with self.logWatchTrackersLock: if not lwID: lwID = len(self.logWatchTrackers) if not name: name = "Watcher {}".format(lwID) if not soft: with sqlite3.connect(databasePath) as conn: c = conn.cursor() c.execute("""insert into watchers values (?, ?);""", (lwID, name)) c.execute("""insert into rules values(?, ?)""", (lwID, str(createNode()))) print("{} created.".format(name), file=sys.stderr) parent_conn, child_conn = multiprocessing.Pipe() process = LogWatch(lwID, name, child_conn) lwTracker = LogWatchTracker(process, parent_conn) self.logWatchTrackers[lwID] = lwTracker self.selector.register(parent_conn, selectors.EVENT_READ, lwTracker) process.start() print("{} started.".format(name), file=sys.stderr) return "0"