@@ -17,18 +17,22 @@ import (
17
17
"context"
18
18
"errors"
19
19
"fmt"
20
+ "io"
20
21
"math"
21
22
"os"
22
23
"path/filepath"
24
+ "strings"
23
25
"testing"
24
26
"time"
25
27
26
28
"github.com/notaryproject/notation-core-go/signature"
27
29
"github.com/notaryproject/notation-core-go/signature/cose"
30
+ "github.com/notaryproject/notation-core-go/signature/jws"
28
31
"github.com/notaryproject/notation-go/internal/mock"
29
32
"github.com/notaryproject/notation-go/plugin"
30
33
"github.com/notaryproject/notation-go/registry"
31
34
"github.com/notaryproject/notation-go/verifier/trustpolicy"
35
+ "github.com/opencontainers/go-digest"
32
36
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
33
37
)
34
38
@@ -47,6 +51,7 @@ func TestSignSuccess(t *testing.T) {
47
51
for _ , tc := range testCases {
48
52
t .Run (tc .name , func (b * testing.T ) {
49
53
opts := SignOptions {}
54
+ opts .SignatureMediaType = jws .MediaTypeEnvelope
50
55
opts .ExpiryDuration = tc .dur
51
56
opts .ArtifactReference = mock .SampleArtifactUri
52
57
@@ -58,11 +63,91 @@ func TestSignSuccess(t *testing.T) {
58
63
}
59
64
}
60
65
66
+ func TestSignBlobSuccess (t * testing.T ) {
67
+ reader := strings .NewReader ("some content" )
68
+ testCases := []struct {
69
+ name string
70
+ dur time.Duration
71
+ mtype string
72
+ agent string
73
+ pConfig map [string ]string
74
+ metadata map [string ]string
75
+ }{
76
+ {"expiryInHours" , 24 * time .Hour , "video/mp4" , "" , nil , nil },
77
+ {"oneSecondExpiry" , 1 * time .Second , "video/mp4" , "" , nil , nil },
78
+ {"zeroExpiry" , 0 , "video/mp4" , "" , nil , nil },
79
+ {"validContentType" , 1 * time .Second , "video/mp4" , "" , nil , nil },
80
+ {"emptyContentType" , 1 * time .Second , "video/mp4" , "someDummyAgent" , map [string ]string {"hi" : "hello" }, map [string ]string {"bye" : "tata" }},
81
+ }
82
+ for _ , tc := range testCases {
83
+ t .Run (tc .name , func (b * testing.T ) {
84
+ opts := SignBlobOptions {
85
+ SignerSignOptions : SignerSignOptions {
86
+ SignatureMediaType : jws .MediaTypeEnvelope ,
87
+ ExpiryDuration : tc .dur ,
88
+ PluginConfig : tc .pConfig ,
89
+ SigningAgent : tc .agent ,
90
+ },
91
+ UserMetadata : expectedMetadata ,
92
+ ContentMediaType : tc .mtype ,
93
+ }
94
+
95
+ _ , _ , err := SignBlob (context .Background (), & dummySigner {}, reader , opts )
96
+ if err != nil {
97
+ b .Fatalf ("Sign failed with error: %v" , err )
98
+ }
99
+ })
100
+ }
101
+ }
102
+
103
+ func TestSignBlobError (t * testing.T ) {
104
+ reader := strings .NewReader ("some content" )
105
+ testCases := []struct {
106
+ name string
107
+ signer BlobSigner
108
+ dur time.Duration
109
+ rdr io.Reader
110
+ sigMType string
111
+ ctMType string
112
+ errMsg string
113
+ }{
114
+ {"negativeExpiry" , & dummySigner {}, - 1 * time .Second , nil , "video/mp4" , jws .MediaTypeEnvelope , "expiry duration cannot be a negative value" },
115
+ {"milliSecExpiry" , & dummySigner {}, 1 * time .Millisecond , nil , "video/mp4" , jws .MediaTypeEnvelope , "expiry duration supports minimum granularity of seconds" },
116
+ {"invalidContentMediaType" , & dummySigner {}, 1 * time .Second , reader , "video/mp4/zoping" , jws .MediaTypeEnvelope , "invalid content media-type 'video/mp4/zoping': mime: unexpected content after media subtype" },
117
+ {"emptyContentMediaType" , & dummySigner {}, 1 * time .Second , reader , "" , jws .MediaTypeEnvelope , "content media-type cannot be empty" },
118
+ {"invalidSignatureMediaType" , & dummySigner {}, 1 * time .Second , reader , "" , "" , "content media-type cannot be empty" },
119
+ {"nilReader" , & dummySigner {}, 1 * time .Second , nil , "video/mp4" , jws .MediaTypeEnvelope , "blobReader cannot be nil" },
120
+ {"nilSigner" , nil , 1 * time .Second , reader , "video/mp4" , jws .MediaTypeEnvelope , "signer cannot be nil" },
121
+ {"signerError" , & dummySigner {fail : true }, 1 * time .Second , reader , "video/mp4" , jws .MediaTypeEnvelope , "expected SignBlob failure" },
122
+ }
123
+ for _ , tc := range testCases {
124
+ t .Run (tc .name , func (t * testing.T ) {
125
+ opts := SignBlobOptions {
126
+ SignerSignOptions : SignerSignOptions {
127
+ SignatureMediaType : jws .MediaTypeEnvelope ,
128
+ ExpiryDuration : tc .dur ,
129
+ PluginConfig : nil ,
130
+ },
131
+ ContentMediaType : tc .sigMType ,
132
+ }
133
+
134
+ _ , _ , err := SignBlob (context .Background (), tc .signer , tc .rdr , opts )
135
+ if err == nil {
136
+ t .Fatalf ("expected error but didnt found" )
137
+ }
138
+ if err .Error () != tc .errMsg {
139
+ t .Fatalf ("expected err message to be '%s' but found '%s'" , tc .errMsg , err .Error ())
140
+ }
141
+ })
142
+ }
143
+ }
144
+
61
145
func TestSignSuccessWithUserMetadata (t * testing.T ) {
62
146
repo := mock .NewRepository ()
63
147
opts := SignOptions {}
64
148
opts .ArtifactReference = mock .SampleArtifactUri
65
149
opts .UserMetadata = expectedMetadata
150
+ opts .SignatureMediaType = jws .MediaTypeEnvelope
66
151
67
152
_ , err := Sign (context .Background (), & verifyMetadataSigner {}, repo , opts )
68
153
if err != nil {
@@ -182,6 +267,9 @@ func TestSignDigestNotMatchResolve(t *testing.T) {
182
267
repo := mock .NewRepository ()
183
268
repo .MissMatchDigest = true
184
269
signOpts := SignOptions {
270
+ SignerSignOptions : SignerSignOptions {
271
+ SignatureMediaType : jws .MediaTypeEnvelope ,
272
+ },
185
273
ArtifactReference : mock .SampleArtifactUri ,
186
274
}
187
275
@@ -320,7 +408,9 @@ func dummyPolicyStatement() (policyStatement trustpolicy.TrustPolicy) {
320
408
return
321
409
}
322
410
323
- type dummySigner struct {}
411
+ type dummySigner struct {
412
+ fail bool
413
+ }
324
414
325
415
func (s * dummySigner ) Sign (ctx context.Context , desc ocispec.Descriptor , opts SignerSignOptions ) ([]byte , * signature.SignerInfo , error ) {
326
416
return []byte ("ABC" ), & signature.SignerInfo {
@@ -330,6 +420,23 @@ func (s *dummySigner) Sign(ctx context.Context, desc ocispec.Descriptor, opts Si
330
420
}, nil
331
421
}
332
422
423
+ func (s * dummySigner ) SignBlob (_ context.Context , descGenFunc BlobDescriptorGenerator , _ SignerSignOptions ) ([]byte , * signature.SignerInfo , error ) {
424
+ if s .fail {
425
+ return nil , nil , errors .New ("expected SignBlob failure" )
426
+ }
427
+
428
+ _ , err := descGenFunc (digest .SHA384 )
429
+ if err != nil {
430
+ return nil , nil , err
431
+ }
432
+
433
+ return []byte ("ABC" ), & signature.SignerInfo {
434
+ SignedAttributes : signature.SignedAttributes {
435
+ SigningTime : time .Now (),
436
+ },
437
+ }, nil
438
+ }
439
+
333
440
type verifyMetadataSigner struct {}
334
441
335
442
func (s * verifyMetadataSigner ) Sign (ctx context.Context , desc ocispec.Descriptor , opts SignerSignOptions ) ([]byte , * signature.SignerInfo , error ) {
0 commit comments