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