X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/22c599abc18895f73e96095f35b71cf3357d41b4..3e20402491f013710dfa1c2f3d0cd6ab0e212585:/hotline/tracker_test.go diff --git a/hotline/tracker_test.go b/hotline/tracker_test.go index b904462..b5c5c55 100644 --- a/hotline/tracker_test.go +++ b/hotline/tracker_test.go @@ -1,15 +1,18 @@ package hotline import ( + "fmt" + "github.com/stretchr/testify/assert" + "io" "reflect" "testing" ) func TestTrackerRegistration_Payload(t *testing.T) { type fields struct { - Port []byte + Port [2]byte UserCount int - PassID []byte + PassID [4]byte Name string Description string } @@ -21,9 +24,9 @@ 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}, + PassID: [4]byte{0x00, 0x00, 0x00, 0x01}, Name: "Test Serv", Description: "Fooz", }, @@ -49,9 +52,142 @@ 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, _ := io.ReadAll(tr); !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, + }, + { + name: "when nameLen exceeds provided data", + args: args{ + data: []byte{ + 0x18, 0x05, 0x30, 0x63, // IP Addr + 0x15, 0x7c, // Port + 0x00, 0x02, // UserCount + 0x00, 0x00, // ?? + 0xff, // Name Len + 0x54, 0x68, 0x65, // Name + 0x03, // Desc Len + 0x54, 0x54, 0x54, // Description + }, + atEOF: false, + }, + wantAdvance: 0, + wantToken: []byte(nil), + wantErr: assert.NoError, + }, + { + name: "when description len exceeds provided data", + 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 + 0xff, // Desc Len + 0x54, 0x54, 0x54, // Description + }, + 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) }) } }