(t *testing.T)
| 38 | } |
| 39 | |
| 40 | func TestClassifyCertificate(t *testing.T) { |
| 41 | idpCert := mustParseCertificate(golden.Get(t, "idp_cert.pem")) |
| 42 | otherCert := mustParseCertificate(golden.Get(t, "cert_2017.pem")) |
| 43 | valid := idpCert.NotBefore.Add(time.Hour) |
| 44 | |
| 45 | signedWith := func(cert *x509.Certificate) *etree.Element { |
| 46 | el := etree.NewElement("Response") |
| 47 | x := el.CreateElement("Signature").CreateElement("KeyInfo").CreateElement("X509Data").CreateElement("X509Certificate") |
| 48 | if cert != nil { |
| 49 | x.SetText(base64.StdEncoding.EncodeToString(cert.Raw)) |
| 50 | } else { |
| 51 | x.SetText("!!!") |
| 52 | } |
| 53 | return el |
| 54 | } |
| 55 | noKeyInfo := etree.NewElement("Response") |
| 56 | noKeyInfo.CreateElement("Signature") |
| 57 | keyInfoWithoutCert := etree.NewElement("Response") |
| 58 | keyInfoWithoutCert.CreateElement("Signature").CreateElement("KeyInfo") |
| 59 | |
| 60 | tests := []struct { |
| 61 | name string |
| 62 | el *etree.Element |
| 63 | roots []*x509.Certificate |
| 64 | now time.Time |
| 65 | want error |
| 66 | }{ |
| 67 | {"trusted and valid", signedWith(idpCert), []*x509.Certificate{otherCert, idpCert}, valid, nil}, |
| 68 | {"untrusted", signedWith(otherCert), []*x509.Certificate{idpCert}, valid, ErrUntrustedCertificate}, |
| 69 | {"trusted but expired", signedWith(idpCert), []*x509.Certificate{idpCert}, idpCert.NotAfter.Add(time.Hour), ErrCertificateNotValidNow}, |
| 70 | {"trusted but not yet valid", signedWith(idpCert), []*x509.Certificate{idpCert}, idpCert.NotBefore.Add(-time.Hour), ErrCertificateNotValidNow}, |
| 71 | {"embedded certificate does not parse", signedWith(nil), []*x509.Certificate{idpCert}, valid, nil}, |
| 72 | {"no KeyInfo, one root", noKeyInfo, []*x509.Certificate{idpCert}, idpCert.NotAfter.Add(time.Hour), ErrCertificateNotValidNow}, |
| 73 | {"no KeyInfo, several roots", noKeyInfo, []*x509.Certificate{idpCert, otherCert}, valid, nil}, |
| 74 | {"KeyInfo without certificate", keyInfoWithoutCert, []*x509.Certificate{idpCert}, valid, nil}, |
| 75 | } |
| 76 | for _, tc := range tests { |
| 77 | t.Run(tc.name, func(t *testing.T) { |
| 78 | assert.Equal(t, tc.want, classifyCertificate(tc.el, tc.roots, tc.now)) |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // allErrorKinds is every error value the ServiceProvider attaches to a |
| 84 | // rejection, so tests can check that exactly the expected one matches. |
nothing calls this directly
no test coverage detected