Debian Codegraph @

internal/openvpn/main_internal_test.go

raw
1 package openvpn
2
3 import (
4 "context"
5 "errors"
6 "io"
7 "log/slog"
8 "strconv"
9 "testing"
10 "time"
11
12 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config"
13 )
14
15 func TestParseManagementInterfaceVersion(t *testing.T) {
16 t.Parallel()
17
18 for _, tc := range []struct {
19 name string
20 versionLine string
21 expect int
22 err error
23 }{
24 {
25 name: "minimum supported version",
26 versionLine: "Management Version: 5",
27 expect: 5,
28 },
29 {
30 name: "multi-digit version",
31 versionLine: "Management Version: 10",
32 expect: 10,
33 },
34 {
35 name: "empty line",
36 versionLine: "",
37 err: ErrUnexpectedResponseFromVersionCommand,
38 },
39 {
40 name: "missing numeric field",
41 versionLine: managementVersionPrefix,
42 err: strconv.ErrSyntax,
43 },
44 {
45 name: "malformed numeric field",
46 versionLine: "Management Version: invalid",
47 err: strconv.ErrSyntax,
48 },
49 {
50 name: "unexpected prefix",
51 versionLine: "Unexpected Management Version: 5",
52 err: ErrUnexpectedResponseFromVersionCommand,
53 },
54 } {
55 t.Run(tc.name, func(t *testing.T) {
56 t.Parallel()
57
58 actual, err := parseManagementInterfaceVersion(tc.versionLine)
59 if !errors.Is(err, tc.err) {
60 t.Fatalf("parseManagementInterfaceVersion(%q) error = %v, want %v", tc.versionLine, err, tc.err)
61 }
62
63 if actual != tc.expect {
64 t.Errorf("parseManagementInterfaceVersion(%q) = %d, want %d", tc.versionLine, actual, tc.expect)
65 }
66 })
67 }
68 }
69
70 func TestManagementCommandName(t *testing.T) {
71 t.Parallel()
72
73 for _, tc := range []struct {
74 name string
75 command string
76 expect string
77 }{
78 {name: "empty", command: "", expect: ""},
79 {name: "whitespace", command: " \t\n", expect: ""},
80 {name: "arguments", command: "status 3", expect: "status"},
81 {name: "leading whitespace", command: " \tstatus 3", expect: "status"},
82 {name: "unicode whitespace", command: "\u2003status 3", expect: "status"},
83 {name: "first line", command: "status 3\r\npassword Auth secret", expect: "status"},
84 {name: "empty first line", command: "\r\nstatus 3", expect: ""},
85 } {
86 t.Run(tc.name, func(t *testing.T) {
87 t.Parallel()
88
89 if name := managementCommandName(tc.command); name != tc.expect {
90 t.Errorf("managementCommandName(%q) = %q, want %q", tc.command, name, tc.expect)
91 }
92 })
93 }
94 }
95
96 func TestSendCommandHonorsContextWhileWaitingForCommandLock(t *testing.T) {
97 t.Parallel()
98
99 client := newCommandTestClient()
100 firstCtx, cancelFirst := context.WithCancel(t.Context())
101 firstErrCh := make(chan error, 1)
102
103 go func() {
104 _, err := client.SendCommand(firstCtx, "first", false)
105 firstErrCh <- err
106 }()
107
108 requireCommand(t, client, "first")
109
110 secondCtx, cancelSecond := context.WithCancel(t.Context())
111 secondErrCh := make(chan error, 1)
112
113 go func() {
114 _, err := client.SendCommand(secondCtx, "second", false)
115 secondErrCh <- err
116 }()
117
118 cancelSecond()
119 requireErrorIs(t, secondErrCh, context.Canceled)
120
121 cancelFirst()
122 requireErrorIs(t, firstErrCh, context.Canceled)
123 client.Shutdown(t.Context())
124 }
125
126 func TestSendCommandHonorsContextWhileEnqueueing(t *testing.T) {
127 t.Parallel()
128
129 client := newCommandTestClient()
130 for range cap(client.commandsCh) {
131 client.commandsCh <- "queued"
132 }
133
134 ctx, cancel := context.WithCancel(t.Context())
135 errCh := make(chan error, 1)
136
137 go func() {
138 _, err := client.SendCommand(ctx, "blocked", false)
139 errCh <- err
140 }()
141
142 requireCommandLockHeld(t, client)
143 cancel()
144 requireErrorIs(t, errCh, context.Canceled)
145 client.Shutdown(t.Context())
146 }
147
148 func TestSendCommandHonorsContextWhileWaitingForResponse(t *testing.T) {
149 t.Parallel()
150
151 client := newCommandTestClient()
152 ctx, cancel := context.WithCancel(t.Context())
153 errCh := make(chan error, 1)
154
155 go func() {
156 _, err := client.SendCommand(ctx, "waiting", false)
157 errCh <- err
158 }()
159
160 requireCommand(t, client, "waiting")
161 cancel()
162 requireErrorIs(t, errCh, context.Canceled)
163 client.Shutdown(t.Context())
164 }
165
166 func TestSendCommandDrainsCanceledCommandResponse(t *testing.T) {
167 t.Parallel()
168
169 client := newCommandTestClient()
170 firstCtx, cancelFirst := context.WithCancel(t.Context())
171 firstErrCh := make(chan error, 1)
172
173 go func() {
174 _, err := client.SendCommand(firstCtx, "first", false)
175 firstErrCh <- err
176 }()
177
178 requireCommand(t, client, "first")
179 cancelFirst()
180 requireErrorIs(t, firstErrCh, context.Canceled)
181
182 secondResultCh := make(chan error, 1)
183
184 go func() {
185 _, err := client.SendCommand(t.Context(), "second", false)
186 secondResultCh <- err
187 }()
188
189 select {
190 case command := <-client.commandsCh:
191 t.Fatalf("received command %q before draining the canceled command response", command)
192 case <-time.After(50 * time.Millisecond):
193 }
194
195 client.commandResponseCh <- "SUCCESS: first command succeeded"
196
197 requireCommand(t, client, "second")
198
199 client.commandResponseCh <- "SUCCESS: second command succeeded"
200
201 requireErrorIs(t, secondResultCh, nil)
202 client.Shutdown(t.Context())
203 }
204
205 func TestShutdownUnblocksSendCommand(t *testing.T) {
206 t.Parallel()
207
208 client := newCommandTestClient()
209 errCh := make(chan error, 1)
210
211 go func() {
212 _, err := client.SendCommand(context.Background(), "waiting", false)
213 errCh <- err
214 }()
215
216 requireCommand(t, client, "waiting")
217 client.Shutdown(t.Context())
218 requireErrorIs(t, errCh, ErrConnectionTerminated)
219 }
220
221 func newCommandTestClient() *Client {
222 conf := config.Defaults
223 conf.OpenVPN.CommandTimeout = time.Hour
224
225 return New(slog.New(slog.NewTextHandler(io.Discard, nil)), &conf) //nolint:sloglint
226 }
227
228 func requireCommand(t *testing.T, client *Client, expected string) {
229 t.Helper()
230
231 select {
232 case actual := <-client.commandsCh:
233 if actual != expected {
234 t.Fatalf("received command %q, want %q", actual, expected)
235 }
236 case <-time.After(time.Second):
237 t.Fatalf("timeout waiting for command %q", expected)
238 }
239 }
240
241 func requireCommandLockHeld(t *testing.T, client *Client) {
242 t.Helper()
243
244 deadline := time.NewTimer(time.Second)
245 defer deadline.Stop()
246
247 ticker := time.NewTicker(time.Millisecond)
248 defer ticker.Stop()
249
250 for {
251 select {
252 case <-deadline.C:
253 t.Fatal("timeout waiting for command lock")
254 case <-ticker.C:
255 if len(client.commandLock) == 0 {
256 return
257 }
258 }
259 }
260 }
261
262 func requireErrorIs(t *testing.T, errCh <-chan error, target error) {
263 t.Helper()
264
265 select {
266 case err := <-errCh:
267 if target == nil && err != nil {
268 t.Fatalf("received unexpected error %v", err)
269 }
270
271 if target != nil && !errors.Is(err, target) {
272 t.Fatalf("received error %v, want %v", err, target)
273 }
274 case <-time.After(time.Second):
275 t.Fatalf("timeout waiting for error %v", target)
276 }
277 }