Debian Codegraph @

plugin/sign_test.go

raw symbols SCIP
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 func TestGenerateSignatureRequest_Validate(t *testing.T) {
22 reqs := []GenerateSignatureRequest{
23 getGenerateSignatureRequest(ContractVersion, "someKeyId", string(KeySpecEC384), string(HashAlgorithmSHA384), []byte("zop")),
24 {
25 ContractVersion: ContractVersion,
26 KeyID: "someKeyId",
27 KeySpec: KeySpecEC384,
28 Hash: HashAlgorithmSHA384,
29 Payload: []byte("somePayload"),
30 PluginConfig: map[string]string{"key1": "value1"},
31 },
32 }
33
34 for _, req := range reqs {
35 if err := req.Validate(); err != nil {
36 t.Errorf("VerifySignatureRequest#Validate failed with error: %+v", err)
37 }
38 }
39 }
40
41 func TestGenerateSignatureRequest_Validate_Error(t *testing.T) {
42 testCases := []struct {
43 name string
44 req GenerateSignatureRequest
45 }{
46 {name: "contractVersion", req: getGenerateSignatureRequest("", "someKeyId", string(KeySpecEC384), string(HashAlgorithmSHA384), []byte("zop"))},
47 {name: "keyId", req: getGenerateSignatureRequest(ContractVersion, "", string(KeySpecEC384), string(HashAlgorithmSHA384), []byte("zop"))},
48 {name: "keySpec", req: getGenerateSignatureRequest(ContractVersion, "someKeyId", "", string(HashAlgorithmSHA384), []byte("zop"))},
49 {name: "hashAlgorithm", req: getGenerateSignatureRequest(ContractVersion, "someKid", string(KeySpecEC384), "", []byte("zop"))},
50 {name: "payload", req: getGenerateSignatureRequest(ContractVersion, "someKeyId", string(KeySpecEC384), string(HashAlgorithmSHA384), []byte(""))},
51 {name: "payload", req: getGenerateSignatureRequest(ContractVersion, "someKeyId", string(KeySpecEC384), string(HashAlgorithmSHA384), nil)},
52 {name: "keySpec", req: getGenerateSignatureRequest(ContractVersion, "someKeyId", "", string(HashAlgorithmSHA384), []byte("zop"))},
53 }
54
55 for _, testcase := range testCases {
56 t.Run(testcase.name, func(t *testing.T) {
57 if err := testcase.req.Validate(); err != nil {
58 expMsg := fmt.Sprintf("{\"errorCode\":\"VALIDATION_ERROR\",\"errorMessage\":\"%s cannot be empty\"}", testcase.name)
59 if err.Error() != expMsg {
60 t.Errorf("expected error message '%s' but got '%s'", expMsg, err.Error())
61 }
62 } else {
63 t.Error("VerifySignatureRequest#Validate didn't returned error")
64 }
65 })
66 }
67 }
68
69 func TestGenerateSignatureRequest_Command(t *testing.T) {
70 req := getGenerateSignatureRequest(ContractVersion, "someKeyId", string(KeySpecEC384), string(HashAlgorithmSHA384), []byte("zop"))
71 if cmd := req.Command(); cmd != CommandGenerateSignature {
72 t.Errorf("DescribeKeyRequest#Command, expected %s but returned %s", CommandGenerateSignature, cmd)
73 }
74 }
75
76 func TestGenerateEnvelopeRequest_Validate(t *testing.T) {
77 reqs := []GenerateEnvelopeRequest{
78 getGenerateEnvelopeRequest(ContractVersion, "someKeyId", "someSET", "somePT", []byte("zop")),
79 {
80 ContractVersion: ContractVersion,
81 KeyID: "someKeyId",
82 SignatureEnvelopeType: "someType",
83 ExpiryDurationInSeconds: 1,
84 Payload: []byte("zap"),
85 PayloadType: ContractVersion,
86 PluginConfig: map[string]string{"key1": "value1"},
87 },
88 }
89
90 for _, req := range reqs {
91 if err := req.Validate(); err != nil {
92 t.Errorf("GenerateEnvelopeRequest#Validate failed with error: %+v", err)
93 }
94 }
95 }
96
97 func TestGenerateEnvelopeRequest_Validate_Error(t *testing.T) {
98 testCases := []struct {
99 name string
100 req GenerateEnvelopeRequest
101 }{
102 {name: "contractVersion", req: getGenerateEnvelopeRequest("", "someKeyId", "someSET", "somePT", []byte("zop"))},
103 {name: "keyId", req: getGenerateEnvelopeRequest(ContractVersion, "", "someSET", "somePT", []byte("zop"))},
104 {name: "signatureEnvelopeType", req: getGenerateEnvelopeRequest(ContractVersion, "someKeyId", "", "somePT", []byte("zop"))},
105 {name: "payloadType", req: getGenerateEnvelopeRequest(ContractVersion, "someKeyId", "someSET", "", []byte("zop"))},
106 {name: "payload", req: getGenerateEnvelopeRequest(ContractVersion, "someKeyId", "someSET", "somePT", []byte(""))},
107 {name: "payload", req: getGenerateEnvelopeRequest(ContractVersion, "someKeyId", "someSET", "somePT", nil)},
108 }
109
110 for _, testcase := range testCases {
111 t.Run(testcase.name, func(t *testing.T) {
112 if err := testcase.req.Validate(); err != nil {
113 expMsg := fmt.Sprintf("{\"errorCode\":\"VALIDATION_ERROR\",\"errorMessage\":\"%s cannot be empty\"}", testcase.name)
114 if err.Error() != expMsg {
115 t.Errorf("expected error message '%s' but got '%s'", expMsg, err.Error())
116 }
117 } else {
118 t.Error("GenerateEnvelopeRequest#Validate didn't returned error")
119 }
120 })
121 }
122 }
123
124 func TestGenerateEnvelopeRequest_Command(t *testing.T) {
125 req := getGenerateEnvelopeRequest(ContractVersion, "someKeyId", string(KeySpecEC384), string(HashAlgorithmSHA384), []byte("zop"))
126 if cmd := req.Command(); cmd != CommandGenerateEnvelope {
127 t.Errorf("DescribeKeyRequest#Command, expected %s but returned %s", CommandGenerateEnvelope, cmd)
128 }
129 }
130
131 func getGenerateSignatureRequest(cv, kid, ks, ha string, pl []byte) GenerateSignatureRequest {
132 return GenerateSignatureRequest{
133 ContractVersion: cv,
134 KeyID: kid,
135 KeySpec: KeySpec(ks),
136 Hash: HashAlgorithm(ha),
137 Payload: pl,
138 }
139 }
140
141 func getGenerateEnvelopeRequest(cv, kid, set, pt string, pl []byte) GenerateEnvelopeRequest {
142 return GenerateEnvelopeRequest{
143 ContractVersion: cv,
144 KeyID: kid,
145 SignatureEnvelopeType: set,
146 PayloadType: pt,
147 Payload: pl,
148 }
149 }