Skip to content

Commit

Permalink
codec: cbor test: support debugging and reuse testCborH
Browse files Browse the repository at this point in the history
  • Loading branch information
ugorji committed Nov 28, 2023
1 parent 77eda90 commit 190086c
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions codec/cbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,18 @@ func TestCborIndefiniteLength(t *testing.T) {
// "break" stop code is not a definite-length string item of the same major type, the string is not
// well-formed."
func TestCborIndefiniteLengthStringChunksCannotMixTypes(t *testing.T) {
defer testSetup(t, nil)()
var handle CborHandle
if !testRecoverPanicToErr {
t.Skip(testSkipIfNotRecoverPanicToErrMsg)
}
var h Handle = testCborH
defer testSetup(t, &h)()

for _, in := range [][]byte{
{cborBdIndefiniteString, 0x40, cborBdBreak}, // byte string chunk in indefinite length text string
{cborBdIndefiniteBytes, 0x60, cborBdBreak}, // text string chunk in indefinite length byte string
} {
var out string
err := NewDecoderBytes(in, &handle).Decode(&out)
err := NewDecoderBytes(in, h).Decode(&out)
if err == nil {
t.Errorf("expected error but decoded 0x%x to: %q", in, out)
}
Expand All @@ -116,12 +119,21 @@ func TestCborIndefiniteLengthStringChunksCannotMixTypes(t *testing.T) {
// Unicode code point (scalar value) cannot be spread between chunks: a new chunk of a text string
// can only be started at a code point boundary."
func TestCborIndefiniteLengthTextStringChunksAreUTF8(t *testing.T) {
defer testSetup(t, nil)()
var handle CborHandle
handle.ValidateUnicode = true
if !testRecoverPanicToErr {
t.Skip(testSkipIfNotRecoverPanicToErrMsg)
}
var h Handle = testCborH
defer testSetup(t, &h)()

bh := testBasicHandle(h)
defer func(oldValidateUnicode bool) {
bh.ValidateUnicode = oldValidateUnicode
}(bh.ValidateUnicode)
bh.ValidateUnicode = true

var out string
err := NewDecoderBytes([]byte{cborBdIndefiniteString, 0x61, 0xc2, 0x61, 0xa3, cborBdBreak}, &handle).Decode(&out)
in := []byte{cborBdIndefiniteString, 0x61, 0xc2, 0x61, 0xa3, cborBdBreak}
err := NewDecoderBytes(in, h).Decode(&out)
if err == nil {
t.Errorf("expected error but decoded to: %q", out)
}
Expand Down

0 comments on commit 190086c

Please sign in to comment.