Skip to content

Commit

Permalink
test: add part of the unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Jan 8, 2024
1 parent a1f356a commit f1a72e8
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
63 changes: 63 additions & 0 deletions internal/crypto/cms/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 cms

import (
"errors"
"testing"
)

func TestSyntaxError(t *testing.T) {
tests := []struct {
name string
err SyntaxError
wantMsg string
}{
{"No detail", SyntaxError{Message: "test"}, "cms: syntax error: test"},
{"With detail", SyntaxError{Message: "test", Detail: errors.New("detail")}, "cms: syntax error: test: detail"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotMsg := tt.err.Error(); gotMsg != tt.wantMsg {
t.Errorf("SyntaxError.Error() = %v, want %v", gotMsg, tt.wantMsg)
}
if gotDetail := tt.err.Unwrap(); gotDetail != tt.err.Detail {
t.Errorf("SyntaxError.Unwrap() = %v, want %v", gotDetail, tt.err.Detail)
}
})
}
}

func TestVerificationError(t *testing.T) {
tests := []struct {
name string
err VerificationError
wantMsg string
}{
{"No detail", VerificationError{Message: "test"}, "cms: verification failure: test"},
{"With detail", VerificationError{Message: "test", Detail: errors.New("detail")}, "cms: verification failure: test: detail"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotMsg := tt.err.Error(); gotMsg != tt.wantMsg {
t.Errorf("VerificationError.Error() = %v, want %v", gotMsg, tt.wantMsg)
}
if gotDetail := tt.err.Unwrap(); gotDetail != tt.err.Detail {
t.Errorf("VerificationError.Unwrap() = %v, want %v", gotDetail, tt.err.Detail)
}
})
}
}
48 changes: 48 additions & 0 deletions internal/crypto/oid/algorithm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 oid

import (
"crypto/x509"
"encoding/asn1"
"testing"
)

func TestToSignatureAlgorithm(t *testing.T) {
tests := []struct {
name string
digestAlg asn1.ObjectIdentifier
sigAlg asn1.ObjectIdentifier
wantResult x509.SignatureAlgorithm
}{
{"SHA256WithRSA", SHA256, RSA, x509.SHA256WithRSA},
{"SHA384WithRSA", SHA384, RSA, x509.SHA384WithRSA},
{"SHA512WithRSA", SHA512, RSA, x509.SHA512WithRSA},
{"SHA256WithRSA direct", SHA256WithRSA, SHA256WithRSA, x509.SHA256WithRSA},
{"SHA384WithRSA direct", SHA384WithRSA, SHA384WithRSA, x509.SHA384WithRSA},
{"SHA512WithRSA direct", SHA512WithRSA, SHA512WithRSA, x509.SHA512WithRSA},
{"ECDSAWithSHA256", ECDSAWithSHA256, ECDSAWithSHA256, x509.ECDSAWithSHA256},
{"ECDSAWithSHA384", ECDSAWithSHA384, ECDSAWithSHA384, x509.ECDSAWithSHA384},
{"ECDSAWithSHA512", ECDSAWithSHA512, ECDSAWithSHA512, x509.ECDSAWithSHA512},
{"UnknownSignatureAlgorithm", asn1.ObjectIdentifier{1, 2, 3}, asn1.ObjectIdentifier{4, 5, 6}, x509.UnknownSignatureAlgorithm},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotResult := ToSignatureAlgorithm(tt.digestAlg, tt.sigAlg); gotResult != tt.wantResult {
t.Errorf("ToSignatureAlgorithm() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}
46 changes: 46 additions & 0 deletions internal/crypto/oid/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 oid

import (
"crypto"
"encoding/asn1"
"testing"
)

func TestToHash(t *testing.T) {
tests := []struct {
name string
alg asn1.ObjectIdentifier
wantHash crypto.Hash
wantExists bool
}{
{"SHA256", SHA256, crypto.SHA256, true},
{"SHA384", SHA384, crypto.SHA384, true},
{"SHA512", SHA512, crypto.SHA512, true},
{"Unknown", asn1.ObjectIdentifier{1, 2, 3}, crypto.Hash(0), false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHash, gotExists := ToHash(tt.alg)
if gotHash != tt.wantHash {
t.Errorf("ToHash() gotHash = %v, want %v", gotHash, tt.wantHash)
}
if gotExists != tt.wantExists {
t.Errorf("ToHash() gotExists = %v, want %v", gotExists, tt.wantExists)
}
})
}
}

0 comments on commit f1a72e8

Please sign in to comment.