Skip to content

Commit

Permalink
fixingUT
Browse files Browse the repository at this point in the history
Signed-off-by: Pritesh Bandi <[email protected]>
  • Loading branch information
priteshbandi committed Jan 19, 2024
1 parent 99707d2 commit 75ce3e0
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/.codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ coverage:
status:
project:
default:
target: 70%
target: 80%
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (c *CLI) unmarshalRequest(request plugin.Request) error {

if err := request.Validate(); err != nil {
c.logger.Errorf("%s validation error :%v", reflect.TypeOf(request), err)
return plugin.NewValidationError(plugin.ErrorMsgMalformedInput)
return plugin.NewValidationErrorf("%s:%s", plugin.ErrorMsgMalformedInput, err.Error())
}

return nil
Expand Down
11 changes: 9 additions & 2 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,36 +121,43 @@ func TestExecuteSuccess(t *testing.T) {
sigGenCli, _ := New(mock.NewSigGeneratorPlugin(false))
tests := map[string]struct {
c *CLI
in string
op string
}{
string(plugin.CommandGetMetadata): {
c: cli,
in: "{}",
op: "{\"name\":\"Example Plugin\",\"description\":\"This is an description of example plugin. 🍺\",\"version\":\"1.0.0\",\"url\":\"https://example.com/notation/plugin\",\"capabilities\":[\"SIGNATURE_VERIFIER.TRUSTED_IDENTITY\",\"SIGNATURE_VERIFIER.REVOCATION_CHECK\",\"SIGNATURE_GENERATOR.ENVELOPE\"]}",
},
string(plugin.Version): {
c: cli,
in: "",
op: "Example Plugin - This is an description of example plugin. 🍺\nVersion: 1.0.0 \n",
},
string(plugin.CommandGenerateEnvelope): {
c: cli,
op: "{\"signatureEnvelope\":\"\",\"signatureEnvelopeType\":\"\",\"annotations\":{\"manifestAnntnKey1\":\"value1\"}}",
in: "{\"contractVersion\":\"1.0\",\"keyId\":\"someKeyId\",\"payloadType\":\"somePT\",\"signatureEnvelopeType\":\"someSET\",\"payload\":\"em9w\"}",
op: "{\"signatureEnvelope\":\"\",\"signatureEnvelopeType\":\"someSET\",\"annotations\":{\"manifestAnntnKey1\":\"value1\"}}",
},
string(plugin.CommandVerifySignature): {
c: cli,
in: "{\"contractVersion\":\"1.0\",\"signature\":{\"criticalAttributes\":{\"contentType\":\"someCT\",\"signingScheme\":\"someSigningScheme\"},\"unprocessedAttributes\":null,\"certificateChain\":[\"emFw\",\"em9w\"]},\"trustPolicy\":{\"trustedIdentities\":null,\"signatureVerification\":[\"SIGNATURE_GENERATOR.RAW\"]}}",
op: "{\"verificationResults\":{\"SIGNATURE_VERIFIER.REVOCATION_CHECK\":{\"success\":true,\"reason\":\"Not revoked\"},\"SIGNATURE_VERIFIER.TRUSTED_IDENTITY\":{\"success\":true,\"reason\":\"Valid trusted Identity\"}},\"processedAttributes\":[]}",
},
string(plugin.CommandGenerateSignature): {
c: sigGenCli,
in: "{\"contractVersion\":\"1.0\",\"keyId\":\"someKeyId\",\"keySpec\":\"EC-384\",\"hashAlgorithm\":\"SHA-384\",\"payload\":\"em9w\"}",
op: "{\"keyId\":\"someKeyId\",\"signature\":\"YWJjZA==\",\"signingAlgorithm\":\"RSASSA-PSS-SHA-256\",\"certificateChain\":[\"YWJjZA==\",\"d3h5eg==\"]}",
},
string(plugin.CommandDescribeKey): {
c: sigGenCli,
in: "{\"contractVersion\":\"1.0\",\"keyId\":\"someKeyId\"}",
op: "{\"keyId\":\"someKeyId\",\"keySpec\":\"RSA-2048\"}",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
closer := setupReader("{}")
closer := setupReader(test.in)
defer closer()
op := captureStdOut(func() {
test.c.Execute(context.Background(), []string{"notation", name})
Expand Down
7 changes: 7 additions & 0 deletions plugin/describekey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func TestDescribeKeyRequest_Validate_Error(t *testing.T) {
}
}

func TestDescribeKeyRequest_Command(t *testing.T) {
req := getDescribeKeyRequest(ContractVersion, "someKeyId")
if cmd := req.Command(); cmd != CommandDescribeKey {
t.Errorf("DescribeKeyRequest#Command, expected %s but returned %s", CommandDescribeKey, cmd)
}
}

func getDescribeKeyRequest(cv, kid string) DescribeKeyRequest {
return DescribeKeyRequest{
ContractVersion: cv,
Expand Down
4 changes: 2 additions & 2 deletions plugin/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewGenericError(msg string) *Error {
return NewError(ErrorCodeGeneric, msg)
}

func NewGenericErrorf(format string, msg string) *Error {
func NewGenericErrorf(format string, msg ...string) *Error {
return NewError(ErrorCodeGeneric, fmt.Sprintf(format, msg))
}

Expand All @@ -65,7 +65,7 @@ func NewValidationError(msg string) *Error {
return NewError(ErrorCodeValidation, msg)
}

func NewValidationErrorf(format string, msg string) *Error {
func NewValidationErrorf(format string, msg ...string) *Error {
return NewError(ErrorCodeValidation, fmt.Sprintf(format, msg))
}

Expand Down
65 changes: 65 additions & 0 deletions plugin/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package plugin

import (
"testing"
)

func TestNewError(t *testing.T) {
msg := "someMSg"
err := NewError(ErrorCodeValidation, msg)
if err != nil {
if ErrorCodeValidation != err.ErrCode {
t.Errorf("NewError, expected errorCode '%s' but found'%s'", ErrorCodeValidation, err.ErrCode)
}

if msg != err.Message {
t.Errorf("NewError, expected message'%s' but found '%s'", msg, err.Message)
}

if err.Metadata != nil {
t.Errorf("NewError, expected metadata to be nil but found '%s'", err.Metadata)
}

expError := "{\"errorCode\":\"VALIDATION_ERROR\",\"errorMessage\":\"someMSg\"}"
if expError != err.Error() {
t.Errorf("NewError#Error, expected error to be '%s' but found '%s'", expError, err.Error())

}

} else {
t.Error("NewError didn't return an error")
}
}

func TestErrorCodes(t *testing.T) {
testCases := []struct {
err *Error
errCode ErrorCode
}{
{err: NewValidationError(""), errCode: ErrorCodeValidation},
{err: NewValidationErrorf("%s", ""), errCode: ErrorCodeValidation},
{err: NewUnsupportedError(""), errCode: ErrorCodeValidation},
{err: NewGenericError(""), errCode: ErrorCodeGeneric},
{err: NewGenericErrorf("%s", ""), errCode: ErrorCodeGeneric},
{err: NewJSONParsingError(""), errCode: ErrorCodeValidation},
{err: NewUnsupportedContractVersionError(""), errCode: ErrorCodeUnsupportedContractVersion},
}
for _, test := range testCases {
if test.errCode != test.err.ErrCode {
t.Errorf("Expected errorCode %s but found %s", test.errCode, test.err.ErrCode)
}
}
}
7 changes: 7 additions & 0 deletions plugin/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ func TestGetMetadataRequest_Validate(t *testing.T) {
}
}
}

func TestGetMetadataRequest_Command(t *testing.T) {
req := GetMetadataRequest{}
if cmd := req.Command(); cmd != CommandGetMetadata {
t.Errorf("DescribeKeyRequest#Command, expected %s but returned %s", CommandGetMetadata, cmd)
}
}
15 changes: 14 additions & 1 deletion plugin/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func TestGenerateSignatureRequest_Validate_Error(t *testing.T) {
}
}

func TestGenerateSignatureRequest_Command(t *testing.T) {
req := getGenerateSignatureRequest(ContractVersion, "someKeyId", string(KeySpecEC384), string(HashAlgorithmSHA384), []byte("zop"))
if cmd := req.Command(); cmd != CommandGenerateSignature {
t.Errorf("DescribeKeyRequest#Command, expected %s but returned %s", CommandGenerateSignature, cmd)
}
}

func TestGenerateEnvelopeRequest_Validate(t *testing.T) {
reqs := []GenerateEnvelopeRequest{
getGenerateEnvelopeRequest(ContractVersion, "someKeyId", "someSET", "somePT", []byte("zop")),
Expand Down Expand Up @@ -108,13 +115,19 @@ func TestGenerateEnvelopeRequest_Validate_Error(t *testing.T) {
t.Errorf("expected error message '%s' but got '%s'", expMsg, err.Error())
}
} else {
fmt.Println(testcase.req)
t.Error("GenerateEnvelopeRequest#Validate didn't returned error")
}
})
}
}

func TestGenerateEnvelopeRequest_Command(t *testing.T) {
req := getGenerateEnvelopeRequest(ContractVersion, "someKeyId", string(KeySpecEC384), string(HashAlgorithmSHA384), []byte("zop"))
if cmd := req.Command(); cmd != CommandGenerateEnvelope {
t.Errorf("DescribeKeyRequest#Command, expected %s but returned %s", CommandGenerateEnvelope, cmd)
}
}

func getGenerateSignatureRequest(cv, kid, ks, ha string, pl []byte) GenerateSignatureRequest {
return GenerateSignatureRequest{
ContractVersion: cv,
Expand Down
11 changes: 11 additions & 0 deletions plugin/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func TestVerifySignatureRequest_Validate_Error(t *testing.T) {
reqWithoutSignature := getVerifySignatureRequest("1.0", "someCT", "someSigningScheme", mockCertChain, []Capability{CapabilitySignatureGenerator})
reqWithoutSignature.Signature = Signature{}

reqWithoutCriticalAttr := getVerifySignatureRequest("1.0", "someCT", "someSigningScheme", mockCertChain, []Capability{CapabilitySignatureGenerator})
reqWithoutCriticalAttr.Signature.CriticalAttributes = CriticalAttributes{}

testCases := []struct {
name string
req VerifySignatureRequest
Expand All @@ -67,6 +70,7 @@ func TestVerifySignatureRequest_Validate_Error(t *testing.T) {
{name: "signature's trustPolicy", req: getVerifySignatureRequest(ContractVersion, "someCT", "someSigningScheme", mockCertChain, nil)},
{name: "signature's trustPolicy's signatureVerification", req: getVerifySignatureRequest(ContractVersion, "someCT", "someSigningScheme", mockCertChain, []Capability{})},
{name: "signature", req: reqWithoutSignature},
{name: "signature's criticalAttributes", req: reqWithoutCriticalAttr},
}

for _, testcase := range testCases {
Expand All @@ -83,6 +87,13 @@ func TestVerifySignatureRequest_Validate_Error(t *testing.T) {
}
}

func TestVerifySignatureRequest_Command(t *testing.T) {
req := getVerifySignatureRequest(ContractVersion, "someCT", "someSigningScheme", mockCertChain, []Capability{CapabilitySignatureGenerator})
if cmd := req.Command(); cmd != CommandVerifySignature {
t.Errorf("DescribeKeyRequest#Command, expected %s but returned %s", CommandVerifySignature, cmd)
}
}

func getVerifySignatureRequest(cv, ct, ss string, cc [][]byte, sv []Capability) VerifySignatureRequest {
return VerifySignatureRequest{
ContractVersion: cv,
Expand Down

0 comments on commit 75ce3e0

Please sign in to comment.