From 7f91cd091d12edde20b62da77ab2613e4c856421 Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 14 Nov 2010 01:10:24 +0000 Subject: [PATCH] Adding new test case to run a ranking game. --- tests/src/pokerth_test/CreateGameTest.java | 2 +- .../pokerth_test/CreateRankingGameTest.java | 2 +- tests/src/pokerth_test/RunNormalGameTest.java | 19 +- .../src/pokerth_test/RunRankingGameTest.java | 216 ++++++++++++++++++ .../src/pokerth_test/StartNormalGameTest.java | 2 +- tests/src/pokerth_test/TestBase.java | 45 +++- 6 files changed, 271 insertions(+), 15 deletions(-) create mode 100644 tests/src/pokerth_test/RunRankingGameTest.java diff --git a/tests/src/pokerth_test/CreateGameTest.java b/tests/src/pokerth_test/CreateGameTest.java index 06a85292..0d631ebf 100644 --- a/tests/src/pokerth_test/CreateGameTest.java +++ b/tests/src/pokerth_test/CreateGameTest.java @@ -39,7 +39,7 @@ public class CreateGameTest extends TestBase { l.add(600); l.add(1000); NetGameInfo gameInfo = createGameInfo(8, EndRaiseModeEnumType.EnumType.raiseByEndValue, 1000, 100, GuestUser + " create test game", l, 10, 0, 7, 2000); - sendMessage(createJoinGameRequestMsg( + sendMessage(createGameRequestMsg( gameInfo, NetGameTypeEnumType.EnumType.normalGame, 20, diff --git a/tests/src/pokerth_test/CreateRankingGameTest.java b/tests/src/pokerth_test/CreateRankingGameTest.java index e95eeb4a..2f5aca11 100644 --- a/tests/src/pokerth_test/CreateRankingGameTest.java +++ b/tests/src/pokerth_test/CreateRankingGameTest.java @@ -37,7 +37,7 @@ public class CreateRankingGameTest extends TestBase { counter++; Collection l = new ArrayList(); NetGameInfo gameInfo = createGameInfo(8, EndRaiseModeEnumType.EnumType.doubleBlinds, 0, 50, GuestUser + " create ranking game " + counter, l, 10, 0, 11, 10000); - sendMessage(createJoinGameRequestMsg( + sendMessage(createGameRequestMsg( gameInfo, NetGameTypeEnumType.EnumType.rankingGame, 20, diff --git a/tests/src/pokerth_test/RunNormalGameTest.java b/tests/src/pokerth_test/RunNormalGameTest.java index c09c1cf7..14fe2a8d 100644 --- a/tests/src/pokerth_test/RunNormalGameTest.java +++ b/tests/src/pokerth_test/RunNormalGameTest.java @@ -1,3 +1,20 @@ +/* PokerTH automated tests. + Copyright (C) 2010 Lothar May + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package pokerth_test; import static org.junit.Assert.fail; @@ -26,7 +43,7 @@ public class RunNormalGameTest extends TestBase { Collection l = new ArrayList(); NetGameInfo gameInfo = createGameInfo(5, EndRaiseModeEnumType.EnumType.doubleBlinds, 0, 100, GuestUser + " run normal game", l, 10, 0, 2, 2000); - sendMessage(createJoinGameRequestMsg( + sendMessage(createGameRequestMsg( gameInfo, NetGameTypeEnumType.EnumType.normalGame, 10, diff --git a/tests/src/pokerth_test/RunRankingGameTest.java b/tests/src/pokerth_test/RunRankingGameTest.java new file mode 100644 index 00000000..3fd60813 --- /dev/null +++ b/tests/src/pokerth_test/RunRankingGameTest.java @@ -0,0 +1,216 @@ +/* PokerTH automated tests. + Copyright (C) 2010 Lothar May + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package pokerth_test; + +import java.net.InetAddress; +import java.net.Socket; +import java.sql.Connection; +import java.sql.DriverManager; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.Collection; + +import org.junit.Before; +import org.junit.Test; + +import pokerth_protocol.*; +import pokerth_protocol.MyActionRequestMessage.MyActionRequestMessageSequenceType; +import pokerth_protocol.NetGameInfo.EndRaiseModeEnumType; +import pokerth_protocol.NetGameInfo.NetGameTypeEnumType; +import pokerth_protocol.StartEventAckMessage.StartEventAckMessageSequenceType; +import pokerth_protocol.StartEventMessage.StartEventMessageSequenceType; + + +public class RunRankingGameTest extends TestBase { + + Connection dbConn; + + @Before + public void dbInit() throws Exception { + final String userName = "username"; + final String password = "password"; + final String url = "jdbc:mysql://localhost:3306/database"; + Class.forName("com.mysql.jdbc.Driver").newInstance (); + dbConn = DriverManager.getConnection(url, userName, password); + } + + @Test + public void testRunRankingGame() throws Exception { + long firstPlayerId = userInit(); + + Collection l = new ArrayList(); + NetGameInfo gameInfo = createGameInfo(5, EndRaiseModeEnumType.EnumType.doubleBlinds, 0, 50, GuestUser + " run ranking game", l, 10, 0, 11, 10000); + sendMessage(createGameRequestMsg( + gameInfo, + NetGameTypeEnumType.EnumType.rankingGame, + 5, + 7, + "")); + + PokerTHMessage msg; + + // Game list update (new game) + msg = receiveMessage(); + if (!msg.isGameListMessageSelected()) { + failOnErrorMessage(msg); + fail("Invalid message."); + } + + // Join game ack. + msg = receiveMessage(); + if (msg.isJoinGameReplyMessageSelected()) { + if (!msg.getJoinGameReplyMessage().getValue().getJoinGameResult().isJoinGameAckSelected()) { + fail("Could not create game!"); + } + } + else { + failOnErrorMessage(msg); + fail("Invalid message."); + } + long gameId = msg.getJoinGameReplyMessage().getValue().getGameId().getValue(); + + // Game list update (player joined). + msg = receiveMessage(); + if (!msg.isGameListMessageSelected()) { + failOnErrorMessage(msg); + fail("Invalid message."); + } + + // Let 9 additional clients join. + Socket s[] = new Socket[9]; + long playerId[] = new long[9]; + for (int i = 0; i < 9; i++) { + s[i] = new Socket("::1", 7234); + String username = "test" + (i+1); + String password = username; + playerId[i] = userInit(s[i], username, password); + sendMessage(joinGameRequestMsg(gameId, ""), s[i]); + do { + msg = receiveMessage(s[i]); + failOnErrorMessage(msg); + } while (!msg.isJoinGameReplyMessageSelected()); + if (!msg.getJoinGameReplyMessage().getValue().getJoinGameResult().isJoinGameAckSelected()) { + fail("User " + username + " could not join ranking game."); + } + + // The player should have joined the game. + msg = receiveMessage(); + if (!msg.isPlayerListMessageSelected()) { + failOnErrorMessage(msg); + fail("Invalid message."); + } + msg = receiveMessage(); + if (!msg.isGamePlayerMessageSelected()) { + failOnErrorMessage(msg); + fail("Invalid message."); + } + msg = receiveMessage(); + if (!msg.isGameListMessageSelected()) { + failOnErrorMessage(msg); + fail("Invalid message."); + } + } + + // Server should automatically send start event. + msg = receiveMessage(); + if (!msg.isStartEventMessageSelected()) { + failOnErrorMessage(msg); + fail("Invalid message."); + } + for (int i = 0; i < 9; i++) { + do { + msg = receiveMessage(s[i]); + failOnErrorMessage(msg); + } while (!msg.isStartEventMessageSelected()); + } + // Acknowledge start event. + StartEventAckMessageSequenceType startType = new StartEventAckMessageSequenceType(); + startType.setGameId(new NonZeroId(gameId)); + StartEventAckMessage startAck = new StartEventAckMessage(); + startAck.setValue(startType); + msg = new PokerTHMessage(); + msg.selectStartEventAckMessage(startAck); + sendMessage(msg); + for (int i = 0; i < 9; i++) { + sendMessage(msg, s[i]); + } + + // Game list update (game now running). + msg = receiveMessage(); + if (!msg.isGameListMessageSelected()) { + failOnErrorMessage(msg); + fail("Invalid message."); + } + + msg = receiveMessage(); + if (!msg.isGameStartMessageSelected()) { + failOnErrorMessage(msg); + fail("Invalid message."); + } + + long handNum = 0; + do { + msg = receiveMessage(); + failOnErrorMessage(msg); + if (msg.isHandStartMessageSelected()) { + handNum++; + } + else if (msg.isPlayersTurnMessageSelected()) { + if (msg.getPlayersTurnMessage().getValue().getPlayerId().getValue() == firstPlayerId) { + NetPlayerAction action = new NetPlayerAction(); + action.setValue(NetPlayerAction.EnumType.actionAllIn); + MyActionRequestMessageSequenceType myRequest = new MyActionRequestMessageSequenceType(); + myRequest.setGameId(new NonZeroId(gameId)); + myRequest.setGameState(msg.getPlayersTurnMessage().getValue().getGameState()); + myRequest.setHandNum(new NonZeroId(handNum)); + myRequest.setMyAction(action); + myRequest.setMyRelativeBet(0); + MyActionRequestMessage myAction = new MyActionRequestMessage(); + myAction.setValue(myRequest); + PokerTHMessage outMsg = new PokerTHMessage(); + outMsg.selectMyActionRequestMessage(myAction); + sendMessage(outMsg); + } + } + for (int i = 0; i < 9; i++) { + while (s[i].getInputStream().available() > 0) { + PokerTHMessage inMsg = receiveMessage(s[i]); + failOnErrorMessage(inMsg); + if (inMsg.isPlayersTurnMessageSelected()) { + if (inMsg.getPlayersTurnMessage().getValue().getPlayerId().getValue() == playerId[i]) { + NetPlayerAction action = new NetPlayerAction(); + action.setValue(NetPlayerAction.EnumType.actionFold); + MyActionRequestMessageSequenceType myRequest = new MyActionRequestMessageSequenceType(); + myRequest.setGameId(new NonZeroId(gameId)); + myRequest.setGameState(inMsg.getPlayersTurnMessage().getValue().getGameState()); + myRequest.setHandNum(new NonZeroId(handNum)); + myRequest.setMyAction(action); + myRequest.setMyRelativeBet(0); + MyActionRequestMessage myAction = new MyActionRequestMessage(); + myAction.setValue(myRequest); + PokerTHMessage outMsg = new PokerTHMessage(); + outMsg.selectMyActionRequestMessage(myAction); + sendMessage(outMsg, s[i]); + } + } + } + } + } while (!msg.isEndOfGameMessageSelected()); + } +} diff --git a/tests/src/pokerth_test/StartNormalGameTest.java b/tests/src/pokerth_test/StartNormalGameTest.java index b3d95955..63794533 100644 --- a/tests/src/pokerth_test/StartNormalGameTest.java +++ b/tests/src/pokerth_test/StartNormalGameTest.java @@ -37,7 +37,7 @@ public class StartNormalGameTest extends TestBase { Collection l = new ArrayList(); NetGameInfo gameInfo = createGameInfo(10, EndRaiseModeEnumType.EnumType.doubleBlinds, 0, 100, GuestUser + " start normal game", l, 10, 0, 11, 20000); - sendMessage(createJoinGameRequestMsg( + sendMessage(createGameRequestMsg( gameInfo, NetGameTypeEnumType.EnumType.normalGame, 10, diff --git a/tests/src/pokerth_test/TestBase.java b/tests/src/pokerth_test/TestBase.java index 6eab23ec..85e84b62 100644 --- a/tests/src/pokerth_test/TestBase.java +++ b/tests/src/pokerth_test/TestBase.java @@ -50,13 +50,12 @@ public abstract class TestBase { protected IEncoder encoder; protected IDecoder decoder; - private Socket sock; + protected Socket sock; @Before public void setUp() throws Exception { Thread.sleep(2000); - InetAddress localaddr = InetAddress.getLocalHost(); - sock = new Socket(localaddr, 7234); + sock = new Socket("::1", 7234); encoder = CoderFactory.getInstance().newEncoder("BER"); decoder = CoderFactory.getInstance().newDecoder("BER"); } @@ -120,18 +119,19 @@ public abstract class TestBase { fail("Invalid message."); } // Waiting for player list update. - msg = receiveMessage(); + msg = receiveMessage(s); if (!msg.isPlayerListMessageSelected()) { failOnErrorMessage(msg); fail("Invalid message."); } } - public void userInit() throws Exception { - userInit(sock); + public long userInit() throws Exception { + return userInit(sock, AuthUser, AuthPassword); } - public void userInit(Socket s) throws Exception { + public long userInit(Socket s, String user, String password) throws Exception { + long playerId = 0; PokerTHMessage msg = receiveMessage(s); AnnounceMessage announce = msg.getAnnounceMessage(); assertTrue(announce.getValue().getServerType().getValue() == ServerTypeEnumType.EnumType.serverTypeInternetAuth); @@ -143,7 +143,7 @@ public abstract class TestBase { requestedVersion.setMajor(PROTOCOL_VERSION_MAJOR); requestedVersion.setMinor(PROTOCOL_VERSION_MINOR); AuthenticatedLogin authLogin = new AuthenticatedLogin(); - authLogin.setClientUserData(scramAuth.executeStep1(AuthUser).getBytes()); + authLogin.setClientUserData(scramAuth.executeStep1(user).getBytes()); LoginChoiceType loginType = new LoginChoiceType(); loginType.selectAuthenticatedLogin(authLogin); InitMessageSequenceType msgType = new InitMessageSequenceType(); @@ -162,7 +162,7 @@ public abstract class TestBase { { String serverFirstMessage = new String(msg.getAuthMessage().getValue().getAuthServerChallenge().getServerChallenge()); AuthClientResponse authClient = new AuthClientResponse(); - authClient.setClientResponse(scramAuth.executeStep2(AuthPassword, serverFirstMessage).getBytes()); + authClient.setClientResponse(scramAuth.executeStep2(password, serverFirstMessage).getBytes()); AuthMessageChoiceType authChoice = new AuthMessageChoiceType(); authChoice.selectAuthClientResponse(authClient); AuthMessage authResponse = new AuthMessage(); @@ -182,20 +182,24 @@ public abstract class TestBase { InitAckMessage initAck = msg.getInitAckMessage(); assertTrue(initAck.getValue().getYourPlayerId().getValue() != 0L); assertTrue(!initAck.getValue().isYourAvatarPresent()); + playerId = initAck.getValue().getYourPlayerId().getValue(); } else { failOnErrorMessage(msg); fail("Invalid message."); } // Waiting for player list update. - msg = receiveMessage(); + do { + msg = receiveMessage(s); + } while (msg.isGameListMessageSelected() || msg.isGamePlayerMessageSelected()); if (!msg.isPlayerListMessageSelected()) { failOnErrorMessage(msg); fail("Invalid message."); } + return playerId; } - public PokerTHMessage createJoinGameRequestMsg(NetGameInfo gameInfo, NetGameTypeEnumType.EnumType type, int playerActionTimeout, int guiSpeed, String password) { + public PokerTHMessage createGameRequestMsg(NetGameInfo gameInfo, NetGameTypeEnumType.EnumType type, int playerActionTimeout, int guiSpeed, String password) { NetGameTypeEnumType gameType = new NetGameTypeEnumType(); gameType.setValue(type); gameInfo.setNetGameType(gameType); @@ -218,6 +222,25 @@ public abstract class TestBase { return msg; } + public PokerTHMessage joinGameRequestMsg(long gameId, String password) { + JoinExistingGame joinExisting = new JoinExistingGame(); + joinExisting.setGameId(new NonZeroId(gameId)); + JoinGameActionChoiceType joinAction = new JoinGameActionChoiceType(); + joinAction.selectJoinExistingGame(joinExisting); + JoinGameRequestMessageSequenceType joinType = new JoinGameRequestMessageSequenceType(); + joinType.setJoinGameAction(joinAction); + if (!password.isEmpty()) { + joinType.setPassword(password); + } + + JoinGameRequestMessage joinRequest = new JoinGameRequestMessage(); + joinRequest.setValue(joinType); + + PokerTHMessage msg = new PokerTHMessage(); + msg.selectJoinGameRequestMessage(joinRequest); + return msg; + } + public NetGameInfo createGameInfo(int delayBetweenHands, EndRaiseModeEnumType.EnumType endMode, int endRaiseValue, int sb, String gameName, Collection manualBlinds, int maxNumPlayers, int raiseEveryMinutes, int raiseEveryHands, int startMoney) { NetGameInfo gameInfo = new NetGameInfo();