2021-08-09 10:49:08 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2022-05-19 14:56:42 +00:00
|
|
|
"bytes"
|
2021-08-09 10:49:08 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2022-05-19 14:56:42 +00:00
|
|
|
"io/ioutil"
|
2021-08-09 10:49:08 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-05-19 14:56:42 +00:00
|
|
|
func TestWrapReader(t *testing.T) {
|
|
|
|
src := make([]byte, 1024*1024+1)
|
|
|
|
_, err := rand.Read(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
h := sha256.Sum256(src)
|
|
|
|
|
|
|
|
streamHash := sha256.New()
|
|
|
|
reader := bytes.NewReader(src)
|
|
|
|
wrappedReader := wrapReader(reader, 64*1024, func(buf []byte) {
|
|
|
|
streamHash.Write(buf)
|
|
|
|
})
|
|
|
|
|
|
|
|
dst, err := ioutil.ReadAll(wrappedReader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, src, dst)
|
|
|
|
require.Equal(t, h[:], streamHash.Sum(nil))
|
|
|
|
}
|