2019-05-28 14:31:35 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-08-18 06:55:42 +00:00
|
|
|
"errors"
|
2019-05-28 14:31:35 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-06-25 23:51:54 +00:00
|
|
|
"reflect"
|
2019-05-28 14:31:35 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadLinesBlockFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
r := bytes.NewBufferString(s)
|
|
|
|
if _, _, err := ReadLinesBlock(r, nil, nil); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
|
|
|
sbr := &singleByteReader{
|
|
|
|
b: []byte(s),
|
|
|
|
}
|
|
|
|
if _, _, err := ReadLinesBlock(sbr, nil, nil); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
|
|
|
fr := &failureReader{}
|
|
|
|
if _, _, err := ReadLinesBlock(fr, nil, nil); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
2023-08-18 06:55:42 +00:00
|
|
|
|
|
|
|
un := &unexpectedEOF{}
|
|
|
|
if _, _, err := ReadLinesBlock(un, nil, nil); err != nil {
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatalf("get unexpected error, expecting io.EOF")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ef := eofErr{}
|
|
|
|
if _, _, err := ReadLinesBlock(ef, nil, nil); err != nil {
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
t.Fatalf("get unexpected error, expecting io.EOF")
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 14:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// empty string
|
|
|
|
f("")
|
|
|
|
|
|
|
|
// too long string
|
|
|
|
b := make([]byte, maxLineSize+1)
|
|
|
|
f(string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
type failureReader struct{}
|
|
|
|
|
2023-09-01 07:34:16 +00:00
|
|
|
func (fr *failureReader) Read(_ []byte) (int, error) {
|
2019-05-28 14:31:35 +00:00
|
|
|
return 0, fmt.Errorf("some error")
|
|
|
|
}
|
|
|
|
|
2023-08-18 06:55:42 +00:00
|
|
|
type unexpectedEOF struct{}
|
|
|
|
|
2023-09-01 07:34:16 +00:00
|
|
|
func (un unexpectedEOF) Read(_ []byte) (int, error) {
|
2023-08-18 06:55:42 +00:00
|
|
|
return 0, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
|
|
|
type eofErr struct{}
|
|
|
|
|
2023-09-01 07:34:16 +00:00
|
|
|
func (eo eofErr) Read(_ []byte) (int, error) {
|
2023-08-18 06:55:42 +00:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
2019-06-25 23:51:54 +00:00
|
|
|
func TestReadLinesBlockMultiLinesSingleByteReader(t *testing.T) {
|
|
|
|
f := func(s string, linesExpected []string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
r := &singleByteReader{
|
|
|
|
b: []byte(s),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
var dstBuf, tailBuf []byte
|
|
|
|
var lines []string
|
|
|
|
for {
|
|
|
|
dstBuf, tailBuf, err = ReadLinesBlock(r, dstBuf, tailBuf)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Fatalf("unexpected error in ReadLinesBlock(%q): %s", s, err)
|
|
|
|
}
|
|
|
|
lines = append(lines, string(dstBuf))
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(lines, linesExpected) {
|
|
|
|
t.Fatalf("unexpected lines after reading %q: got %q; want %q", s, lines, linesExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f("", nil)
|
|
|
|
f("foo", []string{"foo"})
|
|
|
|
f("foo\n", []string{"foo"})
|
|
|
|
f("foo\nbar", []string{"foo", "bar"})
|
|
|
|
f("\nfoo\nbar", []string{"", "foo", "bar"})
|
|
|
|
f("\nfoo\nbar\n", []string{"", "foo", "bar"})
|
|
|
|
f("\nfoo\nbar\n\n", []string{"", "foo", "bar", ""})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadLinesBlockMultiLinesBytesBuffer(t *testing.T) {
|
|
|
|
f := func(s string, linesExpected []string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
r := bytes.NewBufferString(s)
|
|
|
|
var err error
|
|
|
|
var dstBuf, tailBuf []byte
|
|
|
|
var lines []string
|
|
|
|
for {
|
|
|
|
dstBuf, tailBuf, err = ReadLinesBlock(r, dstBuf, tailBuf)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Fatalf("unexpected error in ReadLinesBlock(%q): %s", s, err)
|
|
|
|
}
|
|
|
|
lines = append(lines, string(dstBuf))
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(lines, linesExpected) {
|
|
|
|
t.Fatalf("unexpected lines after reading %q: got %q; want %q", s, lines, linesExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f("", nil)
|
|
|
|
f("foo", []string{"foo"})
|
|
|
|
f("foo\n", []string{"foo"})
|
|
|
|
f("foo\nbar", []string{"foo", "bar"})
|
|
|
|
f("\nfoo\nbar", []string{"\nfoo", "bar"})
|
|
|
|
f("\nfoo\nbar\n", []string{"\nfoo\nbar"})
|
|
|
|
f("\nfoo\nbar\n\n", []string{"\nfoo\nbar\n"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadLinesBlockSuccessSingleByteReader(t *testing.T) {
|
2019-05-28 14:31:35 +00:00
|
|
|
f := func(s, dstBufExpected, tailBufExpected string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
r := &singleByteReader{
|
|
|
|
b: []byte(s),
|
|
|
|
}
|
|
|
|
dstBuf, tailBuf, err := ReadLinesBlock(r, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
if string(dstBuf) != dstBufExpected {
|
|
|
|
t.Fatalf("unexpected dstBuf; got %q; want %q; tailBuf=%q", dstBuf, dstBufExpected, tailBuf)
|
|
|
|
}
|
|
|
|
if string(tailBuf) != tailBufExpected {
|
|
|
|
t.Fatalf("unexpected tailBuf; got %q; want %q; dstBuf=%q", tailBuf, tailBufExpected, dstBuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the same with non-empty dstBuf and tailBuf
|
|
|
|
r = &singleByteReader{
|
|
|
|
b: []byte(s),
|
|
|
|
}
|
|
|
|
dstBuf, tailBuf, err = ReadLinesBlock(r, dstBuf, tailBuf[:0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("non-empty bufs: unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
if string(dstBuf) != dstBufExpected {
|
|
|
|
t.Fatalf("non-empty bufs: unexpected dstBuf; got %q; want %q; tailBuf=%q", dstBuf, dstBufExpected, tailBuf)
|
|
|
|
}
|
|
|
|
if string(tailBuf) != tailBufExpected {
|
|
|
|
t.Fatalf("non-empty bufs: unexpected tailBuf; got %q; want %q; dstBuf=%q", tailBuf, tailBufExpected, dstBuf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f("\n", "", "")
|
|
|
|
f("foo\n", "foo", "")
|
|
|
|
f("\nfoo", "", "")
|
|
|
|
f("foo\nbar", "foo", "")
|
|
|
|
f("foo\nbar\nbaz", "foo", "")
|
2019-06-07 20:36:58 +00:00
|
|
|
f("foo", "foo", "")
|
2019-05-28 14:31:35 +00:00
|
|
|
|
|
|
|
// The maximum line size
|
|
|
|
b := make([]byte, maxLineSize+10)
|
|
|
|
b[maxLineSize] = '\n'
|
|
|
|
f(string(b), string(b[:maxLineSize]), "")
|
|
|
|
}
|
|
|
|
|
2019-06-25 23:51:54 +00:00
|
|
|
func TestReadLinesBlockSuccessBytesBuffer(t *testing.T) {
|
2019-05-28 14:31:35 +00:00
|
|
|
f := func(s, dstBufExpected, tailBufExpected string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
r := bytes.NewBufferString(s)
|
|
|
|
dstBuf, tailBuf, err := ReadLinesBlock(r, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
if string(dstBuf) != dstBufExpected {
|
|
|
|
t.Fatalf("unexpected dstBuf; got %q; want %q; tailBuf=%q", dstBuf, dstBufExpected, tailBuf)
|
|
|
|
}
|
|
|
|
if string(tailBuf) != tailBufExpected {
|
|
|
|
t.Fatalf("unexpected tailBuf; got %q; want %q; dstBuf=%q", tailBuf, tailBufExpected, dstBuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the same with non-empty dstBuf and tailBuf
|
|
|
|
r = bytes.NewBufferString(s)
|
|
|
|
dstBuf, tailBuf, err = ReadLinesBlock(r, dstBuf, tailBuf[:0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("non-empty bufs: unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
if string(dstBuf) != dstBufExpected {
|
|
|
|
t.Fatalf("non-empty bufs: unexpected dstBuf; got %q; want %q; tailBuf=%q", dstBuf, dstBufExpected, tailBuf)
|
|
|
|
}
|
|
|
|
if string(tailBuf) != tailBufExpected {
|
|
|
|
t.Fatalf("non-empty bufs: unexpected tailBuf; got %q; want %q; dstBuf=%q", tailBuf, tailBufExpected, dstBuf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f("\n", "", "")
|
|
|
|
f("foo\n", "foo", "")
|
|
|
|
f("\nfoo", "", "foo")
|
|
|
|
f("foo\nbar", "foo", "bar")
|
|
|
|
f("foo\nbar\nbaz", "foo\nbar", "baz")
|
|
|
|
|
|
|
|
// The maximum line size
|
|
|
|
b := make([]byte, maxLineSize+10)
|
|
|
|
b[maxLineSize] = '\n'
|
|
|
|
f(string(b), string(b[:maxLineSize]), string(b[maxLineSize+1:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
type singleByteReader struct {
|
|
|
|
b []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sbr *singleByteReader) Read(p []byte) (int, error) {
|
|
|
|
if len(sbr.b) == 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n := copy(p, sbr.b[:1])
|
|
|
|
sbr.b = sbr.b[n:]
|
|
|
|
if len(sbr.b) == 0 {
|
|
|
|
return n, io.EOF
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|