Skip to content

Commit 1eed832

Browse files
committed
Add partial/incomplete CBOR Sequences test case.
Signed-off-by: Yan Qing <txr1883@gmail.com>
1 parent b73ccf5 commit 1eed832

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

diagnose.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,14 @@ func (dm *diagMode) diagnose(data []byte) (*diagnose, error) {
147147
}
148148

149149
di := &diagnose{
150-
dm: dm, d: de, w: &bytes.Buffer{},
151150
byteStringEncoding: dm.opts.ByteStringEncoding,
152151
byteStringHexWhitespace: dm.opts.ByteStringHexWhitespace,
153152
byteStringText: dm.opts.ByteStringText,
154153
byteStringEmbeddedCBOR: dm.opts.ByteStringEmbeddedCBOR,
155154
cborSequence: dm.opts.CBORSequence,
156155
indicateFloatPrecision: dm.opts.IndicateFloatPrecision,
156+
157+
dm: dm, d: de, w: &bytes.Buffer{},
157158
}
158159
return di, nil
159160
}
@@ -169,15 +170,15 @@ func Diagnose(data []byte) (string, error) {
169170
}
170171

171172
type diagnose struct {
172-
dm *diagMode
173-
d *decoder
174-
w *bytes.Buffer
175173
byteStringEncoding ByteStringEncoding
176174
byteStringHexWhitespace bool
177175
byteStringText bool
178176
byteStringEmbeddedCBOR bool
179177
cborSequence bool
180178
indicateFloatPrecision bool
179+
dm *diagMode
180+
d *decoder
181+
w *bytes.Buffer
181182
}
182183

183184
func (di *diagnose) diag() (string, error) {
@@ -460,11 +461,11 @@ func (di *diagnose) encodeByteString(val []byte) error {
460461

461462
if di.byteStringEmbeddedCBOR {
462463
if di2, err := di.dm.diagnose(val); err == nil {
463-
if data, err := di2.diag(); err == nil {
464+
if str, err := di2.diag(); err == nil {
464465
if err := di.writeString("<<"); err != nil {
465466
return err
466467
}
467-
if err := di.writeString(string(data)); err != nil {
468+
if err := di.writeString(str); err != nil {
468469
return err
469470
}
470471
return di.writeString(">>")

diagnose_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"testing"
1010
)
1111

12-
func TestDiagnoseExamples(t *testing.T) {
12+
func TestDiagnosticNotationExamples(t *testing.T) {
1313
// https://www.rfc-editor.org/rfc/rfc8949.html#name-examples-of-encoded-cbor-da
1414
testCases := []struct {
1515
cbor []byte
@@ -722,3 +722,61 @@ func TestDiagnoseFloatingPointNumber(t *testing.T) {
722722
})
723723
}
724724
}
725+
726+
func TestDiagnoseCBORSequences(t *testing.T) {
727+
testCases := []struct {
728+
title string
729+
cbor []byte
730+
diag string
731+
opts *DiagOptions
732+
returnError bool
733+
}{
734+
{
735+
"CBOR Sequences without CBORSequence option",
736+
hexDecode("f93e0064494554464401020304"),
737+
``,
738+
&DiagOptions{
739+
CBORSequence: false,
740+
},
741+
true,
742+
},
743+
{
744+
"CBOR Sequences with CBORSequence option",
745+
hexDecode("f93e0064494554464401020304"),
746+
`1.5, "IETF", h'01020304'`,
747+
&DiagOptions{
748+
CBORSequence: true,
749+
},
750+
false,
751+
},
752+
{
753+
"partial/incomplete CBOR Sequences",
754+
hexDecode("f93e00644945544644010203"),
755+
`1.5, "IETF"`,
756+
&DiagOptions{
757+
CBORSequence: true,
758+
},
759+
true,
760+
},
761+
}
762+
763+
for _, tc := range testCases {
764+
t.Run(tc.title, func(t *testing.T) {
765+
dm, err := tc.opts.DiagMode()
766+
if err != nil {
767+
t.Errorf("DiagMode() for 0x%x returned error %q", tc.cbor, err)
768+
}
769+
770+
str, err := dm.Diagnose(tc.cbor)
771+
if tc.returnError && err == nil {
772+
t.Errorf("Diagnose(0x%x) returned error %q", tc.cbor, err)
773+
} else if !tc.returnError && err != nil {
774+
t.Errorf("Diagnose(0x%x) returned error %q", tc.cbor, err)
775+
}
776+
777+
if str != tc.diag {
778+
t.Errorf("Diagnose(0x%x) returned `%s`, want %s", tc.cbor, str, tc.diag)
779+
}
780+
})
781+
}
782+
}

0 commit comments

Comments
 (0)