Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
outdead committed Nov 30, 2020
1 parent f21fdbf commit 65f208a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
59 changes: 58 additions & 1 deletion rcon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import (
"github.com/stretchr/testify/assert"
)

func authHandler(c *rcontest.Context) {
switch c.Request().Body() {
case c.Server().Settings.Password:
rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, "").WriteTo(c.Conn())
rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, c.Request().ID, "").WriteTo(c.Conn())
case "invalid packet type":
rcon.NewPacket(42, c.Request().ID, "").WriteTo(c.Conn())
case "another":
rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, 42, "").WriteTo(c.Conn())
default:
rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, -1, string([]byte{0x00})).WriteTo(c.Conn())
}
}

func commandHandler(c *rcontest.Context) {
writeWithInvalidPadding := func(conn io.Writer, packet *rcon.Packet) {
buffer := bytes.NewBuffer(make([]byte, 0, packet.Size+4))
Expand All @@ -38,9 +52,11 @@ func commandHandler(c *rcontest.Context) {
// Write specific Rust package.
rcon.NewPacket(4, c.Request().ID, "").WriteTo(c.Conn())

rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, c.Request().Body()).WriteTo(c.Conn())
rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, -1, c.Request().Body()).WriteTo(c.Conn())
case "padding":
writeWithInvalidPadding(c.Conn(), rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, ""))
case "another":
rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, 42, "").WriteTo(c.Conn())
default:
rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, "unknown command").WriteTo(c.Conn())
}
Expand Down Expand Up @@ -78,6 +94,34 @@ func TestDial(t *testing.T) {
assert.EqualError(t, err, "authentication failed")
})

t.Run("invalid packet type", func(t *testing.T) {
server := rcontest.NewServer(
rcontest.SetSettings(rcontest.Settings{Password: "password"}),
rcontest.SetAuthHandler(authHandler),
)
defer server.Close()

conn, err := rcon.Dial(server.Addr(), "invalid packet type")
if !assert.Error(t, err) {
assert.NoError(t, conn.Close())
}
assert.Equal(t, rcon.ErrInvalidAuthResponse, err)
})

t.Run("invalid response id", func(t *testing.T) {
server := rcontest.NewServer(
rcontest.SetSettings(rcontest.Settings{Password: "password"}),
rcontest.SetAuthHandler(authHandler),
)
defer server.Close()

conn, err := rcon.Dial(server.Addr(), "another")
if !assert.Error(t, err) {
assert.NoError(t, conn.Close())
}
assert.Equal(t, rcon.ErrInvalidPacketID, err)
})

t.Run("auth success", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password")
if assert.NoError(t, err) {
Expand Down Expand Up @@ -164,6 +208,19 @@ func TestConn_Execute(t *testing.T) {
assert.Equal(t, rcon.ErrInvalidPacketPadding, err)
})

t.Run("invalid response id", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password")
if !assert.NoError(t, err) {
return
}
defer func() {
assert.NoError(t, conn.Close())
}()

_, err = conn.Execute("another")
assert.Equal(t, rcon.ErrInvalidPacketID, err)
})

t.Run("success help command", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password")
if !assert.NoError(t, err) {
Expand Down
2 changes: 1 addition & 1 deletion rcontest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func AuthHandler(c *Context) {
_, _ = rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, "").WriteTo(c.Conn())

// Than write SERVERDATA_AUTH_RESPONSE packet to allow authHandler success.
_, _ = rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, c.Request().ID, "").WriteTo(c.Conn())
_, _ = rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, rcon.SERVERDATA_AUTH_ID, "").WriteTo(c.Conn())
} else {
// If authentication was failed, the ID must be assigned to -1.
_, _ = rcon.NewPacket(rcon.SERVERDATA_AUTH_RESPONSE, -1, string([]byte{0x00})).WriteTo(c.Conn())
Expand Down

0 comments on commit 65f208a

Please sign in to comment.