Skip to content

Commit 111aa5e

Browse files
committed
ci: add tests for CertsToTables and PrintData
1 parent f0607e1 commit 111aa5e

2 files changed

Lines changed: 330 additions & 0 deletions

File tree

internal/certinfo/certinfo_handlers_test.go

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package certinfo
22

33
import (
4+
"bytes"
5+
"crypto/x509"
46
"testing"
57

68
"github.com/stretchr/testify/require"
@@ -96,6 +98,30 @@ func TestCertinfo_GetRemoteCerts(t *testing.T) {
9698
expectSrvHost: "localhost",
9799
expectSrvPort: "46306",
98100
},
101+
{
102+
desc: "IPV6 Enpoint RSA Cert Success",
103+
srvCfg: demoHTTPServerConfig{
104+
serverAddr: "[::1]:46307",
105+
serverName: "example.com",
106+
serverCertFile: RSASampleCertFile,
107+
serverKeyFile: RSASampleCertKeyFile,
108+
},
109+
caCertFile: RSACaCertFile,
110+
expectSrvHost: "::1",
111+
expectSrvPort: "46307",
112+
},
113+
{
114+
desc: "Error wrong ServerName",
115+
srvCfg: demoHTTPServerConfig{
116+
serverAddr: "localhost:46308",
117+
serverName: "example.co.uk",
118+
serverCertFile: RSASampleCertFile,
119+
serverKeyFile: RSASampleCertKeyFile,
120+
},
121+
caCertFile: RSACaCertFile,
122+
expectError: true,
123+
expectMsg: "TLS handshake failed: tls: failed to verify certificate: x509: certificate is valid for example.com, example.net, example.de, not example.co.uk",
124+
},
99125
}
100126

