2018-06-03 05:20:17 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
package psclient
|
2018-06-03 05:20:17 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-08-27 19:35:27 +01:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2018-06-03 05:20:17 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-06-03 05:20:17 +01:00
|
|
|
)
|
|
|
|
|
2018-06-27 19:42:54 +01:00
|
|
|
func TestPieceRanger(t *testing.T) {
|
2018-06-03 05:20:17 +01:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
for i, tt := range []struct {
|
|
|
|
data string
|
|
|
|
size, offset, length int64
|
|
|
|
substr string
|
|
|
|
errString string
|
|
|
|
}{
|
|
|
|
{"", 0, 0, 0, "", ""},
|
|
|
|
{"abcdef", 6, 0, 0, "", ""},
|
|
|
|
{"abcdef", 6, 3, 0, "", ""},
|
|
|
|
{"abcdef", 6, 0, 6, "abcdef", ""},
|
|
|
|
{"abcdef", 6, 0, 5, "abcde", ""},
|
|
|
|
{"abcdef", 6, 0, 4, "abcd", ""},
|
|
|
|
{"abcdef", 6, 1, 4, "bcde", ""},
|
|
|
|
{"abcdef", 6, 2, 4, "cdef", ""},
|
|
|
|
{"abcdefg", 7, 1, 4, "bcde", ""},
|
2018-06-27 19:42:54 +01:00
|
|
|
{"abcdef", 6, 0, 7, "abcdef", "pieceRanger error: range beyond end"},
|
|
|
|
{"abcdef", 6, -1, 7, "abcde", "pieceRanger error: negative offset"},
|
|
|
|
{"abcdef", 6, 0, -1, "abcde", "pieceRanger error: negative length"},
|
2018-06-03 05:20:17 +01:00
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
|
2018-08-27 19:35:27 +01:00
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2018-06-03 05:20:17 +01:00
|
|
|
route := pb.NewMockPieceStoreRoutesClient(ctrl)
|
2018-08-17 18:40:15 +01:00
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
route.EXPECT().Piece(
|
|
|
|
gomock.Any(), gomock.Any(), gomock.Any(),
|
2018-11-20 17:09:35 +00:00
|
|
|
).Return(&pb.PieceSummary{PieceSize: int64(len(tt.data))}, nil)
|
2018-08-17 18:40:15 +01:00
|
|
|
|
|
|
|
stream := pb.NewMockPieceStoreRoutes_RetrieveClient(ctrl)
|
|
|
|
pid := NewPieceID()
|
|
|
|
|
2018-06-03 05:20:17 +01:00
|
|
|
if tt.offset >= 0 && tt.length > 0 && tt.offset+tt.length <= tt.size {
|
2018-08-27 19:35:27 +01:00
|
|
|
msg1 := &pb.PieceRetrieval{
|
|
|
|
PieceData: &pb.PieceRetrieval_PieceData{
|
2018-11-20 17:09:35 +00:00
|
|
|
Id: pid.String(), PieceSize: tt.length, Offset: tt.offset,
|
2018-08-27 19:35:27 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
stream.EXPECT().Send(msg1).Return(nil)
|
|
|
|
stream.EXPECT().Send(gomock.Any()).Return(nil).MinTimes(0).MaxTimes(1)
|
|
|
|
stream.EXPECT().Recv().Return(
|
|
|
|
&pb.PieceRetrievalStream{
|
2018-11-20 17:09:35 +00:00
|
|
|
PieceSize: tt.length,
|
|
|
|
Content: []byte(tt.data)[tt.offset : tt.offset+tt.length],
|
2018-09-10 10:18:42 +01:00
|
|
|
}, nil)
|
|
|
|
stream.EXPECT().Recv().Return(&pb.PieceRetrievalStream{}, io.EOF)
|
2018-06-03 05:20:17 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 16:59:09 +01:00
|
|
|
ctx := context.Background()
|
2018-08-27 19:35:27 +01:00
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
target := &pb.Node{
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: "",
|
|
|
|
Transport: 0,
|
|
|
|
},
|
|
|
|
Id: "test-node-id-1234567",
|
|
|
|
}
|
|
|
|
c, err := NewCustomRoute(route, target, 32*1024, priv)
|
2018-08-17 18:40:15 +01:00
|
|
|
assert.NoError(t, err)
|
2018-10-17 12:40:11 +01:00
|
|
|
rr, err := PieceRanger(ctx, c, stream, pid, &pb.PayerBandwidthAllocation{}, nil)
|
2018-06-03 05:20:17 +01:00
|
|
|
if assert.NoError(t, err, errTag) {
|
2018-06-18 17:46:49 +01:00
|
|
|
assert.Equal(t, tt.size, rr.Size(), errTag)
|
2018-06-03 05:20:17 +01:00
|
|
|
}
|
2018-06-19 16:59:09 +01:00
|
|
|
r, err := rr.Range(ctx, tt.offset, tt.length)
|
2018-06-03 05:20:17 +01:00
|
|
|
if tt.errString != "" {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
2018-06-22 10:23:19 +01:00
|
|
|
continue
|
2018-06-03 05:20:17 +01:00
|
|
|
}
|
2018-06-18 17:46:49 +01:00
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
data, err := ioutil.ReadAll(r)
|
2018-06-03 05:20:17 +01:00
|
|
|
if assert.NoError(t, err, errTag) {
|
|
|
|
assert.Equal(t, []byte(tt.substr), data, errTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 19:42:54 +01:00
|
|
|
func TestPieceRangerSize(t *testing.T) {
|
2018-06-03 05:20:17 +01:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
for i, tt := range []struct {
|
|
|
|
data string
|
|
|
|
size, offset, length int64
|
|
|
|
substr string
|
|
|
|
errString string
|
|
|
|
}{
|
|
|
|
{"", 0, 0, 0, "", ""},
|
|
|
|
{"abcdef", 6, 0, 0, "", ""},
|
|
|
|
{"abcdef", 6, 3, 0, "", ""},
|
|
|
|
{"abcdef", 6, 0, 6, "abcdef", ""},
|
|
|
|
{"abcdef", 6, 0, 5, "abcde", ""},
|
|
|
|
{"abcdef", 6, 0, 4, "abcd", ""},
|
|
|
|
{"abcdef", 6, 1, 4, "bcde", ""},
|
|
|
|
{"abcdef", 6, 2, 4, "cdef", ""},
|
|
|
|
{"abcdefg", 7, 1, 4, "bcde", ""},
|
2018-06-27 19:42:54 +01:00
|
|
|
{"abcdef", 6, 0, 7, "abcdef", "pieceRanger error: range beyond end"},
|
|
|
|
{"abcdef", 6, -1, 7, "abcde", "pieceRanger error: negative offset"},
|
|
|
|
{"abcdef", 6, 0, -1, "abcde", "pieceRanger error: negative length"},
|
2018-06-03 05:20:17 +01:00
|
|
|
} {
|
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
|
|
|
|
route := pb.NewMockPieceStoreRoutesClient(ctrl)
|
2018-08-17 18:40:15 +01:00
|
|
|
pid := NewPieceID()
|
|
|
|
|
|
|
|
stream := pb.NewMockPieceStoreRoutes_RetrieveClient(ctrl)
|
|
|
|
|
2018-08-27 19:35:27 +01:00
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2018-06-03 05:20:17 +01:00
|
|
|
if tt.offset >= 0 && tt.length > 0 && tt.offset+tt.length <= tt.size {
|
2018-08-27 19:35:27 +01:00
|
|
|
msg1 := &pb.PieceRetrieval{
|
|
|
|
PieceData: &pb.PieceRetrieval_PieceData{
|
2018-11-20 17:09:35 +00:00
|
|
|
Id: pid.String(), PieceSize: tt.length, Offset: tt.offset,
|
2018-08-27 19:35:27 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
stream.EXPECT().Send(msg1).Return(nil)
|
|
|
|
stream.EXPECT().Send(gomock.Any()).Return(nil).MinTimes(0).MaxTimes(1)
|
|
|
|
stream.EXPECT().Recv().Return(
|
|
|
|
&pb.PieceRetrievalStream{
|
2018-11-20 17:09:35 +00:00
|
|
|
PieceSize: tt.length,
|
|
|
|
Content: []byte(tt.data)[tt.offset : tt.offset+tt.length],
|
2018-09-10 10:18:42 +01:00
|
|
|
}, nil)
|
|
|
|
stream.EXPECT().Recv().Return(&pb.PieceRetrievalStream{}, io.EOF)
|
2018-06-03 05:20:17 +01:00
|
|
|
}
|
|
|
|
|
2018-06-19 16:59:09 +01:00
|
|
|
ctx := context.Background()
|
2018-08-27 19:35:27 +01:00
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
target := &pb.Node{
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: "",
|
|
|
|
Transport: 0,
|
|
|
|
},
|
|
|
|
Id: "test-node-id-1234567",
|
|
|
|
}
|
|
|
|
c, err := NewCustomRoute(route, target, 32*1024, priv)
|
2018-08-17 18:40:15 +01:00
|
|
|
assert.NoError(t, err)
|
2018-10-17 12:40:11 +01:00
|
|
|
rr := PieceRangerSize(c, stream, pid, tt.size, &pb.PayerBandwidthAllocation{}, nil)
|
2018-06-18 17:46:49 +01:00
|
|
|
assert.Equal(t, tt.size, rr.Size(), errTag)
|
2018-06-19 16:59:09 +01:00
|
|
|
r, err := rr.Range(ctx, tt.offset, tt.length)
|
2018-06-03 05:20:17 +01:00
|
|
|
if tt.errString != "" {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
2018-06-22 10:23:19 +01:00
|
|
|
continue
|
2018-06-03 05:20:17 +01:00
|
|
|
}
|
2018-06-18 17:46:49 +01:00
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
data, err := ioutil.ReadAll(r)
|
2018-06-03 05:20:17 +01:00
|
|
|
if assert.NoError(t, err, errTag) {
|
|
|
|
assert.Equal(t, []byte(tt.substr), data, errTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|