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)
def test_decode(self): stream = util.BufferedByteStream() decoder = pyamf._get_decoder_class(pyamf.AMF0)(stream) stream.write('\x10\x00\tRecordSet\x00\n' 'serverInfo\x08\x00\x00\x00\x00\x00\x06cursor\x00?\xf0\x00\x00\x00' '\x00\x00\x00\x00\x0bcolumnNames\n\x00\x00\x00\x03\x02\x00\x01a' '\x02\x00\x01b\x02\x00\x01c\x00\x0binitialData\n\x00\x00\x00\x03' '\n\x00\x00\x00\x03\x00?\xf0\x00\x00\x00\x00\x00\x00\x00@\x00\x00' '\x00\x00\x00\x00\x00\x00@\x08\x00\x00\x00\x00\x00\x00\n\x00\x00' '\x00\x03\x00@\x10\x00\x00\x00\x00\x00\x00\x00@\x14\x00\x00\x00' '\x00\x00\x00\x00@\x18\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x03' '\x00@\x1c\x00\x00\x00\x00\x00\x00\x00@ \x00\x00\x00\x00\x00\x00' '\x00@"\x00\x00\x00\x00\x00\x00\x00\x07version\x00?\xf0\x00\x00' '\x00\x00\x00\x00\x00\ntotalCount\x00@\x08\x00\x00\x00\x00\x00\x00' '\x00\x00\t\x00\x00\t') stream.seek(0, 0) x = decoder.readElement() self.assertTrue(isinstance(x, amf0.RecordSet)) self.assertEquals(x.columns, ['a', 'b', 'c']) self.assertEquals(x.items, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) self.assertEquals(x.service, None) self.assertEquals(x.id, None)
def decode(stream, context=None, strict=False): """ Decodes the incoming stream. @type stream: L{BufferedByteStream<pyamf.util.BufferedByteStream>} @param stream: AMF data. @type context: L{amf0.Context<pyamf.amf0.Context>} or L{amf3.Context<pyamf.amf3.Context>} @param context: Context. @type strict: C{bool} @param strict: Enforce strict encoding. Default is C{False}. @raise DecodeError: Malformed stream. @raise RuntimeError: Decoder is unable to fully consume the stream buffer. @return: Message envelope. @rtype: L{Envelope} """ if not isinstance(stream, util.BufferedByteStream): stream = util.BufferedByteStream(stream) msg = Envelope() msg.amfVersion = stream.read_uchar() # see http://osflash.org/documentation/amf/envelopes/remoting#preamble # why we are doing this... if msg.amfVersion > 0x09: raise pyamf.DecodeError("Malformed stream (amfVersion=%d)" % msg.amfVersion) if context is None: context = pyamf.get_context(pyamf.AMF0) else: context = copy.copy(context) decoder = pyamf._get_decoder_class(pyamf.AMF0)(stream, context=context, strict=strict) msg.clientType = stream.read_uchar() header_count = stream.read_ushort() for i in xrange(header_count): name, required, data = _read_header(stream, decoder, strict) msg.headers[name] = data if required: msg.headers.set_required(name) body_count = stream.read_short() for i in range(body_count): context.reset() target, payload = _read_body(stream, decoder, strict) msg[target] = payload if strict and stream.remaining() > 0: raise RuntimeError("Unable to fully consume the buffer") return msg
def test_decode(self): stream = util.BufferedByteStream() decoder = pyamf._get_decoder_class(pyamf.AMF0)(stream) stream.write( '\x10\x00\tRecordSet\x00\n' 'serverInfo\x08\x00\x00\x00\x00\x00\x06cursor\x00?\xf0\x00\x00\x00' '\x00\x00\x00\x00\x0bcolumnNames\n\x00\x00\x00\x03\x02\x00\x01a' '\x02\x00\x01b\x02\x00\x01c\x00\x0binitialData\n\x00\x00\x00\x03' '\n\x00\x00\x00\x03\x00?\xf0\x00\x00\x00\x00\x00\x00\x00@\x00\x00' '\x00\x00\x00\x00\x00\x00@\x08\x00\x00\x00\x00\x00\x00\n\x00\x00' '\x00\x03\x00@\x10\x00\x00\x00\x00\x00\x00\x00@\x14\x00\x00\x00' '\x00\x00\x00\x00@\x18\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x03' '\x00@\x1c\x00\x00\x00\x00\x00\x00\x00@ \x00\x00\x00\x00\x00\x00' '\x00@"\x00\x00\x00\x00\x00\x00\x00\x07version\x00?\xf0\x00\x00' '\x00\x00\x00\x00\x00\ntotalCount\x00@\x08\x00\x00\x00\x00\x00\x00' '\x00\x00\t\x00\x00\t') stream.seek(0, 0) x = decoder.readElement() self.assertTrue(isinstance(x, amf0.RecordSet)) self.assertEquals(x.columns, ['a', 'b', 'c']) self.assertEquals(x.items, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) self.assertEquals(x.service, None) self.assertEquals(x.id, None)
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) self.assertEquals(decoder.stream.getvalue(), '123') self.assertEquals(decoder.context, context) context = amf3.Context() decoder = pyamf.get_decoder(pyamf.AMF3, data='456', context=context) self.assertEquals(decoder.stream.getvalue(), '456') self.assertEquals(decoder.context, context)
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, stream="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, stream="456", context=context, strict=True) self.assertEquals(decoder.stream.getvalue(), "456") self.assertEquals(decoder.context, context) self.assertTrue(decoder.strict)
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) element = decoder.readElement() self.context.addAMF3Object(element) return element
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