| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package truststore |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"path/filepath" |
| 21 |
"testing" |
| 22 |
|
| 23 |
corex509 "github.com/notaryproject/notation-core-go/x509" |
| 24 |
"github.com/notaryproject/notation-go/dir" |
| 25 |
) |
| 26 |
|
| 27 |
var trustStore = NewX509TrustStore(dir.NewSysFS(filepath.FromSlash("../testdata/"))) |
| 28 |
|
| 29 |
// TestLoadTrustStore tests a valid trust store |
| 30 |
func TestLoadValidTrustStore(t *testing.T) { |
| 31 |
_, err := trustStore.GetCertificates(context.Background(), "ca", "valid-trust-store") |
| 32 |
if err != nil { |
| 33 |
t.Fatalf("could not get certificates from trust store. %q", err) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// TestLoadValidTrustStoreWithSelfSignedSigningCertificate tests a valid trust store with self-signed signing certificate |
| 38 |
func TestLoadValidTrustStoreWithSelfSignedSigningCertificate(t *testing.T) { |
| 39 |
certs, err := trustStore.GetCertificates(context.Background(), "ca", "valid-trust-store-self-signed") |
| 40 |
if err != nil { |
| 41 |
t.Fatalf("could not get certificates from trust store. %q", err) |
| 42 |
} |
| 43 |
if len(certs) != 1 { |
| 44 |
t.Fatalf("unexpected number of certificates in the trust store, expected: %d, got: %d", 1, len(certs)) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestLoadTrustStoreWithInvalidCerts(t *testing.T) { |
| 49 |
// testing ../testdata/truststore/x509/ca/trust-store-with-invalid-certs/invalid |
| 50 |
expectedErr := fmt.Errorf("failed to read the trusted certificate %s in trust store %s of type %s", "invalid", "trust-store-with-invalid-certs", "ca") |
| 51 |
_, err := trustStore.GetCertificates(context.Background(), "ca", "trust-store-with-invalid-certs") |
| 52 |
if err == nil || err.Error() != expectedErr.Error() { |
| 53 |
t.Fatalf("invalid certs should return error: %q", expectedErr) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestLoadTrustStoreWithLeafCerts(t *testing.T) { |
| 58 |
// testing ../testdata/truststore/x509/ca/trust-store-with-leaf-certs/non-ca.crt |
| 59 |
expectedErrMsg := fmt.Sprintf("failed to validate the trusted certificate %s in trust store %s of type %s", "non-ca.crt", "trust-store-with-leaf-certs", "ca") |
| 60 |
_, err := trustStore.GetCertificates(context.Background(), "ca", "trust-store-with-leaf-certs") |
| 61 |
if err == nil || err.Error() != expectedErrMsg { |
| 62 |
t.Fatalf("leaf cert in a trust store should return error: %s, got: %v", expectedErrMsg, err) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestLoadTrustStoreWithLeafCertsInSingleFile(t *testing.T) { |
| 67 |
// testing ../testdata/truststore/x509/ca/trust-store-with-leaf-certs-in-single-file/RootAndLeafCerts.crt |
| 68 |
expectedErrMsg := fmt.Sprintf("failed to validate the trusted certificate %s in trust store %s of type %s", "RootAndLeafCerts.crt", "trust-store-with-leaf-certs-in-single-file", "ca") |
| 69 |
_, err := trustStore.GetCertificates(context.Background(), "ca", "trust-store-with-leaf-certs-in-single-file") |
| 70 |
if err == nil || err.Error() != expectedErrMsg { |
| 71 |
t.Fatalf("leaf cert in a trust store should return error: %s, got: %v", expectedErrMsg, err) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// TestValidCerts tests valid trust store cert |
| 76 |
func TestValidateCerts(t *testing.T) { |
| 77 |
joinedPath := filepath.FromSlash("../testdata/truststore/x509/ca/valid-trust-store/GlobalSign.der") |
| 78 |
certs, err := corex509.ReadCertificateFile(joinedPath) |
| 79 |
if err != nil { |
| 80 |
t.Fatalf("failed to read the trusted certificate %q: %q", joinedPath, err) |
| 81 |
} |
| 82 |
err = ValidateCertificates(certs) |
| 83 |
if err != nil { |
| 84 |
t.Fatalf("expected to get nil err, got %v", err) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// TestValidateCertsWithLeafCert tests invalid trust store leaf cert |
| 89 |
func TestValidateCertsWithLeafCert(t *testing.T) { |
| 90 |
failurePath := filepath.FromSlash("../testdata/truststore/x509/ca/trust-store-with-leaf-certs/non-ca.crt") |
| 91 |
certs, err := corex509.ReadCertificateFile(failurePath) |
| 92 |
if err != nil { |
| 93 |
t.Fatalf("failed to read the trusted certificate %q: %q", failurePath, err) |
| 94 |
} |
| 95 |
expectedErr := errors.New("certificate with subject \"CN=wabbit-networks.io,O=Notary,L=Seattle,ST=WA,C=US\" is not a CA certificate or self-signed signing certificate") |
| 96 |
err = ValidateCertificates(certs) |
| 97 |
if err == nil || err.Error() != expectedErr.Error() { |
| 98 |
t.Fatalf("leaf cert in a trust store should return error %q, got: %v", expectedErr, err) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestGetCertFromValidTsaTrustStore(t *testing.T) { |
| 103 |
// testing ../testdata/truststore/x509/tsa/test-nonCA/globalsignRoot.cer |
| 104 |
_, err := trustStore.GetCertificates(context.Background(), "tsa", "test-timestamp") |
| 105 |
if err != nil { |
| 106 |
t.Fatalf("expected nil error, but got %s", err) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func TestGetCertFromInvalidTsaTrustStore(t *testing.T) { |
| 111 |
t.Run("non CA certificate", func(t *testing.T) { |
| 112 |
// testing ../testdata/truststore/x509/tsa/test-nonCA/wabbit-networks.io |
| 113 |
expectedErrMsg := `trusted certificate wabbit-networks.io.crt in trust store test-nonCA of type tsa is invalid: certificate with subject "CN=wabbit-networks.io,O=Notary,L=Seattle,ST=WA,C=US" is not a root CA certificate: x509: invalid signature: parent certificate cannot sign this kind of certificate` |
| 114 |
_, err := trustStore.GetCertificates(context.Background(), "tsa", "test-nonCA") |
| 115 |
if err == nil || err.Error() != expectedErrMsg { |
| 116 |
t.Fatalf("expected error: %s, but got %s", expectedErrMsg, err) |
| 117 |
} |
| 118 |
}) |
| 119 |
|
| 120 |
t.Run("not self-issued", func(t *testing.T) { |
| 121 |
//testing ../testdata/truststore/x509/tsa/test-nonSelfIssued/nonSelfIssued.crt |
| 122 |
expectedErrMsg := `trusted certificate nonSelfIssued.crt in trust store test-nonSelfIssued of type tsa is invalid: certificate with subject "CN=Notation Test Revokable RSA Chain Cert 2,O=Notary,L=Seattle,ST=WA,C=US" is not a root CA certificate: issuer (CN=Notation Test Revokable RSA Chain Cert Root,O=Notary,L=Seattle,ST=WA,C=US) and subject (CN=Notation Test Revokable RSA Chain Cert 2,O=Notary,L=Seattle,ST=WA,C=US) are not the same` |
| 123 |
_, err := trustStore.GetCertificates(context.Background(), "tsa", "test-nonSelfIssued") |
| 124 |
if err == nil || err.Error() != expectedErrMsg { |
| 125 |
t.Fatalf("expected error: %s, but got %s", expectedErrMsg, err) |
| 126 |
} |
| 127 |
}) |
| 128 |
} |