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"
|
2023-10-27 11:05:14 +00:00
|
|
|
"errors"
|
2022-08-09 12:37:08 +00:00
|
|
|
"io"
|
2021-08-09 10:49:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
|
2021-08-09 10:49:08 +00:00
|
|
|
"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)
|
|
|
|
})
|
|
|
|
|
2022-08-09 12:37:08 +00:00
|
|
|
dst, err := io.ReadAll(wrappedReader)
|
2022-05-19 14:56:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, src, dst)
|
|
|
|
require.Equal(t, h[:], streamHash.Sum(nil))
|
|
|
|
}
|
2023-10-27 11:05:14 +00:00
|
|
|
|
|
|
|
func TestGoroutinesDontLeakInPutAndHash(t *testing.T) {
|
|
|
|
tc := prepareContext(t)
|
|
|
|
|
|
|
|
content := make([]byte, 128*1024)
|
|
|
|
_, err := rand.Read(content)
|
|
|
|
require.NoError(t, err)
|
|
|
|
payload := bytes.NewReader(content)
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
prm := frostfs.PrmObjectCreate{
|
2023-10-27 11:05:14 +00:00
|
|
|
Filepath: tc.obj,
|
|
|
|
Payload: payload,
|
|
|
|
}
|
|
|
|
|
|
|
|
expErr := errors.New("some error")
|
|
|
|
tc.testFrostFS.SetObjectPutError(tc.obj, expErr)
|
2024-07-22 09:00:17 +00:00
|
|
|
_, err = tc.layer.objectPutAndHash(tc.ctx, prm, tc.bktInfo)
|
2023-10-27 11:05:14 +00:00
|
|
|
require.ErrorIs(t, err, expErr)
|
|
|
|
require.Empty(t, payload.Len(), "body must be read out otherwise goroutines can leak in wrapReader")
|
|
|
|
}
|