Beispiel #1
0
    def round_trip(self, nnn):
        """
        Test writing and reading a varint as the first and
        only field in a buffer.
        """
        # -- write varint -------------------------------------------
        field_nbr = 1 + self.rng.next_int16(1024)
        chan = Channel(LEN_BUFFER)
        write_varint_field(chan, nnn, field_nbr)
        chan.flip()

        # -- read varint --------------------------------------------
        # first the header (which is a varint) ------------
        (prim_type, field_nbr2) = read_field_hdr(chan)
        offset2 = chan.position
        self.assertEqual(PrimTypes.VARINT, prim_type)
        self.assertEqual(field_nbr, field_nbr2)
        self.assertEqual(length_as_varint(field_nbr << 3), offset2)

        # then the varint proper --------------------------
        varint_ = read_raw_varint(chan)
        chan.flip()
        offset3 = chan.limit
        self.assertEqual(nnn, varint_)
        self.assertEqual(offset2 + length_as_varint(nnn), offset3)
Beispiel #2
0
def enum_spec_prefixed_len(val, nnn):
    # val is guaranteed to be a well-formed EnumSpec object

    # we are going to write the header, then a byte count, then the enum
    # name, then one or more EnumPairSpecs
    len_ = length_as_varint(field_hdr_len(nnn, LEN_PLUS_TYPE))
    count = enum_spec_len(val, nnn)
    return len_ + length_as_varint(count) + count
Beispiel #3
0
 def wire_len(self, nnn):
     """
     Return the length of a serialized message including the field
     header, where n is the field number of a nested message or the
     regID if the message is not nested.
     """
     len_ = length_as_varint(field_hdr_len(nnn, LEN_PLUS_TYPE))
     count = self._wire_len()
     return len_ + length_as_varint(count) + count
Beispiel #4
0
def enum_pair_spec_prefixed_len(val, nnn):
    """
    val is guaranteed to be a well-formed EnumPair object; this is
    the length of the spec on the wire, what is written before the spec.
    So it is the total length of the header plus the length of the encoded
    byte count plus the length of the encoded enumPairSpec.
    """
    len_ = length_as_varint(field_hdr_len(nnn, LEN_PLUS_TYPE))
    byte_count = enum_pair_spec_len(val, nnn)
    return len_ + length_as_varint(byte_count) + byte_count
Beispiel #5
0
    def round_trip64(self, nnn):
        """
        Test writing and reading a 64-bit integer as the first and
        only field in a buffer
        """
        chan = Channel(LEN_BUFF)

        # -- write 64-bit value -------------------------------------
        field_nbr = 1 + self.rng.next_int16(1024)
        write_b64_field(chan, nnn, field_nbr)
        chan.flip()

        #       # DEBUG
        #       buf = chan.buffer
        #       print "buffer after writing varint field: ",
        #       dumpBuffer(buf)
        #       # END

        # -- read 64-bit value --------------------------------------
        # first the header (which is a varint) ------------
        (field_type, field_nbr2) = read_field_hdr(chan)
        offset2 = chan.position
        self.assertEqual(PrimTypes.B64, field_type)
        self.assertEqual(field_nbr, field_nbr2)
        self.assertEqual(
            length_as_varint(field_hdr_val(field_nbr, PrimTypes.B64)), offset2)

        # then the varint proper --------------------------
        varint_ = read_raw_b64(chan)
        offset3 = chan.position
        self.assertEqual(nnn, varint_)
        self.assertEqual(offset2 + 8, offset3)
Beispiel #6
0
    def round_trip32(self, nnn):
        """
        Test writing and reading a 32-bit integer as the first and
        only field in a buffer.
        """
        chan = Channel(LEN_BUFF)

        # -- write 32-bit value -------------------------------------
        field_nbr = 1 + self.rng.next_int16(1024)
        write_b32_field(chan, nnn, field_nbr)
        chan.flip()

        # -- read 32-bit value --------------------------------------
        # first the header (which is a varint) ------------
        (field_type, field_nbr2) = read_field_hdr(chan)
        offset2 = chan.position
        self.assertEqual(PrimTypes.B32, field_type)
        self.assertEqual(field_nbr, field_nbr2)
        self.assertEqual(
            length_as_varint(field_hdr_val(field_nbr, PrimTypes.B32)), offset2)

        # then the varint proper --------------------------
        varint_ = read_raw_b32(chan)
        offset3 = chan.position
        self.assertEqual(nnn, varint_)
        self.assertEqual(offset2 + 4, offset3)
