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