Example #1
0
    def test_create(self):
        c = amf3.Context()

        self.assertEqual(c.strings, [])
        self.assertEqual(c.classes, {})
        self.assertEqual(len(c.strings), 0)
        self.assertEqual(len(c.classes), 0)
Example #2
0
    def test_get_encoder(self):
        from pyamf import amf0, amf3

        self.assertEquals(pyamf._get_encoder_class(pyamf.AMF0), amf0.Encoder)
        self.assertEquals(pyamf._get_encoder_class(pyamf.AMF3), amf3.Encoder)
        self.assertRaises(ValueError, pyamf._get_encoder_class, 'spam')

        self.assertTrue(isinstance(pyamf.get_encoder(pyamf.AMF0),
                                   amf0.Encoder))
        self.assertTrue(isinstance(pyamf.get_encoder(pyamf.AMF3),
                                   amf3.Encoder))
        self.assertRaises(ValueError, pyamf.get_encoder, 'spam')

        context = amf0.Context()
        encoder = pyamf.get_encoder(pyamf.AMF0, data='spam', context=context)
        self.assertEquals(encoder.stream.getvalue(), 'spam')
        self.assertEquals(encoder.context, context)
        self.assertFalse(encoder.strict)

        context = amf3.Context()
        encoder = pyamf.get_encoder(pyamf.AMF3, data='eggs', context=context)
        self.assertFalse(encoder.strict)

        encoder = pyamf.get_encoder(pyamf.AMF0, strict=True)
        self.assertTrue(encoder.strict)

        encoder = pyamf.get_encoder(pyamf.AMF3, strict=True)
        self.assertTrue(encoder.strict)
Example #3
0
    def test_get_reference(self):
        x = amf3.Context()
        y = [1, 2, 3]
        z = {'spam': 'eggs'}

        spam_alias = pyamf.register_class(Spam, 'spam.eggs')

        class Foo:
            pass

        foo_alias = pyamf.register_class(Foo, 'foo.bar')

        a = amf3.ClassDefinition(spam_alias)
        b = amf3.ClassDefinition(foo_alias)

        ref1 = x.addObject(y)
        ref2 = x.addObject(z)
        x.addString('abc')
        x.addString('def')
        x.addClass(a, Spam)
        x.addClass(b, Foo)

        self.assertEqual(x.getObjectReference(y), ref1)
        self.assertEqual(x.getObjectReference(z), ref2)
        self.assertEqual(x.getObjectReference({}), -1)

        self.assertEqual(x.getStringReference('abc'), 0)
        self.assertEqual(x.getStringReference('def'), 1)
        self.assertEqual(x.getStringReference('asdfas'), -1)

        self.assertEqual(x.getClass(Spam), a)
        self.assertEqual(x.getClass(Foo), b)
        self.assertEqual(x.getClass(object()), None)
Example #4
0
    def test_get_decoder(self):
        from pyamf import amf0, amf3

        self.assertEquals(pyamf._get_decoder_class(pyamf.AMF0), amf0.Decoder)
        self.assertEquals(pyamf._get_decoder_class(pyamf.AMF3), amf3.Decoder)
        self.assertRaises(ValueError, pyamf._get_decoder_class, 'spam')

        self.assertTrue(isinstance(pyamf.get_decoder(pyamf.AMF0),
                                   amf0.Decoder))
        self.assertTrue(isinstance(pyamf.get_decoder(pyamf.AMF3),
                                   amf3.Decoder))
        self.assertRaises(ValueError, pyamf.get_decoder, 'spam')

        context = amf0.Context()
        decoder = pyamf.get_decoder(pyamf.AMF0,
                                    data='123',
                                    context=context,
                                    strict=True)
        self.assertEquals(decoder.stream.getvalue(), '123')
        self.assertEquals(decoder.context, context)
        self.assertTrue(decoder.strict)

        context = amf3.Context()
        decoder = pyamf.get_decoder(pyamf.AMF3,
                                    data='456',
                                    context=context,
                                    strict=True)
        self.assertEquals(decoder.stream.getvalue(), '456')
        self.assertEquals(decoder.context, context)
        self.assertTrue(decoder.strict)
Example #5
0
    def test_complex_encode_decode_dict(self):
        complex = {'element': 'ignore', 'objects': self.build_complex()}
        self.encoder.writeElement(complex)
        encoded = self.encoder.stream.getvalue()

        context = amf3.Context()
        decoded = amf3.Decoder(encoded, context).readElement()

        self.complex_encode_decode_test(decoded['objects'])
Example #6
0
    def test_add_class(self):
        x = amf3.Context()

        alias = pyamf.register_class(Spam, 'spam.eggs')
        y = amf3.ClassDefinition(alias)

        self.assertEqual(x.addClass(y, Spam), 0)
        self.assertEqual(x.classes, {Spam: y})
        self.assertEqual(x.class_ref, {0: y})
        self.assertEqual(len(x.class_ref), 1)
