| 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 envelope |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"testing" |
| 19 |
"time" |
| 20 |
|
| 21 |
"github.com/notaryproject/notation-core-go/signature" |
| 22 |
"github.com/notaryproject/notation-core-go/signature/cose" |
| 23 |
"github.com/notaryproject/notation-core-go/signature/jws" |
| 24 |
gcose "github.com/veraison/go-cose" |
| 25 |
) |
| 26 |
|
| 27 |
var ( |
| 28 |
validCoseSignatureEnvelope []byte |
| 29 |
) |
| 30 |
|
| 31 |
func init() { |
| 32 |
msg := gcose.Sign1Message{ |
| 33 |
Headers: gcose.NewSign1Message().Headers, |
| 34 |
Payload: []byte("valid"), |
| 35 |
Signature: []byte("valid"), |
| 36 |
} |
| 37 |
validCoseSignatureEnvelope, _ = msg.MarshalCBOR() |
| 38 |
} |
| 39 |
|
| 40 |
const invalidMediaType = "invalid" |
| 41 |
|
| 42 |
func checkErrorEqual(expected, got error) bool { |
| 43 |
if expected == nil && got == nil { |
| 44 |
return true |
| 45 |
} |
| 46 |
if expected != nil && got != nil { |
| 47 |
return expected.Error() == got.Error() |
| 48 |
} |
| 49 |
return false |
| 50 |
} |
| 51 |
|
| 52 |
func TestValidateEnvelopeMediaType(t *testing.T) { |
| 53 |
tests := []struct { |
| 54 |
name string |
| 55 |
mediaType string |
| 56 |
expectedErr error |
| 57 |
}{ |
| 58 |
{ |
| 59 |
name: "jws signature media type", |
| 60 |
mediaType: jws.MediaTypeEnvelope, |
| 61 |
expectedErr: nil, |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "cose signature media type", |
| 65 |
mediaType: cose.MediaTypeEnvelope, |
| 66 |
expectedErr: nil, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "invalid media type", |
| 70 |
mediaType: invalidMediaType, |
| 71 |
expectedErr: errors.New("invalid envelope media type"), |
| 72 |
}, |
| 73 |
} |
| 74 |
for _, tt := range tests { |
| 75 |
t.Run(tt.name, func(t *testing.T) { |
| 76 |
if err := validateEnvelopeMediaType(tt.mediaType); !checkErrorEqual(tt.expectedErr, err) { |
| 77 |
t.Fatalf("expected validate envelope media type err: %v, got: %v", tt.expectedErr, err) |
| 78 |
} |
| 79 |
}) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestValidatePayloadContentType(t *testing.T) { |
| 84 |
payload := &signature.Payload{ |
| 85 |
ContentType: MediaTypePayloadV1, |
| 86 |
} |
| 87 |
err := ValidatePayloadContentType(payload) |
| 88 |
if !isErrEqual(nil, err) { |
| 89 |
t.Fatalf("ValidatePayloadContentType() expects error: %v, but got: %v.", nil, err) |
| 90 |
} |
| 91 |
|
| 92 |
payload = &signature.Payload{ |
| 93 |
ContentType: "invalid", |
| 94 |
} |
| 95 |
err = ValidatePayloadContentType(payload) |
| 96 |
expect := errors.New("payload content type \"invalid\" not supported") |
| 97 |
if !isErrEqual(expect, err) { |
| 98 |
t.Fatalf("ValidatePayloadContentType() expects error: %v, but got: %v.", expect, err) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestSigningTime(t *testing.T) { |
| 103 |
testTime, err := time.Parse(time.RFC3339, "2023-03-14T04:45:22Z") |
| 104 |
if err != nil { |
| 105 |
t.Fatal("failed to generate time") |
| 106 |
} |
| 107 |
signerInfo := signature.SignerInfo{ |
| 108 |
SignedAttributes: signature.SignedAttributes{ |
| 109 |
SigningTime: testTime, |
| 110 |
}, |
| 111 |
} |
| 112 |
signingTime, err := SigningTime(&signerInfo) |
| 113 |
if err != nil { |
| 114 |
t.Fatalf("failed to get signing time from signature: %v", err) |
| 115 |
} |
| 116 |
expectedSigningTime := "2023-03-14T04:45:22Z" |
| 117 |
if signingTime.Format(time.RFC3339) != expectedSigningTime { |
| 118 |
t.Fatalf("expected signing time: %q, got: %q", expectedSigningTime, signingTime.Format(time.RFC3339)) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
func isErrEqual(wanted, got error) bool { |
| 123 |
if wanted == nil && got == nil { |
| 124 |
return true |
| 125 |
} |
| 126 |
if wanted != nil && got != nil { |
| 127 |
return wanted.Error() == got.Error() |
| 128 |
} |
| 129 |
return false |
| 130 |
} |
| 131 |
|
| 132 |
// validateEnvelopeMediaType validetes envelope media type is supported by |
| 133 |
// notation-core-go. |
| 134 |
func validateEnvelopeMediaType(mediaType string) error { |
| 135 |
for _, types := range signature.RegisteredEnvelopeTypes() { |
| 136 |
if mediaType == types { |
| 137 |
return nil |
| 138 |
} |
| 139 |
} |
| 140 |
return errors.New("invalid envelope media type") |
| 141 |
} |