def __init__(self, initializer=None, age=None, **kwargs): if isinstance(initializer, dict): conf = initializer initializer = None else: conf = kwargs super(Method, self).__init__(initializer=initializer, age=age) probe = conf.get("probe", {}) resource = conf.get("resource", {}) hint = conf.get("hint", {}) target = conf.get("target", {}) if hint: # Add the hint to children. for cfg in probe: cfg["hint"] = hints.Overlay(child=cfg.get("hint", {}), parent=hint) self.probe = [Probe(**cfg) for cfg in probe] self.hint = Hint(hint, reformat=False) self.match = MatchStrToList(kwargs.get("match")) self.matcher = Matcher(self.match, self.hint) self.resource = [rdf_protodict.Dict(**r) for r in resource] self.target = triggers.Target(**target) self.triggers = triggers.Triggers() for p in self.probe: # If the probe has a target, use it. Otherwise, use the method's target. target = p.target or self.target self.triggers.Add(p.artifact, target, p)
#!/usr/bin/env python # -*- coding: utf-8 -*- """Tests for triggers.""" from grr.lib import flags from grr.lib.checks import triggers from grr.test_lib import test_lib target_1 = triggers.Target({}) target_2 = triggers.Target(os=["TermOS"], cpe=["cpe:/o:cyberdyne:termos"], label=["t800", "t1000"]) bad_ai = ("BadAI", None, None, None) good_ai = ("GoodAI", None, None, None) termos = ("BadAI", "TermOS", "cpe:/o:cyberdyne:termos") t800 = ("BadAI", "TermOS", "cpe:/o:cyberdyne:termos", "t800") t1000 = ("BadAI", "TermOS", "cpe:/o:cyberdyne:termos", "t1000") class ConditionTest(test_lib.GRRBaseTest): """Test trigger selection methods.""" def testConditionRequiresArtifact(self): self.assertRaises(triggers.DefinitionError, triggers.Condition, None) self.assertRaises(triggers.DefinitionError, triggers.Condition, None, "TermOS", "cpe:/o:cyberdyne:termos", "t800") def testConditionIsHashable(self): c1 = triggers.Condition(*termos) c2 = triggers.Condition(*t800) results = set([c1, c2, c1, c1, c1, c2]) self.assertEqual(2, len(results)) self.assertItemsEqual(set([c1, c2]), results)