Beispiel #1
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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
0
def fdouble_len(val, ndx):
    h_len = field_hdr_len(ndx, FieldTypes.F_DOUBLE)
    return h_len + 8
Beispiel #8
0
def fsint64_len(val, ndx):
    h_len = field_hdr_len(ndx, FieldTypes.F_SINT64)
    return h_len + 8
Beispiel #9
0
def ffloat_len(val, ndx):
    h_len = field_hdr_len(ndx, FieldTypes.F_FLOAT)
    return h_len + 4
Beispiel #10
0
def fsint32_len(val, ndx):
    h_len = field_hdr_len(ndx, FieldTypes.F_SINT32)
    return h_len + 4
Beispiel #11
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))
Beispiel #12
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 #13
0
def vbool_len(val, ndx):
    h_len = field_hdr_len(ndx, FieldTypes.V_BOOL)
    return h_len + 1  # header plus one for value
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 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 #16
0
def fbytes32_len(val, ndx):
    """ Return the length of an fbytes32 field, with ndx the field number. """
    h_len = field_hdr_len(ndx, FieldTypes.F_BYTES32)
    return h_len + 32
Beispiel #17
0
def vsint64_len(val, ndx):
    h_len = field_hdr_len(ndx, FieldTypes.V_SINT64)
    return h_len + length_as_varint(encode_sint64(val))