2019-07-30 18:55:34 +00:00
|
|
|
package pogs
|
|
|
|
|
|
|
|
import (
|
2019-11-21 16:56:04 +00:00
|
|
|
"fmt"
|
2019-07-30 18:55:34 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2019-08-22 18:53:48 +00:00
|
|
|
"time"
|
2019-07-30 18:55:34 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
capnp "zombiezen.com/go/capnproto2"
|
|
|
|
)
|
|
|
|
|
2019-11-21 16:56:04 +00:00
|
|
|
const (
|
|
|
|
testURL = "tunnel.example.com"
|
|
|
|
testTunnelID = "asdfghjkl;"
|
|
|
|
testRetryAfterSeconds = 19
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-22 16:17:23 +00:00
|
|
|
testErr = fmt.Errorf("Invalid credential")
|
|
|
|
testLogLines = []string{"all", "working"}
|
|
|
|
testEventDigest = []byte("asdf")
|
2020-03-05 21:20:12 +00:00
|
|
|
testConnDigest = []byte("lkjh")
|
2019-11-21 16:56:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// *PermanentRegistrationError implements TunnelRegistrationError
|
|
|
|
var _ TunnelRegistrationError = (*PermanentRegistrationError)(nil)
|
|
|
|
|
|
|
|
// *RetryableRegistrationError implements TunnelRegistrationError
|
|
|
|
var _ TunnelRegistrationError = (*RetryableRegistrationError)(nil)
|
|
|
|
|
2019-11-18 19:56:04 +00:00
|
|
|
func TestTunnelRegistration(t *testing.T) {
|
|
|
|
testCases := []*TunnelRegistration{
|
2020-03-05 21:20:12 +00:00
|
|
|
NewSuccessfulTunnelRegistration(testURL, testLogLines, testTunnelID, testEventDigest, testConnDigest),
|
|
|
|
NewSuccessfulTunnelRegistration(testURL, nil, testTunnelID, testEventDigest, testConnDigest),
|
2019-11-21 16:56:04 +00:00
|
|
|
NewPermanentRegistrationError(testErr).Serialize(),
|
|
|
|
NewRetryableRegistrationError(testErr, testRetryAfterSeconds).Serialize(),
|
2019-11-18 19:56:04 +00:00
|
|
|
}
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
|
|
|
capnpEntity, err := tunnelrpc.NewTunnelRegistration(seg)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.Fatal("Couldn't initialize a new message")
|
|
|
|
}
|
|
|
|
err = MarshalTunnelRegistration(capnpEntity, testCase)
|
|
|
|
if !assert.NoError(t, err, "testCase #%v failed to marshal", i) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result, err := UnmarshalTunnelRegistration(capnpEntity)
|
|
|
|
if !assert.NoError(t, err, "testCase #%v failed to unmarshal", i) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:58:49 +00:00
|
|
|
func TestConnectResult(t *testing.T) {
|
|
|
|
testCases := []ConnectResult{
|
|
|
|
&ConnectError{
|
2019-08-22 18:53:48 +00:00
|
|
|
Cause: "it broke",
|
|
|
|
ShouldRetry: false,
|
|
|
|
RetryAfter: 2 * time.Second,
|
|
|
|
},
|
2019-09-17 21:58:49 +00:00
|
|
|
&ConnectSuccess{
|
|
|
|
ServerLocationName: "SFO",
|
|
|
|
Config: sampleClientConfig(),
|
|
|
|
},
|
|
|
|
&ConnectSuccess{
|
|
|
|
ServerLocationName: "",
|
|
|
|
Config: nil,
|
|
|
|
},
|
2019-08-22 18:53:48 +00:00
|
|
|
}
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
|
|
|
capnpEntity, err := tunnelrpc.NewConnectResult(seg)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.Fatal("Couldn't initialize a new message")
|
|
|
|
}
|
|
|
|
err = MarshalConnectResult(capnpEntity, testCase)
|
|
|
|
if !assert.NoError(t, err, "testCase #%v failed to marshal", i) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result, err := UnmarshalConnectResult(capnpEntity)
|
|
|
|
if !assert.NoError(t, err, "testCase #%v failed to unmarshal", i) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:55:34 +00:00
|
|
|
func TestConnectParameters(t *testing.T) {
|
|
|
|
testCases := []*ConnectParameters{
|
|
|
|
sampleConnectParameters(),
|
|
|
|
sampleConnectParameters(func(c *ConnectParameters) {
|
2019-09-18 19:57:20 +00:00
|
|
|
c.IntentLabel = "my_intent"
|
2019-07-30 18:55:34 +00:00
|
|
|
}),
|
|
|
|
sampleConnectParameters(func(c *ConnectParameters) {
|
|
|
|
c.Tags = nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
|
|
|
capnpEntity, err := tunnelrpc.NewCapnpConnectParameters(seg)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.Fatal("Couldn't initialize a new message")
|
|
|
|
}
|
|
|
|
err = MarshalConnectParameters(capnpEntity, testCase)
|
|
|
|
if !assert.NoError(t, err, "testCase index %v failed to marshal", i) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result, err := UnmarshalConnectParameters(capnpEntity)
|
|
|
|
if !assert.NoError(t, err, "testCase index %v failed to unmarshal", i) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sampleConnectParameters(overrides ...func(*ConnectParameters)) *ConnectParameters {
|
|
|
|
cloudflaredID, err := uuid.Parse("ED7BA470-8E54-465E-825C-99712043E01C")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
sample := &ConnectParameters{
|
|
|
|
OriginCert: []byte("my-origin-cert"),
|
|
|
|
CloudflaredID: cloudflaredID,
|
|
|
|
NumPreviousAttempts: 19,
|
|
|
|
Tags: []Tag{
|
|
|
|
Tag{
|
|
|
|
Name: "provision-method",
|
|
|
|
Value: "new",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CloudflaredVersion: "7.0",
|
2019-09-18 19:57:20 +00:00
|
|
|
IntentLabel: "my_intent",
|
2019-07-30 18:55:34 +00:00
|
|
|
}
|
|
|
|
sample.ensureNoZeroFields()
|
|
|
|
for _, f := range overrides {
|
|
|
|
f(sample)
|
|
|
|
}
|
|
|
|
return sample
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnectParameters) ensureNoZeroFields() {
|
|
|
|
ensureNoZeroFieldsInSample(reflect.ValueOf(c), []string{})
|
|
|
|
}
|