]> git.r.bdr.sh - rbdr/mobius/blob - hotline/tracker_test.go
Upload example dialog
[rbdr/mobius] / hotline / tracker_test.go
1 package hotline
2
3 import (
4 "fmt"
5 "github.com/stretchr/testify/assert"
6 "reflect"
7 "testing"
8 )
9
10 func TestTrackerRegistration_Payload(t *testing.T) {
11 type fields struct {
12 Port [2]byte
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{
26 Port: [2]byte{0x00, 0x10},
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 }
54 if got := tr.Read(); !reflect.DeepEqual(got, tt.want) {
55 t.Errorf("Read() = %v, want %v", got, tt.want)
56 }
57 })
58 }
59 }
60
61 func 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 name: "when nameLen exceeds provided data",
144 args: args{
145 data: []byte{
146 0x18, 0x05, 0x30, 0x63, // IP Addr
147 0x15, 0x7c, // Port
148 0x00, 0x02, // UserCount
149 0x00, 0x00, // ??
150 0xff, // Name Len
151 0x54, 0x68, 0x65, // Name
152 0x03, // Desc Len
153 0x54, 0x54, 0x54, // Description
154 },
155 atEOF: false,
156 },
157 wantAdvance: 0,
158 wantToken: []byte(nil),
159 wantErr: assert.NoError,
160 },
161 {
162 name: "when description len exceeds provided data",
163 args: args{
164 data: []byte{
165 0x18, 0x05, 0x30, 0x63, // IP Addr
166 0x15, 0x7c, // Port
167 0x00, 0x02, // UserCount
168 0x00, 0x00, // ??
169 0x03, // Name Len
170 0x54, 0x68, 0x65, // Name
171 0xff, // Desc Len
172 0x54, 0x54, 0x54, // Description
173 },
174 atEOF: false,
175 },
176 wantAdvance: 0,
177 wantToken: []byte(nil),
178 wantErr: assert.NoError,
179 },
180 }
181 for _, tt := range tests {
182 t.Run(tt.name, func(t *testing.T) {
183 gotAdvance, gotToken, err := serverScanner(tt.args.data, tt.args.atEOF)
184 if !tt.wantErr(t, err, fmt.Sprintf("serverScanner(%v, %v)", tt.args.data, tt.args.atEOF)) {
185 return
186 }
187 assert.Equalf(t, tt.wantAdvance, gotAdvance, "serverScanner(%v, %v)", tt.args.data, tt.args.atEOF)
188 assert.Equalf(t, tt.wantToken, gotToken, "serverScanner(%v, %v)", tt.args.data, tt.args.atEOF)
189 })
190 }
191 }