def test_addVarSeq(self): w = Writer() w.addVarSeq([16, 17, 18], 2, 2) self.assertEqual( bytearray(b'\x00\x06' + b'\x00\x10' + b'\x00\x11' + b'\x00\x12'), w.bytes)
def test_addVarSeq_triple_byte_data(self): w = Writer() w.addVarSeq([0xaa, 0xbb, 0xcc], 3, 2) self.assertEqual( bytearray(b'\x00\x09' + b'\x00\x00\xaa' + b'\x00\x00\xbb' + b'\x00\x00\xcc'), w.bytes)
def test_add_five_twice(self): w = Writer() w.add(0x0102030405, 5) w.add(0x1112131415, 5) self.assertEqual( bytearray(b'\x01\x02\x03\x04\x05' b'\x11\x12\x13\x14\x15'), w.bytes)
def extData(self): """Serialise the extension.""" if self.renegotiated_connection is None: return bytearray(0) writer = Writer() writer.addVarSeq(self.renegotiated_connection, 1, 1) return writer.bytes
def test_add_five_twice(self): w = Writer() w.add(0x0102030405, 5) w.add(0x1112131415, 5) self.assertEqual(bytearray(b'\x01\x02\x03\x04\x05' b'\x11\x12\x13\x14\x15'), w.bytes)
def test_add_var_bytes(self): w = Writer() w.add_var_bytes(b'test', 3) self.assertEqual(bytearray( b'\x00\x00\x04' b'test'), w.bytes)
def test_addVarSeq(self): w = Writer() w.addVarSeq([16, 17, 18], 2, 2) self.assertEqual(bytearray( b'\x00\x06' + b'\x00\x10' + b'\x00\x11' + b'\x00\x12'), w.bytes)
def test_addVarSeq_triple_byte_data(self): w = Writer() w.addVarSeq([0xaa, 0xbb, 0xcc], 3, 2) self.assertEqual(bytearray( b'\x00\x09' + b'\x00\x00\xaa' + b'\x00\x00\xbb' + b'\x00\x00\xcc'), w.bytes)
def test_addVarTupleSeq(self): w = Writer() w.addVarTupleSeq([(1, 2), (2, 9)], 1, 2) self.assertEqual(bytearray( b'\x00\x04' + # length b'\x01\x02' + # first tuple b'\x02\x09' # second tuple ), w.bytes)
def test_addVarTupleSeq_with_single_element_tuples(self): w = Writer() w.addVarTupleSeq([[1], [9], [12]], 2, 3) self.assertEqual(bytearray( b'\x00\x00\x06' + # length b'\x00\x01' + # 1st element b'\x00\x09' + # 2nd element b'\x00\x0c'), w.bytes)
def test_addVarTupleSeq(self): w = Writer() w.addVarTupleSeq([(1, 2), (2, 9)], 1, 2) self.assertEqual( bytearray(b'\x00\x04' + # length b'\x01\x02' + # first tuple b'\x02\x09' # second tuple ), w.bytes)
def test_addVarTupleSeq_with_single_element_tuples(self): w = Writer() w.addVarTupleSeq([[1], [9], [12]], 2, 3) self.assertEqual( bytearray(b'\x00\x00\x06' + # length b'\x00\x01' + # 1st element b'\x00\x09' + # 2nd element b'\x00\x0c'), w.bytes)
def post_write(writer, self=msg, size=size, pad_byte=pad_byte, pad=pad): """Monkey patch for the postWrite of handshake messages""" if pad is not None: size = len(pad) header_writer = Writer() header_writer.add(self.handshakeType, 1) header_writer.add(len(writer.bytes) + size, 3) if pad is not None: return header_writer.bytes + writer.bytes + pad elif size < 0: return header_writer.bytes + writer.bytes[:size] else: return header_writer.bytes + writer.bytes + \ bytearray([pad_byte]*size)
def extData(self): """Serialise the extension.""" if self.client_shares is None: return bytearray(0) writer = Writer() for group_id, share in self.client_shares: writer.add(group_id, 2) if group_id in GroupName.allFF: share_length_length = 2 else: share_length_length = 1 writer.addVarSeq(share, 1, share_length_length) ext_writer = Writer() ext_writer.add(len(writer.bytes), 2) ext_writer.bytes += writer.bytes return ext_writer.bytes
def _setup_tls13_handshake_keys(self, state): """Prepare handshake ciphers for the HRR handling""" prf_name = state.prf_name ch_hash = self._ch_hh.digest(prf_name) new_hh = HandshakeHashes() writer = Writer() writer.add(HandshakeType.message_hash, 1) writer.addVarSeq(ch_hash, 1, 3) new_hh.update(writer.bytes) new_hh.update(self._msg.write()) state.handshake_hashes = new_hh
def post_write(writer, self=msg, size=size, pad_byte=pad_byte, pad=pad): """Monkey patch for the postWrite of handshake messages.""" if pad is not None: size = len(pad) header_writer = Writer() header_writer.add(self.handshakeType, 1) header_writer.add(len(writer.bytes) + size, 3) if pad is not None: return header_writer.bytes + writer.bytes + pad elif size < 0: return header_writer.bytes + writer.bytes[:size] else: return header_writer.bytes + writer.bytes + \ bytearray([pad_byte]*size)
def test_add_with_six_bytes_data(self): w = Writer() w.add(0x010203040506, 6) self.assertEqual(bytearray(b'\x01\x02\x03\x04\x05\x06'), w.bytes)
def test_addVarSeq_with_three_byte_overflowing_data(self): w = Writer() with self.assertRaises(ValueError): w.addVarSeq([16, 17, 0x1000000], 3, 2)
def test_add_with_four_bytes_overflowing_data(self): w = Writer() with self.assertRaises(ValueError): w.add(0x0100000000, 4)
def test_addFixSeq(self): w = Writer() w.addFixSeq([16,17,18], 2) self.assertEqual(bytearray(b'\x00\x10\x00\x11\x00\x12'), w.bytes)
def test___init__(self): w = Writer() self.assertEqual(bytearray(0), w.bytes)
def test_add_with_five_overflowing_bytes(self): w = Writer() with self.assertRaises(ValueError): w.add(0x010000000000, 5)
def test_addVarTupleSeq_with_double_byte_invalid_sized_tuples(self): w = Writer() with self.assertRaises(ValueError): w.addVarTupleSeq([(1, 2), (2, 3, 4)], 2, 2)
def test_add_with_overflowing_data(self): w = Writer() with self.assertRaises(ValueError): w.add(256, 1)
def test_addFixSeq(self): w = Writer() w.addFixSeq([16, 17, 18], 2) self.assertEqual(bytearray(b'\x00\x10\x00\x11\x00\x12'), w.bytes)
def test_add_with_multibyte_data(self): w = Writer() w.add(512, 2) self.assertEqual(bytearray(b'\x02\x00'), w.bytes)
def test_add_with_multibyte_field(self): w = Writer() w.add(32, 2) self.assertEqual(bytearray(b'\x00\x20'), w.bytes)
def test_add(self): w = Writer() w.add(255, 1) self.assertEqual(bytearray(b'\xff'), w.bytes)
def test_add_with_four_byte_data(self): w = Writer() w.add(0x01020304, 4) self.assertEqual(bytearray(b'\x01\x02\x03\x04'), w.bytes)
def test_addVarTupleSeq_with_double_byte_overflowing_data(self): w = Writer() with self.assertRaises(ValueError): w.addVarTupleSeq([(1, 2), (3, 0x10000)], 2, 2)
def test_addVarTupleSeq_with_empty_array(self): w = Writer() w.addVarTupleSeq([], 1, 2) self.assertEqual(bytearray( b'\x00\x00'), w.bytes)
def test_add_with_five_bytes_data(self): w = Writer() w.add(0x02, 5) self.assertEqual(bytearray(b'\x00\x00\x00\x00\x02'), w.bytes)
def test_bytes(self): w = Writer() w.bytes += bytearray(b'\xbe\xef') w.add(15, 1) self.assertEqual(bytearray(b'\xbe\xef\x0f'), w.bytes)
def test_add_var_bytes(self): w = Writer() w.add_var_bytes(b'test', 3) self.assertEqual(bytearray(b'\x00\x00\x04' b'test'), w.bytes)
def test_addFixSeq_with_overflowing_data(self): w = Writer() with self.assertRaises(ValueError): w.addFixSeq([16, 17, 256], 1)
def test_add_with_five_underflowing_bytes(self): w = Writer() with self.assertRaises(ValueError): w.add(-1, 5)
def test_addVarTupleSeq_with_empty_array(self): w = Writer() w.addVarTupleSeq([], 1, 2) self.assertEqual(bytearray(b'\x00\x00'), w.bytes)
def test_addVarSeq(self): w = Writer() w.addVarSeq([16, 17, 18], 2, 2) self.assertEqual(bytearray(b"\x00\x06" + b"\x00\x10" + b"\x00\x11" + b"\x00\x12"), w.bytes)
def test_add_with_three_underflowing_data(self): w = Writer() with self.assertRaises(ValueError): w.add(-1, 3)
def test_addVarTupleSeq_with_overflowing_data(self): w = Writer() with self.assertRaises(ValueError): w.addVarTupleSeq([(1, 2), (2, 256)], 1, 2)
def test_add_with_three_byte_data(self): w = Writer() w.add(0xbacc01, 3) self.assertEqual(bytearray(b'\xba\xcc\x01'), w.bytes)
def test_add_with_underflowing_data(self): w = Writer() with self.assertRaises(ValueError): w.add(-1, 1)
def test_addVarTupleSeq_with_invalid_sized_tuples(self): w = Writer() with self.assertRaises(ValueError): w.addVarTupleSeq([(1, 2), (2, 3, 9)], 1, 2)