| 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 plugin |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "testing" |
| 19 | ) |
| 20 | |
| 21 | var mockCertChain = [][]byte{[]byte("zap"), []byte("zop")} |
| 22 | |
| 23 | func TestVerifySignatureRequest_Validate(t *testing.T) { |
| 24 | reqs := []VerifySignatureRequest{ |
| 25 | getVerifySignatureRequest(ContractVersion, "someCT", "someSigningScheme", mockCertChain, []Capability{CapabilitySignatureGenerator}), |
| 26 | { |
| 27 | ContractVersion: "2.0", |
| 28 | Signature: Signature{ |
| 29 | CriticalAttributes: CriticalAttributes{ |
| 30 | ContentType: "someCT", |
| 31 | SigningScheme: "someSigningScheme", |
| 32 | Expiry: nil, |
| 33 | AuthenticSigningTime: nil, |
| 34 | ExtendedAttributes: nil, |
| 35 | }, |
| 36 | UnprocessedAttributes: []string{"upa1", "upa2"}, |
| 37 | CertificateChain: mockCertChain, |
| 38 | }, |
| 39 | TrustPolicy: TrustPolicy{ |
| 40 | TrustedIdentities: []string{"trustedIdentity1", "trustedIdentity2"}, |
| 41 | SignatureVerification: []Capability{CapabilitySignatureGenerator, CapabilityRevocationCheckVerifier}, |
| 42 | }, |
| 43 | PluginConfig: map[string]string{"someKey": "someValue"}, |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | for _, req := range reqs { |
| 48 | if err := req.Validate(); err != nil { |
| 49 | t.Errorf("VerifySignatureRequest#Validate failed with error: %+v", err) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestVerifySignatureRequest_Validate_Error(t *testing.T) { |
| 55 | reqWithoutSignature := getVerifySignatureRequest("1.0", "someCT", "someSigningScheme", mockCertChain, []Capability{CapabilitySignatureGenerator}) |
| 56 | reqWithoutSignature.Signature = Signature{} |
| 57 | |
| 58 | reqWithoutCriticalAttr := getVerifySignatureRequest("1.0", "someCT", "someSigningScheme", mockCertChain, []Capability{CapabilitySignatureGenerator}) |
| 59 | reqWithoutCriticalAttr.Signature.CriticalAttributes = CriticalAttributes{} |
| 60 | |
| 61 | testCases := []struct { |
| 62 | name string |
| 63 | req VerifySignatureRequest |
| 64 | }{ |
| 65 | {name: "contractVersion", req: getVerifySignatureRequest("", "someCT", "someSigningScheme", mockCertChain, []Capability{CapabilitySignatureGenerator})}, |
| 66 | {name: "signature's criticalAttributes's contentType", req: getVerifySignatureRequest(ContractVersion, "", "someSigningScheme", mockCertChain, []Capability{CapabilitySignatureGenerator})}, |
| 67 | {name: "signature's criticalAttributes's signingScheme", req: getVerifySignatureRequest(ContractVersion, "someCT", "", mockCertChain, []Capability{CapabilitySignatureGenerator})}, |
| 68 | {name: "signature's criticalAttributes's certificateChain", req: getVerifySignatureRequest(ContractVersion, "someCT", "someSigningScheme", [][]byte{}, []Capability{CapabilitySignatureGenerator})}, |
| 69 | {name: "signature's criticalAttributes's certificateChain", req: getVerifySignatureRequest(ContractVersion, "someCT", "someSigningScheme", nil, []Capability{CapabilitySignatureGenerator})}, |
| 70 | {name: "signature's trustPolicy", req: getVerifySignatureRequest(ContractVersion, "someCT", "someSigningScheme", mockCertChain, nil)}, |
| 71 | {name: "signature's trustPolicy's signatureVerification", req: getVerifySignatureRequest(ContractVersion, "someCT", "someSigningScheme", mockCertChain, []Capability{})}, |
| 72 | {name: "signature", req: reqWithoutSignature}, |
| 73 | {name: "signature's criticalAttributes", req: reqWithoutCriticalAttr}, |
| 74 | } |
| 75 | |
| 76 | for _, testcase := range testCases { |
| 77 | t.Run(testcase.name, func(t *testing.T) { |
| 78 | if err := testcase.req.Validate(); err != nil { |
| 79 | expMsg := fmt.Sprintf("{\"errorCode\":\"VALIDATION_ERROR\",\"errorMessage\":\"%s cannot be empty\"}", testcase.name) |
| 80 | if err.Error() != expMsg { |
| 81 | t.Errorf("expected error message '%s' but got '%s'", expMsg, err.Error()) |
| 82 | } |
| 83 | } else { |
| 84 | t.Error("VerifySignatureRequest#Validate didn't returned error") |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestVerifySignatureRequest_Command(t *testing.T) { |
| 91 | req := getVerifySignatureRequest(ContractVersion, "someCT", "someSigningScheme", mockCertChain, []Capability{CapabilitySignatureGenerator}) |
| 92 | if cmd := req.Command(); cmd != CommandVerifySignature { |
| 93 | t.Errorf("DescribeKeyRequest#Command, expected %s but returned %s", CommandVerifySignature, cmd) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func getVerifySignatureRequest(cv, ct, ss string, cc [][]byte, sv []Capability) VerifySignatureRequest { |
| 98 | return VerifySignatureRequest{ |
| 99 | ContractVersion: cv, |
| 100 | Signature: Signature{ |
| 101 | CriticalAttributes: CriticalAttributes{ |
| 102 | ContentType: ct, |
| 103 | SigningScheme: ss, |
| 104 | }, |
| 105 | CertificateChain: cc, |
| 106 | }, |
| 107 | TrustPolicy: TrustPolicy{ |
| 108 | SignatureVerification: sv, |
| 109 | }, |
| 110 | } |
| 111 | } |