101127
for _, tc := range tests {
@@ -131,3 +157,223 @@ func TestCertinfo_GetRemoteCerts(t *testing.T) {
131157
})
132158
}
133159
}
160+
161+
func TestCertinfo_CertsToTables(t *testing.T) {
162+
rsaSampleCert, err := GetCertsFromBundle(
163+
RSASampleCertFile,
164+
inputReader,
165+
)
166+
require.NoError(t, err)
167+
168+
ecdsaCert, err := GetCertsFromBundle(
169+
ECDSASampleCertificate,
170+
inputReader,
171+
)
172+
require.NoError(t, err)
173+
174+
ed25519Cert, err := GetCertsFromBundle(
175+
ED25519SampleCertificate,
176+
inputReader,
177+
)
178+
require.NoError(t, err)
179+
180+
tests := []struct {
181+
desc string
182+
cert *x509.Certificate
183+
subject string
184+
isCA string
185+
expiration string
186+
dnsNames string
187+
publicKeyAlgorithm string
188+
signatureAlgorithm string
189+
}{
190+
// TODO: add expired cert case
191+
{
192+
desc: "RSA CA Cert",
193+
cert: RSACaCertParent,
194+
subject: "Subject CN=RSA Testing CA",
195+
isCA: "IsCA true",
196+
expiration: "Expiration 23 hours from now",
197+
dnsNames: "DNSNames []",
198+
publicKeyAlgorithm: "PublicKeyAlgorithm RSA",
199+
signatureAlgorithm: "SignatureAlgorithm SHA256-RSA",
200+
},
201+
{
202+
desc: "RSA Cert",
203+
cert: rsaSampleCert[0],
204+
subject: "Subject CN=RSA Testing Sample Certificate",
205+
isCA: "IsCA false",
206+
expiration: "Expiration 23 hours from now",
207+
dnsNames: "DNSNames [example.com, example.net, example.de]",
208+
publicKeyAlgorithm: "PublicKeyAlgorithm RSA",
209+
signatureAlgorithm: "SignatureAlgorithm SHA256-RSA",
210+
},
211+
{
212+
desc: "ECDSA CA Cert",
213+
cert: ecdsaCert[0],
214+
subject: "Subject CN=example.com,O=Example Org",
215+
isCA: "IsCA true",
216+
dnsNames: "DNSNames []",
217+
publicKeyAlgorithm: "PublicKeyAlgorithm ECDSA",
218+
signatureAlgorithm: "SignatureAlgorithm ECDSA-SHA256",
219+
},
220+
{
221+
desc: "ED25519 CA Cert",
222+
cert: ed25519Cert[0],
223+
subject: "Subject CN=example.com,O=Example Org",
224+
isCA: "IsCA true",
225+
dnsNames: "DNSNames []",
226+
publicKeyAlgorithm: "PublicKeyAlgorithm Ed25519",
227+
signatureAlgorithm: "SignatureAlgorithm Ed25519",
228+
},
229+
}
230+
231+
for _, tc := range tests {
232+
tt := tc
233+
t.Run(tt.desc, func(t *testing.T) {
234+
t.Parallel()
235+
236+
buffer := bytes.Buffer{}
237+
certs := []*x509.Certificate{
238+
tt.cert,
239+
}
240+
CertsToTables(&buffer, certs)
241+
242+
got := buffer.String()
243+
244+
for _, want := range []string{
245+
"Certificate",
246+
"Subject",
247+
"Issuer",
248+
"NotBefore",
249+
"NotAfter",
250+
"Expiration",
251+
"IsCA",
252+
"AuthorityKeyId",
253+
"SubjectKeyId",
254+
"PublicKeyAlgorithm",
255+
"SignatureAlgorithm",
256+
"SerialNumber",
257+
"Fingerprint SHA-256",
258+
tt.isCA,
259+
tt.dnsNames,
260+
tt.publicKeyAlgorithm,
261+
tt.signatureAlgorithm,
262+
} {
263+
require.Contains(t, got, want)
264+
}
265+
})
266+
}
267+
}
268+
269+
func TestCertinfo_PrintData(t *testing.T) {
270+
noErrorsTests := []struct {
271+
desc string
272+
keyFile string
273+
certFile string
274+
caCertFile string
275+
keyCertMatch bool
276+
tlsEndpoint string
277+
srvCfg demoHTTPServerConfig
278+
}{
279+
{
280+
desc: "local CA cert and key",
281+
keyFile: RSACaCertKeyFile,
282+
certFile: RSACaCertFile,
283+
keyCertMatch: true,
284+
},
285+
{
286+
desc: "local cert and key with CA",
287+
keyFile: RSASampleCertKeyFile,
288+
certFile: RSASampleCertFile,
289+
caCertFile: RSACaCertFile,
290+
keyCertMatch: true,
291+
},
292+
{
293+
desc: "local key and remote TLS Enpoint",
294+
keyFile: RSASampleCertKeyFile,
295+
caCertFile: RSACaCertFile,
296+
keyCertMatch: true,
297+
tlsEndpoint: "localhost:46401",
298+
srvCfg: demoHTTPServerConfig{
299+
serverAddr: "localhost:46401",
300+
serverName: "example.com",
301+
serverCertFile: RSASampleCertFile,
302+
serverKeyFile: RSASampleCertKeyFile,
303+
},
304+
},
305+
}
306+
307+
for _, tc := range noErrorsTests {
308+
tt := tc
309+
t.Run("No errors test - "+tt.desc, func(t *testing.T) {
310+
t.Parallel()
311+
312+
buffer := bytes.Buffer{}
313+
314+
cc, err := NewCertinfoConfig()
315+
require.NoError(t, err)
316+
317+
cc.SetPrivateKeyFromFile(tt.keyFile, "notSet", inputReader)
318+
cc.SetCertsFromFile(tt.certFile, inputReader)
319+
cc.SetCaPoolFromFile(tt.caCertFile, inputReader)
320+
321+
if tt.tlsEndpoint != emptyString {
322+
ts, errSrv := NewHTTPSTestServer(tt.srvCfg)
323+
require.NoError(t, errSrv)
324+
325+
defer ts.Close()
326+
327+
cc.SetTLSServerName(tt.srvCfg.serverName)
328+
cc.SetTLSEndpoint(tt.tlsEndpoint)
329+
}
330+
331+
errPrint := cc.PrintData(&buffer)
332+
require.NoError(t, errPrint)
333+
334+
got := buffer.String()
335+
for _, want := range []string{
336+
"Certinfo",
337+
"Certificate",
338+
"Subject",
339+
"Issuer",
340+
"NotBefore",
341+
"NotAfter",
342+
"Expiration",
343+
"IsCA",
344+
"AuthorityKeyId",
345+
"SubjectKeyId",
346+
"PublicKeyAlgorithm",
347+
"SignatureAlgorithm",
348+
"SerialNumber",
349+
"Fingerprint SHA-256",
350+
} {
351+
require.Contains(t, got, want)
352+
}
353+
354+
if tt.keyFile != emptyString {
355+
require.Contains(t, got, "PrivateKey file: "+tt.keyFile)
356+
}
357+
358+
if tt.certFile != emptyString {
359+
require.Contains(t, got, "Certificate bundle file: "+tt.certFile)
360+
}
361+
362+
if tt.caCertFile != emptyString {
363+
require.Contains(t, got, "CA Certificates file: "+tt.caCertFile)
364+
}
365+
366+
if tt.keyFile != emptyString && tt.keyCertMatch {
367+
require.Contains(t, got, "PrivateKey match: true")
368+
} else {
369+
require.Contains(t, got, "PrivateKey match: false")
370+
}
371+
372+
if tt.tlsEndpoint != emptyString {
373+
require.Contains(t, got, "TLSEndpoint Certificates")
374+
require.Contains(t, got, "Endpoint: "+tt.tlsEndpoint)
375+
require.Contains(t, got, "ServerName: "+tt.srvCfg.serverName)
376+
}
377+
})
378+
}
379+
}

