| 1 |
package openvpn //nolint:testpackage // Verify the private command formatter's exact OpenVPN byte boundary. |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/openvpn/connection" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
) |
| 11 |
|
| 12 |
func TestBuildOAuth2StartURL(t *testing.T) { |
| 13 |
t.Parallel() |
| 14 |
|
| 15 |
for _, tc := range []struct { |
| 16 |
name string |
| 17 |
baseURL string |
| 18 |
want string |
| 19 |
}{ |
| 20 |
{ |
| 21 |
name: "without trailing slash", |
| 22 |
baseURL: "https://vpn.example.com", |
| 23 |
want: "https://vpn.example.com/oauth2/start?state=encrypted-state", |
| 24 |
}, |
| 25 |
{ |
| 26 |
name: "with trailing slash", |
| 27 |
baseURL: "https://vpn.example.com/", |
| 28 |
want: "https://vpn.example.com/oauth2/start?state=encrypted-state", |
| 29 |
}, |
| 30 |
} { |
| 31 |
t.Run(tc.name, func(t *testing.T) { |
| 32 |
t.Parallel() |
| 33 |
|
| 34 |
got := buildOAuth2StartURL(tc.baseURL, "encrypted-state") |
| 35 |
require.Equal(t, tc.want, got) |
| 36 |
}) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestBuildClientPendingAuthCommand(t *testing.T) { |
| 41 |
t.Parallel() |
| 42 |
|
| 43 |
for _, tc := range []struct { |
| 44 |
name string |
| 45 |
client connection.Client |
| 46 |
timeout time.Duration |
| 47 |
want string |
| 48 |
}{ |
| 49 |
{ |
| 50 |
name: "whole-second timeout", |
| 51 |
client: connection.Client{CID: 12345, KID: 67890}, |
| 52 |
timeout: 300 * time.Second, |
| 53 |
want: `client-pending-auth 12345 67890 "WEB_AUTH::https://vpn.example.com/oauth2/start?state=encrypted-state" 300`, |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "fractional timeout", |
| 57 |
client: connection.Client{CID: 1, KID: 0}, |
| 58 |
timeout: 1500 * time.Millisecond, |
| 59 |
want: `client-pending-auth 1 0 "WEB_AUTH::https://vpn.example.com/oauth2/start?state=encrypted-state" 2`, |
| 60 |
}, |
| 61 |
} { |
| 62 |
t.Run(tc.name, func(t *testing.T) { |
| 63 |
t.Parallel() |
| 64 |
|
| 65 |
command, err := buildClientPendingAuthCommand( |
| 66 |
tc.client, |
| 67 |
"https://vpn.example.com/oauth2/start?state=encrypted-state", |
| 68 |
tc.timeout, |
| 69 |
) |
| 70 |
require.NoError(t, err) |
| 71 |
require.Equal(t, tc.want, command) |
| 72 |
}) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestBuildClientPendingAuthCommandLimit(t *testing.T) { |
| 77 |
t.Parallel() |
| 78 |
|
| 79 |
client := connection.Client{CID: 12345, KID: 0} |
| 80 |
|
| 81 |
command, err := buildClientPendingAuthCommand(client, strings.Repeat("a", 979), 300*time.Second) |
| 82 |
require.NoError(t, err) |
| 83 |
require.Len(t, command, openVPNManagementCommandBodyLimit) |
| 84 |
|
| 85 |
_, err = buildClientPendingAuthCommand(client, strings.Repeat("a", 980), 300*time.Second) |
| 86 |
require.EqualError(t, err, "client-pending-auth command is 1024 bytes; OpenVPN accepts at most 1023 bytes") |
| 87 |
} |