Skip to content

Commit da0f9a6

Browse files
authored
Improve Algorithm.String() message for unknown values (veraison#167)
Signed-off-by: qmuntal <[email protected]>
1 parent 4451940 commit da0f9a6

9 files changed

+30
-18
lines changed

algorithm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (a Algorithm) String() string {
103103
case AlgorithmReserved:
104104
return "Reserved"
105105
default:
106-
return "unknown algorithm value " + strconv.Itoa(int(a))
106+
return "Algorithm(" + strconv.Itoa(int(a)) + ")"
107107
}
108108
}
109109

algorithm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestAlgorithm_String(t *testing.T) {
2626
{AlgorithmES512, "ES512"},
2727
{AlgorithmEdDSA, "EdDSA"},
2828
{AlgorithmReserved, "Reserved"},
29-
{7, "unknown algorithm value 7"},
29+
{7, "Algorithm(7)"},
3030
}
3131
for _, tt := range tests {
3232
t.Run(tt.want, func(t *testing.T) {

headers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (h ProtectedHeader) Algorithm() (Algorithm, error) {
119119
case int64:
120120
return Algorithm(alg), nil
121121
case string:
122-
return AlgorithmReserved, fmt.Errorf("unknown algorithm value %q", alg)
122+
return AlgorithmReserved, fmt.Errorf("Algorithm(%q)", alg)
123123
default:
124124
return AlgorithmReserved, ErrInvalidAlgorithm
125125
}

headers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func TestProtectedHeader_Algorithm(t *testing.T) {
423423
h: ProtectedHeader{
424424
HeaderLabelAlgorithm: "foo",
425425
},
426-
wantErr: errors.New("unknown algorithm value \"foo\""),
426+
wantErr: errors.New("Algorithm(\"foo\")"),
427427
},
428428
{
429429
name: "invalid algorithm",

key_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ func TestNewKeyOKP(t *testing.T) {
861861
}, {
862862
name: "invalid alg", args: args{Algorithm(-100), x, d},
863863
want: nil,
864-
wantErr: `unsupported algorithm "unknown algorithm value -100"`,
864+
wantErr: `unsupported algorithm "Algorithm(-100)"`,
865865
}, {
866866
name: "x and d missing", args: args{AlgorithmEdDSA, nil, nil},
867867
want: nil,
@@ -940,7 +940,7 @@ func TestNewNewKeyEC2(t *testing.T) {
940940
}, {
941941
name: "invalid alg", args: args{Algorithm(-100), ec256x, ec256y, ec256d},
942942
want: nil,
943-
wantErr: `unsupported algorithm "unknown algorithm value -100"`,
943+
wantErr: `unsupported algorithm "Algorithm(-100)"`,
944944
}, {
945945
name: "x, y and d missing", args: args{AlgorithmES512, nil, nil, nil},
946946
want: nil,

signer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type DigestSigner interface {
5050
// Note: `*rsa.PrivateKey`, `*ecdsa.PrivateKey`, and `ed25519.PrivateKey`
5151
// implement `crypto.Signer`.
5252
func NewSigner(alg Algorithm, key crypto.Signer) (Signer, error) {
53+
var errReason string
5354
switch alg {
5455
case AlgorithmPS256, AlgorithmPS384, AlgorithmPS512:
5556
vk, ok := key.Public().(*rsa.PublicKey)
@@ -88,7 +89,12 @@ func NewSigner(alg Algorithm, key crypto.Signer) (Signer, error) {
8889
return &ed25519Signer{
8990
key: key,
9091
}, nil
92+
case AlgorithmReserved:
93+
errReason = "can't be implemented"
94+
case AlgorithmRS256, AlgorithmRS384, AlgorithmRS512:
95+
errReason = "no built-in implementation available"
9196
default:
92-
return nil, fmt.Errorf("can't create new Signer for %s: %w", alg, ErrAlgorithmNotSupported)
97+
errReason = "unknown algorithm"
9398
}
99+
return nil, fmt.Errorf("can't create new Signer for %s: %s: %w", alg, errReason, ErrAlgorithmNotSupported)
94100
}

signer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ func TestNewSigner(t *testing.T) {
113113
{
114114
name: "unsupported rsa signing algorithm",
115115
alg: AlgorithmRS256,
116-
wantErr: "can't create new Signer for RS256: algorithm not supported",
116+
wantErr: "can't create new Signer for RS256: no built-in implementation available: algorithm not supported",
117117
},
118118
{
119-
name: "unknown algorithm",
120-
alg: 0,
121-
wantErr: "can't create new Signer for Reserved: algorithm not supported",
119+
name: "reserved algorithm",
120+
alg: AlgorithmReserved,
121+
wantErr: "can't create new Signer for Reserved: can't be implemented: algorithm not supported",
122122
},
123123
{
124124
name: "unassigned algorithm",
125125
alg: -1,
126-
wantErr: "can't create new Signer for unknown algorithm value -1: algorithm not supported",
126+
wantErr: "can't create new Signer for Algorithm(-1): unknown algorithm: algorithm not supported",
127127
},
128128
}
129129
for _, tt := range tests {

verifier.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type DigestVerifier interface {
3939
//
4040
// The returned signer for rsa and ecdsa keys also implements `cose.DigestSigner`.
4141
func NewVerifier(alg Algorithm, key crypto.PublicKey) (Verifier, error) {
42+
var errReason string
4243
switch alg {
4344
case AlgorithmPS256, AlgorithmPS384, AlgorithmPS512:
4445
vk, ok := key.(*rsa.PublicKey)
@@ -74,7 +75,12 @@ func NewVerifier(alg Algorithm, key crypto.PublicKey) (Verifier, error) {
7475
return &ed25519Verifier{
7576
key: vk,
7677
}, nil
78+
case AlgorithmReserved:
79+
errReason = "can't be implemented"
80+
case AlgorithmRS256, AlgorithmRS384, AlgorithmRS512:
81+
errReason = "no built-in implementation available"
7782
default:
78-
return nil, fmt.Errorf("can't create new Verifier for %s: %w", alg, ErrAlgorithmNotSupported)
83+
errReason = "unknown algorithm"
7984
}
85+
return nil, fmt.Errorf("can't create new Verifier for %s: %s: %w", alg, errReason, ErrAlgorithmNotSupported)
8086
}

verifier_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ func TestNewVerifier(t *testing.T) {
109109
{
110110
name: "unsupported rsa signing algorithm",
111111
alg: AlgorithmRS256,
112-
wantErr: "can't create new Verifier for RS256: algorithm not supported",
112+
wantErr: "can't create new Verifier for RS256: no built-in implementation available: algorithm not supported",
113113
},
114114
{
115-
name: "unknown algorithm",
116-
alg: 0,
117-
wantErr: "can't create new Verifier for Reserved: algorithm not supported",
115+
name: "reserved algorithm",
116+
alg: AlgorithmReserved,
117+
wantErr: "can't create new Verifier for Reserved: can't be implemented: algorithm not supported",
118118
},
119119
{
120120
name: "unassigned algorithm",
121121
alg: -1,
122-
wantErr: "can't create new Verifier for unknown algorithm value -1: algorithm not supported",
122+
wantErr: "can't create new Verifier for Algorithm(-1): unknown algorithm: algorithm not supported",
123123
},
124124
{
125125
name: "bogus ecdsa public key (point not on curve)",

0 commit comments

Comments
 (0)