def testArray(self): sample = rdf_protodict.RDFValueArray() # Add a string. sample.Append("hello") self.assertLen(sample, 1) self.assertEqual(sample[0], "hello") # Add another RDFValue sample.Append(rdfvalue.RDFString("hello")) self.assertIsInstance(sample[1], rdfvalue.RDFString) # Test iterator. sample_list = list(sample) self.assertIsInstance(sample_list, list) self.assertIsInstance(sample_list[0], Text) self.assertIsInstance(sample_list[1], rdfvalue.RDFString) # Test initialization from a list of variable types. test_list = [ 1, 2, # Integers. None, # None. rdfvalue.RDFDatetime.Now(), # An RDFValue instance. [1, 2], # A nested list. u"升级程序", # Unicode. ] sample = rdf_protodict.RDFValueArray(test_list) for x, y in zip(sample, test_list): self.assertEqual(x.__class__, y.__class__) self.assertEqual(x, y)
def Run(self, args): # TODO(hanuszczak): Why are these an instance variables? self.context = args.context self.filter_query = args.query with vfs.VFSOpen(args.pathspec, progress_callback=self.Progress) as fd: data = fd.Read(self.MAX_PLIST_SIZE) plist = plistlib.load(io.BytesIO(data)) # Create the query parser parser = plist_lib.PlistFilterParser(str(self.filter_query)).Parse() filter_imp = plist_lib.PlistFilterImplementation matcher = parser.Compile(filter_imp) if self.context: # Obtain the values for the context using the value expander value_expander = filter_imp.FILTERS["ValueExpander"] iterator = value_expander().Expand(plist, self.context) else: # If we didn't get a context, the context is the whole plist iterator = [plist] reply = rdf_protodict.RDFValueArray() for item in iterator: # As we're setting the context manually, we need to account for types if isinstance(item, list): for sub_item in item: partial_plist = plist_lib.PlistValueToPlainValue(sub_item) if matcher.Matches(partial_plist): reply.Append(sub_item) else: partial_plist = plist_lib.PlistValueToPlainValue(item) if matcher.Matches(partial_plist): reply.Append(partial_plist) self.SendReply(reply)
def testAgePreserved(self): data = rdf_protodict.RDFValueArray([1, 2, 3]) data.age = rdfvalue.RDFDatetime.Now() original_age = data.age now = rdfvalue.RDFDatetime.Now() self.assertLess((now - data.age), rdfvalue.Duration("5s")) embedded = rdf_protodict.EmbeddedRDFValue(payload=data) self.assertEqual(embedded.payload.age, original_age) new_log = rdf_protodict.EmbeddedRDFValue(embedded).payload self.assertEqual( new_log.age, original_age, "Age not preserved: %s != %s" % (new_log.age.AsMicrosecondsSinceEpoch(), original_age.AsMicrosecondsSinceEpoch()))
def GenerateSample(self, number=0): return rdf_protodict.EmbeddedRDFValue(rdf_protodict.RDFValueArray([number]))