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