Skip to content

Commit

Permalink
Test ClientCertificateCredential errors (Azure#17552)
Browse files Browse the repository at this point in the history
  • Loading branch information
chlowell authored Apr 12, 2022
1 parent 6b8b2d9 commit ac68403
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
35 changes: 35 additions & 0 deletions sdk/azidentity/client_certificate_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ var allCertTests = []certTest{
newCertTest("pkcs12Encrypted", "testdata/certificate_encrypted_key.pfx", "password"),
}

func TestParseCertificates_Error(t *testing.T) {
for _, path := range []string{
"testdata/certificate_empty.pem", // malformed file (no cert block)
"testdata/certificate_encrypted_key.pfx", // requires a password we won't provide
"testdata/certificate_nokey.pem",
"testdata/certificate-two-keys.pem",
} {
t.Run(path, func(t *testing.T) {
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
_, _, err = ParseCertificates(data, nil)
if err == nil {
t.Fatal("expected an error")
}
})
}
}

func TestClientCertificateCredential_InvalidTenantID(t *testing.T) {
test := allCertTests[0]
cred, err := NewClientCertificateCredential(badTenantID, fakeClientID, test.certs, test.key, nil)
Expand Down Expand Up @@ -150,6 +170,21 @@ func TestClientCertificateCredential_NoPrivateKey(t *testing.T) {
}
}

func TestClientCertificateCredential_WrongKey(t *testing.T) {
data, err := os.ReadFile("testdata/certificate-wrong-key.pem")
if err != nil {
t.Fatal(err)
}
certs, key, err := ParseCertificates(data, nil)
if err != nil {
t.Fatal(err)
}
_, err = NewClientCertificateCredential("tenantID", "clientID", certs, key, nil)
if err == nil {
t.Fatal("expected an error")
}
}

func TestClientCertificateCredential_Live(t *testing.T) {
tests := []struct {
name string
Expand Down
79 changes: 79 additions & 0 deletions sdk/azidentity/testdata/certificate-two-keys.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAunkGHWyBYbIp6G97dwFeMhB/7c/y1SPlABi6cUJ6hp7gFeRm
Nwl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IZz5cAst+ybPtQj4
i7LFEWjxis+NLQMHhyE4fIGIkEjzU0uGDugifheIwKALqYEgMDrcoolwvGMdPxGo
Qps7tkad5vZV9d9+tTbI+DMB16Y51S04/u1dGFz3jSrDVF08PznJc99VB69OReiC
K17n8Xyox/VAaYsRFbOAJpLRWwcnotDpFQbgiLrmXxNOoiWPNbQsQzaQx7cR9okQ
v5RTpFAkrdjadhMsXFFiQh+axlaGD368ZGAj5ZoyOiXkV88tNCtyP/RDgW5ftQQ7
fdv05bNXhDfLgEgQvVSDfClDL1hKukLmLQS3ILfB4FlM/XmE+FW/qgo9aSx2XIbx
E4ie
-----END CERTIFICATE-----
48 changes: 48 additions & 0 deletions sdk/azidentity/testdata/certificate-wrong-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAunkGHWyBYbIp6G97dwFeMhB/7c/y1SPlABi6cUJ6hp7gFeRm
Nwl4gDvBmY8e8t6ANQxn3vv3HOp/QZmFl7Cr8aSjvD0JAT2CBbQ/O/Lgzb+5FaGR
vBFbBJ4AcXeHnzJ4ilsCrTJXtIWfo497uAHePQ7F3AtC9vLlf3kOoc7EIkdJ00Cf
+EKjTbU4UhgBUq+zqPMc8QTUyYXvgb8AxPCTJAktL9tiVpsthmK0SsOEZUiscL/U
Ga/N4EonCklD1AAgWHye0bl0kDhzjJSHAuKBrQ6zLIRs6+9OB6Pg4gcmH+Rup5H2
dSO09N/YBCiiJZTSlqockB3oym2t5z9et2SiNwIDAQABAoIBAQCKzivPG0X0AztO
2i19mHcVrVKNI44POnjsaXvfcyzhqMIFic7MiTA5xEGInRDcmOO2mVV4lvaLf8La
gfz/vXNAnN2E8aoSUkbHGDU52sGcZmrPv0VMSV8HQNXzoJZD2r3/v19urVq79fuv
NM9TWZCkwqpl8bwXNxe+m85YhCFboY9G543qmuXzKAQLoSupT0e4eIo2IGp7eJYK
5J/wtlEumUdhsKo1ajLojDgsgPKfr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