Example #7
0
    def test_add_string(self):
        x = amf3.Context()
        y = 'abc'

        self.assertEqual(x.addString(y), 0)
        self.assertTrue(y in x.strings)
        self.assertEqual(len(x.strings), 1)

        self.assertEqual(x.addString(''), -1)

        self.assertRaises(TypeError, x.addString, 132)
Example #8
0
    def test_clear(self):
        x = amf3.Context()
        y = [1, 2, 3]

        x.addObject(y)
        x.addString('spameggs')
        x.clear()

        self.assertEqual(x.strings, [])
        self.assertEqual(len(x.strings), 0)
        self.assertFalse('spameggs' in x.strings)
Example #9
0
    def test_get_by_reference(self):
        x = amf3.Context()
        y = [1, 2, 3]
        z = {'spam': 'eggs'}

        alias_spam = pyamf.register_class(Spam, 'spam.eggs')

        class Foo:
            pass

        class Bar:
            pass

        alias_foo = pyamf.register_class(Foo, 'foo.bar')

        a = amf3.ClassDefinition(alias_spam)
        b = amf3.ClassDefinition(alias_foo)

        x.addObject(y)
        x.addObject(z)
        x.addString('abc')
        x.addString('def')
        x.addClass(a, Foo)
        x.addClass(b, Bar)

        self.assertEqual(x.getObject(0), y)
        self.assertEqual(x.getObject(1), z)
        self.assertEqual(x.getObject(2), None)
        self.assertRaises(TypeError, x.getObject, '')
        self.assertRaises(TypeError, x.getObject, 2.2323)

        self.assertEqual(x.getString(0), 'abc')
        self.assertEqual(x.getString(1), 'def')
        self.assertEqual(x.getString(2), None)
        self.assertRaises(TypeError, x.getString, '')
        self.assertRaises(TypeError, x.getString, 2.2323)

        self.assertEqual(x.getClass(Foo), a)
        self.assertEqual(x.getClass(Bar), b)
        self.assertEqual(x.getClass(2), None)

        self.assertEqual(x.getClassByReference(0), a)
        self.assertEqual(x.getClassByReference(1), b)
        self.assertEqual(x.getClassByReference(2), None)

        self.assertEqual(x.getObject(2), None)
        self.assertEqual(x.getString(2), None)
        self.assertEqual(x.getClass(2), None)
        self.assertEqual(x.getClassByReference(2), None)
Example #10
0
    def writeAMF3(self, data):
        """
        Writes an element to the datastream in L{AMF3<pyamf.amf3>} format.

        @type data: C{mixed}
        @param data: The data to be encoded to the AMF0 data stream.
        """
        if not hasattr(self.context, 'amf3_context'):
            from pyamf import amf3

            self.context.amf3_context = amf3.Context()

        self.context.addAMF3Object(data)
        encoder = pyamf._get_encoder_class(pyamf.AMF3)(self.stream, self.context.amf3_context)

        self.writeType(ASTypes.AMF3)
        encoder.writeElement(data)
Example #11
0
    def readAMF3(self):
        """
        Read AMF3 elements from the data stream.

        @rtype: C{mixed}
        @return: The AMF3 element read from the stream
        """
        if not hasattr(self.context, 'amf3_context'):
            from pyamf import amf3

            self.context.amf3_context = amf3.Context()

        decoder = pyamf._get_decoder_class(pyamf.AMF3)(self.stream, self.context.amf3_context, strict=self.strict)

        element = decoder.readElement()
        self.context.addAMF3Object(element)

        return element
Example #12
0
def parse_stacktrace(filename, c, e, v):

    event_id = e
    value_id = v

    with open(filename, "rb") as f:

        buf = f.read()

        pos = 0

        while True:
            context = amf3.Context()
            decoder = amf3.Decoder(buf[pos:], context)
            try:
                data = decoder.readElement()
            except pyamf.EOStream:
                #print "EOStream"
                break
            except IOError:  # premature end of stream
                #print "IOError"
                break
            except Exception, e:
                print "Warning:", e, pos
                pos = pos + 1
                continue
            if not data:
                pos = pos + 1
                continue
            pos = pos + decoder.stream.tell()

            if type(data) != list:
                print "Warning: top level data not valid: ", type(data)
                continue

            l = len(data)
            name, session, method, opcode = data[0].split(":", 4)
            c.execute(
                "INSERT INTO stack_events (event_id,session_method,opcode) VALUES (?,?,?);",
                (event_id, "%s:%s" % (session, method), opcode))

            #if dbtool.verbose:
            #    print "\n", event_id, data[0]
            for i in range(0, (l - 1) / 2):
                t = data[(i * 2) + 1]
                a = data[(i * 2) + 2]
                #print event_id, t, data[0], repr(a)[:32]
                if type(a) != pyamf.amf3.ByteArray:
                    print "Warning: top level data not valid: ", type(a)
                    d = None
                else:
                    try:
                        d = amf3.Decoder(a).readElement()
                    except:
                        print "Warning: decoder failed @", data[0], t
                        d = ""

                value_id = insert_stackval(c, value_id, event_id, t, d, -1, 0)

            event_id = event_id + 1

        return (event_id, value_id)