forked from notaryproject/notation-core-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Junjie Gao <[email protected]>
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |