2020-05-01 20:59:52 +00:00
|
|
|
package certutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadOriginCert(t *testing.T) {
|
|
|
|
cert, err := DecodeOriginCert([]byte{})
|
|
|
|
assert.Equal(t, fmt.Errorf("Cannot decode empty certificate"), err)
|
|
|
|
assert.Nil(t, cert)
|
|
|
|
|
2022-11-14 14:50:17 +00:00
|
|
|
blocks, err := ioutil.ReadFile("test-cert-unknown-block.pem")
|
2020-05-01 20:59:52 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
cert, err = DecodeOriginCert(blocks)
|
2022-11-16 12:05:09 +00:00
|
|
|
assert.Equal(t, fmt.Errorf("Unknown block RSA PRIVATE KEY in the certificate"), err)
|
2022-11-14 14:50:17 +00:00
|
|
|
assert.Nil(t, cert)
|
2022-11-14 14:50:17 +00:00
|
|
|
}
|
2022-11-16 12:05:09 +00:00
|
|
|
|
2022-11-14 14:50:17 +00:00
|
|
|
func TestJSONArgoTunnelTokenEmpty(t *testing.T) {
|
|
|
|
cert, err := DecodeOriginCert([]byte{})
|
|
|
|
blocks, err := ioutil.ReadFile("test-cert-no-token.pem")
|
2022-11-16 12:05:09 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
cert, err = DecodeOriginCert(blocks)
|
2022-11-14 14:50:17 +00:00
|
|
|
assert.Equal(t, fmt.Errorf("Missing token in the certificate"), err)
|
|
|
|
assert.Nil(t, cert)
|
2020-05-01 20:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONArgoTunnelToken(t *testing.T) {
|
|
|
|
// The given cert's Argo Tunnel Token was generated by base64 encoding this JSON:
|
|
|
|
// {
|
|
|
|
// "zoneID": "7b0a4d77dfb881c1a3b7d61ea9443e19",
|
2022-11-14 14:50:17 +00:00
|
|
|
// "apiToken": "test-service-key",
|
2020-05-01 20:59:52 +00:00
|
|
|
// "accountID": "abcdabcdabcdabcd1234567890abcdef"
|
|
|
|
// }
|
2022-11-14 14:50:17 +00:00
|
|
|
CloudflareTunnelTokenTest(t, "test-cloudflare-tunnel-cert-json.pem")
|
2020-05-01 20:59:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 14:50:17 +00:00
|
|
|
func CloudflareTunnelTokenTest(t *testing.T, path string) {
|
2020-05-01 20:59:52 +00:00
|
|
|
blocks, err := ioutil.ReadFile(path)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
cert, err := DecodeOriginCert(blocks)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, cert)
|
|
|
|
assert.Equal(t, "7b0a4d77dfb881c1a3b7d61ea9443e19", cert.ZoneID)
|
|
|
|
key := "test-service-key"
|
2022-11-14 14:50:17 +00:00
|
|
|
assert.Equal(t, key, cert.APIToken)
|
2020-05-01 20:59:52 +00:00
|
|
|
}
|