def testStaticDynamicObj(self): self.class_mapper.mapClass( class_def.DynamicClassDef(self.Spam, 'alias.spam', ('spam', ))) test = self.Spam() test.ham = 'foo' encoded = '\x0A\x1B\x15alias.spam' # obj header encoded += '\x09spam' # static attr definition encoded += '\x06\x09eggs' # static attrs encoded += '\x07ham\x06\x07foo\x01' #dynamic attrs result = decode.decode( DecoderContext(encoded, class_def_mapper=self.class_mapper, amf3=True)) self.assertEquals('eggs', result.spam) self.assertEquals('foo', result.ham) result = decode.decode( DecoderContext(StringIO(encoded), class_def_mapper=self.class_mapper, amf3=True)) self.assertEquals('eggs', result.spam) self.assertEquals('foo', result.ham) self.class_mapper.unmapClass(self.Spam)
def testAnonObj(self): encoded = '\x03' #header encoded += '\x00\x04spam\x02\x00\x04eggs' #values encoded += '\x00\x00\t' # terminator result = decode.decode(DecoderContext(encoded)) self.assertEquals('eggs', result['spam']) result = decode.decode(DecoderContext(StringIO(encoded))) self.assertEquals('eggs', result['spam'])
def testMixedArray(self): encoded = '\x08\x00\x00\x00\x00' # mixed array header encoded += '\x00\x04spam\x02\x00\x04eggs' #values encoded += '\x00\x00\t' # terminator result = decode.decode(DecoderContext(encoded)) self.assertEquals('eggs', result['spam']) result = decode.decode(DecoderContext(StringIO(encoded))) self.assertEquals('eggs', result['spam'])
def testArray(self): decoded = [0, 1, 1.23456789] encoded = '\x0A\x00\x00\x00\x03' # 3 element array header encoded += '\x00\x00\x00\x00\x00\x00\x00\x00\x00' # element 1 encoded += '\x00\x3f\xf0\x00\x00\x00\x00\x00\x00' #element 2 encoded += '\x00\x3f\xf3\xc0\xca\x42\x83\xde\x1b' #element 3 result = decode.decode(DecoderContext(encoded)) for i, obj in enumerate(decoded): self.assertEquals(obj, result[i]) result = decode.decode(DecoderContext(StringIO(encoded))) for i, obj in enumerate(decoded): self.assertEquals(obj, result[i])
def testArrayCollection(self): encoded = '\x0A\x07\x43flex.messaging.io.ArrayCollection' # Object header encoded += '\x09\x09\x01' #array header encoded += '\x04\x00' #array element 1 encoded += '\x04\x01' #array element 2 encoded += '\x04\x02' #array element 3 encoded += '\x04\x03' #array element 4 result = decode.decode(DecoderContext(encoded, amf3=True)) for i in range(4): self.assertEquals(i, result[i]) result = decode.decode(DecoderContext(StringIO(encoded), amf3=True)) for i in range(4): self.assertEquals(i, result[i])
def testComplexDict(self): complex = {'element': 'ignore', 'objects': self.buildComplex()} enc_context = EncoderContext(use_collections=False, use_proxies=False, class_def_mapper=self.class_mapper, amf3=True) encoded = encode(complex, enc_context) decoded = decode(DecoderContext(encoded, class_def_mapper=self.class_mapper, amf3=True)) self.resultTest(decoded['objects'])
def testDate(self): import datetime encoded = '\x08\x01Bp+6!\x15\x80\x00' result = decode.decode(DecoderContext(encoded, amf3=True)) self.assertEquals(2005, result.year) self.assertEquals(3, result.month) self.assertEquals(18, result.day) self.assertEquals(1, result.hour) self.assertEquals(58, result.minute) self.assertEquals(31, result.second) encoded = '\x08\x01Bo%\xe2\xb2\x80\x00\x00' result = decode.decode(DecoderContext(encoded, amf3=True)) self.assertEquals(2003, result.year) self.assertEquals(12, result.month) self.assertEquals(1, result.day)
def testDict(self): encoded = '\x0A\x0B\x01' # Object header encoded += '\x09spam' # key encoded += '\x06\x09eggs' #value encoded += '\x01' # empty string terminator result = decode.decode(DecoderContext(encoded, amf3=True)) self.assertEquals('eggs', result['spam'])
def testLongStringAmf3(self): decoded = 's' * 65537 encoded = encode(decoded, EncoderContext(amf3=True)) result = decode(DecoderContext(encoded, amf3=True)) self.assertEquals(len(decoded), len(result)) for char in result: self.assertEquals('s', char)
def testLongListAmf3(self): decoded = [None] * 65537 encoded = encode(decoded, EncoderContext(amf3=True)) result = decode(DecoderContext(encoded, amf3=True)) self.assertEquals(len(decoded), len(result)) for val in result: self.assertEquals(None, val)
def testDictRef(self): encoded = '\x09\x05\x01' #array header encoded += '\x0A\x0B\x01\x09spam\x06\x09eggs\x01' #array element 1 (dict encoded) encoded += '\x0A\x02' # array element 2 (reference to dict) result = decode.decode(DecoderContext(encoded, amf3=True)) self.assertEquals('eggs', result[0]['spam']) self.assertEquals(result[0], result[1])
def testEncodeXmlRef(self): import xml.dom.minidom encoded = '\x09\x05\x01' #array header encoded += '\x0B\x81\x2B<?xml version="1.0" ?><test>\n <test_me>tester</test_me>\n </test>' # array element 1 (encoded dom) encoded += '\x0B\x02' # array element 2 (reference to dom) result = decode.decode(DecoderContext(encoded, amf3=True)) self.assertEquals(xml.dom.minidom.Document, result[0].__class__) self.assertEquals(result[0], result[1])
def testEncodeFloat(self): tests = { 0.1: '\x05\x3f\xb9\x99\x99\x99\x99\x99\x9a', 0.123456789: '\x05\x3f\xbf\x9a\xdd\x37\x39\x63\x5f' } for number, encoding in tests.iteritems(): self.assertEquals( number, decode.decode(DecoderContext(encoding, amf3=True)))
def testXml(self): import xml.dom.minidom encoded = '\x0F' # XML header encoded += '\x00\x00\x00\x55' # String header encoded += '<?xml version="1.0" ?><test>\n <test_me>tester</test_me>\n </test>' # encoded XML result = decode.decode(DecoderContext(encoded)) self.assertEquals(xml.dom.minidom.Document, result.__class__)
def testEncodeLong(self): tests = { 0x10000000: '\x05\x41\xb0\x00\x00\x00\x00\x00\x00', -0x10000001: '\x05\xc1\xb0\x00\x00\x01\x00\x00\x00', -0x10000000: '\x04\xc0\x80\x80\x00' } for number, encoding in tests.iteritems(): self.assertEquals( number, decode.decode(DecoderContext(encoding, amf3=True)))
def testArray(self): encoded = '\x09\x09\x01' #array header encoded += '\x04\x00' #array element 1 encoded += '\x04\x01' #array element 2 encoded += '\x04\x02' #array element 3 encoded += '\x04\x03' #array element 4 result = decode.decode(DecoderContext(encoded, amf3=True)) for i, val in enumerate(result): self.assertEquals(i, val)
def testArrayCollectionRef(self): encoded = '\x0A\x07\x43flex.messaging.io.ArrayCollection\x09\x05\x01' # array header encoded += '\x0A\x01\x09\x09\x01\x04\x00\x04\x01\x04\x02\x04\x03' # array element 1 (test_tuple encoded) encoded += '\x0A\x04' # array element 2 (reference to test_tuple) result = decode.decode(DecoderContext(encoded, amf3=True)) self.assertEquals([].__class__, result.__class__) self.assertEquals(result[0], result[1]) for i in range(4): self.assertEquals(i, result[0][i])
def testUnicode(self): tests = { u'': '\x06\x01', u'hello': '\x06\x0bhello', u'ᚠᛇᚻ': '\x06\x13\xe1\x9a\xa0\xe1\x9b\x87\xe1\x9a\xbb' } for string, encoding in tests.iteritems(): self.assertEquals( string, decode.decode(DecoderContext(encoding, amf3=True)))
def testDate(self): import datetime encoded = '\x0BBp+6!\x15\x80\x00\x00\x00' result = decode.decode(DecoderContext(encoded)) self.assertEquals(2005, result.year) self.assertEquals(3, result.month) self.assertEquals(18, result.day) self.assertEquals(1, result.hour) self.assertEquals(58, result.minute) self.assertEquals(31, result.second)
def testReusableDecoderContext(self): import amfast.encoder encoder = amfast.encoder.Encoder(amf3=True) pre = {'foo': 'bar'} post = encoder.encode(pre) ct = DecoderContext(post, amf3=True) result = decode.decode(ct) assert result == pre self.assertRaises(buffer.BufferError, decode.decode, ct)
def testDynamicObj(self): self.class_mapper.mapClass( class_def.DynamicClassDef(self.Spam, 'alias.spam', ())) encoded = '\x0A\x0B\x15alias.spam' encoded += '\x09spam\x06\x09eggs\x01' # dynamic attrs result = decode.decode( DecoderContext(encoded, class_def_mapper=self.class_mapper, amf3=True)) self.assertEquals('eggs', result.spam) result = decode.decode( DecoderContext(StringIO(encoded), class_def_mapper=self.class_mapper, amf3=True)) self.assertEquals('eggs', result.spam) self.class_mapper.unmapClass(self.Spam)
def testStaticObj(self): self.class_mapper.mapClass( class_def.ClassDef(self.Spam, 'alias.spam', ('spam', ))) encoded = '\x0A\x13\x15alias.spam' encoded += '\x09spam' # static attr definition encoded += '\x06\x09eggs' # static attrs result = decode.decode( DecoderContext(encoded, class_def_mapper=self.class_mapper, amf3=True)) self.assertEquals('eggs', result.spam) result = decode.decode( DecoderContext(StringIO(encoded), class_def_mapper=self.class_mapper, amf3=True)) self.assertEquals('eggs', result.spam) self.class_mapper.unmapClass(self.Spam)
def testNumber(self): tests = { 0: '\x00\x00\x00\x00\x00\x00\x00\x00\x00', 0.2: '\x00\x3f\xc9\x99\x99\x99\x99\x99\x9a', 1: '\x00\x3f\xf0\x00\x00\x00\x00\x00\x00', 42: '\x00\x40\x45\x00\x00\x00\x00\x00\x00', -123: '\x00\xc0\x5e\xc0\x00\x00\x00\x00\x00', 1.23456789: '\x00\x3f\xf3\xc0\xca\x42\x83\xde\x1b' } for number, encoding in tests.iteritems(): self.assertEquals(number, decode.decode(DecoderContext(encoding)))
def testReferences(self): encoded = '\x0A\x00\x00\x00\x04' # 3 element array header encoded += '\x03\x00\x04spam\x02\x00\x04eggs\x00\x00\t' # obj 1 encoded += '\x07\x00\x01' # ref to obj 1 encoded += '\x07\x00\x01' # ref to obj 1 encoded += '\x07\x00\x00' # circular ref result = decode.decode(DecoderContext(encoded)) self.assertEquals(4, len(result)) self.assertEquals(result, result.pop(-1)) for obj in result: self.assertEquals('eggs', obj['spam'])
def testStaticObj(self): self.class_mapper.mapClass( class_def=class_def.ClassDef(self.Spam, 'alias.spam', ('spam'))) encoded = '\x10\x00\x0Aalias.spam' encoded += '\x00\x04spam\x02\x00\x04eggs\x00\x00\x09' # dynamic attrs result = decode.decode( DecoderContext(encoded, class_def_mapper=self.class_mapper)) self.class_mapper.unmapClass(self.Spam) self.assertEquals('eggs', result.spam)
def testUnicodeRefs(self): test = ['hello', 'hello', 'hello', 'hello'] encoded = '\x09\x09\x01' # array header encoded += '\x06\x0bhello' # array element 1 (hello encoded) encoded += '\x06\x00' #array element 2 (reference to hello) encoded += '\x06\x00' #array element 3 (reference to hello) encoded += '\x06\x00' #array element 4 (reference to hello) results = decode.decode(DecoderContext(encoded, amf3=True)) for result in results: self.assertEquals('hello', result)
def testClassRef(self): self.class_mapper.mapClass( class_def.ClassDef(self.Spam, 'alias.spam', ('spam', ))) encoded = '\x09\x05\x01' #array header encoded += '\x0A\x13\x15alias.spam\x09spam\x06\x09eggs' # array element 1 encoded += '\x0A\x01\x06\x07foo' # array element 2 result = decode.decode( DecoderContext(encoded, class_def_mapper=self.class_mapper, amf3=True)) self.class_mapper.unmapClass(self.Spam) self.assertEquals(result[0].__class__, result[1].__class__)
def testDecodeTypes(self): dec_mapper = class_def.ClassDefMapper() dec_mapper.mapClass(class_def.DynamicClassDef(self.TestObject, 'test_complex.test', static_attrs=(), decode_types={ '_float': float, '_int': int, '_str': str})) dec_mapper.mapClass(class_def.DynamicClassDef(self.TestSubObject, 'test_complex.sub', ())) complex = self.buildComplex() enc_context = EncoderContext(class_def_mapper=self.class_mapper, amf3=True, include_private=True) encoded = encode(complex, enc_context) decoded = decode(DecoderContext(encoded, class_def_mapper=dec_mapper, amf3=True)) self.assertEquals('float', decoded[0]._float.__class__.__name__) self.assertEquals('int', decoded[0]._int.__class__.__name__) self.assertEquals('str', decoded[0]._str.__class__.__name__)
def testInt(self): tests = { 0: '\x04\x00', 0x35: '\x04\x35', 0x7f: '\x04\x7f', 0x80: '\x04\x81\x00', 0xd4: '\x04\x81\x54', 0x3fff: '\x04\xff\x7f', 0x4000: '\x04\x81\x80\x00', 0x1a53f: '\x04\x86\xca\x3f', 0x1fffff: '\x04\xff\xff\x7f', 0x200000: '\x04\x80\xc0\x80\x00', -0x01: '\x04\xff\xff\xff\xff', -0x2a: '\x04\xff\xff\xff\xd6', 0xfffffff: '\x04\xbf\xff\xff\xff', -0x10000000: '\x04\xc0\x80\x80\x00' } for integer, encoding in tests.iteritems(): self.assertEquals( integer, decode.decode(DecoderContext(encoding, amf3=True)))
def testExternizeable(self): custom_encoding = '\x01\x02\x03\x04\x05' class ExternClass(class_def.ExternClassDef): def readExternal(ext_self, obj, context): byte_string = context.buffer.read(5) self.assertEquals(custom_encoding, byte_string) obj.spam = 'eggs' self.class_mapper.mapClass( ExternClass(self.Spam, 'alias.spam', ('spam', ))) encoded = '\x0A\x07\x15alias.spam' encoded += custom_encoding # raw bytes result = decode.decode( DecoderContext(encoded, class_def_mapper=self.class_mapper, amf3=True)) self.class_mapper.unmapClass(self.Spam) self.assertEquals('eggs', result.spam)
def testCopyDecoderContext(self): con = DecoderContext(self.test_string, amf3=True) con_2 = con.copy(amf3=True) self._testDecoderContext(con_2)