2020-05-29 17:06:44 +00:00
|
|
|
package fr32_test
|
2020-05-28 17:49:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-17 13:26:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
|
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/fr32"
|
2020-05-28 17:49:25 +00:00
|
|
|
|
|
|
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
2020-08-16 10:40:35 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-05-28 17:49:25 +00:00
|
|
|
|
2020-08-16 10:40:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-28 17:49:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteTwoPcs(t *testing.T) {
|
|
|
|
tf, _ := ioutil.TempFile("/tmp/", "scrb-")
|
|
|
|
|
|
|
|
paddedSize := abi.PaddedPieceSize(16 << 20)
|
|
|
|
n := 2
|
|
|
|
|
|
|
|
var rawBytes []byte
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
buf := bytes.Repeat([]byte{0xab * byte(i)}, int(paddedSize.Unpadded()))
|
|
|
|
rawBytes = append(rawBytes, buf...)
|
|
|
|
|
|
|
|
rf, w, _ := ffiwrapper.ToReadableFile(bytes.NewReader(buf), int64(len(buf)))
|
|
|
|
|
2020-06-15 12:32:17 +00:00
|
|
|
_, _, _, err := ffi.WriteWithAlignment(abi.RegisteredSealProof_StackedDrg32GiBV1, rf, abi.UnpaddedPieceSize(len(buf)), tf, nil)
|
2020-05-28 17:49:25 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := w(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 10:40:35 +00:00
|
|
|
if _, err := tf.Seek(io.SeekStart, 0); err != nil { // nolint:staticcheck
|
2020-05-28 17:49:25 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ffiBytes, err := ioutil.ReadAll(tf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tf.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(tf.Name()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:17:23 +00:00
|
|
|
outBytes := make([]byte, int(paddedSize)*n)
|
2020-05-29 17:06:44 +00:00
|
|
|
fr32.Pad(rawBytes, outBytes)
|
2020-05-28 17:49:25 +00:00
|
|
|
require.Equal(t, ffiBytes, outBytes)
|
|
|
|
|
2020-05-28 22:17:23 +00:00
|
|
|
unpadBytes := make([]byte, int(paddedSize.Unpadded())*n)
|
2020-05-29 17:06:44 +00:00
|
|
|
fr32.Unpad(ffiBytes, unpadBytes)
|
2020-05-28 17:49:25 +00:00
|
|
|
require.Equal(t, rawBytes, unpadBytes)
|
|
|
|
}
|