|
1 | 1 | package certinfo |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "crypto/x509" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | "github.com/stretchr/testify/require" |
@@ -96,6 +98,30 @@ func TestCertinfo_GetRemoteCerts(t *testing.T) { |
96 | 98 | expectSrvHost: "localhost", |
97 | 99 | expectSrvPort: "46306", |
98 | 100 | }, |
| 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 | + }, |
99 | 125 | } |
100 | 126 |
|
101 | 127 | for _, tc := range tests { |
@@ -131,3 +157,223 @@ func TestCertinfo_GetRemoteCerts(t *testing.T) { |
131 | 157 | }) |
132 | 158 | } |
133 | 159 | } |
| 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 | +} |
0 commit comments