| 1 |
package openvpn_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net/url" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 10 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types" |
| 11 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/openvpn" |
| 12 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testsuite" |
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
) |
| 16 |
|
| 17 |
func BenchmarkOpenVPNHandler(b *testing.B) { |
| 18 |
b.StopTimer() |
| 19 |
|
| 20 |
ctx, cancel := context.WithCancel(b.Context()) |
| 21 |
b.Cleanup(cancel) |
| 22 |
|
| 23 |
conf := config.Defaults |
| 24 |
conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}} |
| 25 |
conf.HTTP.Secret = testsuite.Secret |
| 26 |
conf.OpenVPN.Bypass = config.OpenVPNBypass{CommonNames: make(types.RegexpSlice, 0)} |
| 27 |
|
| 28 |
suite := testsuite.New(&conf, testsuite.WithDiscardLogger()) |
| 29 |
errOpenVPNClientCh := suite.SetupManagementEnvironment(ctx, b, nil) |
| 30 |
openVPNClient := suite.GetOpenVPNClient() |
| 31 |
managementInterfaceConn := suite.GetManagementInterfaceConn() |
| 32 |
|
| 33 |
suite.ExpectVersionAndReleaseHold(b) |
| 34 |
|
| 35 |
tests := []struct { |
| 36 |
name string |
| 37 |
client string |
| 38 |
}{ |
| 39 |
{ |
| 40 |
"short", |
| 41 |
">CLIENT:CONNECT,0,1\r\n>CLIENT:ENV,untrusted_ip=127.0.0.1\r\n>CLIENT:ENV,common_name=test\r\n>CLIENT:ENV,IV_SSO=webauth\r\n>CLIENT:ENV,END\r\n", |
| 42 |
}, |
| 43 |
{ |
| 44 |
"real", |
| 45 |
">CLIENT:CONNECT,0,1\r\n>CLIENT:ENV,n_clients=0\r\n>CLIENT:ENV,password=\r\n>CLIENT:ENV,untrusted_port=17016\r\n>CLIENT:ENV,untrusted_ip=192.168.65.1\r\n>CLIENT:ENV,[email protected]\r\n>CLIENT:ENV,username=\r\n>CLIENT:ENV,IV_BS64DL=1\r\n>CLIENT:ENV,IV_SSO=webauth,openurl,crtext\r\n>CLIENT:ENV,IV_GUI_VER=OCmacOS_3.4.4-4629\r\n>CLIENT:ENV,IV_AUTO_SESS=1\r\n>CLIENT:ENV,IV_CIPHERS=AES-128-GCM:AES-192-GCM:AES-256-GCM:CHACHA20-POLY1305\r\n>CLIENT:ENV,IV_MTU=1600\r\n>CLIENT:ENV,IV_PROTO=990\r\n>CLIENT:ENV,IV_TCPNL=1\r\n>CLIENT:ENV,IV_NCP=2\r\n>CLIENT:ENV,IV_PLAT=mac\r\n>CLIENT:ENV,IV_VER=3.8.1\r\n>CLIENT:ENV,tls_serial_hex_0=51:b3:55:90:65:af:71:5c:d5:52:2b:0b:00:14:8d:ee\r\n>CLIENT:ENV,tls_serial_0=108598624241397715647038806614705737198\r\n>CLIENT:ENV,tls_digest_sha256_0=d3:6d:1d:96:f8:bd:7e:e8:db:c4:0f:53:a1:76:f0:ca:9e:78:63:bf:c6:4a:ac:b9:e6:ed:84:62:f5:ac:5d:b8\r\n>CLIENT:ENV,tls_digest_0=b7:73:bd:6c:31:31:49:63:0d:0c:11:6d:0c:13:d0:b4:8f:97:33:7d\r\n>CLIENT:ENV,[email protected]\r\n>CLIENT:ENV,[email protected]\r\n>CLIENT:ENV,tls_serial_hex_1=01:b3:95:f8:1a:9f:9f:fe:7c:27:ad:29:c1:93:23:ae:08:7f:ab:36\r\n>CLIENT:ENV,tls_serial_1=9713888317380397892476539918183380788698917686\r\n>CLIENT:ENV,tls_digest_sha256_1=75:1a:a1:63:bb:e9:c7:f3:e3:bf:e1:08:f1:36:b7:36:90:04:da:dd:b8:78:b1:cf:d5:ac:09:b6:36:31:a7:db\r\n>CLIENT:ENV,tls_digest_1=d4:bc:00:89:e5:01:0c:27:3d:ea:4a:b5:42:8b:f7:3d:19:7a:a2:25\r\n>CLIENT:ENV,tls_id_1=CN=Easy-RSA CA\r\n>CLIENT:ENV,X509_1_CN=Easy-RSA CA\r\n>CLIENT:ENV,remote_port_1=1194\r\n>CLIENT:ENV,local_port_1=1194\r\n>CLIENT:ENV,proto_1=udp\r\n>CLIENT:ENV,daemon_pid=7\r\n>CLIENT:ENV,daemon_start_time=1703401559\r\n>CLIENT:ENV,daemon_log_redirect=0\r\n>CLIENT:ENV,daemon=0\r\n>CLIENT:ENV,verb=3\r\n>CLIENT:ENV,config=/etc/openvpn/openvpn.conf\r\n>CLIENT:ENV,ifconfig_local=100.64.0.1\r\n>CLIENT:ENV,ifconfig_netmask=255.255.255.0\r\n>CLIENT:ENV,script_context=init\r\n>CLIENT:ENV,tun_mtu=1500\r\n>CLIENT:ENV,dev=tun0\r\n>CLIENT:ENV,dev_type=tun\r\n>CLIENT:ENV,redirect_gateway=0\r\n>CLIENT:ENV,END\r\n", |
| 46 |
}, |
| 47 |
} |
| 48 |
|
| 49 |
b.ResetTimer() |
| 50 |
b.StartTimer() |
| 51 |
|
| 52 |
for _, tc := range tests { |
| 53 |
b.Run(tc.name, func(b *testing.B) { |
| 54 |
for b.Loop() { |
| 55 |
suite.SendMessagef(b, tc.client) |
| 56 |
assert.Contains(b, suite.ReadLine(b), "client-pending-auth 0 1 \"WEB_AUTH::") |
| 57 |
suite.SendMessagef(b, "SUCCESS: client-pending-auth command succeeded") |
| 58 |
} |
| 59 |
|
| 60 |
b.ReportAllocs() |
| 61 |
}) |
| 62 |
} |
| 63 |
|
| 64 |
b.StopTimer() |
| 65 |
|
| 66 |
openVPNClient.Shutdown(b.Context()) |
| 67 |
require.NoError(b, managementInterfaceConn.Close()) |
| 68 |
require.NoError(b, <-errOpenVPNClientCh) |
| 69 |
} |
| 70 |
|
| 71 |
func BenchmarkOpenVPNPassthrough(b *testing.B) { |
| 72 |
b.StopTimer() |
| 73 |
|
| 74 |
conf := config.Defaults |
| 75 |
conf.OpenVPN.Passthrough.Enabled = true |
| 76 |
conf.OpenVPN.Passthrough.Address = types.URL{URL: &url.URL{Scheme: "tcp", Host: "127.0.0.1:0"}} |
| 77 |
conf.OpenVPN.Passthrough.Password = testsuite.Secret |
| 78 |
|
| 79 |
suite := testsuite.New(&conf) |
| 80 |
errOpenVPNClientCh := suite.SetupManagementEnvironment(b.Context(), b, nil) |
| 81 |
openVPNClient := suite.GetOpenVPNClient() |
| 82 |
|
| 83 |
if conf.OpenVPN.Password != "" { |
| 84 |
suite.SendMessagef(b, "ENTER PASSWORD:") |
| 85 |
suite.ExpectMessage(b, conf.OpenVPN.Password.String()) |
| 86 |
suite.SendMessagef(b, "SUCCESS: password is correct") |
| 87 |
} |
| 88 |
|
| 89 |
suite.ExpectVersionAndReleaseHold(b) |
| 90 |
|
| 91 |
var passThroughAddr []string |
| 92 |
|
| 93 |
for range 10 { |
| 94 |
passThroughAddr = rePassThroughLogListen.FindStringSubmatch(suite.Logs()) |
| 95 |
if passThroughAddr != nil { |
| 96 |
break |
| 97 |
} |
| 98 |
|
| 99 |
time.Sleep(50 * time.Millisecond) |
| 100 |
} |
| 101 |
|
| 102 |
require.Len(b, passThroughAddr, 2, "unexpected log output: %s", suite.Logs()) |
| 103 |
|
| 104 |
passThroughNetConn, err := testsuite.WaitUntilListening(b.Context(), b, "tcp", passThroughAddr[1]) |
| 105 |
require.NoError(b, err) |
| 106 |
|
| 107 |
passThroughConn := testsuite.NewConn(passThroughNetConn) |
| 108 |
|
| 109 |
buf := make([]byte, len("ENTER PASSWORD:")) |
| 110 |
_, err = passThroughNetConn.Read(buf) |
| 111 |
require.NoError(b, err) |
| 112 |
require.Equal(b, "ENTER PASSWORD:", string(buf)) |
| 113 |
|
| 114 |
passThroughConn.SendAndExpectMessage(b, testsuite.Secret, "SUCCESS: password is correct") |
| 115 |
|
| 116 |
tests := []struct { |
| 117 |
command string |
| 118 |
response string |
| 119 |
}{ |
| 120 |
{ |
| 121 |
"pid", |
| 122 |
"SUCCESS: pid=7", |
| 123 |
}, |
| 124 |
{ |
| 125 |
"status", |
| 126 |
OpenVPNManagementInterfaceCommandResultStatus, |
| 127 |
}, |
| 128 |
{ |
| 129 |
"status 2", |
| 130 |
OpenVPNManagementInterfaceCommandResultStatus2, |
| 131 |
}, |
| 132 |
{ |
| 133 |
"help", |
| 134 |
OpenVPNManagementInterfaceCommandResultHelp, |
| 135 |
}, |
| 136 |
} |
| 137 |
|
| 138 |
passThroughConn.ExpectMessage(b, openvpn.WelcomeBanner) |
| 139 |
|
| 140 |
b.Cleanup(func() { |
| 141 |
openVPNClient.Shutdown(b.Context()) |
| 142 |
|
| 143 |
select { |
| 144 |
case err := <-errOpenVPNClientCh: |
| 145 |
require.NoError(b, err) |
| 146 |
case <-time.After(1 * time.Second): |
| 147 |
b.Fatalf("timeout waiting for connection to close. Logs:\n\n%s", suite.Logs()) |
| 148 |
} |
| 149 |
}) |
| 150 |
|
| 151 |
b.ResetTimer() |
| 152 |
b.StartTimer() |
| 153 |
|
| 154 |
for _, tc := range tests { |
| 155 |
b.Run(tc.command, func(b *testing.B) { |
| 156 |
b.ReportAllocs() |
| 157 |
b.ResetTimer() |
| 158 |
|
| 159 |
for b.Loop() { |
| 160 |
passThroughConn.SendMessagef(b, tc.command) |
| 161 |
suite.ExpectMessage(b, tc.command) |
| 162 |
suite.SendMessagef(b, tc.response) |
| 163 |
passThroughConn.ExpectMessage(b, tc.response) |
| 164 |
} |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
b.StopTimer() |
| 169 |
} |