Beispiel #1
0
    def testArray(self):
        sample = rdfvalue.RDFValueArray()

        # Add a string.
        sample.Append("hello")
        self.assertEqual(len(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], str)
        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 = rdfvalue.RDFValueArray(test_list)

        for x, y in zip(sample, test_list):
            self.assertEqual(x.__class__, y.__class__)
            self.assertEqual(x, y)
Beispiel #2
0
    def Layout(self, request, response):
        """Render the protobuf as a table."""
        # Remove these from the request in case we need to pass it to another
        # renderer.
        start = int(request.REQ.pop("start", 0))
        length = int(request.REQ.pop("length", 10))

        # We can get called again to render from an existing cache.
        cache = request.REQ.pop("cache", None)
        if cache:
            self.cache = aff4.FACTORY.Open(cache, token=request.token)
            self.proxy = rdfvalue.RDFValueArray(self.cache.Read(1000000))

        else:
            # We need to create a cache if this is too long.
            if len(self.proxy) > length:
                # Make a cache
                with aff4.FACTORY.Create(None,
                                         "TempMemoryFile",
                                         token=request.token) as self.cache:
                    data = rdfvalue.RDFValueArray()
                    data.Extend(self.proxy)
                    self.cache.Write(data.SerializeToString())

        self.data = []

        self.next_start = 0
        for i, element in enumerate(self.proxy):
            if i < start:
                continue

            elif len(self.data) > length:
                self.next_start = i
                self.length = 100
                break

            renderer = FindRendererForObject(element)
            if renderer:
                try:
                    self.data.append(renderer.RawHTML(request))
                except Exception as e:  # pylint: disable=broad-except
                    logging.error("Unable to render %s with %s: %s",
                                  type(element), renderer, e)

        response = super(RDFValueArrayRenderer, self).Layout(request, response)
        if self.next_start:
            response = self.CallJavascript(response,
                                           "RDFValueArrayRenderer.Layout",
                                           next_start=self.next_start,
                                           cache_urn=self.cache.urn,
                                           array_length=self.length)
        return response
Beispiel #3
0
    def Run(self, args):
        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 = binplist.readPlist(cStringIO.StringIO(data))

            # Create the query parser
            parser = plist_lib.PlistFilterParser(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 = rdfvalue.RDFValueArray()
            for item in iterator:
                # As we're setting the context manually, we need to account for types
                if isinstance(item, types.ListType):
                    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)
Beispiel #4
0
  def testAgePreserved(self):
    data = rdfvalue.RDFValueArray([1, 2, 3])
    now = rdfvalue.RDFDatetime().Now()
    original_age = data.age.Now()

    self.assertTrue((now - data.age) < rdfvalue.Duration("5s"))

    embedded = rdfvalue.EmbeddedRDFValue(payload=data)
    self.assertEqual(embedded.payload.age, original_age)

    new_log = rdfvalue.EmbeddedRDFValue(embedded).payload
    self.assertEqual(new_log.age, original_age, "Age not preserved: %s != %s" %
                     (new_log.age.AsMicroSecondsFromEpoch(),
                      original_age.AsMicroSecondsFromEpoch()))
Beispiel #5
0
 def GenerateSample(self, number=0):
     return rdfvalue.EmbeddedRDFValue(rdfvalue.RDFValueArray([number]))