]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/tracker_test.go
Fix regression in 1.5 login behavior
[rbdr/mobius] / hotline / tracker_test.go
index b90446274fa70f28ba6088b956c44f37392941e3..3e0be5def4a3c3f6602dc1d2925f63710eb720cc 100644 (file)
@@ -1,13 +1,15 @@
 package hotline
 
 import (
+       "fmt"
+       "github.com/stretchr/testify/assert"
        "reflect"
        "testing"
 )
 
 func TestTrackerRegistration_Payload(t *testing.T) {
        type fields struct {
-               Port        []byte
+               Port        [2]byte
                UserCount   int
                PassID      []byte
                Name        string
@@ -21,7 +23,7 @@ func TestTrackerRegistration_Payload(t *testing.T) {
                {
                        name: "returns expected payload bytes",
                        fields: fields{
-                               Port:        []byte{0x00, 0x10},
+                               Port:        [2]byte{0x00, 0x10},
                                UserCount:   2,
                                PassID:      []byte{0x00, 0x00, 0x00, 0x01},
                                Name:        "Test Serv",
@@ -49,9 +51,103 @@ func TestTrackerRegistration_Payload(t *testing.T) {
                                Name:        tt.fields.Name,
                                Description: tt.fields.Description,
                        }
-                       if got := tr.Payload(); !reflect.DeepEqual(got, tt.want) {
-                               t.Errorf("Payload() = %v, want %v", got, tt.want)
+                       if got := tr.Read(); !reflect.DeepEqual(got, tt.want) {
+                               t.Errorf("Read() = %v, want %v", got, tt.want)
                        }
                })
        }
 }
+
+func Test_serverScanner(t *testing.T) {
+       type args struct {
+               data  []byte
+               atEOF bool
+       }
+       tests := []struct {
+               name        string
+               args        args
+               wantAdvance int
+               wantToken   []byte
+               wantErr     assert.ErrorAssertionFunc
+       }{
+               {
+                       name: "when a full server entry is provided",
+                       args: args{
+                               data: []byte{
+                                       0x18, 0x05, 0x30, 0x63, // IP Addr
+                                       0x15, 0x7c, // Port
+                                       0x00, 0x02, // UserCount
+                                       0x00, 0x00, // ??
+                                       0x03,             // Name Len
+                                       0x54, 0x68, 0x65, // Name
+                                       0x03,             // Desc Len
+                                       0x54, 0x54, 0x54, // Description
+                               },
+                               atEOF: false,
+                       },
+                       wantAdvance: 18,
+                       wantToken: []byte{
+                               0x18, 0x05, 0x30, 0x63, // IP Addr
+                               0x15, 0x7c, // Port
+                               0x00, 0x02, // UserCount
+                               0x00, 0x00, // ??
+                               0x03,             // Name Len
+                               0x54, 0x68, 0x65, // Name
+                               0x03,             // Desc Len
+                               0x54, 0x54, 0x54, // Description
+                       },
+                       wantErr: assert.NoError,
+               },
+               {
+                       name: "when extra bytes are provided",
+                       args: args{
+                               data: []byte{
+                                       0x18, 0x05, 0x30, 0x63, // IP Addr
+                                       0x15, 0x7c, // Port
+                                       0x00, 0x02, // UserCount
+                                       0x00, 0x00, // ??
+                                       0x03,             // Name Len
+                                       0x54, 0x68, 0x65, // Name
+                                       0x03,             // Desc Len
+                                       0x54, 0x54, 0x54, // Description
+                                       0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
+                               },
+                               atEOF: false,
+                       },
+                       wantAdvance: 18,
+                       wantToken: []byte{
+                               0x18, 0x05, 0x30, 0x63, // IP Addr
+                               0x15, 0x7c, // Port
+                               0x00, 0x02, // UserCount
+                               0x00, 0x00, // ??
+                               0x03,             // Name Len
+                               0x54, 0x68, 0x65, // Name
+                               0x03,             // Desc Len
+                               0x54, 0x54, 0x54, // Description
+                       },
+                       wantErr: assert.NoError,
+               },
+               {
+                       name: "when insufficient bytes are provided",
+                       args: args{
+                               data: []byte{
+                                       0, 0,
+                               },
+                               atEOF: false,
+                       },
+                       wantAdvance: 0,
+                       wantToken:   []byte(nil),
+                       wantErr:     assert.NoError,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       gotAdvance, gotToken, err := serverScanner(tt.args.data, tt.args.atEOF)
+                       if !tt.wantErr(t, err, fmt.Sprintf("serverScanner(%v, %v)", tt.args.data, tt.args.atEOF)) {
+                               return
+                       }
+                       assert.Equalf(t, tt.wantAdvance, gotAdvance, "serverScanner(%v, %v)", tt.args.data, tt.args.atEOF)
+                       assert.Equalf(t, tt.wantToken, gotToken, "serverScanner(%v, %v)", tt.args.data, tt.args.atEOF)
+               })
+       }
+}