Skip to content

Commit

Permalink
Enhance individual UT in go-cose library to handle specific errors (#137
Browse files Browse the repository at this point in the history
)

Signed-off-by: Yogesh Deshpande <yogesh.deshpande@arm.com>
  • Loading branch information
yogeshbdeshpande authored Mar 10, 2023
1 parent 134c56b commit 3b32cdb
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 233 deletions.
17 changes: 10 additions & 7 deletions cbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_byteString_UnmarshalCBOR(t *testing.T) {
name string
data []byte
want byteString
wantErr bool
wantErr string
}{
{
name: "valid string",
Expand All @@ -43,33 +43,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
18 changes: 11 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,15 @@ 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) {
t.Errorf("I2OSP() error = %v, wantErr %v", err, tt.wantErr)
return
} else if err == nil && (tt.wantErr != "") {
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

0 comments on commit 3b32cdb

Please sign in to comment.