From a57c344b11fa8b705608191d4e3af7f3da4b2c6f Mon Sep 17 00:00:00 2001 From: lotodore Date: Sat, 31 Jan 2015 22:51:03 +0100 Subject: [PATCH] Adding unit test for WebSocket communication and fixing a bug found by the unit test. --- src/net/common/sessiondata.cpp | 2 +- .../pokerth/test/WebSocketAnnounceTest.java | 136 ++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 tests/src/de/pokerth/test/WebSocketAnnounceTest.java diff --git a/src/net/common/sessiondata.cpp b/src/net/common/sessiondata.cpp index e0e762ac..06f71466 100644 --- a/src/net/common/sessiondata.cpp +++ b/src/net/common/sessiondata.cpp @@ -57,7 +57,7 @@ SessionData::SessionData(boost::shared_ptr sock, S } SessionData::SessionData(boost::shared_ptr webData, SessionId id, SessionDataCallback &cb, boost::asio::io_service &ioService, int /*filler*/) - : m_webData(webData), m_id(id), m_state(SessionData::Init), m_readyFlag(false), m_wantsLobbyMsg(true), + : m_webData(webData), m_id(id), m_state(SessionData::Auth), m_readyFlag(false), m_wantsLobbyMsg(true), m_activityTimeoutSec(0), m_activityWarningRemainingSec(0), m_initTimeoutTimer(ioService), m_globalTimeoutTimer(ioService), m_activityTimeoutTimer(ioService), m_callback(cb), m_authSession(NULL), m_curAuthStep(0) { diff --git a/tests/src/de/pokerth/test/WebSocketAnnounceTest.java b/tests/src/de/pokerth/test/WebSocketAnnounceTest.java new file mode 100644 index 00000000..66454de2 --- /dev/null +++ b/tests/src/de/pokerth/test/WebSocketAnnounceTest.java @@ -0,0 +1,136 @@ +/* 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 de.pokerth.test; + +import static org.junit.Assert.*; + +import java.net.URI; +import java.nio.ByteBuffer; +import java.util.concurrent.ArrayBlockingQueue; + +import org.junit.Test; + +import de.pokerth.protocol.ProtoBuf.AnnounceMessage; +import de.pokerth.protocol.ProtoBuf.AuthClientRequestMessage; +import de.pokerth.protocol.ProtoBuf.AuthMessage; +import de.pokerth.protocol.ProtoBuf.InitDoneMessage; +import de.pokerth.protocol.ProtoBuf.AnnounceMessage.ServerType; +import de.pokerth.protocol.ProtoBuf.AuthMessage.AuthMessageType; +import de.pokerth.protocol.ProtoBuf.PokerTHMessage; +import de.pokerth.protocol.ProtoBuf.LobbyMessage.LobbyMessageType; +import de.pokerth.protocol.ProtoBuf.PokerTHMessage.PokerTHMessageType; + +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.handshake.ServerHandshake; + +public class WebSocketTest extends TestBase { + + protected static final String POKERTHWEBSOCKET = "ws://localhost:7233/pokerthwebsocket"; + + class PokerTHWebSocketClient extends WebSocketClient { + + public ArrayBlockingQueue messageQueue; + + public PokerTHWebSocketClient(URI serverUri) { + super(serverUri); + messageQueue = new ArrayBlockingQueue(100); + } + + @Override + public void onOpen(ServerHandshake handshakedata) { + } + + @Override + public void onMessage(String message) { + fail("All messages should be binary."); + } + + @Override + public void onMessage(ByteBuffer message) { + try { + messageQueue.put(message); + } catch (InterruptedException e) { + fail("Message interrupted"); + } + } + + @Override + public void onClose(int code, String reason, boolean remote) { + } + + @Override + public void onError(Exception ex) { + fail("WebSocket error: " + ex.getStackTrace()); + } + } + + protected void TestAnnounceMsg(AnnounceMessage announce, int numPlayersOnServer) { + assertEquals(PROTOCOL_VERSION_MAJOR, announce.getProtocolVersion().getMajorVersion()); + assertEquals(PROTOCOL_VERSION_MINOR, announce.getProtocolVersion().getMinorVersion()); + assertEquals(ServerType.serverTypeInternetAuth, announce.getServerType()); + assertEquals(numPlayersOnServer, announce.getNumPlayersOnServer()); + } + + @Test + public void testWebSocketGuestLogin() throws Exception { + + PokerTHWebSocketClient webClient = new PokerTHWebSocketClient(new URI(POKERTHWEBSOCKET)); + + webClient.connectBlocking(); + ByteBuffer buffer = webClient.messageQueue.take(); + PokerTHMessage msg = PokerTHMessage.parseFrom(buffer.array()); + assertTrue(msg.hasAnnounceMessage()); + TestAnnounceMsg(msg.getAnnounceMessage(), 0); + + AnnounceMessage.Version requestedVersion = AnnounceMessage.Version.newBuilder() + .setMajorVersion(PROTOCOL_VERSION_MAJOR) + .setMinorVersion(PROTOCOL_VERSION_MINOR) + .build(); + AuthClientRequestMessage init = AuthClientRequestMessage.newBuilder() + .setBuildId(0) + .setLogin(AuthClientRequestMessage.LoginType.guestLogin) + .setRequestedVersion(requestedVersion) + .setNickName(GuestUser) + .build(); + AuthMessage auth = AuthMessage.newBuilder() + .setMessageType(AuthMessageType.Type_AuthClientRequestMessage) + .setAuthClientRequestMessage(init) + .build(); + msg = PokerTHMessage.newBuilder() + .setMessageType(PokerTHMessageType.Type_AuthMessage) + .setAuthMessage(auth) + .build(); + webClient.send(msg.toByteArray()); + + buffer = webClient.messageQueue.take(); + msg = PokerTHMessage.parseFrom(buffer.array()); + + if (msg.hasLobbyMessage() && msg.getMessageType() == PokerTHMessageType.Type_LobbyMessage + && msg.getLobbyMessage().hasInitDoneMessage() && msg.getLobbyMessage().getMessageType() == LobbyMessageType.Type_InitDoneMessage) { + InitDoneMessage initDone = msg.getLobbyMessage().getInitDoneMessage(); + assertTrue(initDone.getYourPlayerId() != 0L); + assertTrue(!initDone.hasYourAvatarHash()); + } + else { + failOnErrorMessage(msg); + fail("Invalid message."); + } + + webClient.close(); + } +}