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 20, 2024
1 parent 99707d2 commit 703fdcc
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 15 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%
7 changes: 5 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (c *CLI) Execute(ctx context.Context, args []string) {
func (c *CLI) printVersion(ctx context.Context) {
md := c.getMetadata(ctx, c.pl)

fmt.Printf("%s - %s\nVersion: %s \n", md.Name, md.Description, md.Version)
fmt.Printf("%s - %s\nVersion: %s\n", md.Name, md.Description, md.Version)
}

// validateArgs validate commands/arguments passed to executable.
Expand All @@ -142,7 +142,10 @@ 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)
if e, ok := err.(*plugin.Error); ok {
return plugin.NewValidationErrorf("%s: %s", plugin.ErrorMsgMalformedInput, e.Message)
}
return plugin.NewValidationErrorf("%s", plugin.ErrorMsgMalformedInput)
}

return nil
Expand Down
13 changes: 10 additions & 3 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,
op: "Example Plugin - This is an description of example plugin. 🍺\nVersion: 1.0.0 \n",
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
9 changes: 5 additions & 4 deletions example/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ func (p *ExamplePlugin) VerifySignature(ctx context.Context, req *plugin.VerifyS

func (p *ExamplePlugin) GetMetadata(ctx context.Context, req *plugin.GetMetadataRequest) (*plugin.GetMetadataResponse, error) {
return &plugin.GetMetadataResponse{
Name: "com.example.plugin",
Description: "This is an description of example plugin",
URL: "https://example.com/notation/plugin",
Version: "1.0.0",
SupportedContractVersions: []string{plugin.ContractVersion},
Name: "com.example.plugin",
Description: "This is an description of example plugin",
URL: "https://example.com/notation/plugin",
Version: "1.0.0",
Capabilities: []plugin.Capability{
plugin.CapabilityEnvelopeGenerator,
plugin.CapabilityTrustedIdentityVerifier,
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
8 changes: 4 additions & 4 deletions plugin/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func NewGenericError(msg string) *Error {
return NewError(ErrorCodeGeneric, msg)
}

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

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

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

func NewUnsupportedContractVersionError(version string) *Error {
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 703fdcc

Please sign in to comment.