From 84be9c62a89d71a0e6fe71ad997391c2f9c11cd9 Mon Sep 17 00:00:00 2001 From: lotodore Date: Fri, 18 Feb 2011 13:39:22 +0000 Subject: [PATCH] Polishing chat and adding test cases for different chat messages. --- src/net/common/servergamestate.cpp | 19 ++- tests/src/pokerth_test/ChatTest.java | 224 +++++++++++++++++++++++++-- 2 files changed, 229 insertions(+), 14 deletions(-) diff --git a/src/net/common/servergamestate.cpp b/src/net/common/servergamestate.cpp index 89ccc6e3..d405f7c4 100644 --- a/src/net/common/servergamestate.cpp +++ b/src/net/common/servergamestate.cpp @@ -255,15 +255,18 @@ AbstractServerGameStateReceiving::ProcessPacket(boost::shared_ptr se } // Chat text is always allowed. else if (packet->GetMsg()->present == PokerTHMessage_PR_chatRequestMessage) { + bool chatSent = false; + ChatRequestMessage_t *netChatRequest = &packet->GetMsg()->choice.chatRequestMessage; // Only forward if this player is known and not a guest. if (session.playerData && session.playerData->GetRights() != PLAYER_RIGHTS_GUEST) { // Forward chat text to all players. // TODO: Some limitation needed. - ChatRequestMessage_t *netChatRequest = &packet->GetMsg()->choice.chatRequestMessage; if (netChatRequest->chatRequestType.present == chatRequestType_PR_chatRequestTypeLobby || netChatRequest->chatRequestType.present == chatRequestType_PR_chatRequestTypePrivate) { - if (!server->IsRunning()) + if (!server->IsRunning()) { server->GetLobbyThread().HandleChatRequest(session, *netChatRequest); + chatSent = true; + } } else if (netChatRequest->chatRequestType.present == chatRequestType_PR_chatRequestTypeGame) { boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); packet->GetMsg()->present = PokerTHMessage_PR_chatMessage; @@ -277,6 +280,7 @@ AbstractServerGameStateReceiving::ProcessPacket(boost::shared_ptr se (char *)netChatRequest->chatText.buf, netChatRequest->chatText.size); server->SendToAllPlayers(packet, SessionData::Game); + chatSent = true; // Send the message to the chat cleaner bot for ranking games. //if (server->GetGameData().gameType == GAME_TYPE_RANKING) @@ -289,6 +293,17 @@ AbstractServerGameStateReceiving::ProcessPacket(boost::shared_ptr se //} } } + // Reject chat otherwise. + if (!chatSent) { + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_chatRejectMessage; + ChatRejectMessage_t *netReject = &packet->GetMsg()->choice.chatRejectMessage; + OCTET_STRING_fromBuf( + &netReject->chatText, + (char *)netChatRequest->chatText.buf, + netChatRequest->chatText.size); + server->GetLobbyThread().GetSender().Send(session.sessionData, packet); + } } else if (packet->GetMsg()->present == PokerTHMessage_PR_subscriptionRequestMessage) { SubscriptionRequestMessage_t *netSubscription = &packet->GetMsg()->choice.subscriptionRequestMessage; if (netSubscription->subscriptionAction == subscriptionAction_resubscribeGameList) { diff --git a/tests/src/pokerth_test/ChatTest.java b/tests/src/pokerth_test/ChatTest.java index 60b54230..dc8f94eb 100644 --- a/tests/src/pokerth_test/ChatTest.java +++ b/tests/src/pokerth_test/ChatTest.java @@ -1,41 +1,241 @@ package pokerth_test; +import static org.junit.Assert.*; + import java.net.Socket; +import java.util.ArrayList; +import java.util.Collection; +import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; import org.junit.Test; import pokerth_protocol.ChatRequestMessage; +import pokerth_protocol.ChatRequestTypeGame; import pokerth_protocol.ChatRequestTypeLobby; +import pokerth_protocol.ChatRequestTypePrivate; +import pokerth_protocol.NetGameInfo; +import pokerth_protocol.NonZeroId; import pokerth_protocol.PokerTHMessage; +import pokerth_protocol.StartEventAckMessage; import pokerth_protocol.ChatRequestMessage.ChatRequestMessageSequenceType; import pokerth_protocol.ChatRequestMessage.ChatRequestMessageSequenceType.ChatRequestTypeChoiceType; +import pokerth_protocol.NetGameInfo.EndRaiseModeEnumType; +import pokerth_protocol.NetGameInfo.NetGameTypeEnumType; +import pokerth_protocol.StartEventAckMessage.StartEventAckMessageSequenceType; public class ChatTest extends TestBase { - @Test - public void testLobbyChat() throws Exception { - guestInit(); - - Socket s[] = new Socket[9]; - for (int i = 0; i < 9; i++) { - s[i] = new Socket("localhost", 7234); - String username = "test" + (i+1); - String password = username; - userInit(s[i], username, password); - } + final String ChatText = "Hello World ÖÄÜöäüẞ€"; + PokerTHMessage createLobbyChatMsg(String chatText) { ChatRequestTypeLobby chatLobby = new ChatRequestTypeLobby(); ChatRequestTypeChoiceType chatType = new ChatRequestTypeChoiceType(); chatType.selectChatRequestTypeLobby(chatLobby); ChatRequestMessageSequenceType chatSeq = new ChatRequestMessageSequenceType(); chatSeq.setChatRequestType(chatType); - chatSeq.setChatText("Hello World"); + chatSeq.setChatText(chatText); ChatRequestMessage chatRequest = new ChatRequestMessage(); chatRequest.setValue(chatSeq); PokerTHMessage msg = new PokerTHMessage(); msg.selectChatRequestMessage(chatRequest); + return msg; + } + + PokerTHMessage createGameChatMsg(String chatText, long gameId) { + ChatRequestTypeGame chatGame = new ChatRequestTypeGame(); + chatGame.setGameId(new NonZeroId(gameId)); + ChatRequestTypeChoiceType chatType = new ChatRequestTypeChoiceType(); + chatType.selectChatRequestTypeGame(chatGame); + ChatRequestMessageSequenceType chatSeq = new ChatRequestMessageSequenceType(); + chatSeq.setChatRequestType(chatType); + chatSeq.setChatText(chatText); + ChatRequestMessage chatRequest = new ChatRequestMessage(); + chatRequest.setValue(chatSeq); + PokerTHMessage msg = new PokerTHMessage(); + msg.selectChatRequestMessage(chatRequest); + return msg; + } + + PokerTHMessage createPrivateChatMsg(String chatText, long playerId) { + ChatRequestTypePrivate chatPrivate = new ChatRequestTypePrivate(); + chatPrivate.setTargetPlayerId(new NonZeroId(playerId)); + ChatRequestTypeChoiceType chatType = new ChatRequestTypeChoiceType(); + chatType.selectChatRequestTypePrivate(chatPrivate); + ChatRequestMessageSequenceType chatSeq = new ChatRequestMessageSequenceType(); + chatSeq.setChatRequestType(chatType); + chatSeq.setChatText(chatText); + ChatRequestMessage chatRequest = new ChatRequestMessage(); + chatRequest.setValue(chatSeq); + PokerTHMessage msg = new PokerTHMessage(); + msg.selectChatRequestMessage(chatRequest); + return msg; + } + + @Test + public void testChat() throws Exception { + long myId = guestInit(); + + Socket s[] = new Socket[9]; + long playerId[] = new long[9]; + for (int i = 0; i < 9; i++) { + s[i] = new Socket("localhost", 7234); + String username = "test" + (i+1); + String password = username; + playerId[i] = userInit(s[i], username, password); + } + + PokerTHMessage msg = createLobbyChatMsg(ChatText + 1); + // Message as guest user should be rejected. sendMessage(msg); + do { + msg = receiveMessage(); + } while (msg.isPlayerListMessageSelected()); + assertTrue(msg.isChatRejectMessageSelected()); + assertEquals(ChatText + 1, msg.getChatRejectMessage().getValue().getChatText()); + + // Message as registered user should be sent to other users and guests. + msg = createLobbyChatMsg(ChatText + 2); + sendMessage(msg, s[0]); + + msg = receiveMessage(); + assertTrue(msg.isChatMessageSelected()); + assertEquals(ChatText + 2, msg.getChatMessage().getValue().getChatText()); + assertTrue(msg.getChatMessage().getValue().getChatType().isChatTypeLobbySelected()); + assertEquals(playerId[0], msg.getChatMessage().getValue().getChatType().getChatTypeLobby().getPlayerId().getValue().longValue()); + + for (int i = 0; i < 9; i++) { + do { + msg = receiveMessage(s[i]); + } while (msg.isPlayerListMessageSelected()); + assertTrue(msg.isChatMessageSelected()); + assertEquals(ChatText + 2, msg.getChatMessage().getValue().getChatText()); + assertTrue(msg.getChatMessage().getValue().getChatType().isChatTypeLobbySelected()); + assertEquals(playerId[0], msg.getChatMessage().getValue().getChatType().getChatTypeLobby().getPlayerId().getValue().longValue()); + } + + // A game chat message, if not within a game, should be rejected. + msg = createGameChatMsg(ChatText + 3, 1); + sendMessage(msg); + + msg = receiveMessage(); + assertTrue(msg.isChatRejectMessageSelected()); + assertEquals(ChatText + 3, msg.getChatRejectMessage().getValue().getChatText()); + + msg = createGameChatMsg(ChatText + 4, 1); + sendMessage(msg, s[0]); + + msg = receiveMessage(s[0]); + assertTrue(msg.isChatRejectMessageSelected()); + assertEquals(ChatText + 4, msg.getChatRejectMessage().getValue().getChatText()); + + // Guests are not allowed to send private messages in the lobby. + msg = createPrivateChatMsg(ChatText + 5, playerId[1]); + sendMessage(msg); + + msg = receiveMessage(); + assertTrue(msg.isChatRejectMessageSelected()); + assertEquals(ChatText + 5, msg.getChatRejectMessage().getValue().getChatText()); + + // Registered users are allowed to send private messages in the lobby. + msg = createPrivateChatMsg(ChatText + 6, playerId[1]); + sendMessage(msg, s[0]); + + msg = receiveMessage(s[1]); + assertTrue(msg.isChatMessageSelected()); + assertEquals(ChatText + 6, msg.getChatMessage().getValue().getChatText()); + assertTrue(msg.getChatMessage().getValue().getChatType().isChatTypePrivateSelected()); + assertEquals(playerId[0], msg.getChatMessage().getValue().getChatType().getChatTypePrivate().getPlayerId().getValue().longValue()); + + // Game messages can be sent by registered users within a game. + Collection l = new ArrayList(); + NetGameInfo gameInfo = createGameInfo(5, EndRaiseModeEnumType.EnumType.doubleBlinds, 0, 100, GuestUser + " game list normal game", l, 10, 0, 2, 2000); + sendMessage(createGameRequestMsg( + gameInfo, + NetGameTypeEnumType.EnumType.normalGame, + 10, + 5, + "", + false)); + do { + msg = receiveMessage(); + failOnErrorMessage(msg); + } while (!msg.isJoinGameReplyMessageSelected()); + assertTrue(msg.getJoinGameReplyMessage().getValue().getJoinGameResult().isJoinGameAckSelected()); + long gameId = msg.getJoinGameReplyMessage().getValue().getGameId().getValue().longValue(); + + // Let 8 players join the game, and test game chat. + for (int i = 0; i < 8; i++) { + sendMessage(joinGameRequestMsg(gameId, "", false), s[i]); + do { + msg = receiveMessage(s[i]); + failOnErrorMessage(msg); + } while (!msg.isJoinGameReplyMessageSelected()); + assertTrue(msg.getJoinGameReplyMessage().getValue().getJoinGameResult().isJoinGameAckSelected()); + } + + // Guest user: not allowed. + msg = createGameChatMsg(ChatText + 7, gameId); + sendMessage(msg); + do { + msg = receiveMessage(); + failOnErrorMessage(msg); + } while (!msg.isChatRejectMessageSelected()); + assertEquals(ChatText + 7, msg.getChatRejectMessage().getValue().getChatText()); + + // Other users: allowed. + for (int c = 0; c < 8; c++) { + msg = createGameChatMsg(ChatText + "c" + c, gameId); + sendMessage(msg, s[c]); + do { + msg = receiveMessage(); + failOnErrorMessage(msg); + assertFalse(msg.isChatRejectMessageSelected()); + } while (!msg.isChatMessageSelected()); + + assertEquals(ChatText + "c" + c, msg.getChatMessage().getValue().getChatText()); + assertTrue(msg.getChatMessage().getValue().getChatType().isChatTypeGameSelected()); + assertEquals(playerId[c], msg.getChatMessage().getValue().getChatType().getChatTypeGame().getPlayerId().getValue().longValue()); + assertEquals(gameId, msg.getChatMessage().getValue().getChatType().getChatTypeGame().getGameId().getValue().longValue()); + + for (int i = 0; i < 8; i++) { + do { + msg = receiveMessage(s[i]); + failOnErrorMessage(msg); + assertFalse(msg.isChatRejectMessageSelected()); + } while (!msg.isChatMessageSelected()); + assertEquals(ChatText + "c" + c, msg.getChatMessage().getValue().getChatText()); + assertTrue(msg.getChatMessage().getValue().getChatType().isChatTypeGameSelected()); + assertEquals(playerId[c], msg.getChatMessage().getValue().getChatType().getChatTypeGame().getPlayerId().getValue().longValue()); + assertEquals(gameId, msg.getChatMessage().getValue().getChatType().getChatTypeGame().getGameId().getValue().longValue()); + } + } + + // Private and lobby messages are forbidden once the game is running. + sendMessage(joinGameRequestMsg(gameId, "", false), s[8]); + do { + msg = receiveMessage(s[8]); + failOnErrorMessage(msg); + // This player was not in the game and should not have received chat messages. + assertFalse(msg.isChatMessageSelected()); + } while (!msg.isJoinGameReplyMessageSelected()); + assertTrue(msg.getJoinGameReplyMessage().getValue().getJoinGameResult().isJoinGameAckSelected()); + + + // Server should confirm start event. + do { + msg = receiveMessage(s[0]); + failOnErrorMessage(msg); + } while (!msg.isGameStartMessageSelected()); + + // Private chat message should now be rejected. + msg = createPrivateChatMsg(ChatText + 8, playerId[1]); + sendMessage(msg, s[0]); + do { + msg = receiveMessage(s[0]); + failOnErrorMessage(msg); + assertFalse(msg.isChatMessageSelected()); + } while (!msg.isChatRejectMessageSelected()); for (int i = 0; i < 9; i++) { s[i].close();