def test_invalid_action(self): packet_buffer = PacketBuffer() VarInt.send(200, packet_buffer) # action_id packet_buffer.reset_cursor() with self.assertRaises(ValueError): PlayerListItemPacket().read(packet_buffer)
def test_base_action(self): packet_buffer = PacketBuffer() UUID.send(fake_uuid, packet_buffer) packet_buffer.reset_cursor() with self.assertRaises(NotImplementedError): action = PlayerListItemPacket.Action() action.read(packet_buffer)
def make_add_player_packet(self, context, display_name=True): packet_buffer = PacketBuffer() packet = PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.AddPlayerAction, actions=[ PlayerListItemPacket.AddPlayerAction( uuid=fake_uuid, name='goodmonson', properties=[ PlayerListItemPacket.PlayerProperty( name='property1', value='value1', signature=None), PlayerListItemPacket.PlayerProperty( name='property2', value='value2', signature='gm') ], gamemode=42, ping=69, display_name='Goodmonson' if display_name else None ), ], ) if display_name: self.assertEqual( str(packet), "0x%02X PlayerListItemPacket(" "action_type=AddPlayerAction, actions=[AddPlayerAction(" "uuid=%r, name='goodmonson', properties=[PlayerProperty(" "name='property1', value='value1', signature=None), " "PlayerProperty(name='property2', value='value2', " "signature='gm')], gamemode=42, ping=69, " "display_name='Goodmonson')])" % (packet.id, fake_uuid)) packet.write_fields(packet_buffer) packet_buffer.reset_cursor() return packet_buffer
def test_add_player_action(self): player_list = PlayerListItemPacket.PlayerList() packet_buffer = self.make_add_player_packet() packet = PlayerListItemPacket() packet.read(packet_buffer) packet.apply(player_list) self.assertIn(fake_uuid, player_list.players_by_uuid) player = player_list.players_by_uuid[fake_uuid] self.assertEqual(player.name, "player") self.assertEqual(player.properties[0].name, "property1") self.assertIsNone(player.properties[0].signature) self.assertEqual(player.properties[1].value, "value2") self.assertEqual(player.properties[1].signature, "signature") self.assertEqual(player.gamemode, 42) self.assertEqual(player.ping, 69) self.assertEqual(player.display_name, "display")
def make_add_player_packet(context, display_name=True): packet_buffer = PacketBuffer() PlayerListItemPacket( context=context, action_type=PlayerListItemPacket.AddPlayerAction, actions=[ PlayerListItemPacket.AddPlayerAction( uuid=fake_uuid, name='goodmonson', properties=[ PlayerListItemPacket.PlayerProperty(name='property1', value='value1', signature=None), PlayerListItemPacket.PlayerProperty(name='property2', value='value2', signature='gm') ], gamemode=42, ping=69, display_name='Goodmonson' if display_name else None), ], ).write_fields(packet_buffer) packet_buffer.reset_cursor() return packet_buffer
def test_add_and_others(self): player_list = PlayerListItemPacket.PlayerList() by_uuid = player_list.players_by_uuid packet_buffer = self.make_add_player_packet(display_name=False) self.read_and_apply(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 = self.make_action_base(1) VarInt.send(43, packet_buffer) # gamemode self.read_and_apply(packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].gamemode, 43) # Change the ping packet_buffer = self.make_action_base(2) VarInt.send(70, packet_buffer) # ping self.read_and_apply(packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].ping, 70) # Change the display name packet_buffer = self.make_action_base(3) Boolean.send(True, packet_buffer) String.send("display2", packet_buffer) self.read_and_apply(packet_buffer, player_list) self.assertEqual(by_uuid[fake_uuid].display_name, "display2") # Remove the display name packet_buffer = self.make_action_base(3) Boolean.send(False, packet_buffer) self.read_and_apply(packet_buffer, player_list) self.assertIsNone(by_uuid[fake_uuid].display_name) # Remove the player packet_buffer = self.make_action_base(4) self.read_and_apply(packet_buffer, player_list) self.assertNotIn(fake_uuid, player_list.players_by_uuid)
def test_add_player_action(self): for protocol_version in TEST_VERSIONS: context = ConnectionContext(protocol_version=protocol_version) player_list = PlayerListItemPacket.PlayerList() packet_buffer = self.make_add_player_packet(context) packet = PlayerListItemPacket(context) packet.read(packet_buffer) packet.apply(player_list) self.assertIn(fake_uuid, player_list.players_by_uuid) player = player_list.players_by_uuid[fake_uuid] self.assertEqual(player.name, 'goodmonson') self.assertEqual(player.properties[0].name, 'property1') self.assertIsNone(player.properties[0].signature) self.assertEqual(player.properties[1].value, 'value2') self.assertEqual(player.properties[1].signature, 'gm') self.assertEqual(player.gamemode, 42) self.assertEqual(player.ping, 69) self.assertEqual(player.display_name, 'Goodmonson')
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 read_and_apply(self, context, packet_buffer, player_list): packet_buffer.reset_cursor() packet = PlayerListItemPacket(context) packet.read(packet_buffer) packet.apply(player_list)
def read_and_apply(self, packet_buffer, player_list): packet_buffer.reset_cursor() packet = PlayerListItemPacket() packet.read(packet_buffer) packet.apply(player_list)