1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
package hotline
import (
"fmt"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
func TestTrackerRegistration_Payload(t *testing.T) {
type fields struct {
Port [2]byte
UserCount int
PassID []byte
Name string
Description string
}
tests := []struct {
name string
fields fields
want []byte
}{
{
name: "returns expected payload bytes",
fields: fields{
Port: [2]byte{0x00, 0x10},
UserCount: 2,
PassID: []byte{0x00, 0x00, 0x00, 0x01},
Name: "Test Serv",
Description: "Fooz",
},
want: []byte{
0x00, 0x01,
0x00, 0x10,
0x00, 0x02,
0x00, 0x00,
0x00, 0x00, 0x00, 0x01,
0x09,
0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x65, 0x72, 0x76,
0x04,
0x46, 0x6f, 0x6f, 0x7a,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &TrackerRegistration{
Port: tt.fields.Port,
UserCount: tt.fields.UserCount,
PassID: tt.fields.PassID,
Name: tt.fields.Name,
Description: tt.fields.Description,
}
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,
},
{
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)
})
}
}
|