Beispiel #7
0
 def do_len_plus_test(length, ndx):
     """
     Verify that fields of interesting lengths have expected
     raw encodings.
     """
     string = [0] * length
     k = len(string)
     len_ = raw.field_hdr_len(ndx, FieldTypes.L_BYTES)
     expected_len = len_ + raw.length_as_varint(k) + k
     self.assertEqual(expected_len, typed.lbytes_len(string, ndx))
Beispiel #8
0
    def round_trip(self, string):
        """
        Verify that a unicode string converted to wire format and then
        back again is the same string.

        This tests writing and reading a string of bytes as the first and
        only field in a buffer.
        """
        chan = Channel(LEN_BUFF)

        # -- write the bytearray ------------------------------------
        field_nbr = 1 + self.rng.next_int16(1024)
        write_len_plus_field(chan, string, field_nbr)
        chan.flip()

#       # DEBUG
#       print("buffer after writing lenPlus field: " + str(chan.buffer))
#       # END

        # -- read the value written ---------------------------------
        # first the header (which is a varint) ------------
        (field_type, field_nbr2,) = read_field_hdr(chan)
        offset2 = chan.position
        self.assertEqual(PrimTypes.LEN_PLUS, field_type)
        self.assertEqual(field_nbr, field_nbr2)
        self.assertEqual(
            length_as_varint(field_hdr_val(field_nbr, PrimTypes.LEN_PLUS)),
            offset2)

        # then the actual value written -------------------
        tstamp = read_raw_len_plus(chan)
        offset3 = chan.position
        self.assertEqual(string, tstamp)
        self.assertEqual(
            offset2 +
            length_as_varint(
                len(string)) +
            len(string),
            offset3)
Beispiel #9
0
def lmsg_len(val, ndx):
    """ Return the length of an lmsg field, with ndx the field number. """
    h_len = field_hdr_len(ndx, FieldTypes.L_MSG)
    v_len = val.wire_len
    return h_len + length_as_varint(v_len) + v_len
Beispiel #10
0
def lbytes_len(val, ndx):
    """ Return the length of an lbytes field, with ndx the field number. """
    h_len = field_hdr_len(ndx, FieldTypes.L_BYTES)
    v_len = len(val)
    return h_len + length_as_varint(v_len) + v_len
Beispiel #11
0
def l_string_len(val, ndx):
    h_len = field_hdr_len(ndx, FieldTypes.L_STRING)
    v_len = len(val)
    return h_len + length_as_varint(v_len) + v_len
Beispiel #12
0
def vsint64_len(val, ndx):
    h_len = field_hdr_len(ndx, FieldTypes.V_SINT64)
    return h_len + length_as_varint(encode_sint64(val))
Beispiel #13
0
def vuint64_len(val, ndx):
    h_len = field_hdr_len(ndx, FieldTypes.V_UINT64)
    # XXX we constrain val to this range of non-negative ints
    return h_len + length_as_varint(val & 0xffffffffffffffff)
Beispiel #14
0
def field_spec_prefixed_len(val, nnn):
    # val is guaranteed to be a well-formed fieldSpec object
    len_ = length_as_varint(field_hdr_len(nnn, LEN_PLUS_TYPE))
    count = field_spec_len(val, nnn)
    return len_ + length_as_varint(count) + count
