]> git.r.bdr.sh - rbdr/mobius/blame - hotline/tracker_test.go
patch: v0.10.11
[rbdr/mobius] / hotline / tracker_test.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
8ff2b66d
JH
4 "fmt"
5 "github.com/stretchr/testify/assert"
6988a057
JH
6 "reflect"
7 "testing"
8)
9
10func TestTrackerRegistration_Payload(t *testing.T) {
11 type fields struct {
40414f92 12 Port [2]byte
6988a057
JH
13 UserCount int
14 PassID []byte
15 Name string
16 Description string
17 }
18 tests := []struct {
19 name string
20 fields fields
21 want []byte
22 }{
23 {
24 name: "returns expected payload bytes",
25 fields: fields{
40414f92 26 Port: [2]byte{0x00, 0x10},
6988a057
JH
27 UserCount: 2,
28 PassID: []byte{0x00, 0x00, 0x00, 0x01},
29 Name: "Test Serv",
30 Description: "Fooz",
31 },
32 want: []byte{
33 0x00, 0x01,
34 0x00, 0x10,
35 0x00, 0x02,
36 0x00, 0x00,
37 0x00, 0x00, 0x00, 0x01,
38 0x09,
39 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x65, 0x72, 0x76,
40 0x04,
41 0x46, 0x6f, 0x6f, 0x7a,
42 },
43 },
44 }
45 for _, tt := range tests {
46 t.Run(tt.name, func(t *testing.T) {
47 tr := &TrackerRegistration{
48 Port: tt.fields.Port,
49 UserCount: tt.fields.UserCount,
50 PassID: tt.fields.PassID,
51 Name: tt.fields.Name,
52 Description: tt.fields.Description,
53 }
8ff2b66d
JH
54 if got := tr.Read(); !reflect.DeepEqual(got, tt.want) {
55 t.Errorf("Read() = %v, want %v", got, tt.want)
6988a057
JH
56 }
57 })
58 }
59}
8ff2b66d
JH
60
61func Test_serverScanner(t *testing.T) {
62 type args struct {
63 data []byte
64 atEOF bool
65 }
66 tests := []struct {
67 name string
68 args args
69 wantAdvance int
70 wantToken []byte
71 wantErr assert.ErrorAssertionFunc
72 }{
73 {
74 name: "when a full server entry is provided",
75 args: args{
76 data: []byte{
77 0x18, 0x05, 0x30, 0x63, // IP Addr
78 0x15, 0x7c, // Port
79 0x00, 0x02, // UserCount
80 0x00, 0x00, // ??
81 0x03, // Name Len
82 0x54, 0x68, 0x65, // Name
83 0x03, // Desc Len
84 0x54, 0x54, 0x54, // Description
85 },
86 atEOF: false,
87 },
88 wantAdvance: 18,
89 wantToken: []byte{
90 0x18, 0x05, 0x30, 0x63, // IP Addr
91 0x15, 0x7c, // Port
92 0x00, 0x02, // UserCount
93 0x00, 0x00, // ??
94 0x03, // Name Len
95 0x54, 0x68, 0x65, // Name
96 0x03, // Desc Len
97 0x54, 0x54, 0x54, // Description
98 },
99 wantErr: assert.NoError,
100 },
101 {
102 name: "when extra bytes are provided",
103 args: args{
104 data: []byte{
105 0x18, 0x05, 0x30, 0x63, // IP Addr
106 0x15, 0x7c, // Port
107 0x00, 0x02, // UserCount
108 0x00, 0x00, // ??
109 0x03, // Name Len
110 0x54, 0x68, 0x65, // Name
111 0x03, // Desc Len
112 0x54, 0x54, 0x54, // Description
113 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54,
114 },
115 atEOF: false,
116 },
117 wantAdvance: 18,
118 wantToken: []byte{
119 0x18, 0x05, 0x30, 0x63, // IP Addr
120 0x15, 0x7c, // Port
121 0x00, 0x02, // UserCount
122 0x00, 0x00, // ??
123 0x03, // Name Len
124 0x54, 0x68, 0x65, // Name
125 0x03, // Desc Len
126 0x54, 0x54, 0x54, // Description
127 },
128 wantErr: assert.NoError,
129 },
130 {
131 name: "when insufficient bytes are provided",
132 args: args{
133 data: []byte{
134 0, 0,
135 },
136 atEOF: false,
137 },
138 wantAdvance: 0,
139 wantToken: []byte(nil),
140 wantErr: assert.NoError,
141 },
142 }
143 for _, tt := range tests {
144 t.Run(tt.name, func(t *testing.T) {
145 gotAdvance, gotToken, err := serverScanner(tt.args.data, tt.args.atEOF)
146 if !tt.wantErr(t, err, fmt.Sprintf("serverScanner(%v, %v)", tt.args.data, tt.args.atEOF)) {
147 return
148 }
149 assert.Equalf(t, tt.wantAdvance, gotAdvance, "serverScanner(%v, %v)", tt.args.data, tt.args.atEOF)
150 assert.Equalf(t, tt.wantToken, gotToken, "serverScanner(%v, %v)", tt.args.data, tt.args.atEOF)
151 })
152 }
153}