Skip to content

Commit

Permalink
According to fxamacker' review: 1. check tag data item for validity; …
Browse files Browse the repository at this point in the history
…2. update from valid() to wellformed().

Signed-off-by: Yan Qing <[email protected]>
  • Loading branch information
zensh committed May 1, 2023
1 parent a1c3064 commit 72e9118
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
14 changes: 11 additions & 3 deletions diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (di *diagnose) diag() (string, error) {
// CBOR Sequence
firstItem := true
for {
switch err := di.valid(); err {
switch err := di.wellformed(); err {
case nil:
if !firstItem {
if err = di.writeString(", "); err != nil {
Expand All @@ -204,9 +204,9 @@ func (di *diagnose) diag() (string, error) {
}
}

func (di *diagnose) valid() error {
func (di *diagnose) wellformed() error {
off := di.d.off
err := di.d.valid(di.dm.cborSequence)
err := di.d.wellformed(di.dm.cborSequence)
di.d.off = off
return err
}
Expand Down Expand Up @@ -372,11 +372,19 @@ func (di *diagnose) item() error { //nolint:gocyclo
_, _, tagNum := di.d.getHead()
switch tagNum {
case 2:
if di.d.nextCBORType() != cborTypeByteString {
return errors.New("cbor: tag number 2 must be followed by byte string, got " + t.String())
}

b := di.d.parseByteString()
bi := new(big.Int).SetBytes(b)
return di.writeString(bi.String())

case 3:
if di.d.nextCBORType() != cborTypeByteString {
return errors.New("cbor: tag number 3 must be followed by byte string, got " + t.String())
}

b := di.d.parseByteString()
bi := new(big.Int).SetBytes(b)
bi.Add(bi, big.NewInt(1))
Expand Down
73 changes: 73 additions & 0 deletions diagnose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,76 @@ func TestDiagnoseCBORSequences(t *testing.T) {
})
}
}

func TestDiagnoseTag(t *testing.T) {
testCases := []struct {
title string
cbor []byte
diag string
opts *DiagOptions
returnError bool
}{
{
"CBOR tag number 2 with not well-formed encoded CBOR data item",
hexDecode("c201"),
``,
&DiagOptions{},
true,
},
{
"CBOR tag number 3 with not well-formed encoded CBOR data item",
hexDecode("c301"),
``,
&DiagOptions{},
true,
},
{
"CBOR tag number 2 with well-formed encoded CBOR data item",
hexDecode("c240"),
`0`,
&DiagOptions{},
false,
},
{
"CBOR tag number 3 with well-formed encoded CBOR data item",
hexDecode("c340"),
`-1`, // -1 - n
&DiagOptions{},
false,
},
{
"CBOR tag number 2 with well-formed encoded CBOR data item",
hexDecode("c249010000000000000000"),
`18446744073709551616`,
&DiagOptions{},
false,
},
{
"CBOR tag number 3 with well-formed encoded CBOR data item",
hexDecode("c349010000000000000000"),
`-18446744073709551617`, // -1 - n
&DiagOptions{},
false,
},
}

for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
dm, err := tc.opts.DiagMode()
if err != nil {
t.Errorf("DiagMode() for 0x%x returned error %q", tc.cbor, err)
}

str, err := dm.Diagnose(tc.cbor)
if tc.returnError && err == nil {
t.Errorf("Diagnose(0x%x) returned error %q", tc.cbor, err)
} else if !tc.returnError && err != nil {
t.Errorf("Diagnose(0x%x) returned error %q", tc.cbor, err)
}

if str != tc.diag {
t.Errorf("Diagnose(0x%x) returned `%s`, want %s", tc.cbor, str, tc.diag)
}
})
}
}

0 comments on commit 72e9118

Please sign in to comment.