2020-11-23 04:14:08 -07:00
|
|
|
package aghio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-22 01:58:18 -07:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2020-11-23 04:14:08 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-12 03:27:44 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-23 04:14:08 -07:00
|
|
|
)
|
|
|
|
|
2021-05-24 07:28:11 -07:00
|
|
|
func TestLimitReader(t *testing.T) {
|
2020-11-23 04:14:08 -07:00
|
|
|
testCases := []struct {
|
2021-10-22 01:58:18 -07:00
|
|
|
wantErrMsg string
|
|
|
|
name string
|
|
|
|
n int64
|
2020-11-23 04:14:08 -07:00
|
|
|
}{{
|
2021-10-22 01:58:18 -07:00
|
|
|
wantErrMsg: "",
|
|
|
|
name: "positive",
|
|
|
|
n: 1,
|
2020-11-23 04:14:08 -07:00
|
|
|
}, {
|
2021-10-22 01:58:18 -07:00
|
|
|
wantErrMsg: "",
|
|
|
|
name: "zero",
|
|
|
|
n: 0,
|
2020-11-23 04:14:08 -07:00
|
|
|
}, {
|
2021-10-22 01:58:18 -07:00
|
|
|
wantErrMsg: "aghio: invalid n in LimitReader: -1",
|
|
|
|
name: "negative",
|
|
|
|
n: -1,
|
2020-11-23 04:14:08 -07:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-05-24 07:28:11 -07:00
|
|
|
_, err := LimitReader(nil, tc.n)
|
2021-10-22 01:58:18 -07:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
2020-11-23 04:14:08 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 07:28:11 -07:00
|
|
|
func TestLimitedReader_Read(t *testing.T) {
|
2020-11-23 04:14:08 -07:00
|
|
|
testCases := []struct {
|
2021-03-25 08:03:29 -07:00
|
|
|
err error
|
2020-11-23 04:14:08 -07:00
|
|
|
name string
|
|
|
|
rStr string
|
2021-03-25 08:03:29 -07:00
|
|
|
limit int64
|
2020-11-23 04:14:08 -07:00
|
|
|
want int
|
|
|
|
}{{
|
2021-03-25 08:03:29 -07:00
|
|
|
err: nil,
|
2020-11-23 04:14:08 -07:00
|
|
|
name: "perfectly_match",
|
|
|
|
rStr: "abc",
|
2021-03-25 08:03:29 -07:00
|
|
|
limit: 3,
|
2020-11-23 04:14:08 -07:00
|
|
|
want: 3,
|
|
|
|
}, {
|
2021-03-25 08:03:29 -07:00
|
|
|
err: io.EOF,
|
2020-11-23 04:14:08 -07:00
|
|
|
name: "eof",
|
|
|
|
rStr: "",
|
2021-03-25 08:03:29 -07:00
|
|
|
limit: 3,
|
2020-11-23 04:14:08 -07:00
|
|
|
want: 0,
|
|
|
|
}, {
|
|
|
|
err: &LimitReachedError{
|
|
|
|
Limit: 0,
|
|
|
|
},
|
2021-03-25 08:03:29 -07:00
|
|
|
name: "limit_reached",
|
|
|
|
rStr: "abc",
|
|
|
|
limit: 0,
|
|
|
|
want: 0,
|
2020-11-23 04:14:08 -07:00
|
|
|
}, {
|
2021-03-25 08:03:29 -07:00
|
|
|
err: nil,
|
2020-11-23 04:14:08 -07:00
|
|
|
name: "truncated",
|
|
|
|
rStr: "abc",
|
2021-03-25 08:03:29 -07:00
|
|
|
limit: 2,
|
2020-11-23 04:14:08 -07:00
|
|
|
want: 2,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2021-10-22 01:58:18 -07:00
|
|
|
readCloser := io.NopCloser(strings.NewReader(tc.rStr))
|
|
|
|
lreader, err := LimitReader(readCloser, tc.limit)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, lreader)
|
|
|
|
|
2020-11-23 04:14:08 -07:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
buf := make([]byte, tc.limit+1)
|
2021-10-22 01:58:18 -07:00
|
|
|
n, rerr := lreader.Read(buf)
|
|
|
|
require.Equal(t, rerr, tc.err)
|
2020-11-23 04:14:08 -07:00
|
|
|
|
2021-02-12 03:27:44 -07:00
|
|
|
assert.Equal(t, tc.want, n)
|
2020-11-23 04:14:08 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 07:28:11 -07:00
|
|
|
func TestLimitedReader_LimitReachedError(t *testing.T) {
|
2021-10-22 01:58:18 -07:00
|
|
|
testutil.AssertErrorMsg(t, "attempted to read more than 0 bytes", &LimitReachedError{
|
|
|
|
Limit: 0,
|
|
|
|
})
|
2020-11-23 04:14:08 -07:00
|
|
|
}
|