def testDate(self): import datetime test = datetime.datetime(2005, 3, 18, 1, 58, 31) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals('\x08\x01Bp+6!\x15\x80\x00', buf) test = datetime.date(2003, 12, 1) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals('\x08\x01Bo%\xe2\xb2\x80\x00\x00', buf)
def encode(self, obj, amf3): context = EncoderContext(use_collections=True, amf3=amf3, use_proxies=False, class_def_mapper=self.class_mapper) return encode.encode(obj, context)
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 amfastEncode(self, obj, amf3=False, use_proxies=False): enc_context = EncoderContext(use_collections=use_proxies, use_proxies=use_proxies, class_def_mapper=self.class_mapper, amf3=amf3) return encode.encode(obj, enc_context)
def testLongStringAmf0(self): decoded = 's' * 65537 encoded = encode(decoded) result = decode(encoded) self.assertEquals(len(decoded), len(result)) for char in result: self.assertEquals('s', char)
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 testDict(self): result = '\x0A\x0B\x01' # Object header result += '\x09spam' # key result += '\x06\x09eggs' #value result += '\x01' # empty string terminator buf = encode.encode({'spam': 'eggs'}, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testList(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 self.assertEquals(encoded, encode.encode(decoded))
def testString(self): tests = { '': '\x02\x00\x00', 'hello': '\x02\x00\x05hello' } for string, encoding in tests.iteritems(): self.assertEquals(encoding, encode.encode(string))
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 testAnonymousObj(self): decoded = self.Spam() decoded.spam = 'eggs' encoded = '\x03' #header encoded += '\x00\x04spam\x02\x00\x04eggs' #values encoded += '\x00\x00\t' # terminator self.assertEquals(encoded, encode.encode(decoded))
def testLongListAmf0(self): decoded = [None] * 65537 encoded = encode(decoded) result = decode(encoded) self.assertEquals(len(decoded), len(result)) for val in result: self.assertEquals(None, val)
def testNoCollection(self): from amfast.class_def.as_types import AsNoProxy 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 self.assertEquals(encoded, encode.encode(AsNoProxy(decoded)))
def testFloat(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(): buf = encode.encode(number, EncoderContext(amf3=True)) self.assertEquals(encoding, buf)
def testDictAsObjectProxy(self): result = '\x0A\x07\x3Bflex.messaging.io.ObjectProxy' # Object header result += '\x0A\x0B\x01' # Object header result += '\x09spam' # key result += '\x06\x09eggs' #value result += '\x01' # empty string terminator buf = encode.encode({'spam': 'eggs'}, EncoderContext(use_proxies=True, amf3=True)) self.assertEquals(result, buf)
def testString(self): tests = { '': '\x06\x01', 'hello': '\x06\x0bhello' } for string, encoding in tests.iteritems(): buf = encode.encode(string, EncoderContext(amf3=True)) self.assertEquals(encoding, buf)
def testAnonObj(self): test = self.Spam() result = '\x0A\x0B\x01' # Object header result += '\x09spam' # key result += '\x06\x09eggs' #value result += '\x01' # empty string terminator buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testObjectProxyRef(self): test_dict = {'spam': 'eggs'} test = (test_dict, test_dict) result = '\x09\x05\x01' #array header result += '\x0A\x07\x3Bflex.messaging.io.ObjectProxy\x0A\x0B\x01\x09spam\x06\x09eggs\x01' #array element 1 (test_dict encoded) result += '\x0A\x02' # array element 2 (reference to test_dict) buf = encode.encode(test, EncoderContext(use_proxies=True, amf3=True)) self.assertEquals(result, buf)
def testAnonObjRef(self): test_obj = self.Spam() test = (test_obj, test_obj) result = '\x09\x05\x01' #array header result += '\x0A\x0B\x01\x09spam\x06\x09eggs\x01' #array element 1 (test_obj encoded) result += '\x0A\x02' # array element 2 (reference to test_obj) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testListRefs(self): test_list = [0, 1, 2, 3]; test = (test_list, test_list) result = '\x09\x05\x01' # array header result += '\x09\x09\x01\x04\x00\x04\x01\x04\x02\x04\x03' # array element 1 (test_list encoded) result += '\x09\x02' # array element 2 (reference to test_list) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
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(): buf = encode.encode(string, EncoderContext(amf3=True)) self.assertEquals(encoding, buf)
def testTupleRefs(self): test_tuple = (0, 1, 2, 3) test = [test_tuple, test_tuple] result = '\x09\x05\x01' # array header result += '\x09\x09\x01\x04\x00\x04\x01\x04\x02\x04\x03' # array element 1 (test_tuple encoded) result += '\x09\x02' # array element 2 (reference to test_tuple) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testLong(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(): buf = encode.encode(number, EncoderContext(amf3=True)) self.assertEquals(encoding, buf)
def testDictRef(self): test_dict = {'spam': 'eggs'}; test = (test_dict, test_dict) result = '\x09\x05\x01' #array header result += '\x0A\x0B\x01\x09spam\x06\x09eggs\x01' #array element 1 (test_dict encoded) result += '\x0A\x02' # array element 2 (reference to test_dict) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testTupleRefs(self): test_tuple = (0, 1, 2, 3); test = [test_tuple, test_tuple] result = '\x09\x05\x01' # array header result += '\x09\x09\x01\x04\x00\x04\x01\x04\x02\x04\x03' # array element 1 (test_tuple encoded) result += '\x09\x02' # array element 2 (reference to test_tuple) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testArrayCollectionRef(self): test_tuple = (0, 1, 2, 3); test = [test_tuple, test_tuple] result = '\x0A\x07\x43flex.messaging.io.ArrayCollection\x09\x05\x01' # array header result += '\x0A\x01\x09\x09\x01\x04\x00\x04\x01\x04\x02\x04\x03' # array element 1 (test_tuple encoded) result += '\x0A\x04' # array element 2 (reference to test_tuple) buf = encode.encode(test, EncoderContext(use_collections=True, amf3=True)) self.assertEquals(result, buf)
def testListRefs(self): test_list = [0, 1, 2, 3] test = (test_list, test_list) result = '\x09\x05\x01' # array header result += '\x09\x09\x01\x04\x00\x04\x01\x04\x02\x04\x03' # array element 1 (test_list encoded) result += '\x09\x02' # array element 2 (reference to test_list) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testList(self): test = [0, 1, 2, 3] result = '\x09\x09\x01' #array header result += '\x04\x00' #array element 1 result += '\x04\x01' #array element 2 result += '\x04\x02' #array element 3 result += '\x04\x03' #array element buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testDictForceNoProxy(self): from amfast.class_def.as_types import AsNoProxy result = '\x0A\x0B\x01' # Object header result += '\x09spam' # key result += '\x06\x09eggs' #value result += '\x01' # empty string terminator buf = encode.encode(AsNoProxy({'spam': 'eggs'}), EncoderContext(amf3=True, use_proxies=True)) self.assertEquals(result, buf)
def testList(self): test = [0, 1, 2, 3]; result = '\x09\x09\x01' #array header result += '\x04\x00' #array element 1 result += '\x04\x01' #array element 2 result += '\x04\x02' #array element 3 result += '\x04\x03' #array element buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf);
def testXml(self): import xml.dom.minidom xml_str = '<?xml version="1.0" ?><test>\n <test_me>tester</test_me>\n </test>' # encoded XML decoded = xml.dom.minidom.parseString(xml_str) encoded = '\x0F' # XML header encoded += '\x00\x00\x00\x55' # String header encoded += xml_str self.assertEquals(encoded, encode.encode(decoded))
def testDictForcedObjectProxy(self): from amfast.class_def.as_types import AsProxy result = '\x0A\x07\x3Bflex.messaging.io.ObjectProxy' # Object header result += '\x0A\x0B\x01' # Object header result += '\x09spam' # key result += '\x06\x09eggs' #value result += '\x01' # empty string terminator buf = encode.encode(AsProxy({'spam': 'eggs'}), EncoderContext(use_proxies=False, amf3=True)) self.assertEquals(result, buf)
def testArrayCollectionRef(self): test_tuple = (0, 1, 2, 3) test = [test_tuple, test_tuple] result = '\x0A\x07\x43flex.messaging.io.ArrayCollection\x09\x05\x01' # array header result += '\x0A\x01\x09\x09\x01\x04\x00\x04\x01\x04\x02\x04\x03' # array element 1 (test_tuple encoded) result += '\x0A\x04' # array element 2 (reference to test_tuple) buf = encode.encode(test, EncoderContext(use_collections=True, amf3=True)) self.assertEquals(result, buf)
def testRefsOff(self): hello = u'hello' test = [hello, hello, '', hello] result = '\x09\x09\x01' # array header result += '\x06\x0bhello' # array element 1 (hello encoded) result += '\x06\x0bhello' #array element 2 (hello encoded) result += '\x06\x01' #array element 3 (empty string) result += '\x06\x0bhello' #array element 4 (hello encoded) buf = encode.encode(test, EncoderContext(use_references=False, amf3=True)) self.assertEquals(result, buf)
def testUnicodeRefs(self): hello = u'hello' test = [hello, hello, '', hello] result = '\x09\x09\x01' # array header result += '\x06\x0bhello' # array element 1 (hello encoded) result += '\x06\x00' #array element 2 (reference to hello) result += '\x06\x01' #array element 3 (empty string) result += '\x06\x00' #array element 4 (reference to hello) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testDateReferences(self): import datetime test_date = datetime.datetime(2005, 3, 18, 1, 58, 31) test = [test_date, test_date] result = '\x09\x05\x01' #array header result += '\x08\x01Bp+6!\x15\x80\x00' #array element 1 (test_date encoded) result += '\x08\x02' # array element 2 (reference to test_date) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testDictRef(self): test_dict = { 'spam': 'eggs' } test = (test_dict, test_dict) result = '\x09\x05\x01' #array header result += '\x0A\x0B\x01\x09spam\x06\x09eggs\x01' #array element 1 (test_dict encoded) result += '\x0A\x02' # array element 2 (reference to test_dict) buf = encode.encode(test, EncoderContext(amf3=True)) self.assertEquals(result, buf)
def testDynamicObj(self): self.class_mapper.mapClass(class_def.DynamicClassDef(self.Spam, 'alias.spam', ())) test = self.Spam() result = '\x0A\x0B\x15alias.spam' result += '\x09spam\x06\x09eggs\x01' # dynamic attrs buf = encode.encode(test, EncoderContext(\ class_def_mapper=self.class_mapper, amf3=True)) self.class_mapper.unmapClass(self.Spam) self.assertEquals(result, buf)
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(encoding, encode.encode(number))