]> git.r.bdr.sh - rbdr/mobius/blob - hotline/handshake_test.go
Fix missing version in Docker and Makefile build
[rbdr/mobius] / hotline / handshake_test.go
1 package hotline
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 func TestHandshakeWrite(t *testing.T) {
9 tests := []struct {
10 name string
11 input []byte
12 expected handshake
13 expectedError string
14 }{
15 {
16 name: "Valid Handshake",
17 input: []byte{0x54, 0x52, 0x54, 0x50, 0x48, 0x4F, 0x54, 0x4C, 0x00, 0x01, 0x00, 0x02},
18 expected: handshake{
19 Protocol: [4]byte{0x54, 0x52, 0x54, 0x50},
20 SubProtocol: [4]byte{0x48, 0x4F, 0x54, 0x4C},
21 Version: [2]byte{0x00, 0x01},
22 SubVersion: [2]byte{0x00, 0x02},
23 },
24 expectedError: "",
25 },
26 {
27 name: "Invalid Handshake Size",
28 input: []byte{0x54, 0x52, 0x54, 0x50},
29 expected: handshake{},
30 expectedError: "invalid handshake size",
31 },
32 {
33 name: "Empty Handshake Data",
34 input: []byte{},
35 expected: handshake{},
36 expectedError: "invalid handshake size",
37 },
38 }
39
40 for _, tt := range tests {
41 t.Run(tt.name, func(t *testing.T) {
42 var h handshake
43 n, err := h.Write(tt.input)
44
45 if tt.expectedError != "" {
46 if err == nil || err.Error() != tt.expectedError {
47 t.Fatalf("expected error %q, got %q", tt.expectedError, err)
48 }
49 } else {
50 if err != nil {
51 t.Fatalf("unexpected error: %v", err)
52 }
53 if n != handshakeSize {
54 t.Fatalf("expected %d bytes written, got %d", handshakeSize, n)
55 }
56 if h != tt.expected {
57 t.Fatalf("expected handshake %+v, got %+v", tt.expected, h)
58 }
59 }
60 })
61 }
62 }
63
64 func TestHandshakeValid(t *testing.T) {
65 tests := []struct {
66 name string
67 input handshake
68 expected bool
69 }{
70 {
71 name: "Valid Handshake",
72 input: handshake{
73 Protocol: [4]byte{0x54, 0x52, 0x54, 0x50}, // TRTP
74 SubProtocol: [4]byte{0x48, 0x4F, 0x54, 0x4C}, // HOTL
75 Version: [2]byte{0x00, 0x01},
76 SubVersion: [2]byte{0x00, 0x02},
77 },
78 expected: true,
79 },
80 {
81 name: "Invalid Protocol",
82 input: handshake{
83 Protocol: [4]byte{0x00, 0x00, 0x00, 0x00},
84 SubProtocol: [4]byte{0x48, 0x4F, 0x54, 0x4C}, // HOTL
85 Version: [2]byte{0x00, 0x01},
86 SubVersion: [2]byte{0x00, 0x02},
87 },
88 expected: false,
89 },
90 {
91 name: "Invalid SubProtocol",
92 input: handshake{
93 Protocol: [4]byte{0x54, 0x52, 0x54, 0x50}, // TRTP
94 SubProtocol: [4]byte{0x00, 0x00, 0x00, 0x00},
95 Version: [2]byte{0x00, 0x01},
96 SubVersion: [2]byte{0x00, 0x02},
97 },
98 expected: false,
99 },
100 {
101 name: "Invalid Protocol and SubProtocol",
102 input: handshake{
103 Protocol: [4]byte{0x00, 0x00, 0x00, 0x00},
104 SubProtocol: [4]byte{0x00, 0x00, 0x00, 0x00},
105 Version: [2]byte{0x00, 0x01},
106 SubVersion: [2]byte{0x00, 0x02},
107 },
108 expected: false,
109 },
110 {
111 name: "Valid Handshake with Different Version",
112 input: handshake{
113 Protocol: [4]byte{0x54, 0x52, 0x54, 0x50}, // TRTP
114 SubProtocol: [4]byte{0x48, 0x4F, 0x54, 0x4C}, // HOTL
115 Version: [2]byte{0x00, 0x02},
116 SubVersion: [2]byte{0x00, 0x03},
117 },
118 expected: true,
119 },
120 }
121
122 for _, tt := range tests {
123 t.Run(tt.name, func(t *testing.T) {
124 result := tt.input.Valid()
125 if result != tt.expected {
126 t.Fatalf("expected %v, got %v", tt.expected, result)
127 }
128 })
129 }
130 }
131
132 // readWriteBuffer combines input and output buffers to implement io.ReadWriter
133 type readWriteBuffer struct {
134 input *bytes.Buffer
135 output *bytes.Buffer
136 }
137
138 func (rw *readWriteBuffer) Read(p []byte) (int, error) {
139 return rw.input.Read(p)
140 }
141
142 func (rw *readWriteBuffer) Write(p []byte) (int, error) {
143 return rw.output.Write(p)
144 }
145
146 func TestPerformHandshake(t *testing.T) {
147 tests := []struct {
148 name string
149 input []byte
150 expectedOutput []byte
151 expectedError string
152 }{
153 {
154 name: "Valid Handshake",
155 input: []byte{
156 0x54, 0x52, 0x54, 0x50, // TRTP
157 0x48, 0x4F, 0x54, 0x4C, // HOTL
158 0x00, 0x01, 0x00, 0x02, // Version 1, SubVersion 2
159 },
160 expectedOutput: []byte{0x54, 0x52, 0x54, 0x50, 0x00, 0x00, 0x00, 0x00},
161 expectedError: "",
162 },
163 {
164 name: "Invalid Handshake Size",
165 input: []byte{
166 0x54, 0x52, 0x54, 0x50, // TRTP
167 },
168 expectedOutput: nil,
169 expectedError: "read handshake: invalid handshake size",
170 },
171 {
172 name: "Invalid Protocol",
173 input: []byte{
174 0x00, 0x00, 0x00, 0x00, // Invalid protocol
175 0x48, 0x4F, 0x54, 0x4C, // HOTL
176 0x00, 0x01, 0x00, 0x02, // Version 1, SubVersion 2
177 },
178 expectedOutput: nil,
179 expectedError: "invalid protocol or sub-protocol in handshake",
180 },
181 {
182 name: "Invalid SubProtocol",
183 input: []byte{
184 0x54, 0x52, 0x54, 0x50, // TRTP
185 0x00, 0x00, 0x00, 0x00, // Invalid sub-protocol
186 0x00, 0x01, 0x00, 0x02, // Version 1, SubVersion 2
187 },
188 expectedOutput: nil,
189 expectedError: "invalid protocol or sub-protocol in handshake",
190 },
191 {
192 name: "Binary Read Error",
193 input: []byte{
194 0xFF, 0xFF, 0xFF, 0xFF, // Invalid data
195 0xFF, 0xFF, 0xFF, 0xFF,
196 0xFF, 0xFF, 0xFF, 0xFF,
197 },
198 expectedOutput: nil,
199 expectedError: "invalid protocol or sub-protocol in handshake",
200 },
201 }
202
203 for _, tt := range tests {
204 t.Run(tt.name, func(t *testing.T) {
205 inputBuffer := bytes.NewBuffer(tt.input)
206 outputBuffer := &bytes.Buffer{}
207 rw := &readWriteBuffer{
208 input: inputBuffer,
209 output: outputBuffer,
210 }
211
212 err := performHandshake(rw)
213
214 if tt.expectedError != "" {
215 if err == nil || err.Error() != tt.expectedError {
216 t.Fatalf("expected error %q, got %q", tt.expectedError, err)
217 }
218 } else {
219 if err != nil {
220 t.Fatalf("unexpected error: %v", err)
221 }
222 output := outputBuffer.Bytes()
223 if !bytes.Equal(output, tt.expectedOutput) {
224 t.Fatalf("expected output %v, got %v", tt.expectedOutput, output)
225 }
226 }
227 })
228 }
229 }