-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
return ErrUnexpectedEOF when Decode input is too short
The added test cases would fail before this fix: --- FAIL: TestDecodeError/Empty (0.00s) unmarshal_test.go:28: Decode("") got "EOF", want "unexpected EOF" --- FAIL: TestDecodeError/NoSpace (0.00s) unmarshal_test.go:28: Decode("foo") got "EOF", want "unexpected EOF" They would still return an error, but they'd just return io.EOF. While that error signals that we've hit the end of input, it doesn't signal to the codec user that something went wrong. An io.EOF is not a worrying error, in many situations. If the input is empty, or is too short to be a valid git object, we should return a clear error to signal the operation did not work. io.ErrUnexpectedEOF is unambiguous, and will make go-ipfs start erroring on this edge case. For ipfs/kubo#8378.
- Loading branch information
Showing
2 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ipldgit | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
basicnode "github.com/ipld/go-ipld-prime/node/basic" | ||
) | ||
|
||
func TestUnmarshalError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
want string | ||
}{ | ||
{"Empty", "", "unexpected EOF"}, | ||
{"Whitespace", " ", "unrecognized object type"}, | ||
{"NoSpace", "foo", "unexpected EOF"}, | ||
{"BadType", "foo ", "unrecognized object type"}, | ||
} | ||
for _, test := range tests { | ||
t.Run("Decode/"+test.name, func(t *testing.T) { | ||
nb := basicnode.Prototype.Any.NewBuilder() | ||
err := Decode(nb, strings.NewReader(test.input)) | ||
got := fmt.Sprint(err) | ||
if !strings.Contains(got, test.want) { | ||
t.Fatalf("Decode(%q) got %q, want %q", test.input, got, test.want) | ||
} | ||
}) | ||
t.Run("ParseObject/"+test.name, func(t *testing.T) { | ||
_, err := ParseObject(strings.NewReader(test.input)) | ||
got := fmt.Sprint(err) | ||
if !strings.Contains(got, test.want) { | ||
t.Fatalf("Decode(%q) got %q, want %q", test.input, got, test.want) | ||
} | ||
}) | ||
} | ||
|
||
} |