2018-04-11 14:41:50 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package eestream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-05-14 21:30:57 +01:00
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
2018-04-24 03:17:50 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-04-11 14:41:50 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2018-05-04 21:52:49 +01:00
|
|
|
"math/rand"
|
2018-04-11 14:41:50 +01:00
|
|
|
"testing"
|
2018-05-14 21:30:57 +01:00
|
|
|
"time"
|
2018-04-11 14:41:50 +01:00
|
|
|
|
2018-04-24 03:17:50 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-04-11 14:41:50 +01:00
|
|
|
"github.com/vivint/infectious"
|
2018-04-24 03:17:50 +01:00
|
|
|
|
|
|
|
"storj.io/storj/internal/pkg/readcloser"
|
2018-05-14 21:30:57 +01:00
|
|
|
"storj.io/storj/pkg/ranger"
|
2018-04-11 14:41:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRS(t *testing.T) {
|
2018-05-30 16:27:09 +01:00
|
|
|
ctx := context.Background()
|
2018-04-11 14:41:50 +01:00
|
|
|
data := randData(32 * 1024)
|
|
|
|
fc, err := infectious.NewFEC(2, 4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
es := NewRSScheme(fc, 8*1024)
|
|
|
|
rs, err := NewRedundancyStrategy(es, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
readers, err := EncodeReader(ctx, bytes.NewReader(data), rs, 0)
|
2018-05-30 16:27:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-04-17 14:39:14 +01:00
|
|
|
readerMap := make(map[int]io.ReadCloser, len(readers))
|
2018-04-11 14:41:50 +01:00
|
|
|
for i, reader := range readers {
|
2018-04-17 14:39:14 +01:00
|
|
|
readerMap[i] = ioutil.NopCloser(reader)
|
2018-04-11 14:41:50 +01:00
|
|
|
}
|
2018-05-30 16:27:09 +01:00
|
|
|
decoder := DecodeReaders(ctx, readerMap, rs, 32*1024, 0)
|
2018-05-14 21:30:57 +01:00
|
|
|
defer decoder.Close()
|
|
|
|
data2, err := ioutil.ReadAll(decoder)
|
2018-04-11 14:41:50 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(data, data2) {
|
|
|
|
t.Fatalf("rs encode/decode failed")
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 03:17:50 +01:00
|
|
|
|
2018-05-04 21:52:49 +01:00
|
|
|
// Check that io.ReadFull will return io.ErrUnexpectedEOF
|
|
|
|
// if DecodeReaders return less data than expected.
|
2018-05-30 16:27:09 +01:00
|
|
|
func TestRSUnexpectedEOF(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-05-04 21:52:49 +01:00
|
|
|
data := randData(32 * 1024)
|
|
|
|
fc, err := infectious.NewFEC(2, 4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
es := NewRSScheme(fc, 8*1024)
|
|
|
|
rs, err := NewRedundancyStrategy(es, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
readers, err := EncodeReader(ctx, bytes.NewReader(data), rs, 0)
|
2018-05-30 16:27:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-05-04 21:52:49 +01:00
|
|
|
readerMap := make(map[int]io.ReadCloser, len(readers))
|
|
|
|
for i, reader := range readers {
|
|
|
|
readerMap[i] = ioutil.NopCloser(reader)
|
|
|
|
}
|
2018-05-30 16:27:09 +01:00
|
|
|
decoder := DecodeReaders(ctx, readerMap, rs, 32*1024, 0)
|
2018-05-14 21:30:57 +01:00
|
|
|
defer decoder.Close()
|
2018-05-04 21:52:49 +01:00
|
|
|
// Try ReadFull more data from DecodeReaders than available
|
|
|
|
data2 := make([]byte, len(data)+1024)
|
2018-05-14 21:30:57 +01:00
|
|
|
_, err = io.ReadFull(decoder, data2)
|
2018-05-04 21:52:49 +01:00
|
|
|
assert.EqualError(t, err, io.ErrUnexpectedEOF.Error())
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:30:57 +01:00
|
|
|
func TestRSRanger(t *testing.T) {
|
2018-05-30 16:27:09 +01:00
|
|
|
ctx := context.Background()
|
2018-05-14 21:30:57 +01:00
|
|
|
data := randData(32 * 1024)
|
|
|
|
fc, err := infectious.NewFEC(2, 4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
es := NewRSScheme(fc, 8*1024)
|
|
|
|
rs, err := NewRedundancyStrategy(es, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-05-14 21:30:57 +01:00
|
|
|
encKey := sha256.Sum256([]byte("the secret key"))
|
2018-05-15 16:11:03 +01:00
|
|
|
var firstNonce [12]byte
|
|
|
|
encrypter, err := NewAESGCMEncrypter(
|
2018-05-14 21:30:57 +01:00
|
|
|
&encKey, &firstNonce, rs.DecodedBlockSize())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-05-30 16:27:09 +01:00
|
|
|
readers, err := EncodeReader(ctx, TransformReader(PadReader(ioutil.NopCloser(
|
2018-07-03 09:35:01 +01:00
|
|
|
bytes.NewReader(data)), encrypter.InBlockSize()), encrypter, 0), rs, 0)
|
2018-05-30 16:27:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-05-14 21:30:57 +01:00
|
|
|
pieces, err := readAll(readers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
rrs := map[int]ranger.RangeCloser{}
|
2018-05-14 21:30:57 +01:00
|
|
|
for i, piece := range pieces {
|
2018-08-06 15:24:30 +01:00
|
|
|
rrs[i] = ranger.ByteRangeCloser(piece)
|
2018-05-14 21:30:57 +01:00
|
|
|
}
|
2018-05-15 16:11:03 +01:00
|
|
|
decrypter, err := NewAESGCMDecrypter(
|
2018-05-14 21:30:57 +01:00
|
|
|
&encKey, &firstNonce, rs.DecodedBlockSize())
|
2018-07-03 09:35:01 +01:00
|
|
|
rc, err := Decode(rrs, rs, 0)
|
2018-05-14 21:30:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
defer rc.Close()
|
|
|
|
rr, err := Transform(rc, decrypter)
|
2018-05-14 21:30:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-19 16:59:09 +01:00
|
|
|
rr, err = UnpadSlow(ctx, rr)
|
2018-05-14 21:30:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-19 16:59:09 +01:00
|
|
|
r, err := rr.Range(ctx, 0, rr.Size())
|
2018-06-18 17:46:49 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
data2, err := ioutil.ReadAll(r)
|
2018-05-14 21:30:57 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(data, data2) {
|
|
|
|
t.Fatalf("rs encode/decode failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 09:35:01 +01:00
|
|
|
func TestNewRedundancyStrategy(t *testing.T) {
|
2018-05-30 16:27:09 +01:00
|
|
|
for i, tt := range []struct {
|
|
|
|
min int
|
|
|
|
opt int
|
2018-07-03 09:35:01 +01:00
|
|
|
expMin int
|
|
|
|
expOpt int
|
|
|
|
errString string
|
|
|
|
}{
|
|
|
|
{0, 0, 4, 4, ""},
|
|
|
|
{-1, 0, 0, 0, "eestream error: negative minimum threshold"},
|
|
|
|
{1, 0, 0, 0, "eestream error: minimum threshold less than required count"},
|
|
|
|
{5, 0, 0, 0, "eestream error: minimum threshold greater than total count"},
|
|
|
|
{0, -1, 0, 0, "eestream error: negative optimum threshold"},
|
|
|
|
{0, 1, 0, 0, "eestream error: optimum threshold less than required count"},
|
|
|
|
{0, 5, 0, 0, "eestream error: optimum threshold greater than total count"},
|
|
|
|
{3, 4, 3, 4, ""},
|
|
|
|
{0, 3, 0, 0, "eestream error: minimum threshold greater than optimum threshold"},
|
|
|
|
{4, 3, 0, 0, "eestream error: minimum threshold greater than optimum threshold"},
|
|
|
|
{4, 4, 4, 4, ""},
|
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
fc, err := infectious.NewFEC(2, 4)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
es := NewRSScheme(fc, 8*1024)
|
|
|
|
rs, err := NewRedundancyStrategy(es, tt.min, tt.opt)
|
|
|
|
if tt.errString != "" {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
assert.Equal(t, tt.expMin, rs.MinimumThreshold(), errTag)
|
|
|
|
assert.Equal(t, tt.expOpt, rs.OptimumThreshold(), errTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRSEncoderInputParams(t *testing.T) {
|
|
|
|
for i, tt := range []struct {
|
2018-05-30 16:27:09 +01:00
|
|
|
mbm int
|
|
|
|
errString string
|
|
|
|
}{
|
2018-07-03 09:35:01 +01:00
|
|
|
{0, ""},
|
|
|
|
{-1, "eestream error: negative max buffer memory"},
|
|
|
|
{1024, ""},
|
2018-05-30 16:27:09 +01:00
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
ctx := context.Background()
|
|
|
|
data := randData(32 * 1024)
|
|
|
|
fc, err := infectious.NewFEC(2, 4)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
2018-06-22 10:23:19 +01:00
|
|
|
continue
|
2018-05-30 16:27:09 +01:00
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
es := NewRSScheme(fc, 8*1024)
|
|
|
|
rs, err := NewRedundancyStrategy(es, 0, 0)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = EncodeReader(ctx, bytes.NewReader(data), rs, tt.mbm)
|
2018-05-30 16:27:09 +01:00
|
|
|
if tt.errString == "" {
|
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRSRangerInputParams(t *testing.T) {
|
|
|
|
for i, tt := range []struct {
|
|
|
|
mbm int
|
|
|
|
errString string
|
|
|
|
}{
|
2018-07-03 09:35:01 +01:00
|
|
|
{0, ""},
|
|
|
|
{-1, "eestream error: negative max buffer memory"},
|
|
|
|
{1024, ""},
|
2018-05-30 16:27:09 +01:00
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
ctx := context.Background()
|
|
|
|
data := randData(32 * 1024)
|
|
|
|
fc, err := infectious.NewFEC(2, 4)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
2018-06-22 10:23:19 +01:00
|
|
|
continue
|
2018-05-30 16:27:09 +01:00
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
es := NewRSScheme(fc, 8*1024)
|
|
|
|
rs, err := NewRedundancyStrategy(es, 0, 0)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = EncodeReader(ctx, bytes.NewReader(data), rs, tt.mbm)
|
2018-05-30 16:27:09 +01:00
|
|
|
if tt.errString == "" {
|
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:52:49 +01:00
|
|
|
// Some pieces will read error.
|
|
|
|
// Test will pass if at least required number of pieces are still good.
|
2018-05-14 21:30:57 +01:00
|
|
|
func TestRSErrors(t *testing.T) {
|
2018-05-04 21:52:49 +01:00
|
|
|
for i, tt := range []testCase{
|
2018-04-24 03:17:50 +01:00
|
|
|
{4 * 1024, 1024, 1, 1, 0, false},
|
|
|
|
{4 * 1024, 1024, 1, 1, 1, true},
|
|
|
|
{4 * 1024, 1024, 1, 2, 0, false},
|
|
|
|
{4 * 1024, 1024, 1, 2, 1, false},
|
|
|
|
{4 * 1024, 1024, 1, 2, 2, true},
|
|
|
|
{4 * 1024, 1024, 2, 4, 0, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 1, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 2, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 3, true},
|
|
|
|
{4 * 1024, 1024, 2, 4, 4, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 0, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 1, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 2, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 3, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 4, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 5, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 6, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 7, true},
|
|
|
|
} {
|
2018-05-04 21:52:49 +01:00
|
|
|
testRSProblematic(t, tt, i, func(in []byte) io.ReadCloser {
|
|
|
|
return readcloser.FatalReadCloser(
|
2018-04-24 03:17:50 +01:00
|
|
|
errors.New("I am an error piece"))
|
2018-05-04 21:52:49 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some pieces will read EOF at the beginning (byte 0).
|
|
|
|
// Test will pass if those pieces are less than required.
|
|
|
|
func TestRSEOF(t *testing.T) {
|
|
|
|
for i, tt := range []testCase{
|
|
|
|
{4 * 1024, 1024, 1, 1, 0, false},
|
|
|
|
{4 * 1024, 1024, 1, 1, 1, true},
|
|
|
|
{4 * 1024, 1024, 1, 2, 0, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{4 * 1024, 1024, 1, 2, 1, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
{4 * 1024, 1024, 1, 2, 2, true},
|
|
|
|
{4 * 1024, 1024, 2, 4, 0, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 1, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{4 * 1024, 1024, 2, 4, 2, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
{4 * 1024, 1024, 2, 4, 3, true},
|
|
|
|
{4 * 1024, 1024, 2, 4, 4, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 0, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 1, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 2, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{6 * 1024, 1024, 3, 7, 3, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 4, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
{6 * 1024, 1024, 3, 7, 5, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 6, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 7, true},
|
|
|
|
} {
|
|
|
|
testRSProblematic(t, tt, i, func(in []byte) io.ReadCloser {
|
|
|
|
return readcloser.LimitReadCloser(
|
|
|
|
ioutil.NopCloser(bytes.NewReader(in)), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some pieces will read EOF earlier than expected
|
|
|
|
// Test will pass if those pieces are less than required.
|
|
|
|
func TestRSEarlyEOF(t *testing.T) {
|
|
|
|
for i, tt := range []testCase{
|
|
|
|
{4 * 1024, 1024, 1, 1, 0, false},
|
|
|
|
{4 * 1024, 1024, 1, 1, 1, true},
|
|
|
|
{4 * 1024, 1024, 1, 2, 0, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{4 * 1024, 1024, 1, 2, 1, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
{4 * 1024, 1024, 1, 2, 2, true},
|
|
|
|
{4 * 1024, 1024, 2, 4, 0, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 1, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{4 * 1024, 1024, 2, 4, 2, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
{4 * 1024, 1024, 2, 4, 3, true},
|
|
|
|
{4 * 1024, 1024, 2, 4, 4, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 0, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 1, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 2, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{6 * 1024, 1024, 3, 7, 3, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 4, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
{6 * 1024, 1024, 3, 7, 5, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 6, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 7, true},
|
|
|
|
} {
|
|
|
|
testRSProblematic(t, tt, i, func(in []byte) io.ReadCloser {
|
|
|
|
// Read EOF after 500 bytes
|
|
|
|
return readcloser.LimitReadCloser(
|
|
|
|
ioutil.NopCloser(bytes.NewReader(in)), 500)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some pieces will read EOF later than expected.
|
|
|
|
// Test will pass if at least required number of pieces are still good.
|
|
|
|
func TestRSLateEOF(t *testing.T) {
|
|
|
|
for i, tt := range []testCase{
|
|
|
|
{4 * 1024, 1024, 1, 1, 0, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{4 * 1024, 1024, 1, 1, 1, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
{4 * 1024, 1024, 1, 2, 0, false},
|
|
|
|
{4 * 1024, 1024, 1, 2, 1, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{4 * 1024, 1024, 1, 2, 2, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
{4 * 1024, 1024, 2, 4, 0, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 1, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 2, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{4 * 1024, 1024, 2, 4, 3, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 4, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
{6 * 1024, 1024, 3, 7, 0, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 1, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 2, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 3, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 4, false},
|
2018-05-14 21:30:57 +01:00
|
|
|
{6 * 1024, 1024, 3, 7, 5, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 6, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 7, false},
|
2018-05-04 21:52:49 +01:00
|
|
|
} {
|
|
|
|
testRSProblematic(t, tt, i, func(in []byte) io.ReadCloser {
|
|
|
|
// extend the input with random number of random bytes
|
|
|
|
random := randData(1 + rand.Intn(10000))
|
|
|
|
extended := append(in, random...)
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(extended))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some pieces will read random data.
|
|
|
|
// Test will pass if there are enough good pieces for error correction.
|
|
|
|
func TestRSRandomData(t *testing.T) {
|
|
|
|
for i, tt := range []testCase{
|
|
|
|
{4 * 1024, 1024, 1, 1, 0, false},
|
|
|
|
{4 * 1024, 1024, 1, 1, 1, true},
|
|
|
|
{4 * 1024, 1024, 1, 2, 0, false},
|
|
|
|
{4 * 1024, 1024, 1, 2, 1, true},
|
|
|
|
{4 * 1024, 1024, 1, 2, 2, true},
|
|
|
|
{4 * 1024, 1024, 2, 4, 0, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 1, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 2, true},
|
|
|
|
{4 * 1024, 1024, 2, 4, 3, true},
|
|
|
|
{4 * 1024, 1024, 2, 4, 4, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 0, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 1, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 2, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 4, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 5, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 6, true},
|
|
|
|
{6 * 1024, 1024, 3, 7, 7, true},
|
|
|
|
} {
|
|
|
|
testRSProblematic(t, tt, i, func(in []byte) io.ReadCloser {
|
|
|
|
// return random data instead of expected one
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(randData(len(in))))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:30:57 +01:00
|
|
|
// Some pieces will read slowly
|
|
|
|
func TestRSSlow(t *testing.T) {
|
|
|
|
for i, tt := range []testCase{
|
|
|
|
{4 * 1024, 1024, 1, 1, 0, false},
|
|
|
|
{4 * 1024, 1024, 1, 2, 0, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 0, false},
|
|
|
|
{4 * 1024, 1024, 2, 4, 1, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 0, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 1, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 2, false},
|
|
|
|
{6 * 1024, 1024, 3, 7, 3, false},
|
|
|
|
} {
|
|
|
|
start := time.Now()
|
|
|
|
testRSProblematic(t, tt, i, func(in []byte) io.ReadCloser {
|
|
|
|
// sleep 1 second before every read
|
|
|
|
return ioutil.NopCloser(SlowReader(bytes.NewReader(in), 1*time.Second))
|
|
|
|
})
|
|
|
|
if time.Since(start) > 1*time.Second {
|
|
|
|
t.Fatalf("waited for slow reader")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:52:49 +01:00
|
|
|
type testCase struct {
|
|
|
|
dataSize int
|
|
|
|
blockSize int
|
|
|
|
required int
|
|
|
|
total int
|
|
|
|
problematic int
|
|
|
|
fail bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type problematicReadCloser func([]byte) io.ReadCloser
|
|
|
|
|
|
|
|
func testRSProblematic(t *testing.T, tt testCase, i int, fn problematicReadCloser) {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
2018-05-30 16:27:09 +01:00
|
|
|
ctx := context.Background()
|
2018-05-04 21:52:49 +01:00
|
|
|
data := randData(tt.dataSize)
|
|
|
|
fc, err := infectious.NewFEC(tt.required, tt.total)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
return
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
es := NewRSScheme(fc, tt.blockSize)
|
|
|
|
rs, err := NewRedundancyStrategy(es, 0, 0)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
readers, err := EncodeReader(ctx, bytes.NewReader(data), rs, 3*1024)
|
2018-05-30 16:27:09 +01:00
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
return
|
|
|
|
}
|
2018-05-04 21:52:49 +01:00
|
|
|
// read all readers in []byte buffers to avoid deadlock if later
|
|
|
|
// we don't read in parallel from all of them
|
|
|
|
pieces, err := readAll(readers)
|
|
|
|
if !assert.NoError(t, err, errTag) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
readerMap := make(map[int]io.ReadCloser, len(readers))
|
|
|
|
// some readers will return EOF later
|
|
|
|
for i := 0; i < tt.problematic; i++ {
|
|
|
|
readerMap[i] = fn(pieces[i])
|
|
|
|
}
|
|
|
|
// the rest will operate normally
|
|
|
|
for i := tt.problematic; i < tt.total; i++ {
|
|
|
|
readerMap[i] = ioutil.NopCloser(bytes.NewReader(pieces[i]))
|
|
|
|
}
|
2018-05-30 16:27:09 +01:00
|
|
|
decoder := DecodeReaders(ctx, readerMap, rs, int64(tt.dataSize), 3*1024)
|
2018-05-14 21:30:57 +01:00
|
|
|
defer decoder.Close()
|
|
|
|
data2, err := ioutil.ReadAll(decoder)
|
2018-05-04 21:52:49 +01:00
|
|
|
if tt.fail {
|
|
|
|
if err == nil && bytes.Compare(data, data2) == 0 {
|
|
|
|
assert.Fail(t, "expected to fail, but didn't", errTag)
|
2018-04-24 03:17:50 +01:00
|
|
|
}
|
2018-05-04 21:52:49 +01:00
|
|
|
} else if assert.NoError(t, err, errTag) {
|
|
|
|
assert.Equal(t, data, data2, errTag)
|
2018-04-24 03:17:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAll(readers []io.Reader) ([][]byte, error) {
|
|
|
|
pieces := make([][]byte, len(readers))
|
|
|
|
errs := make(chan error, len(readers))
|
|
|
|
var err error
|
|
|
|
for i := range readers {
|
|
|
|
go func(i int) {
|
|
|
|
pieces[i], err = ioutil.ReadAll(readers[i])
|
|
|
|
errs <- err
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
for range readers {
|
|
|
|
err := <-errs
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pieces, nil
|
|
|
|
}
|
2018-05-14 21:30:57 +01:00
|
|
|
|
|
|
|
func SlowReader(r io.Reader, delay time.Duration) io.Reader {
|
|
|
|
return &slowReader{Reader: r, Delay: delay}
|
|
|
|
}
|
|
|
|
|
|
|
|
type slowReader struct {
|
|
|
|
Reader io.Reader
|
|
|
|
Delay time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slowReader) Read(p []byte) (n int, err error) {
|
|
|
|
time.Sleep(s.Delay)
|
|
|
|
return s.Reader.Read(p)
|
|
|
|
}
|
2018-05-30 16:27:09 +01:00
|
|
|
|
|
|
|
func TestEncoderStalledReaders(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
data := randData(120 * 1024)
|
|
|
|
fc, err := infectious.NewFEC(30, 60)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-03 09:35:01 +01:00
|
|
|
es := NewRSScheme(fc, 1024)
|
|
|
|
rs, err := NewRedundancyStrategy(es, 35, 50)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
readers, err := EncodeReader(ctx, bytes.NewReader(data), rs, 0)
|
2018-05-30 16:27:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
start := time.Now()
|
|
|
|
_, err = readAllStalled(readers, 25)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if time.Since(start) > 1*time.Second {
|
|
|
|
t.Fatalf("waited for slow reader")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAllStalled(readers []io.Reader, stalled int) ([][]byte, error) {
|
|
|
|
pieces := make([][]byte, len(readers))
|
|
|
|
errs := make(chan error, len(readers))
|
|
|
|
var err error
|
|
|
|
for i := stalled; i < len(readers); i++ {
|
|
|
|
go func(i int) {
|
|
|
|
pieces[i], err = ioutil.ReadAll(readers[i])
|
|
|
|
errs <- err
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
for i := stalled; i < len(readers); i++ {
|
|
|
|
err := <-errs
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pieces, nil
|
|
|
|
}
|