internal/certinfo/certinfo_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,87 @@ func TestCertinfo_SetTLSServerName(t *testing.T) {
324324
})
325325
}
326326
}
327+
328+
func TestCertinfo_SetTLSEndpoint(t *testing.T) {
329+
tests := []struct {
330+
desc string
331+
endpoint string
332+
expectEnpoint string
333+
expectHost string
334+
expectPort string
335+
processErr bool
336+
expectMsg string
337+
}{
338+
{
339+
desc: "success",
340+
endpoint: "localhost:443",
341+
expectEnpoint: "localhost:443",
342+
expectHost: "localhost",
343+
expectPort: "443",
344+
},
345+
{
346+
desc: "success IPV6",
347+
endpoint: "[::1]:443",
348+
expectEnpoint: "[::1]:443",
349+
expectHost: "::1",
350+
expectPort: "443",
351+
},
352+
{
353+
desc: "success IPV4",
354+
endpoint: "127.0.0.1:443",
355+
expectEnpoint: "127.0.0.1:443",
356+
expectHost: "127.0.0.1",
357+
expectPort: "443",
358+
},
359+
{
360+
desc: "error malformed host",
361+
endpoint: "localh#$%ost:443",
362+
processErr: true,
363+
expectMsg: "unable to get endpoint certificates: TLS handshake failed: dial tcp: lookup localh#$%ost: no such host",
364+
},
365+
{
366+
desc: "error missing port",
367+
endpoint: "localhost",
368+
processErr: true,
369+
expectMsg: "invalid TLS endpoint \"\": address localhost: missing port in address",
370+
},
371+
{
372+
desc: "error missing host",
373+
endpoint: ":80443",
374+
processErr: true,
375+
expectMsg: "unable to get endpoint certificates: TLS handshake failed: dial tcp: address 80443: invalid port",
376+
},
377+
{
378+
desc: "error endpoint includes scheme",
379+
endpoint: "https://localhost:80443",
380+
processErr: true,
381+
expectMsg: "invalid TLS endpoint \"\": address https://localhost:80443: too many colons in address",
382+
},
383+
}
384+
385+
for _, tc := range tests {
386+
tt := tc
387+
t.Run(tt.desc, func(t *testing.T) {
388+
t.Parallel()
389+
390+
cc, errNew := NewCertinfoConfig()
391+
require.NoError(t, errNew)
392+
393+
err := cc.SetTLSEndpoint(tt.endpoint)
394+
395+
if !tt.processErr {
396+
// skip requiring NoError since SetTLSEndpoint will always return network errors
397+
// in this case. See tests related to GetRemoteCerts for more
398+
399+
// require.NoError(t, err)
400+
require.Equal(t, tt.expectEnpoint, cc.TLSEndpoint, "check TLSEndpoint")
401+
require.Equal(t, tt.expectHost, cc.TLSEndpointHost, "check TLSEndpointHost")
402+
require.Equal(t, tt.expectPort, cc.TLSEndpointPort, "check TLSEndpointPort")
403+
404+
return
405+
}
406+
407+
require.EqualError(t, err, tt.expectMsg)
408+
})
409+
}
410+
}

0 commit comments

Comments
 (0)