| 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 mock contains various mock structures required for testing. |
| 15 | package mock |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "fmt" |
| 20 | |
| 21 | "github.com/notaryproject/notation-plugin-framework-go/plugin" |
| 22 | ) |
| 23 | |
| 24 | // Mock plugin used only for testing. |
| 25 | type mockPlugin struct { |
| 26 | fail bool |
| 27 | envGenerator bool |
| 28 | } |
| 29 | |
| 30 | func NewPlugin(failPlugin bool) *mockPlugin { |
| 31 | return &mockPlugin{ |
| 32 | fail: failPlugin, |
| 33 | envGenerator: true, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func NewSigGeneratorPlugin(failPlugin bool) *mockPlugin { |
| 38 | return &mockPlugin{ |
| 39 | fail: failPlugin, |
| 40 | envGenerator: false, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func (p *mockPlugin) DescribeKey(ctx context.Context, req *plugin.DescribeKeyRequest) (*plugin.DescribeKeyResponse, error) { |
| 45 | if p.fail || p.envGenerator { |
| 46 | return nil, fmt.Errorf("DescribeKey() expected error") |
| 47 | } |
| 48 | return &plugin.DescribeKeyResponse{ |
| 49 | KeyID: "someKeyId", |
| 50 | KeySpec: plugin.KeySpecRSA2048, |
| 51 | }, nil |
| 52 | } |
| 53 | |
| 54 | func (p *mockPlugin) GenerateSignature(ctx context.Context, req *plugin.GenerateSignatureRequest) (*plugin.GenerateSignatureResponse, error) { |
| 55 | if p.fail || p.envGenerator { |
| 56 | return nil, fmt.Errorf("GenerateSignature() expected error") |
| 57 | } |
| 58 | return &plugin.GenerateSignatureResponse{ |
| 59 | KeyID: "someKeyId", |
| 60 | Signature: []byte("abcd"), |
| 61 | SigningAlgorithm: plugin.SignatureAlgorithmRSASSA_PSS_SHA256, |
| 62 | CertificateChain: [][]byte{[]byte("abcd"), []byte("wxyz")}, |
| 63 | }, nil |
| 64 | } |
| 65 | |
| 66 | func (p *mockPlugin) GenerateEnvelope(ctx context.Context, req *plugin.GenerateEnvelopeRequest) (*plugin.GenerateEnvelopeResponse, error) { |
| 67 | if p.fail || !p.envGenerator { |
| 68 | return nil, fmt.Errorf("GenerateEnvelope() expected error") |
| 69 | } |
| 70 | return &plugin.GenerateEnvelopeResponse{ |
| 71 | SignatureEnvelope: []byte(""), |
| 72 | SignatureEnvelopeType: req.SignatureEnvelopeType, |
| 73 | Annotations: map[string]string{"manifestAnntnKey1": "value1"}, |
| 74 | }, nil |
| 75 | } |
| 76 | |
| 77 | func (p *mockPlugin) VerifySignature(ctx context.Context, req *plugin.VerifySignatureRequest) (*plugin.VerifySignatureResponse, error) { |
| 78 | if p.fail { |
| 79 | return nil, fmt.Errorf("VerifySignature() expected error") |
| 80 | } |
| 81 | upAttrs := req.Signature.UnprocessedAttributes |
| 82 | pAttrs := make([]interface{}, len(upAttrs)) |
| 83 | for i := range upAttrs { |
| 84 | pAttrs[i] = upAttrs[i] |
| 85 | } |
| 86 | |
| 87 | return &plugin.VerifySignatureResponse{ |
| 88 | ProcessedAttributes: pAttrs, |
| 89 | VerificationResults: map[plugin.Capability]*plugin.VerificationResult{ |
| 90 | plugin.CapabilityTrustedIdentityVerifier: { |
| 91 | Success: true, |
| 92 | Reason: "Valid trusted Identity", |
| 93 | }, |
| 94 | plugin.CapabilityRevocationCheckVerifier: { |
| 95 | Success: true, |
| 96 | Reason: "Not revoked", |
| 97 | }, |
| 98 | }, |
| 99 | }, nil |
| 100 | } |
| 101 | |
| 102 | func (p *mockPlugin) GetMetadata(ctx context.Context, req *plugin.GetMetadataRequest) (*plugin.GetMetadataResponse, error) { |
| 103 | if p.fail { |
| 104 | return nil, fmt.Errorf("GetMetadata() expected error") |
| 105 | } |
| 106 | |
| 107 | cap := []plugin.Capability{ |
| 108 | plugin.CapabilityTrustedIdentityVerifier, |
| 109 | plugin.CapabilityRevocationCheckVerifier, |
| 110 | } |
| 111 | if p.envGenerator { |
| 112 | cap = append(cap, plugin.CapabilityEnvelopeGenerator) |
| 113 | } else { |
| 114 | cap = append(cap, plugin.CapabilitySignatureGenerator) |
| 115 | } |
| 116 | |
| 117 | return &plugin.GetMetadataResponse{ |
| 118 | Name: "Example Plugin", |
| 119 | Description: "This is an description of example plugin. 🍺", |
| 120 | URL: "https://example.com/notation/plugin", |
| 121 | Version: "1.0.0", |
| 122 | Capabilities: cap, |
| 123 | }, nil |
| 124 | } |