Skip to content

Commit

Permalink
fix: use protowire for Links bytes decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Mar 16, 2022
1 parent fa6d623 commit a17ace3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
48 changes: 32 additions & 16 deletions compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type testCase struct {
expectedForm string
encodeError string
decodeError string
testEncode bool
}

var testCases = []testCase{
Expand Down Expand Up @@ -64,6 +65,13 @@ var testCases = []testCase{
}`,
encodeError: "missing required fields: Links",
},
{
name: "Data some, short",
node: &pbNode{data: dataSome},
expectedBytes: "0a0500010203",
decodeError: "unexpected EOF",
encodeError: "missing required fields: Links",
},
{
name: "Links zero",
node: &pbNode{links: []pbLink{}},
Expand Down Expand Up @@ -113,6 +121,12 @@ var testCases = []testCase{
}`,
},
{
name: "Links Hash some, short",
node: &pbNode{links: []pbLink{{hash: acid}}},
expectedBytes: "120b0a090155000500010203",
decodeError: "unexpected EOF",
testEncode: false,
}, {
name: "Links Name zero",
node: &pbNode{links: []pbLink{{name: zeroName, hasName: true}}},
expectedBytes: "12021200",
Expand Down Expand Up @@ -211,24 +225,26 @@ func verifyRoundTrip(t *testing.T, tc testCase) {
node := buildNode(*tc.node)
actualBytes, err = nodeToString(t, node)

if tc.encodeError != "" {
if err != nil {
if !strings.Contains(err.Error(), tc.encodeError) {
t.Fatalf("got unexpeced encode error: [%v] (expected [%v])", err.Error(), tc.encodeError)
if tc.testEncode {
if tc.encodeError != "" {
if err != nil {
if !strings.Contains(err.Error(), tc.encodeError) {
t.Fatalf("got unexpeced encode error: [%v] (expected [%v])", err.Error(), tc.encodeError)
}
} else {
t.Fatalf("did not get expected encode error: %v", tc.encodeError)
}
} else {
t.Fatalf("did not get expected encode error: %v", tc.encodeError)
}
} else {
if err != nil {
t.Fatal(err)
} else {
if actualBytes != tc.expectedBytes {
t.Logf(
"Expected bytes: [%v]\nGot: [%v]\n",
tc.expectedBytes,
actualBytes)
t.Error("Did not match")
if err != nil {
t.Fatal(err)
} else {
if actualBytes != tc.expectedBytes {
t.Logf(
"Expected bytes: [%v]\nGot: [%v]\n",
tc.expectedBytes,
actualBytes)
t.Error("Did not match")
}
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func DecodeBytes(na ipld.NodeAssembler, src []byte) error {
haveData = true

case 2:
bytesLen, n := protowire.ConsumeVarint(remaining)
chunk, n := protowire.ConsumeBytes(remaining)
if n < 0 {
return protowire.ParseError(n)
}
Expand All @@ -123,10 +123,9 @@ func DecodeBytes(na ipld.NodeAssembler, src []byte) error {
if err != nil {
return err
}
if err := unmarshalLink(remaining[:bytesLen], curLink); err != nil {
if err := unmarshalLink(chunk, curLink); err != nil {
return err
}
remaining = remaining[bytesLen:]
if err := curLink.Finish(); err != nil {
return err
}
Expand Down

0 comments on commit a17ace3

Please sign in to comment.