Skip to content

Commit ea7c0ca

Browse files
committed
rfq: add tls test cases
Adds some basic test cases for configuring transport credentials.
1 parent 138eef4 commit ea7c0ca

File tree

2 files changed

+98
-45
lines changed

2 files changed

+98
-45
lines changed

rfq/oracle_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ func runQuerySalePriceTest(t *testing.T, tc *testCaseQuerySalePrice) {
141141

142142
// Create a new RPC price oracle client and connect to the mock service.
143143
serviceAddr := fmt.Sprintf("rfqrpc://%s", testServiceAddress)
144-
client, err := NewRpcPriceOracle(serviceAddr, DefaultTLSConfig())
144+
insecureTLS := &TLSConfig{Enabled: false}
145+
client, err := NewRpcPriceOracle(serviceAddr, insecureTLS)
145146
require.NoError(t, err)
146147

147148
// Query for an ask price.
@@ -239,6 +240,13 @@ type testCaseQueryPurchasePrice struct {
239240
assetGroupKey *btcec.PublicKey
240241
}
241242

243+
// insecureTLS returns a TLSConfig with TLS disabled.
244+
func insecureTLS() *TLSConfig {
245+
return &TLSConfig{
246+
Enabled: false,
247+
}
248+
}
249+
242250
// runQueryPurchasePriceTest runs the RPC price oracle client QueryBuyPrice
243251
// test.
244252
func runQueryPurchasePriceTest(t *testing.T, tc *testCaseQueryPurchasePrice) {
@@ -251,7 +259,7 @@ func runQueryPurchasePriceTest(t *testing.T, tc *testCaseQueryPurchasePrice) {
251259

252260
// Create a new RPC price oracle client and connect to the mock service.
253261
serviceAddr := fmt.Sprintf("rfqrpc://%s", testServiceAddress)
254-
client, err := NewRpcPriceOracle(serviceAddr, DefaultTLSConfig())
262+
client, err := NewRpcPriceOracle(serviceAddr, insecureTLS())
255263
require.NoError(t, err)
256264

257265
// Query for an ask price.

rfq/tls_test.go

Lines changed: 88 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/stretchr/testify/require"
77
)
88

9-
// Test certificate data - a valid self-signed certificate for testing
10-
const validTestCertPEM = `-----BEGIN CERTIFICATE-----
9+
// validCertificate is a valid certificate.
10+
const validCertificate = `-----BEGIN CERTIFICATE-----
1111
MIICmjCCAYICCQCuu1gzY+BBKjANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0
1212
ZXN0MB4XDTI1MDgyODEwNDA1NVoXDTI1MDgyOTEwNDA1NVowDzENMAsGA1UEAwwE
1313
dGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALTWCm8l3d9nE2QK
@@ -24,62 +24,107 @@ Wo7g6udwyA48doEVJMjThFLPcW7xmsy6Ldew682m1kD8/ag+9qihX1IJyiqiEjha
2424
BcoNuBHB65RxQM5fpA7hkEFm1bxBoowGX2hx6VCCeBBwREISRfgvkUxZahUXNg==
2525
-----END CERTIFICATE-----`
2626

27-
// Invalid PEM data for testing failure cases
28-
const invalidTestCertPEM = `-----BEGIN CERTIFICATE-----
27+
// invalidCertificate is an invalid certificate.
28+
const invalidCertificate = `-----BEGIN CERTIFICATE-----
2929
This is not a valid certificate
3030
-----END CERTIFICATE-----`
3131

32-
// DefaultTLSConfig returns a default TLS configuration for testing.
33-
func DefaultTLSConfig() *TLSConfig {
34-
return &TLSConfig{
35-
InsecureSkipVerify: true,
36-
}
37-
}
32+
// testCaseConfigureTransportCredentials is a test case for the
33+
// configureTransportCredentials function.
34+
type testCaseConfigureTransportCredentials struct {
35+
name string
3836

39-
// TestConfigureTransportCredentials_InsecureSkipVerify tests the function
40-
// when InsecureSkipVerify is true.
41-
func TestConfigureTransportCredentials_InsecureSkipVerify(t *testing.T) {
42-
config := &TLSConfig{
43-
InsecureSkipVerify: true,
44-
}
37+
expectInsecure bool
4538

46-
creds, err := configureTransportCredentials(config)
39+
tlsConfig *TLSConfig
40+
}
4741

48-
require.NoError(t, err)
49-
require.NotNil(t, creds)
42+
// runConfigureTransportCredentialsTest tests that we get the expected
43+
// security protocol from the provided test case.
44+
func runConfigureTransportCredentialsTest(t *testing.T,
45+
tc *testCaseConfigureTransportCredentials) {
5046

51-
// Verify that we got insecure credentials by checking the type
52-
require.Equal(t, "insecure", creds.Info().SecurityProtocol)
53-
}
47+
creds, err := configureTransportCredentials(tc.tlsConfig)
5448

55-
// TestConfigureTransportCredentials_ValidCustomCertificates tests the
56-
// function when valid custom certificates are provided.
57-
func TestConfigureTransportCredentials_ValidCustomCertificates(t *testing.T) {
58-
config := &TLSConfig{
59-
InsecureSkipVerify: false,
60-
CustomCertificates: []byte(validTestCertPEM),
61-
}
49+
// We should never see an error here.
50+
require.Nil(t, err)
6251

63-
creds, err := configureTransportCredentials(config)
52+
protocol := creds.Info().SecurityProtocol
6453

65-
require.NoError(t, err)
66-
require.NotNil(t, creds)
54+
if tc.expectInsecure {
55+
require.Equal(t, "insecure", protocol)
56+
return
57+
}
6758

68-
// Verify that we got TLS credentials (not insecure)
69-
require.Equal(t, "tls", creds.Info().SecurityProtocol)
59+
require.Equal(t, "tls", protocol)
7060
}
7161

72-
// TestConfigureTransportCredentials_NoCredentialsConfigured tests the
73-
// function when no credentials are configured.
74-
func TestConfigureTransportCredentials_NoCredentialsConfigured(t *testing.T) {
75-
config := &TLSConfig{
62+
// defaultTLSConfig is the default TLS config.
63+
func DefaultTLSConfig() *TLSConfig {
64+
return &TLSConfig{
65+
Enabled: true,
7666
InsecureSkipVerify: false,
77-
CustomCertificates: nil,
67+
TrustSystemRootCAs: true,
7868
}
69+
}
7970

80-
creds, err := configureTransportCredentials(config)
71+
// TestConfigureTransportCredentials tests the configureTransportCredentials
72+
// function.
73+
func TestConfigureTransportCredentials(t *testing.T) {
74+
testCases := []*testCaseConfigureTransportCredentials{
75+
{
76+
name: "default configuration",
77+
expectInsecure: false,
78+
tlsConfig: DefaultTLSConfig(),
79+
},
80+
{
81+
name: "tls disabled",
82+
expectInsecure: true,
83+
tlsConfig: &TLSConfig{
84+
Enabled: false,
85+
},
86+
},
87+
{
88+
name: "trust os root CAs",
89+
expectInsecure: false,
90+
tlsConfig: &TLSConfig{
91+
Enabled: true,
92+
InsecureSkipVerify: false,
93+
TrustSystemRootCAs: true,
94+
},
95+
},
96+
{
97+
name: "no trust os root CAs",
98+
expectInsecure: false,
99+
tlsConfig: &TLSConfig{
100+
Enabled: true,
101+
InsecureSkipVerify: false,
102+
TrustSystemRootCAs: false,
103+
},
104+
},
105+
{
106+
name: "valid custom certificate",
107+
expectInsecure: false,
108+
tlsConfig: &TLSConfig{
109+
Enabled: true,
110+
InsecureSkipVerify: false,
111+
TrustSystemRootCAs: false,
112+
CustomCertificates: []byte(validCertificate),
113+
},
114+
},
115+
{
116+
name: "invalid custom certificate",
117+
expectInsecure: false,
118+
tlsConfig: &TLSConfig{
119+
Enabled: true,
120+
InsecureSkipVerify: false,
121+
TrustSystemRootCAs: false,
122+
CustomCertificates: []byte(invalidCertificate),
123+
},
124+
},
125+
}
81126

82-
require.NoError(t, err)
83-
require.NotNil(t, creds)
84-
require.Equal(t, "tls", creds.Info().SecurityProtocol)
127+
for _, tc := range testCases {
128+
runConfigureTransportCredentialsTest(t, tc)
129+
}
85130
}

0 commit comments

Comments
 (0)