Skip to content

Commit ba70a34

Browse files
committed
test(server): update authentication test
1 parent cadb148 commit ba70a34

File tree

5 files changed

+58
-54
lines changed

5 files changed

+58
-54
lines changed

server/src/test/java/sc/server/helpers/StringNetworkInterface.kt

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,22 @@ package sc.server.helpers
22

33
import sc.networking.INetworkInterface
44
import java.io.ByteArrayOutputStream
5-
import java.io.IOException
65
import java.io.InputStream
76
import java.io.OutputStream
87

98
class StringNetworkInterface(data: String): INetworkInterface {
9+
private val inputStream = NonEndingByteArrayInputStream(data.toByteArray())
1010
private val outputStream = ByteArrayOutputStream()
11-
private val inputStream: InputStream = NonEndingByteArrayInputStream(data.toByteArray())
1211

13-
@Throws(IOException::class)
12+
override fun getInputStream(): InputStream = inputStream
13+
override fun getOutputStream(): OutputStream = outputStream
1414
override fun close() {
15+
inputStream.close()
16+
outputStream.close()
1517
}
1618

17-
@Throws(IOException::class)
18-
override fun getInputStream(): InputStream {
19-
return inputStream
19+
fun readData(): String {
20+
outputStream.flush()
21+
return outputStream.toString()
2022
}
21-
22-
@Throws(IOException::class)
23-
override fun getOutputStream(): OutputStream {
24-
return outputStream
25-
}
26-
27-
@get:Throws(IOException::class)
28-
val data: String
29-
get() {
30-
outputStream.flush()
31-
return outputStream.toString()
32-
}
33-
34-
override fun toString(): String = "String@" + this.hashCode()
3523
}

server/src/test/java/sc/server/network/ClientXmlReadTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void clientSendPacketTest() throws IOException {
4848

4949
client.start();
5050
client.send(new ExamplePacket());
51-
String data = stringInterface.getData();
51+
String data = stringInterface.readData();
5252
assertEquals("<protocol>\n <example/>", data);
5353
}
5454

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
package sc.server.network
22

3+
import io.kotest.assertions.throwables.shouldThrow
34
import io.kotest.core.spec.style.FunSpec
4-
import io.kotest.matchers.collections.shouldHaveSize
5+
import io.kotest.matchers.booleans.*
6+
import io.kotest.matchers.collections.*
7+
import sc.protocol.requests.AuthenticateRequest
58
import sc.protocol.requests.JoinGameRequest
9+
import sc.server.Configuration
610
import sc.server.Lobby
711
import sc.server.helpers.StringNetworkInterface
812

13+
class DummyClient(private val data: String = "", private val networkInterface: StringNetworkInterface = StringNetworkInterface(data)): Client(networkInterface) {
14+
init {
15+
start()
16+
}
17+
fun readData() = networkInterface.readData()
18+
}
19+
920
class LobbyRequestTest: FunSpec({
10-
test("join without gametype") {
11-
val dummy = Client(StringNetworkInterface("")).apply { start() }
12-
val lobby = Lobby()
13-
val callback = PacketCallback(JoinGameRequest(null))
14-
lobby.onRequest(dummy, callback)
15-
await("Starts a game") { lobby.games shouldHaveSize 1 }
21+
val lobby = Lobby()
22+
val dummy = DummyClient()
23+
context("join game") {
24+
test("without gametype creates a room") {
25+
lobby.onRequest(dummy, PacketCallback(JoinGameRequest(null)))
26+
lobby.games shouldHaveSize 1
27+
}
28+
}
29+
context("admin authentication") {
30+
val pwd = "Bobby"
31+
test("wrong password") {
32+
shouldThrow<AuthenticationFailedException> {
33+
lobby.onRequest(dummy, PacketCallback(AuthenticateRequest(pwd)))
34+
}
35+
dummy.isAdministrator.shouldBeFalse()
36+
}
37+
test("wrong password") {
38+
Configuration.set(Configuration.PASSWORD_KEY, pwd)
39+
lobby.onRequest(dummy, PacketCallback(AuthenticateRequest(pwd)))
40+
dummy.isAdministrator.shouldBeTrue()
41+
}
1642
}
1743
})

server/src/test/java/sc/server/network/RequestTest.kt

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package sc.server.network
22

33
import io.kotest.matchers.*
44
import io.kotest.matchers.ints.*
5-
import org.junit.jupiter.api.Assertions.*
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Assertions.assertFalse
67
import org.junit.jupiter.api.BeforeEach
78
import org.junit.jupiter.api.Test
89
import sc.framework.plugins.Pausable
@@ -12,14 +13,15 @@ import sc.protocol.requests.PauseGameRequest
1213
import sc.protocol.requests.StepRequest
1314
import sc.protocol.room.ErrorMessage
1415
import sc.protocol.room.MoveRequest
16+
import sc.protocol.room.WelcomeMessage
1517
import sc.server.client.PlayerListener
1618
import sc.server.client.TestLobbyClientListener
1719
import sc.server.gaming.GameRoom
1820
import sc.server.helpers.TestHelper
1921
import sc.server.plugins.TestMove
2022
import sc.server.plugins.TestPlugin
21-
import sc.protocol.room.WelcomeMessage
2223

24+
// TODO Migrate to LobbyRequestTest
2325
class RequestTest: RealServerTest() {
2426
private lateinit var player1: LobbyClient
2527
private lateinit var player2: LobbyClient
@@ -35,22 +37,6 @@ class RequestTest: RealServerTest() {
3537
Thread.sleep(200)
3638
}
3739

38-
@Test
39-
fun authenticationRequest() {
40-
player1.authenticate(PASSWORD)
41-
Thread.sleep(200)
42-
val clients = lobby.clientManager.clients
43-
assertTrue(clients[0].isAdministrator)
44-
assertEquals(3, lobby.clientManager.clients.size.toLong())
45-
46-
player2.authenticate("PASSWORD_FAIL_TEST")
47-
Thread.sleep(200)
48-
49-
// Player2 got kicked
50-
assertEquals(2, lobby.clientManager.clients.size.toLong())
51-
assertFalse(clients[1].isAdministrator)
52-
}
53-
5440
@Test
5541
fun observationRequest() {
5642
player1.joinGame(TestPlugin.TEST_PLUGIN_UUID)

server/src/test/java/sc/server/plugins/TestGame.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ data class TestGame(
2727
move.perform(currentState)
2828
next()
2929
}
30-
30+
3131
override fun checkWinCondition(): WinCondition? {
3232
return if (currentState.round > 1) {
3333
WinCondition(if (currentState.state % 2 == 0) Team.ONE else Team.TWO, TestWinReason.WIN)
3434
} else null
3535
}
36-
36+
3737
override fun onPlayerJoined(): Player {
3838
if (players.size < 2) {
3939
return if (players.isEmpty()) {
@@ -44,13 +44,17 @@ data class TestGame(
4444
}
4545
throw TooManyPlayersException()
4646
}
47-
47+
4848
override fun getScoreFor(player: Player) =
49-
if(player.hasLeft())
50-
PlayerScore(ScoreCause.LEFT, "Spieler ist rausgeflogen.", 0)
51-
else
52-
PlayerScore(true, "Spieler hat gewonnen.")
53-
49+
when {
50+
player.hasLeft() ->
51+
PlayerScore(ScoreCause.LEFT, "Spieler ist rausgeflogen.", 0)
52+
player.hasViolated() ->
53+
PlayerScore(ScoreCause.RULE_VIOLATION, player.violationReason!!, 0)
54+
else ->
55+
PlayerScore(true, "Spieler hat gewonnen.")
56+
}
57+
5458
override fun getTimeoutFor(player: Player): ActionTimeout =
5559
ActionTimeout(false)
5660

0 commit comments

Comments
 (0)