-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(stream_decoder): io.EOF is not error when parsing (#7)
- Loading branch information
1 parent
52f8457
commit f1a2718
Showing
2 changed files
with
86 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,68 @@ | ||
package y3 | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStreamParser1(t *testing.T) { | ||
data := []byte{0x01, 0x03, 0x01, 0x02, 0x03} | ||
reader := &pr{buf: data} | ||
|
||
p, err := ReadPacket(reader) | ||
assert.NoError(t, err) | ||
assert.Equal(t, data, p) | ||
} | ||
|
||
func TestStreamParser2(t *testing.T) { | ||
data := []byte{0x01, 0x03, 0x01, 0x02, 0x03, 0x04} | ||
reader := &pr{buf: data} | ||
|
||
p, err := ReadPacket(reader) | ||
assert.NoError(t, err) | ||
assert.Equal(t, data[:5], p) | ||
} | ||
|
||
func TestStreamParser3(t *testing.T) { | ||
data := []byte{0x01, 0x03, 0x01, 0x02} | ||
reader := &pr{buf: data} | ||
|
||
p, err := ReadPacket(reader) | ||
assert.ErrorIs(t, err, ErrMalformed) | ||
assert.Equal(t, []byte(nil), p) | ||
} | ||
|
||
func TestStreamParser4(t *testing.T) { | ||
data := []byte{} | ||
reader := &pr{buf: data} | ||
|
||
p, err := ReadPacket(reader) | ||
assert.ErrorIs(t, err, ErrMalformed) | ||
assert.Equal(t, []byte(nil), p) | ||
} | ||
|
||
func TestStreamParser5(t *testing.T) { | ||
data := []byte{0x01} | ||
reader := &pr{buf: data} | ||
|
||
p, err := ReadPacket(reader) | ||
assert.ErrorIs(t, err, ErrMalformed) | ||
assert.Equal(t, []byte(nil), p) | ||
} | ||
|
||
type pr struct { | ||
buf []byte | ||
off int | ||
} | ||
|
||
func (pr *pr) Read(buf []byte) (int, error) { | ||
if pr.off >= len(pr.buf) { | ||
return 0, io.EOF | ||
} | ||
|
||
copy(buf, []byte{pr.buf[pr.off]}) | ||
pr.off++ | ||
return 1, nil | ||
} |