Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance individual UT in go-cose library to handle specific errors #137

Merged
merged 2 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions cbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_byteString_UnmarshalCBOR(t *testing.T) {
name string
data []byte
want byteString
wantErr bool
wantErr string
}{
{
name: "valid string",
Expand All @@ -40,33 +40,36 @@ func Test_byteString_UnmarshalCBOR(t *testing.T) {
{
name: "undefined string",
data: []byte{0xf7},
wantErr: true,
wantErr: "cbor: require bstr type",
},
{
name: "nil CBOR data",
data: nil,
wantErr: true,
wantErr: "EOF",
},
{
name: "empty CBOR data",
data: []byte{},
wantErr: true,
wantErr: "EOF",
},
{
name: "tagged string",
data: []byte{0xc2, 0x40},
wantErr: true,
wantErr: "cbor: require bstr type",
},
{
name: "array of bytes", // issue #46
data: []byte{0x82, 0x00, 0x1},
wantErr: true,
wantErr: "cbor: require bstr type",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got byteString
if err := got.UnmarshalCBOR(tt.data); (err != nil) != tt.wantErr {
err := got.UnmarshalCBOR(tt.data)
if err != nil && (err.Error() != tt.wantErr) {
t.Errorf("byteString.UnmarshalCBOR() error = %v, wantErr %v", err, tt.wantErr)
} else if err == nil && (tt.wantErr != "") {
t.Errorf("byteString.UnmarshalCBOR() error = %v, wantErr %v", err, tt.wantErr)
}
if !bytes.Equal(got, tt.want) {
Expand Down
15 changes: 8 additions & 7 deletions ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ func TestI2OSP(t *testing.T) {
x *big.Int
buf []byte
want []byte
wantErr bool
wantErr string
}{
{
name: "negative int",
x: big.NewInt(-1),
buf: make([]byte, 2),
wantErr: true,
wantErr: "I2OSP: negative integer",
},
{
name: "integer too large #1",
x: big.NewInt(1),
buf: make([]byte, 0),
wantErr: true,
wantErr: "I2OSP: integer too large",
},
{
name: "integer too large #2",
x: big.NewInt(256),
buf: make([]byte, 0),
wantErr: true,
wantErr: "I2OSP: integer too large",
},
{
name: "integer too large #3",
x: big.NewInt(1 << 24),
buf: make([]byte, 3),
wantErr: true,
wantErr: "I2OSP: integer too large",
},
{
name: "zero length string",
Expand Down Expand Up @@ -98,11 +98,12 @@ func TestI2OSP(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := I2OSP(tt.x, tt.buf)
if (err != nil) != tt.wantErr {
if err != nil && (err.Error() != tt.wantErr) {
yogeshbdeshpande marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("I2OSP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got := tt.buf; !tt.wantErr && !reflect.DeepEqual(got, tt.want) {

if got := tt.buf; (tt.wantErr == "") && !reflect.DeepEqual(got, tt.want) {
t.Errorf("I2OSP() = %v, want %v", got, tt.want)
}
})
Expand Down
Loading