Beispiel #15
0
    def test_len_funcs(self):
        """
        Verify that varint length functions return correct values.

        Tests are performed using randomly selected field numbers
        (in the range 0 .. (2^16)-1) and integer values in the same
        range.
        """
        ndx = self.rng.next_int16()  # random field number
        value = self.rng.next_int16()  # random integer value

        # == varint types ===========================================
        # ERROR because field_hdr_len 2nd param should be PrimType
        # ********************************************************
        len_ = raw.field_hdr_len(ndx, FieldTypes.V_BOOL)
        self.assertEqual(len_ + 1, typed.vbool_len(True, ndx))
        self.assertEqual(len_ + 1, typed.vbool_len(False, ndx))

        len_ = raw.field_hdr_len(ndx, FieldTypes.V_ENUM)
        zzz = len_ + raw.length_as_varint(value)
        self.assertEqual(zzz, typed.venum_len(value, ndx))
        # self.assertEqual( x, typed.vEnumLen(-x, n) )

        value = self.rng.next_int32()
        self.assertTrue(value >= 0)

        len_ = raw.field_hdr_len(ndx, FieldTypes.V_UINT32)
        zzz = len_ + raw.length_as_varint(value)
        self.assertEqual(zzz, typed.vuint32_len(value, ndx))

        value = self.rng.next_int32()
        self.assertTrue(value >= 0)
        value = value - 0x80000000

        len_ = raw.field_hdr_len(ndx, FieldTypes.V_SINT32)
        ppp = typed.encode_sint32(value)
        zzz = len_ + raw.length_as_varint(ppp)
        self.assertEqual(zzz, typed.vsint32_len(value, ndx))

        value = self.rng.next_int64()
        self.assertTrue(value >= 0)

        len_ = raw.field_hdr_len(ndx, FieldTypes.V_UINT64)
        zzz = len_ + raw.length_as_varint(value)
        self.assertEqual(zzz, typed.vuint64_len(value, ndx))

        value = self.rng.next_int64()
        self.assertTrue(value >= 0)
        value = value - 0x8000000000000000

        len_ = raw.field_hdr_len(ndx, FieldTypes.V_SINT64)
        ppp = typed.encode_sint64(value)
        zzz = len_ + raw.length_as_varint(ppp)
        self.assertEqual(zzz, typed.vsint64_len(value, ndx))

        # == fixed length 4 byte ====================================
        value = self.rng.next_int64()  # value should be ignored

        self.assertTrue(value >= 0)
        value = value - 0x8000000000000000

        # x is a signed 64 bit value whose value should be irrelevant
        len_ = raw.field_hdr_len(ndx, FieldTypes.F_UINT32)
        self.assertEqual(len_ + 4, typed.fuint32_len(value, ndx))

        len_ = raw.field_hdr_len(ndx, FieldTypes.F_SINT32)
        self.assertEqual(len_ + 4, typed.fsint32_len(value, ndx))

        len_ = raw.field_hdr_len(ndx, FieldTypes.F_FLOAT)
        self.assertEqual(len_ + 4, typed.ffloat_len(value, ndx))

        # == fixed length 8 byte ====================================
        # n is that signed 64 bit value whose value should be irrelevant
        len_ = raw.field_hdr_len(ndx, FieldTypes.F_UINT64)
        self.assertEqual(len_ + 8, typed.fuint64_len(value, ndx))
        len_ = raw.field_hdr_len(ndx, FieldTypes.F_SINT64)
        self.assertEqual(len_ + 8, typed.fsint64_len(value, ndx))
        len_ = raw.field_hdr_len(ndx, FieldTypes.F_DOUBLE)
        self.assertEqual(len_ + 8, typed.fdouble_len(value, ndx))

        # == LEN PLUS types =========================================
        def do_len_plus_test(length, ndx):
            """
            Verify that fields of interesting lengths have expected
            raw encodings.
            """
            string = [0] * length
            k = len(string)
            len_ = raw.field_hdr_len(ndx, FieldTypes.L_BYTES)
            expected_len = len_ + raw.length_as_varint(k) + k
            self.assertEqual(expected_len, typed.lbytes_len(string, ndx))

        # -- lString ---------------------------------------
        string = self.rng.next_file_name(256)
        len_ = raw.field_hdr_len(ndx, FieldTypes.L_STRING)
        k = len(string)
        expected_len = len_ + raw.length_as_varint(k) + k
        self.assertEqual(expected_len, typed.l_string_len(string, ndx))

        # -- lBytes ----------------------------------------
        do_len_plus_test(0x7f, ndx)
        do_len_plus_test(0x80, ndx)
        do_len_plus_test(0x3fff, ndx)
        do_len_plus_test(0x4000, ndx)

        # -- lMsg ------------------------------------------
        # XXX STUB

        # -- fixed length byte arrays -------------------------------
        buf = [0] * 512  # length functions should ignore actual size

        len_ = raw.field_hdr_len(ndx, FieldTypes.F_BYTES16)
        self.assertEqual(len_ + 16, typed.fbytes16_len(buf, ndx))

        len_ = raw.field_hdr_len(ndx, FieldTypes.F_BYTES20)
        self.assertEqual(len_ + 20, typed.fbytes20_len(buf, ndx))

        len_ = raw.field_hdr_len(ndx, FieldTypes.F_BYTES32)
        self.assertEqual(len_ + 32, typed.fbytes32_len(buf, ndx))