Skip to content

Commit

Permalink
Remove testify assert from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
outdead committed Nov 30, 2020
1 parent 8cbc5d5 commit c57a30b
Showing 1 changed file with 108 additions and 65 deletions.
173 changes: 108 additions & 65 deletions rcon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/gorcon/rcon"
"github.com/gorcon/rcon/rcontest"
"github.com/stretchr/testify/assert"
)

func authHandler(c *rcontest.Context) {
Expand Down Expand Up @@ -141,116 +140,155 @@ func TestConn_Execute(t *testing.T) {

t.Run("incorrect command", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password")
if !assert.NoError(t, err) {
return
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
defer assert.NoError(t, conn.Close())
defer conn.Close()

result, err := conn.Execute("")
assert.Equal(t, err, rcon.ErrCommandEmpty)
assert.Equal(t, 0, len(result))
if !errors.Is(err, rcon.ErrCommandEmpty) {
t.Errorf("got err %q, want %q", err, rcon.ErrCommandEmpty)
}

if len(result) != 0 {
t.Fatalf("got result len %d, want %d", len(result), 0)
}

result, err = conn.Execute(string(make([]byte, 1001)))
assert.Equal(t, err, rcon.ErrCommandTooLong)
assert.Equal(t, 0, len(result))
if !errors.Is(err, rcon.ErrCommandTooLong) {
t.Errorf("got err %q, want %q", err, rcon.ErrCommandTooLong)
}

if len(result) != 0 {
t.Fatalf("got result len %d, want %d", len(result), 0)
}
})

t.Run("closed network connection 1", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password", rcon.SetDeadline(0))
if !assert.NoError(t, err) {
return
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
assert.NoError(t, conn.Close())
conn.Close()

result, err := conn.Execute("help")
assert.EqualError(t, err, fmt.Sprintf("write tcp %s->%s: use of closed network connection", conn.LocalAddr(), conn.RemoteAddr()))
assert.Equal(t, 0, len(result))
wantErrMsg := fmt.Sprintf("write tcp %s->%s: use of closed network connection", conn.LocalAddr(), conn.RemoteAddr())
if err == nil || err.Error() != wantErrMsg {
t.Errorf("got err %q, want to contain %q", err, wantErrMsg)
}

if len(result) != 0 {
t.Fatalf("got result len %d, want %d", len(result), 0)
}
})

t.Run("closed network connection 2", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password")
if !assert.NoError(t, err) {
return
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
assert.NoError(t, conn.Close())
conn.Close()

result, err := conn.Execute("help")
assert.EqualError(t, err, fmt.Sprintf("set tcp %s: use of closed network connection", conn.LocalAddr()))
assert.Equal(t, 0, len(result))
wantErrMsg := fmt.Sprintf("set tcp %s: use of closed network connection", conn.LocalAddr())
if err == nil || err.Error() != wantErrMsg {
t.Errorf("got err %q, want to contain %q", err, wantErrMsg)
}

if len(result) != 0 {
t.Fatalf("got result len %d, want %d", len(result), 0)
}
})

t.Run("read deadline", func(t *testing.T) {
server := rcontest.NewServer(rcontest.SetSettings(rcontest.Settings{Password: "password", CommandResponseDelay: 2 * time.Second}))
defer server.Close()

conn, err := rcon.Dial(server.Addr(), "password", rcon.SetDeadline(1*time.Second))
if !assert.NoError(t, err) {
return
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
defer func() {
assert.NoError(t, conn.Close())
}()
defer conn.Close()

result, err := conn.Execute("deadline")
assert.EqualError(t, err, fmt.Sprintf("read tcp %s->%s: i/o timeout", conn.LocalAddr(), conn.RemoteAddr()))
wantErrMsg := fmt.Sprintf("read tcp %s->%s: i/o timeout", conn.LocalAddr(), conn.RemoteAddr())
if err == nil || err.Error() != wantErrMsg {
t.Errorf("got err %q, want to contain %q", err, wantErrMsg)
}

assert.Equal(t, 0, len(result))
if len(result) != 0 {
t.Fatalf("got result len %d, want %d", len(result), 0)
}
})

t.Run("invalid padding", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password")
if !assert.NoError(t, err) {
return
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
defer conn.Close()

result, err := conn.Execute("padding")
if !errors.Is(err, rcon.ErrInvalidPacketPadding) {
t.Errorf("got err %q, want %q", err, rcon.ErrInvalidPacketPadding)
}
defer func() {
assert.NoError(t, conn.Close())
}()

_, err = conn.Execute("padding")
assert.Equal(t, rcon.ErrInvalidPacketPadding, err)
if len(result) != 2 {
t.Fatalf("got result len %d, want %d", len(result), 2)
}
})

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

result, err := conn.Execute("another")
if !errors.Is(err, rcon.ErrInvalidPacketID) {
t.Errorf("got err %q, want %q", err, rcon.ErrInvalidPacketID)
}
defer func() {
assert.NoError(t, conn.Close())
}()

_, err = conn.Execute("another")
assert.Equal(t, rcon.ErrInvalidPacketID, err)
if len(result) != 0 {
t.Fatalf("got result len %d, want %d", len(result), 0)
}
})

t.Run("success help command", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password")
if !assert.NoError(t, err) {
return
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
defer func() {
assert.NoError(t, conn.Close())
}()
defer conn.Close()

result, err := conn.Execute("help")
assert.NoError(t, err)
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}

assert.Equal(t, "lorem ipsum dolor sit amet", result)
resultWant := "lorem ipsum dolor sit amet"
if result != resultWant {
t.Fatalf("got result %q, want %q", result, resultWant)
}
})

t.Run("rust workaround", func(t *testing.T) {
conn, err := rcon.Dial(server.Addr(), "password", rcon.SetDeadline(1*time.Second))
if !assert.NoError(t, err) {
return
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
defer func() {
assert.NoError(t, conn.Close())
}()
defer conn.Close()

result, err := conn.Execute("rust")
assert.NoError(t, err)
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}

assert.Equal(t, "rust", result)
resultWant := "rust"
if result != resultWant {
t.Fatalf("got result %q, want %q", result, resultWant)
}
})

if run := getVar("TEST_PZ_SERVER", "false"); run == "true" {
Expand Down Expand Up @@ -304,16 +342,18 @@ func TestConn_Execute(t *testing.T) {

conn, err := rcon.Dial(addr, password)
if err != nil {
t.Fatal(err)
t.Fatalf("got err %q, want %v", err, nil)
}
defer func() {
assert.NoError(t, conn.Close())
}()
defer conn.Close()

result, err := conn.Execute("help")
assert.NoError(t, err)
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}

assert.Equal(t, needle, result)
if result != needle {
t.Fatalf("got result %q, want %q", result, needle)
}
})
}

Expand All @@ -324,15 +364,18 @@ func TestConn_Execute(t *testing.T) {
t.Run("rust server", func(t *testing.T) {
conn, err := rcon.Dial(addr, password)
if err != nil {
t.Fatal(err)
t.Fatalf("got err %q, want %v", err, nil)
}
defer func() {
assert.NoError(t, conn.Close())
}()
defer conn.Close()

result, err := conn.Execute("status")
assert.NoError(t, err)
assert.NotEmpty(t, result)
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}

if result == "" {
t.Fatal("got empty result, want value")
}

fmt.Println(result)
})
Expand Down

0 comments on commit c57a30b

Please sign in to comment.