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

Add more tests for Diagnose and DiagnoseFirst #405

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
}
}