Debian Codegraph @

internal/openvpn/status_test.go

raw
1 package openvpn //nolint:testpackage // Exercise the status parser directly with malformed management output.
2
3 import (
4 "strconv"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8 )
9
10 const status3ClientHeader = "HEADER\tCLIENT_LIST\tCommon Name\tReal Address\tVirtual Address\tVirtual IPv6 Address\t" +
11 "Bytes Received\tBytes Sent\tConnected Since\tConnected Since (time_t)\tUsername\tClient ID\tPeer ID\tData Channel Cipher"
12
13 func TestStatusClientIDsByUsername(t *testing.T) {
14 t.Parallel()
15
16 status := status3ClientHeader + "\r\n" +
17 status3ClientLine("alice", 7) + "\r\n" +
18 status3ClientLine("Alice", 8) + "\r\n" +
19 status3ClientLine("alice", 10) + "\r\n" +
20 status3ClientLine("alice", 12) + "\r\n" +
21 status3ClientLine("alice", 12) + "\r\nEND\r\n"
22
23 clientIDs, err := statusClientIDsByUsername(status, "alice", 10)
24
25 require.NoError(t, err)
26 require.Equal(t, []uint64{7, 12}, clientIDs)
27 }
28
29 func TestStatusClientIDsByUsernameRejectsInvalidStatus(t *testing.T) {
30 t.Parallel()
31
32 tests := []struct {
33 name string
34 status string
35 err string
36 }{
37 {
38 name: "missing header",
39 status: status3ClientLine("alice", 7) + "\nEND\n",
40 err: "client list header is missing Username or Client ID",
41 },
42 {
43 name: "missing client ID column",
44 status: "HEADER\tCLIENT_LIST\tUsername\nEND\n",
45 err: "client list header is missing Username or Client ID",
46 },
47 {
48 name: "short client record",
49 status: status3ClientHeader + "\nCLIENT_LIST\talice\nEND\n",
50 err: "client list record has 2 fields",
51 },
52 {
53 name: "invalid client ID",
54 status: status3ClientHeader + "\n" + status3ClientLineWithID("alice", "invalid") + "\nEND\n",
55 err: `parse client list client ID "invalid"`,
56 },
57 }
58
59 for _, testCase := range tests {
60 t.Run(testCase.name, func(t *testing.T) {
61 t.Parallel()
62
63 clientIDs, err := statusClientIDsByUsername(testCase.status, "alice", 10)
64
65 require.ErrorContains(t, err, testCase.err)
66 require.Nil(t, clientIDs)
67 })
68 }
69 }
70
71 func status3ClientLine(username string, clientID uint64) string {
72 return status3ClientLineWithID(username, strconv.FormatUint(clientID, 10))
73 }
74
75 func status3ClientLineWithID(username, clientID string) string {
76 return "CLIENT_LIST\tclient\t127.0.0.1:1194\t10.8.0.2\t\t1\t2\t2026-07-19 12:00:00\t1784455200\t" +
77 username + "\t" + clientID + "\t0\tAES-256-GCM"
78 }