Skip to content

Commit 00c49eb

Browse files
committed
Send password rejection before kicking client
1 parent 37e4166 commit 00c49eb

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

libs/s25main/network/GameServer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,12 @@ bool GameServer::OnGameMessage(const GameMessage_Server_Password& msg)
938938
} else
939939
playerInfos[msg.senderPlayerID].isHost = false;
940940

941-
player->sendMsgAsync(new GameMessage_Server_Password(passwordok));
942-
943941
if(passwordok == "false")
942+
{
943+
player->sendMsg(GameMessage_Server_Password(passwordok));
944944
KickPlayer(msg.senderPlayerID, KickReason::WrongPassword, __LINE__);
945+
} else
946+
player->sendMsgAsync(new GameMessage_Server_Password(passwordok));
945947
return true;
946948
}
947949

tests/s25Main/network/testGameClient.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,34 @@ BOOST_AUTO_TEST_CASE(ClientDetectsMapBufferOverflow)
287287
BOOST_TEST(client.GetState() == ClientState::Stopped);
288288
}
289289

290+
BOOST_AUTO_TEST_CASE(ClientReportsWrongPasswordResponse)
291+
{
292+
GameClient client;
293+
GameMessageInterface& clientMsgInterface = client;
294+
MockClientInterface callbacks;
295+
client.SetInterface(&callbacks);
296+
TestServer server;
297+
const auto serverPort = server.tryListen();
298+
BOOST_TEST_REQUIRE(serverPort >= 0);
299+
const auto serverType = rttr::test::randomEnum<ServerType>();
300+
mock::sequence s;
301+
MOCK_EXPECT(callbacks.CI_NextConnectState).in(s).with(ConnectState::Initiated).once();
302+
MOCK_EXPECT(callbacks.CI_NextConnectState).in(s).with(ConnectState::VerifyServer).once();
303+
MOCK_EXPECT(callbacks.CI_NextConnectState).in(s).with(ConnectState::QueryPw).once();
304+
MOCK_EXPECT(callbacks.CI_Error).in(s).with(ClientError::WrongPassword).once();
305+
306+
BOOST_TEST_REQUIRE(client.Connect("localhost", rttr::test::randString(10), serverType, serverPort, false, false));
307+
clientMsgInterface.OnGameMessage(GameMessage_Player_Id(1));
308+
clientMsgInterface.OnGameMessage(GameMessage_Server_TypeOK(GameMessage_Server_TypeOK::StatusCode::Ok, ""));
309+
BOOST_TEST_REQUIRE(boost::dynamic_pointer_cast<GameMessage_Server_Type>(client.GetMainPlayer().sendQueue.pop()));
310+
BOOST_TEST_REQUIRE(
311+
boost::dynamic_pointer_cast<GameMessage_Server_Password>(client.GetMainPlayer().sendQueue.pop()));
312+
313+
clientMsgInterface.OnGameMessage(GameMessage_Server_Password("false"));
314+
315+
BOOST_TEST(client.GetState() == ClientState::Stopped);
316+
}
317+
290318
using namespace std::chrono_literals;
291319

292320
BOOST_AUTO_TEST_CASE(CanSetNewSpeed)

0 commit comments

Comments
 (0)