def test_packet_str(self): class ExamplePacket(Packet): id = 0x00 packet_name = 'example' definition = [{ 'alpha': VarInt }, { 'beta': VarInt }, { 'gamma': VarInt }] class Alpha(Enum): ZERO = 0 class Beta(Enum): ONE = 1 self.assertEqual( str(ExamplePacket(ConnectionContext(), alpha=0, beta=0, gamma=0)), '0x00 ExamplePacket(alpha=ZERO, beta=0, gamma=0)')
def test_encryption_online_server(self, encrypt): connection = mock.MagicMock() connection.context = ConnectionContext(protocol_version=latest_proto) reactor = LoginReactor(connection) packet = clientbound.login.EncryptionRequestPacket() packet.server_id = "123" packet.public_key = b"asdf" packet.verify_token = b"23" secret = b"secret" encrypt.generate_shared_secret.return_value = secret encrypt.encrypt_token_and_secret.return_value = (b"a", b"b") encrypt.generate_verification_hash.return_value = b"hash" reactor.react(packet) encrypt.encrypt_token_and_secret.assert_called_once_with( packet.public_key, packet.verify_token, secret ) connection.auth_token.join.assert_called_once_with(b"hash") self.assertEqual(connection.write_packet.call_count, 1)
def setUp(self): self.context = ConnectionContext() self.context.protocol_version = SUPPORTED_PROTOCOL_VERSIONS[-1]
def test_packet_roundtrip(self): self.packet_roundtrip(ConnectionContext(protocol_version=106)) self.packet_roundtrip(ConnectionContext(protocol_version=107)) self.packet_roundtrip(ConnectionContext(protocol_version=379)) self.packet_roundtrip(ConnectionContext(protocol_version=379), width=0)
def test_add_and_others(self): for protocol_version in TEST_VERSIONS: context = ConnectionContext(protocol_version=protocol_version) player_list = PlayerListItemPacket.PlayerList() by_uuid = player_list.players_by_uuid packet_buffer = self.make_add_player_packet( context, display_name=False) self.read_and_apply(context, packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].gamemode, 42) self.assertEqual(by_uuid[fake_uuid].ping, 69) self.assertIsNone(by_uuid[fake_uuid].display_name) # Change the game mode packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.UpdateGameModeAction, actions=[ PlayerListItemPacket.UpdateGameModeAction( uuid=fake_uuid, gamemode=43), ], ) self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=UpdateGameModeAction, actions=[" "UpdateGameModeAction(uuid=%r, gamemode=43)])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].gamemode, 43) # Change the ping packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.UpdateLatencyAction, actions=[ PlayerListItemPacket.UpdateLatencyAction( uuid=fake_uuid, ping=70), ], ) self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=UpdateLatencyAction, actions=[" "UpdateLatencyAction(uuid=%r, ping=70)])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].ping, 70) # Change the display name packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.UpdateDisplayNameAction, actions=[ PlayerListItemPacket.UpdateDisplayNameAction( uuid=fake_uuid, display_name='Badmonson'), ], ) self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=UpdateDisplayNameAction, actions=[" "UpdateDisplayNameAction(uuid=%r, display_name='Badmonson')])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].display_name, 'Badmonson') # Remove the display name packet_buffer = PacketBuffer() PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.UpdateDisplayNameAction, actions=[ PlayerListItemPacket.UpdateDisplayNameAction( uuid=fake_uuid, display_name=None), ], ).write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertIsNone(by_uuid[fake_uuid].display_name) # Remove the player packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.RemovePlayerAction, actions=[ PlayerListItemPacket.RemovePlayerAction(uuid=fake_uuid), ], ) self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=RemovePlayerAction, actions=[RemovePlayerAction(" "uuid=%r)])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) self.read_and_apply(context, packet_buffer, player_list) self.assertNotIn(fake_uuid, player_list.players_by_uuid)
def test_spawn_object_packet(self): for protocol_version in TEST_VERSIONS: logging.debug('protocol_version = %r' % protocol_version) context = ConnectionContext(protocol_version=protocol_version) EntityType = clientbound.play.SpawnObjectPacket.field_enum( 'type_id', context) pos_look = PositionAndLook( position=(Vector(68.0, 38.0, 76.0) if protocol_version >= 100 else Vector(68, 38, 76)), yaw=263.494, pitch=180) velocity = Vector(21, 55, 41) entity_id, type_name, type_id = 49846, 'EGG', EntityType.EGG packet = clientbound.play.SpawnObjectPacket(context=context, x=pos_look.x, y=pos_look.y, z=pos_look.z, yaw=pos_look.yaw, pitch=pos_look.pitch, velocity_x=velocity.x, velocity_y=velocity.y, velocity_z=velocity.z, entity_id=entity_id, type_id=type_id, data=1) if protocol_version >= 49: object_uuid = 'd9568851-85bc-4a10-8d6a-261d130626fa' packet.object_uuid = object_uuid self.assertEqual(packet.objectUUID, object_uuid) self.assertEqual(packet.position_and_look, pos_look) self.assertEqual(packet.position, pos_look.position) self.assertEqual(packet.velocity, velocity) self.assertEqual(packet.type, type_name) self.assertEqual( str(packet), "0x%02X SpawnObjectPacket(entity_id=49846, " "object_uuid='d9568851-85bc-4a10-8d6a-261d130626fa', " "type_id=EGG, x=68.0, y=38.0, z=76.0, pitch=180, yaw=263.494, " "data=1, velocity_x=21, velocity_y=55, velocity_z=41)" % packet.id if protocol_version >= 100 else "0x%02X SpawnObjectPacket(entity_id=49846, " "object_uuid='d9568851-85bc-4a10-8d6a-261d130626fa', " "type_id=EGG, x=68, y=38, z=76, pitch=180, yaw=263.494, " "data=1, velocity_x=21, velocity_y=55, velocity_z=41)" % packet.id if protocol_version >= 49 else "0x%02X SpawnObjectPacket(entity_id=49846, type_id=EGG, " "x=68, y=38, z=76, pitch=180, yaw=263.494, data=1, " "velocity_x=21, velocity_y=55, velocity_z=41)" % packet.id) packet2 = clientbound.play.SpawnObjectPacket( context=context, position_and_look=pos_look, velocity=velocity, type=type_name, entity_id=entity_id, data=1) if protocol_version >= 49: packet2.object_uuid = object_uuid self.assertEqual(packet.__dict__, packet2.__dict__) packet2.position = pos_look.position self.assertEqual(packet.position, packet2.position) packet2.data = 0 if protocol_version < 49: del packet2.velocity self._test_read_write_packet(packet, context, yaw=360 / 256, pitch=360 / 256) self._test_read_write_packet(packet2, context, yaw=360 / 256, pitch=360 / 256)