]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/tracker_test.go
More minor cleanup
[rbdr/mobius] / hotline / tracker_test.go
index 72d84ba882bf614486ec51d74572492606efd72d..3e0be5def4a3c3f6602dc1d2925f63710eb720cc 100644 (file)
@@ -1,6 +1,8 @@
 package hotline
 
 import (
+       "fmt"
+       "github.com/stretchr/testify/assert"
        "reflect"
        "testing"
 )
@@ -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)
+               })
+       }
+}