def testLists(self): query = "'foo' in ('foo', 'bar') and 1 not in (5, 2, 3,17)" expected = expression.Intersection( expression.Membership(expression.Literal("foo"), expression.Literal(("foo", "bar"))), expression.Complement( expression.Membership(expression.Literal(1), expression.Literal((5, 2, 3, 17))))) self.assertQueryMatches(query, expected)
def testPrecedence(self): query = "5 == 1 * 5 and Process/name is 'init'" expected = expression.Intersection( expression.Equivalence( expression.Literal(5), expression.Product(expression.Literal(1), expression.Literal(5))), expression.Equivalence(expression.Binding("Process/name"), expression.Literal("init"))) self.assertQueryMatches(query, expected)
def testLetSubexpr(self): query = ("Process/parent matches (Process/command is 'init' and " "Process/pid is 1)") expected = expression.Let( expression.Binding("Process/parent"), expression.Intersection( expression.Equivalence(expression.Binding("Process/command"), expression.Literal("init")), expression.Equivalence(expression.Binding("Process/pid"), expression.Literal(1)))) self.assertQueryMatches(query, expected)
def testRealExample(self): original = expression.Intersection( expression.Let( expression.Let(expression.Binding('MemoryDescriptor'), expression.Binding('process')), expression.Equivalence( expression.Let(expression.Binding('Process'), expression.Binding('command')), expression.Literal('Adium'))), expression.Intersection( expression.Membership( expression.Literal('execute'), expression.Let(expression.Binding('MemoryDescriptor'), expression.Binding('permissions'))), expression.Membership( expression.Literal('write'), expression.Let(expression.Binding('MemoryDescriptor'), expression.Binding('permissions'))))) # Two binary intersections become one variadic intersection and the # let-forms now have a Binding as their LHS whenever possible. expected = expression.Intersection( expression.Let( expression.Binding('MemoryDescriptor'), expression.Let( expression.Binding('process'), expression.Equivalence( expression.Let(expression.Binding('Process'), expression.Binding('command')), expression.Literal('Adium')))), expression.Membership( expression.Literal('execute'), expression.Let(expression.Binding('MemoryDescriptor'), expression.Binding('permissions'))), expression.Membership( expression.Literal('write'), expression.Let(expression.Binding('MemoryDescriptor'), expression.Binding('permissions')))) self.assertResult(original, expected)
def render(self, renderer): renderer.table_header([ ("User", "user", "15"), ("Session", "session", "15"), ("Terminal vnode", "vnode", "30"), ("Recovered input", "input", "75"), ("Recovered output", "output", "75")]) for terminal in self.session.entities.find("has component Terminal"): buffer_in = self.session.entities.find_first( expression.Intersection( expression.Equivalence( expression.Binding("Buffer/purpose"), expression.Literal("terminal_input")), expression.Equivalence( expression.Binding("Buffer/context"), expression.Literal(terminal.identity)))) buffer_out = self.session.entities.find_first( expression.Intersection( expression.Equivalence( expression.Binding("Buffer/purpose"), expression.Literal("terminal_output")), expression.Equivalence( expression.Binding("Buffer/context"), expression.Literal(terminal.identity)))) renderer.table_row( terminal["Terminal/session"]["Session/user"]["User/username"], terminal["Terminal/session"]["Session/sid"], terminal.get("Terminal/file", complete=True)["File/path"], repr(self.SHORTENER.sub("<whitespace>", buffer_in["Buffer/contents"])), repr(self.SHORTENER.sub("<whitespace>", buffer_out["Buffer/contents"])))
def testBigQuery(self): query = ("(Process/pid is 1 and Process/command in ('init', 'initd')) " "or any Process/children matches (Process/command not in " "('launchd', 'foo'))") expected = expression.Union( expression.Intersection( expression.Equivalence(expression.Binding("Process/pid"), expression.Literal(1)), expression.Membership(expression.Binding("Process/command"), expression.Literal(("init", "initd")))), expression.LetAny( expression.Binding("Process/children"), expression.Complement( expression.Membership( expression.Binding("Process/command"), expression.Literal(("launchd", "foo")))))) self.assertQueryMatches(query, expected)
def as_query(self): """A query that'll match entities with this identity.""" union = [] for _, keys, vals in self.indices: if isinstance(keys, tuple): intersection = [] for idx, key in enumerate(keys): intersection.append( expression.Equivalence(expression.Binding(key), expression.Literal(vals[idx]))) union.append(expression.Intersection(*intersection)) else: union.append( expression.Equivalence(expression.Binding(keys), expression.Literal(vals))) if len(union) == 1: return union[0] return expression.Union(*union)
def testEliminateEmpties(self): self.assertResult(expression.Intersection(expression.Literal(True)), expression.Literal(True))