Skip to content

Commit

Permalink
Merge pull request #405 from fxamacker/fxamacker/increase-test-coverage
Browse files Browse the repository at this point in the history
Add more tests for Diagnose and DiagnoseFirst
  • Loading branch information
fxamacker committed May 15, 2023
2 parents 32a24cd + 7caf128 commit cdd8c70
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![](https://github.com/fxamacker/images/raw/master/cbor/v2.5.0/fxamacker_cbor_banner.png)](#cbor-library-in-go)

[![](https://github.com/fxamacker/cbor/workflows/ci/badge.svg)](https://github.com/fxamacker/cbor/actions?query=workflow%3Aci)
[![](https://github.com/fxamacker/cbor/workflows/cover%20%E2%89%A598%25/badge.svg)](https://github.com/fxamacker/cbor/actions?query=workflow%3A%22cover+%E2%89%A598%25%22)
[![](https://github.com/fxamacker/cbor/workflows/cover%20%E2%89%A596%25/badge.svg)](https://github.com/fxamacker/cbor/actions?query=workflow%3A%22cover+%E2%89%A596%25%22)
[![](https://github.com/fxamacker/cbor/workflows/linters/badge.svg)](https://github.com/fxamacker/cbor/actions?query=workflow%3Alinters)
[![CodeQL](https://github.com/fxamacker/cbor/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/fxamacker/cbor/actions/workflows/codeql-analysis.yml)
[![](https://img.shields.io/badge/fuzzing-3%2B%20billion%20execs-44c010)](#fuzzing-and-code-coverage)
Expand Down
187 changes: 120 additions & 67 deletions diagnose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cbor
import (
"bytes"
"fmt"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -448,82 +449,51 @@ func TestDiagnoseByteString(t *testing.T) {
},
},
{
"without ByteStringEmbeddedCBOR and CBORSequence option",
"without ByteStringEmbeddedCBOR",
hexDecode("4101"),
`h'01'`,
&DiagOptions{
ByteStringEmbeddedCBOR: false,
CBORSequence: false,
},
},
{
"with ByteStringEmbeddedCBOR and CBORSequence option",
"with ByteStringEmbeddedCBOR",
hexDecode("4101"),
`<<1>>`,
&DiagOptions{
ByteStringEmbeddedCBOR: true,
CBORSequence: true,
},
},
{
"without ByteStringEmbeddedCBOR and CBORSequence option",
"multi CBOR items without ByteStringEmbeddedCBOR",
hexDecode("420102"),
`h'0102'`,
&DiagOptions{
ByteStringEmbeddedCBOR: false,
CBORSequence: false,
},
},
{
"with ByteStringEmbeddedCBOR and CBORSequence option",
"multi CBOR items with ByteStringEmbeddedCBOR",
hexDecode("420102"),
`<<1, 2>>`,
&DiagOptions{
ByteStringEmbeddedCBOR: true,
CBORSequence: true,
},
},
{
"with CBORSequence option",
hexDecode("0102"),
`1, 2`,
&DiagOptions{
CBORSequence: true,
},
},
{
"with ByteStringEmbeddedCBOR and CBORSequence option",
"multi CBOR items with ByteStringEmbeddedCBOR",
hexDecode("4563666F6FF6"),
`h'63666f6ff6'`,
&DiagOptions{
ByteStringEmbeddedCBOR: false,
CBORSequence: false,
},
},
{
"with ByteStringEmbeddedCBOR and CBORSequence option",
hexDecode("4563666F6FF6"),
`<<"foo", null>>`,
&DiagOptions{
ByteStringEmbeddedCBOR: true,
CBORSequence: true,
},
},
{
"with ByteStringEmbeddedCBOR and without CBORSequence option",
"multi CBOR items with ByteStringEmbeddedCBOR",
hexDecode("4563666F6FF6"),
`<<"foo", null>>`,
&DiagOptions{
ByteStringEmbeddedCBOR: true,
CBORSequence: false,
},
},
{
"with CBORSequence option",
hexDecode("63666F6FF6"),
`"foo", null`,
&DiagOptions{
CBORSequence: true,
},
},
{
Expand Down Expand Up @@ -561,36 +531,6 @@ func TestDiagnoseByteString(t *testing.T) {
}
})
}

t.Run("invalid encoding", func(t *testing.T) {
opts := &DiagOptions{
ByteStringEncoding: ByteStringBase64Encoding + 1,
}
_, err := opts.DiagMode()
if err == nil {
t.Errorf("DiagMode() with invalid ByteStringEncoding option didn't return error")
}
})

t.Run("without CBORSequence option", func(t *testing.T) {
cborData := hexDecode("63666F6FF6")
_, err := Diagnose(cborData)
if err == nil {
t.Errorf("Diagnose(0x%x) didn't return error", cborData)
} else if !strings.Contains(err.Error(), `extraneous data`) {
t.Errorf("Diagnose(0x%x) returned error %q", cborData, err)
}
})

t.Run("invalid indefinite length byte string", func(t *testing.T) {
cborData := hexDecode("5f4060ff")
_, err := Diagnose(cborData)
if err == nil {
t.Errorf("Diagnose(0x%x) didn't return error", cborData)
} else if !strings.Contains(err.Error(), `wrong element type`) {
t.Errorf("Diagnose(0x%x) returned error %q", cborData, err)
}
})
}

func TestDiagnoseTextString(t *testing.T) {
Expand All @@ -600,6 +540,24 @@ func TestDiagnoseTextString(t *testing.T) {
diag string
opts *DiagOptions
}{
{
"\t",
hexDecode("6109"),
`"\t"`,
&DiagOptions{},
},
{
"\r",
hexDecode("610d"),
`"\r"`,
&DiagOptions{},
},
{
"other ascii",
hexDecode("611b"),
`"\u001b"`,
&DiagOptions{},
},
{
"valid UTF-8 text in byte string",
hexDecode("4d68656c6c6f2c20e4bda0e5a5bd"),
Expand Down Expand Up @@ -922,6 +880,24 @@ func TestDiagnoseCBORSequences(t *testing.T) {
},
false,
},
{
"CBOR Sequences with CBORSequence option",
hexDecode("0102"),
`1, 2`,
&DiagOptions{
CBORSequence: true,
},
false,
},
{
"CBOR Sequences with CBORSequence option",
hexDecode("63666F6FF6"),
`"foo", null`,
&DiagOptions{
CBORSequence: true,
},
false,
},
{
"partial/incomplete CBOR Sequences",
hexDecode("f93e00644945544644010203"),
Expand Down Expand Up @@ -1026,3 +1002,80 @@ func TestDiagnoseTag(t *testing.T) {
})
}
}

func TestDiagnoseOptions(t *testing.T) {
opts := DiagOptions{
ByteStringEncoding: ByteStringBase32Encoding,
ByteStringHexWhitespace: true,
ByteStringText: false,
ByteStringEmbeddedCBOR: true,
CBORSequence: false,
FloatPrecisionIndicator: true,
MaxNestedLevels: 100,
MaxArrayElements: 101,
MaxMapPairs: 102,
}
dm, err := opts.DiagMode()
if err != nil {
t.Errorf("DiagMode() returned an error %v", err)
}
opts2 := dm.DiagOptions()
if !reflect.DeepEqual(opts, opts2) {
t.Errorf("DiagOptions() returned wrong options %v, want %v", opts2, opts)
}

opts = DiagOptions{
ByteStringEncoding: ByteStringBase64Encoding,
ByteStringHexWhitespace: false,
ByteStringText: true,
ByteStringEmbeddedCBOR: false,
CBORSequence: true,
FloatPrecisionIndicator: false,
MaxNestedLevels: 100,
MaxArrayElements: 101,
MaxMapPairs: 102,
}
dm, err = opts.DiagMode()
if err != nil {
t.Errorf("DiagMode() returned an error %v", err)
}
opts2 = dm.DiagOptions()
if !reflect.DeepEqual(opts, opts2) {
t.Errorf("DiagOptions() returned wrong options %v, want %v", opts2, opts)
}
}

func TestInvalidDiagnoseOptions(t *testing.T) {
opts := &DiagOptions{
ByteStringEncoding: ByteStringBase64Encoding + 1,
}
_, err := opts.DiagMode()
if err == nil {
t.Errorf("DiagMode() with invalid ByteStringEncoding option didn't return error")
}
}

func TestDiagnoseExtraneousData(t *testing.T) {
cborData := hexDecode("63666F6FF6")
_, err := Diagnose(cborData)
if err == nil {
t.Errorf("Diagnose(0x%x) didn't return error", cborData)
} else if !strings.Contains(err.Error(), `extraneous data`) {
t.Errorf("Diagnose(0x%x) returned error %q", cborData, err)
}

_, _, err = DiagnoseFirst(cborData)
if err != nil {
t.Errorf("DiagnoseFirst(0x%x) returned error %v", cborData, err)
}
}

func TestDiagnoseNotwellformedData(t *testing.T) {
cborData := hexDecode("5f4060ff")
_, err := Diagnose(cborData)
if err == nil {
t.Errorf("Diagnose(0x%x) didn't return error", cborData)
} else if !strings.Contains(err.Error(), `wrong element type`) {
t.Errorf("Diagnose(0x%x) returned error %q", cborData, err)
}
}

0 comments on commit cdd8c70

Please sign in to comment.