Пример #1
0
    def test_mixed_array(self):
        x = miniamf.MixedArray()
        x.update({0: u'hello', 'spam': u'eggs'})

        self.assertEncoded(x, b'\t\x03\tspam\x06\teggs\x01\x06\x0bhello')

        x = miniamf.MixedArray()
        x.update({0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 'a': 'a'})

        self.assertEncoded(
            x,
            b'\x09\x0d\x03\x61\x06\x00\x01\x04\x00\x04\x01\x04\x02\x04\x03\x04'
            b'\x04\x04\x05')
Пример #2
0
    def test_mixed_array(self):
        bytes = (
            b'\x08\x00\x00\x00\x00\x00\x01a\x00?\xf0\x00\x00\x00\x00\x00\x00'
            b'\x00\x01c\x00@\x08\x00\x00\x00\x00\x00\x00\x00\x01b\x00@\x00\x00'
            b'\x00\x00\x00\x00\x00\x00\x00\t')

        self.assertDecoded(miniamf.MixedArray(a=1, b=2, c=3), bytes)

        self.buf.append(bytes)
        self.decoder.readElement()
Пример #3
0
    def test_object(self):
        obj = miniamf.MixedArray(spam='eggs')

        self.x.writeObject(obj)
        self.assertEqual(self.buf.getvalue(), b'\t\x01\tspam\x06\teggs\x01')
        self.buf.truncate()
        self.buf.seek(0, 0)

        # check references
        self.x.writeObject(obj)
        self.assertEqual(self.buf.getvalue(), b'\t\x00')
Пример #4
0
    def test_mixed_array(self):
        d = miniamf.MixedArray(a=1, b=2, c=3)

        bytes = (b'\x08\x00\x00\x00\x00',
                 (b'\x00\x01a\x00?\xf0\x00\x00\x00\x00\x00\x00',
                  b'\x00\x01c\x00@\x08\x00\x00\x00\x00\x00\x00',
                  b'\x00\x01b\x00@\x00\x00\x00\x00\x00\x00\x00'),
                 b'\x00\x00\t')

        self.assertEncoded(d, bytes)

        # test the reference
        self.assertEqual(self.encode(d), b'\x07\x00\x00')
Пример #5
0
    def test_numerical_keys_mixed_array(self):
        """
        Numerical keys in L{miniamf.MixedArray} must not cause a KeyError on
        decode.

        @see: #843
        """
        x = miniamf.MixedArray({'10': u'foobar'})

        bytes = miniamf.encode(x, encoding=miniamf.AMF0)

        d = list(miniamf.decode(bytes, encoding=miniamf.AMF0))

        self.assertEqual(d, [{10: u'foobar'}])
Пример #6
0
    def readArray(self):
        """
        Reads an array from the stream.

        @warning: There is a very specific problem with AMF3 where the first
        three bytes of an encoded empty C{dict} will mirror that of an encoded
        C{{'': 1, '2': 2}}
        """
        size = self.readInteger(False)

        if size & REFERENCE_BIT == 0:
            return self.context.getObject(size >> 1)

        size >>= 1

        key = self.readString()

        if key == '':
            # integer indexes only -> python list
            result = []
            self.context.addObject(result)

            for i in range(size):
                result.append(self.readElement())

            return result

        result = miniamf.MixedArray()
        self.context.addObject(result)

        while key:
            result[key] = self.readElement()
            key = self.readString()

        for i in range(size):
            el = self.readElement()
            result[i] = el

        return result
Пример #7
0
    def readMixedArray(self):
        """
        Read mixed array.

        @rtype: L{miniamf.MixedArray}
        """
        # TODO: something with the length/strict
        self.stream.read_ulong()  # length

        obj = miniamf.MixedArray()
        self.context.addObject(obj)

        attrs = self.readObjectAttributes(obj)

        for key, value in six.iteritems(attrs):
            try:
                key = int(key)
            except ValueError:
                pass

            obj[key] = value

        return obj
Пример #8
0
 def x():
     y = miniamf.MixedArray()
     y.update({'': 1, 0: 1})
     self.encode(y)