2020-10-14 13:42:00 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-11 10:31:05 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/edgediscovery"
|
2020-10-14 13:42:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-21 15:23:18 +00:00
|
|
|
testNoTTL = 0
|
|
|
|
noWarpRoutingEnabled = false
|
2020-10-14 13:42:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testNamedTunnelConfig = &NamedTunnelConfig{
|
2020-11-23 21:36:16 +00:00
|
|
|
Credentials: Credentials{
|
2020-10-14 13:42:00 +00:00
|
|
|
AccountTag: "testAccountTag",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
func mockFetcher(getError bool, protocolPercent ...edgediscovery.ProtocolPercent) PercentageFetcher {
|
|
|
|
return func() (edgediscovery.ProtocolPercents, error) {
|
|
|
|
if getError {
|
2021-11-12 15:38:06 +00:00
|
|
|
return nil, fmt.Errorf("failed to fetch percentage")
|
2021-10-11 10:31:05 +00:00
|
|
|
}
|
|
|
|
return protocolPercent, nil
|
2020-10-14 13:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type dynamicMockFetcher struct {
|
2021-10-11 10:31:05 +00:00
|
|
|
protocolPercents edgediscovery.ProtocolPercents
|
|
|
|
err error
|
2020-10-14 13:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dmf *dynamicMockFetcher) fetch() PercentageFetcher {
|
2021-10-11 10:31:05 +00:00
|
|
|
return func() (edgediscovery.ProtocolPercents, error) {
|
|
|
|
return dmf.protocolPercents, dmf.err
|
2020-10-14 13:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewProtocolSelector(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-01-21 15:23:18 +00:00
|
|
|
name string
|
|
|
|
protocol string
|
|
|
|
expectedProtocol Protocol
|
|
|
|
hasFallback bool
|
|
|
|
expectedFallback Protocol
|
|
|
|
warpRoutingEnabled bool
|
|
|
|
namedTunnelConfig *NamedTunnelConfig
|
|
|
|
fetchFunc PercentageFetcher
|
|
|
|
wantErr bool
|
2020-10-14 13:42:00 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "classic tunnel",
|
|
|
|
protocol: "h2mux",
|
|
|
|
expectedProtocol: H2mux,
|
|
|
|
namedTunnelConfig: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named tunnel over h2mux",
|
|
|
|
protocol: "h2mux",
|
|
|
|
expectedProtocol: H2mux,
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: func() (edgediscovery.ProtocolPercents, error) { return nil, nil },
|
2020-10-14 13:42:00 +00:00
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named tunnel over http2",
|
|
|
|
protocol: "http2",
|
|
|
|
expectedProtocol: HTTP2,
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}),
|
2020-10-14 13:42:00 +00:00
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
2022-01-05 17:58:49 +00:00
|
|
|
name: "named tunnel http2 disabled still gets http2 because it is manually picked",
|
2020-10-14 13:42:00 +00:00
|
|
|
protocol: "http2",
|
2022-01-05 17:58:49 +00:00
|
|
|
expectedProtocol: HTTP2,
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}),
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
2022-01-05 17:58:49 +00:00
|
|
|
{
|
|
|
|
name: "named tunnel quic disabled still gets quic because it is manually picked",
|
|
|
|
protocol: "quic",
|
|
|
|
expectedProtocol: QUIC,
|
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: -1}),
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named tunnel quic and http2 disabled",
|
|
|
|
protocol: "auto",
|
|
|
|
expectedProtocol: H2mux,
|
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: -1}),
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
2021-10-11 10:31:05 +00:00
|
|
|
{
|
|
|
|
name: "named tunnel quic disabled",
|
2022-01-05 17:58:49 +00:00
|
|
|
protocol: "auto",
|
2021-10-11 10:31:05 +00:00
|
|
|
expectedProtocol: HTTP2,
|
|
|
|
// Hasfallback true is because if http2 fails, then we further fallback to h2mux.
|
|
|
|
hasFallback: true,
|
|
|
|
expectedFallback: H2mux,
|
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: -1}),
|
2020-10-14 13:42:00 +00:00
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named tunnel auto all http2 disabled",
|
|
|
|
protocol: "auto",
|
|
|
|
expectedProtocol: H2mux,
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}),
|
2020-10-14 13:42:00 +00:00
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named tunnel auto to h2mux",
|
|
|
|
protocol: "auto",
|
|
|
|
expectedProtocol: H2mux,
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}),
|
2020-10-14 13:42:00 +00:00
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named tunnel auto to http2",
|
|
|
|
protocol: "auto",
|
|
|
|
expectedProtocol: HTTP2,
|
|
|
|
hasFallback: true,
|
|
|
|
expectedFallback: H2mux,
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named tunnel auto to quic",
|
|
|
|
protocol: "auto",
|
|
|
|
expectedProtocol: QUIC,
|
|
|
|
hasFallback: true,
|
|
|
|
expectedFallback: HTTP2,
|
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}),
|
2020-10-14 13:42:00 +00:00
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
2021-01-21 15:23:18 +00:00
|
|
|
{
|
|
|
|
name: "warp routing requesting h2mux",
|
|
|
|
protocol: "h2mux",
|
2021-10-11 10:31:05 +00:00
|
|
|
expectedProtocol: HTTP2Warp,
|
|
|
|
hasFallback: false,
|
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
|
|
|
|
warpRoutingEnabled: true,
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "warp routing requesting h2mux picks HTTP2 even if http2 percent is -1",
|
|
|
|
protocol: "h2mux",
|
|
|
|
expectedProtocol: HTTP2Warp,
|
2021-01-21 15:23:18 +00:00
|
|
|
hasFallback: false,
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}),
|
2021-01-21 15:23:18 +00:00
|
|
|
warpRoutingEnabled: true,
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "warp routing http2",
|
|
|
|
protocol: "http2",
|
2021-10-11 10:31:05 +00:00
|
|
|
expectedProtocol: HTTP2Warp,
|
2021-01-21 15:23:18 +00:00
|
|
|
hasFallback: false,
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
|
|
|
|
warpRoutingEnabled: true,
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "warp routing quic",
|
2022-01-05 17:58:49 +00:00
|
|
|
protocol: "auto",
|
2021-10-11 10:31:05 +00:00
|
|
|
expectedProtocol: QUICWarp,
|
|
|
|
hasFallback: true,
|
|
|
|
expectedFallback: HTTP2Warp,
|
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}),
|
2021-01-21 15:23:18 +00:00
|
|
|
warpRoutingEnabled: true,
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "warp routing auto",
|
|
|
|
protocol: "auto",
|
2021-10-11 10:31:05 +00:00
|
|
|
expectedProtocol: HTTP2Warp,
|
2021-01-21 15:23:18 +00:00
|
|
|
hasFallback: false,
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
|
|
|
|
warpRoutingEnabled: true,
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "warp routing auto- quic",
|
|
|
|
protocol: "auto",
|
|
|
|
expectedProtocol: QUICWarp,
|
|
|
|
hasFallback: true,
|
|
|
|
expectedFallback: HTTP2Warp,
|
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}, edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}),
|
2021-01-21 15:23:18 +00:00
|
|
|
warpRoutingEnabled: true,
|
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
},
|
2020-10-14 13:42:00 +00:00
|
|
|
{
|
|
|
|
// None named tunnel can only use h2mux, so specifying an unknown protocol is not an error
|
|
|
|
name: "classic tunnel unknown protocol",
|
|
|
|
protocol: "unknown",
|
|
|
|
expectedProtocol: H2mux,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named tunnel unknown protocol",
|
|
|
|
protocol: "unknown",
|
2021-10-11 10:31:05 +00:00
|
|
|
fetchFunc: mockFetcher(false, edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}),
|
2020-10-14 13:42:00 +00:00
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "named tunnel fetch error",
|
2021-10-11 10:31:05 +00:00
|
|
|
protocol: "auto",
|
|
|
|
fetchFunc: mockFetcher(true),
|
2020-10-14 13:42:00 +00:00
|
|
|
namedTunnelConfig: testNamedTunnelConfig,
|
2021-07-28 22:31:22 +00:00
|
|
|
expectedProtocol: HTTP2,
|
|
|
|
wantErr: false,
|
2020-10-14 13:42:00 +00:00
|
|
|
},
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
|
2020-10-14 13:42:00 +00:00
|
|
|
for _, test := range tests {
|
2021-10-11 10:31:05 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
selector, err := NewProtocolSelector(test.protocol, test.warpRoutingEnabled, test.namedTunnelConfig, test.fetchFunc, testNoTTL, &log)
|
|
|
|
if test.wantErr {
|
|
|
|
assert.Error(t, err, fmt.Sprintf("test %s failed", test.name))
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err, fmt.Sprintf("test %s failed", test.name))
|
|
|
|
assert.Equal(t, test.expectedProtocol, selector.Current(), fmt.Sprintf("test %s failed", test.name))
|
|
|
|
fallback, ok := selector.Fallback()
|
|
|
|
assert.Equal(t, test.hasFallback, ok, fmt.Sprintf("test %s failed", test.name))
|
|
|
|
if test.hasFallback {
|
|
|
|
assert.Equal(t, test.expectedFallback, fallback, fmt.Sprintf("test %s failed", test.name))
|
|
|
|
}
|
2020-10-14 13:42:00 +00:00
|
|
|
}
|
2021-10-11 10:31:05 +00:00
|
|
|
})
|
2020-10-14 13:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoProtocolSelectorRefresh(t *testing.T) {
|
|
|
|
fetcher := dynamicMockFetcher{}
|
2021-01-21 15:23:18 +00:00
|
|
|
selector, err := NewProtocolSelector("auto", noWarpRoutingEnabled, testNamedTunnelConfig, fetcher.fetch(), testNoTTL, &log)
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, H2mux, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}}
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}}
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.Equal(t, H2mux, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}}
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
|
|
|
|
|
|
|
fetcher.err = fmt.Errorf("failed to fetch")
|
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}}
|
2020-10-14 13:42:00 +00:00
|
|
|
fetcher.err = nil
|
|
|
|
assert.Equal(t, H2mux, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}}
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.Equal(t, H2mux, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}}
|
|
|
|
assert.Equal(t, QUIC, selector.Current())
|
2020-10-14 13:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP2ProtocolSelectorRefresh(t *testing.T) {
|
|
|
|
fetcher := dynamicMockFetcher{}
|
2022-01-05 17:58:49 +00:00
|
|
|
// Since the user chooses http2 on purpose, we always stick to it.
|
2021-01-21 15:23:18 +00:00
|
|
|
selector, err := NewProtocolSelector("http2", noWarpRoutingEnabled, testNamedTunnelConfig, fetcher.fetch(), testNoTTL, &log)
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}}
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}}
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
|
|
|
|
|
|
|
fetcher.err = fmt.Errorf("failed to fetch")
|
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}}
|
2020-10-14 13:42:00 +00:00
|
|
|
fetcher.err = nil
|
2022-01-05 17:58:49 +00:00
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
2020-10-14 13:42:00 +00:00
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 0}}
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: 100}}
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "http2", Percentage: -1}}
|
2022-01-05 17:58:49 +00:00
|
|
|
assert.Equal(t, HTTP2, selector.Current())
|
2020-10-14 13:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProtocolSelectorRefreshTTL(t *testing.T) {
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher := dynamicMockFetcher{}
|
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}}
|
2021-01-21 15:23:18 +00:00
|
|
|
selector, err := NewProtocolSelector("auto", noWarpRoutingEnabled, testNamedTunnelConfig, fetcher.fetch(), time.Hour, &log)
|
2020-10-14 13:42:00 +00:00
|
|
|
assert.NoError(t, err)
|
2021-10-11 10:31:05 +00:00
|
|
|
assert.Equal(t, QUIC, selector.Current())
|
2020-10-14 13:42:00 +00:00
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
fetcher.protocolPercents = edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 0}}
|
|
|
|
assert.Equal(t, QUIC, selector.Current())
|
2020-10-14 13:42:00 +00:00
|
|
|
}
|