Debian Codegraph @

verifier/helpers_test.go

raw
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 verifier
15
16 import (
17 "context"
18 "errors"
19 "fmt"
20 "strconv"
21 "strings"
22 "testing"
23
24 "github.com/notaryproject/notation-core-go/signature"
25 "github.com/notaryproject/notation-go"
26 "github.com/notaryproject/notation-go/dir"
27 "github.com/notaryproject/notation-go/verifier/trustpolicy"
28 "github.com/notaryproject/notation-go/verifier/truststore"
29 )
30
31 func TestGetArtifactDigestFromUri(t *testing.T) {
32
33 tests := []struct {
34 artifactReference string
35 digest string
36 wantErr bool
37 }{
38 {"domain.com/repository@sha256:digest", "sha256:digest", false},
39 {"domain.com:80/repository:digest", "", true},
40 {"domain.com/repository", "", true},
41 {"domain.com/repository@sha256", "", true},
42 {"domain.com/repository@sha256:", "", true},
43 {"", "", true},
44 {"domain.com", "", true},
45 }
46 for i, tt := range tests {
47 t.Run(strconv.Itoa(i), func(t *testing.T) {
48 digest, err := getArtifactDigestFromReference(tt.artifactReference)
49
50 if tt.wantErr != (err != nil) {
51 t.Fatalf("TestGetArtifactDigestFromUri Error: %q WantErr: %v Input: %q", err, tt.wantErr, tt.artifactReference)
52 } else if digest != tt.digest {
53 t.Fatalf("TestGetArtifactDigestFromUri Want: %q Got: %v", tt.digest, digest)
54 }
55 })
56 }
57 }
58
59 func TestLoadX509TrustStore(t *testing.T) {
60 // load "ca" and "signingAuthority" trust store
61 caStore := "ca:valid-trust-store"
62 signingAuthorityStore := "signingAuthority:valid-trust-store"
63 dummyPolicy := dummyPolicyDocument().TrustPolicies[0]
64 dummyPolicy.TrustStores = []string{caStore, signingAuthorityStore}
65 dir.UserConfigDir = "testdata"
66 x509truststore := truststore.NewX509TrustStore(dir.ConfigFS())
67 _, err := loadX509TrustStores(context.Background(), signature.SigningSchemeX509, dummyPolicy.Name, dummyPolicy.TrustStores, x509truststore)
68 if err != nil {
69 t.Fatalf("TestLoadX509TrustStore should not throw error for a valid trust store. Error: %v", err)
70 }
71 _, err = loadX509TrustStores(context.Background(), signature.SigningSchemeX509SigningAuthority, dummyPolicy.Name, dummyPolicy.TrustStores, x509truststore)
72 if err != nil {
73 t.Fatalf("TestLoadX509TrustStore should not throw error for a valid trust store. Error: %v", err)
74 }
75 }
76
77 func TestIsCriticalFailure(t *testing.T) {
78 var dummyError = errors.New("critical failure")
79 tests := []struct {
80 result notation.ValidationResult
81 criticalFailure bool
82 }{
83 {notation.ValidationResult{Action: trustpolicy.ActionEnforce, Error: dummyError}, true},
84 {notation.ValidationResult{Action: trustpolicy.ActionLog, Error: dummyError}, false},
85 {notation.ValidationResult{Action: trustpolicy.ActionSkip, Error: dummyError}, false},
86 }
87 for i, tt := range tests {
88 t.Run(strconv.Itoa(i), func(t *testing.T) {
89 endResult := isCriticalFailure(&tt.result)
90
91 if endResult != tt.criticalFailure {
92 t.Fatalf("TestIsCriticalFailure Expected: %v Got: %v", tt.criticalFailure, endResult)
93 }
94 })
95 }
96 }
97
98 func TestLoadX509TSATrustStores(t *testing.T) {
99 policyDoc := trustpolicy.Document{
100 Version: "1.0",
101 TrustPolicies: []trustpolicy.TrustPolicy{
102 {
103 Name: "testTSA",
104 RegistryScopes: []string{"*"},
105 SignatureVerification: trustpolicy.SignatureVerification{VerificationLevel: "strict"},
106 TrustStores: []string{"tsa:test-timestamp"},
107 TrustedIdentities: []string{"*"},
108 },
109 },
110 }
111 dir.UserConfigDir = "testdata"
112 x509truststore := truststore.NewX509TrustStore(dir.ConfigFS())
113 policyStatement := policyDoc.TrustPolicies[0]
114 _, err := loadX509TSATrustStores(context.Background(), signature.SigningSchemeX509, policyStatement.Name, policyStatement.TrustStores, x509truststore)
115 if err != nil {
116 t.Fatalf("TestLoadX509TrustStore should not throw error for a valid trust store. Error: %v", err)
117 }
118
119 _, err = loadX509TSATrustStores(context.Background(), signature.SigningSchemeX509SigningAuthority, policyStatement.Name, policyStatement.TrustStores, x509truststore)
120 expectedErrMsg := "error while loading the TSA trust store, signing scheme must be notary.x509, but got notary.x509.signingAuthority"
121 if err == nil || err.Error() != expectedErrMsg {
122 t.Fatalf("expected %s, but got %s", expectedErrMsg, err)
123 }
124 }
125
126 func getArtifactDigestFromReference(artifactReference string) (string, error) {
127 invalidUriErr := fmt.Errorf("artifact URI %q could not be parsed, make sure it is the fully qualified OCI artifact URI without the scheme/protocol. e.g domain.com:80/my/repository@sha256:digest", artifactReference)
128 i := strings.LastIndex(artifactReference, "@")
129 if i < 0 || i+1 == len(artifactReference) {
130 return "", invalidUriErr
131 }
132
133 j := strings.LastIndex(artifactReference[i+1:], ":")
134 if j < 0 || j+1 == len(artifactReference[i+1:]) {
135 return "", invalidUriErr
136 }
137
138 return artifactReference[i+1:], nil
139 }
140
141 func dummyPolicyDocument() (policyDoc trustpolicy.Document) {
142 return trustpolicy.Document{
143 Version: "1.0",
144 TrustPolicies: []trustpolicy.TrustPolicy{
145 {
146 Name: "test-statement-name",
147 RegistryScopes: []string{"registry.acme-rockets.io/software/net-monitor"},
148 SignatureVerification: trustpolicy.SignatureVerification{VerificationLevel: "strict"},
149 TrustStores: []string{"ca:valid-trust-store", "signingAuthority:valid-trust-store"},
150 TrustedIdentities: []string{"x509.subject:CN=Notation Test Root,O=Notary,L=Seattle,ST=WA,C=US"},
151 },
152 },
153 }
154 }
155
156 func dummyInvalidPolicyDocument() (policyDoc trustpolicy.Document) {
157 return trustpolicy.Document{
158 TrustPolicies: []trustpolicy.TrustPolicy{
159 {
160 Name: "invalid",
161 },
162 },
163 }
164 }