2019-03-30 11:21:49 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metainfo_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-06-05 17:41:02 +01:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2019-03-30 11:21:49 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zeebo/errs"
|
2019-06-21 19:15:58 +01:00
|
|
|
"go.uber.org/zap"
|
2019-06-05 17:41:02 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-03-30 11:21:49 +00:00
|
|
|
|
2019-06-05 17:41:02 +01:00
|
|
|
"storj.io/storj/internal/memory"
|
2019-03-30 11:21:49 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testplanet"
|
2019-06-26 11:38:51 +01:00
|
|
|
"storj.io/storj/internal/testrand"
|
2019-07-03 17:14:37 +01:00
|
|
|
"storj.io/storj/pkg/eestream"
|
2019-05-24 17:51:27 +01:00
|
|
|
"storj.io/storj/pkg/macaroon"
|
2019-03-30 11:21:49 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-06-21 19:15:58 +01:00
|
|
|
"storj.io/storj/satellite"
|
2019-03-30 11:21:49 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-06-05 17:41:02 +01:00
|
|
|
"storj.io/storj/uplink/metainfo"
|
2019-03-30 11:21:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// mockAPIKeys is mock for api keys store of pointerdb
|
|
|
|
type mockAPIKeys struct {
|
|
|
|
info console.APIKeyInfo
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetByKey return api key info for given key
|
2019-05-24 17:51:27 +01:00
|
|
|
func (keys *mockAPIKeys) GetByKey(ctx context.Context, key macaroon.APIKey) (*console.APIKeyInfo, error) {
|
2019-03-30 11:21:49 +00:00
|
|
|
return &keys.info, keys.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidAPIKey(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
planet, err := testplanet.New(t, 1, 1, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
|
|
|
|
for _, invalidAPIKey := range []string{"", "invalid", "testKey"} {
|
|
|
|
client, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], invalidAPIKey)
|
|
|
|
require.NoError(t, err)
|
2019-06-25 16:36:23 +01:00
|
|
|
defer ctx.Check(client.Close)
|
2019-03-30 11:21:49 +00:00
|
|
|
|
|
|
|
_, _, err = client.CreateSegment(ctx, "hello", "world", 1, &pb.RedundancyScheme{}, 123, time.Now())
|
2019-05-24 17:51:27 +01:00
|
|
|
assertUnauthenticated(t, err, false)
|
2019-03-30 11:21:49 +00:00
|
|
|
|
|
|
|
_, err = client.CommitSegment(ctx, "testbucket", "testpath", 0, &pb.Pointer{}, nil)
|
2019-05-24 17:51:27 +01:00
|
|
|
assertUnauthenticated(t, err, false)
|
2019-03-30 11:21:49 +00:00
|
|
|
|
|
|
|
_, err = client.SegmentInfo(ctx, "testbucket", "testpath", 0)
|
2019-05-24 17:51:27 +01:00
|
|
|
assertUnauthenticated(t, err, false)
|
2019-03-30 11:21:49 +00:00
|
|
|
|
|
|
|
_, _, err = client.ReadSegment(ctx, "testbucket", "testpath", 0)
|
2019-05-24 17:51:27 +01:00
|
|
|
assertUnauthenticated(t, err, false)
|
2019-03-30 11:21:49 +00:00
|
|
|
|
|
|
|
_, err = client.DeleteSegment(ctx, "testbucket", "testpath", 0)
|
2019-05-24 17:51:27 +01:00
|
|
|
assertUnauthenticated(t, err, false)
|
2019-03-30 11:21:49 +00:00
|
|
|
|
|
|
|
_, _, err = client.ListSegments(ctx, "testbucket", "", "", "", true, 1, 0)
|
2019-05-24 17:51:27 +01:00
|
|
|
assertUnauthenticated(t, err, false)
|
2019-03-30 11:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:51:27 +01:00
|
|
|
func TestRestrictedAPIKey(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
planet, err := testplanet.New(t, 1, 1, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
|
|
|
|
key, err := macaroon.ParseAPIKey(planet.Uplinks[0].APIKey[planet.Satellites[0].ID()])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Caveat macaroon.Caveat
|
|
|
|
CreateSegmentAllowed bool
|
|
|
|
CommitSegmentAllowed bool
|
|
|
|
SegmentInfoAllowed bool
|
|
|
|
ReadSegmentAllowed bool
|
|
|
|
DeleteSegmentAllowed bool
|
|
|
|
ListSegmentsAllowed bool
|
2019-06-25 00:36:10 +01:00
|
|
|
ReadBucketAllowed bool
|
2019-05-24 17:51:27 +01:00
|
|
|
}{
|
|
|
|
{ // Everything disallowed
|
|
|
|
Caveat: macaroon.Caveat{
|
|
|
|
DisallowReads: true,
|
|
|
|
DisallowWrites: true,
|
|
|
|
DisallowLists: true,
|
|
|
|
DisallowDeletes: true,
|
|
|
|
},
|
2019-06-25 00:36:10 +01:00
|
|
|
ReadBucketAllowed: true,
|
2019-05-24 17:51:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
{ // Read only
|
|
|
|
Caveat: macaroon.Caveat{
|
|
|
|
DisallowWrites: true,
|
|
|
|
DisallowDeletes: true,
|
|
|
|
},
|
|
|
|
SegmentInfoAllowed: true,
|
|
|
|
ReadSegmentAllowed: true,
|
|
|
|
ListSegmentsAllowed: true,
|
2019-06-25 00:36:10 +01:00
|
|
|
ReadBucketAllowed: true,
|
2019-05-24 17:51:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
{ // Write only
|
|
|
|
Caveat: macaroon.Caveat{
|
|
|
|
DisallowReads: true,
|
|
|
|
DisallowLists: true,
|
|
|
|
},
|
|
|
|
CreateSegmentAllowed: true,
|
|
|
|
CommitSegmentAllowed: true,
|
|
|
|
DeleteSegmentAllowed: true,
|
2019-06-25 00:36:10 +01:00
|
|
|
ReadBucketAllowed: true,
|
2019-05-24 17:51:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
{ // Bucket restriction
|
|
|
|
Caveat: macaroon.Caveat{
|
|
|
|
AllowedPaths: []*macaroon.Caveat_Path{{
|
|
|
|
Bucket: []byte("otherbucket"),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{ // Path restriction
|
|
|
|
Caveat: macaroon.Caveat{
|
|
|
|
AllowedPaths: []*macaroon.Caveat_Path{{
|
|
|
|
Bucket: []byte("testbucket"),
|
|
|
|
EncryptedPathPrefix: []byte("otherpath"),
|
|
|
|
}},
|
|
|
|
},
|
2019-06-25 00:36:10 +01:00
|
|
|
ReadBucketAllowed: true,
|
2019-05-24 17:51:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
{ // Time restriction after
|
|
|
|
Caveat: macaroon.Caveat{
|
|
|
|
NotAfter: func(x time.Time) *time.Time { return &x }(time.Now()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{ // Time restriction before
|
|
|
|
Caveat: macaroon.Caveat{
|
|
|
|
NotBefore: func(x time.Time) *time.Time { return &x }(time.Now().Add(time.Hour)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
restrictedKey, err := key.Restrict(test.Caveat)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
client, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], restrictedKey.Serialize())
|
|
|
|
require.NoError(t, err)
|
2019-06-25 16:36:23 +01:00
|
|
|
defer ctx.Check(client.Close)
|
2019-05-24 17:51:27 +01:00
|
|
|
|
|
|
|
_, _, err = client.CreateSegment(ctx, "testbucket", "testpath", 1, &pb.RedundancyScheme{}, 123, time.Now())
|
|
|
|
assertUnauthenticated(t, err, test.CreateSegmentAllowed)
|
|
|
|
|
|
|
|
_, err = client.CommitSegment(ctx, "testbucket", "testpath", 0, &pb.Pointer{}, nil)
|
|
|
|
assertUnauthenticated(t, err, test.CommitSegmentAllowed)
|
|
|
|
|
|
|
|
_, err = client.SegmentInfo(ctx, "testbucket", "testpath", 0)
|
|
|
|
assertUnauthenticated(t, err, test.SegmentInfoAllowed)
|
|
|
|
|
|
|
|
_, _, err = client.ReadSegment(ctx, "testbucket", "testpath", 0)
|
|
|
|
assertUnauthenticated(t, err, test.ReadSegmentAllowed)
|
|
|
|
|
|
|
|
_, err = client.DeleteSegment(ctx, "testbucket", "testpath", 0)
|
|
|
|
assertUnauthenticated(t, err, test.DeleteSegmentAllowed)
|
|
|
|
|
|
|
|
_, _, err = client.ListSegments(ctx, "testbucket", "testpath", "", "", true, 1, 0)
|
|
|
|
assertUnauthenticated(t, err, test.ListSegmentsAllowed)
|
|
|
|
|
2019-06-25 00:36:10 +01:00
|
|
|
_, _, err = client.ReadSegment(ctx, "testbucket", "", -1)
|
|
|
|
assertUnauthenticated(t, err, test.ReadBucketAllowed)
|
2019-05-24 17:51:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertUnauthenticated(t *testing.T, err error, allowed bool) {
|
2019-03-30 11:21:49 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2019-05-24 17:51:27 +01:00
|
|
|
// If it's allowed, we allow any non-unauthenticated error because
|
|
|
|
// some calls error after authentication checks.
|
2019-03-30 11:21:49 +00:00
|
|
|
if err, ok := status.FromError(errs.Unwrap(err)); ok {
|
2019-05-24 17:51:27 +01:00
|
|
|
assert.Equal(t, codes.Unauthenticated == err.Code(), !allowed)
|
|
|
|
} else if !allowed {
|
2019-03-30 11:21:49 +00:00
|
|
|
assert.Fail(t, "got unexpected error", "%T", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceList(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
planet, err := testplanet.New(t, 1, 6, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
|
|
|
|
items := []struct {
|
|
|
|
Key string
|
|
|
|
Value []byte
|
|
|
|
}{
|
|
|
|
{Key: "sample.😶", Value: []byte{1}},
|
|
|
|
{Key: "müsic", Value: []byte{2}},
|
|
|
|
{Key: "müsic/söng1.mp3", Value: []byte{3}},
|
|
|
|
{Key: "müsic/söng2.mp3", Value: []byte{4}},
|
|
|
|
{Key: "müsic/album/söng3.mp3", Value: []byte{5}},
|
|
|
|
{Key: "müsic/söng4.mp3", Value: []byte{6}},
|
|
|
|
{Key: "ビデオ/movie.mkv", Value: []byte{7}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
err := planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", item.Key, item.Value)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := planet.Uplinks[0].GetConfig(planet.Satellites[0])
|
2019-07-02 11:08:02 +01:00
|
|
|
metainfo, _, cleanup, err := testplanet.DialMetainfo(ctx, planet.Uplinks[0].Log.Named("metainfo"), config, planet.Uplinks[0].Identity)
|
2019-03-30 11:21:49 +00:00
|
|
|
require.NoError(t, err)
|
2019-07-01 15:35:10 +01:00
|
|
|
defer ctx.Check(cleanup)
|
2019-03-30 11:21:49 +00:00
|
|
|
|
|
|
|
type Test struct {
|
|
|
|
Request storj.ListOptions
|
|
|
|
Expected storj.ObjectList // objects are partial
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := metainfo.ListObjects(ctx, "testbucket", storj.ListOptions{Recursive: true, Direction: storj.After})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []storj.Object{
|
|
|
|
{Path: "müsic"},
|
|
|
|
{Path: "müsic/album/söng3.mp3"},
|
|
|
|
{Path: "müsic/söng1.mp3"},
|
|
|
|
{Path: "müsic/söng2.mp3"},
|
|
|
|
{Path: "müsic/söng4.mp3"},
|
|
|
|
{Path: "sample.😶"},
|
|
|
|
{Path: "ビデオ/movie.mkv"},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, len(expected), len(list.Items))
|
|
|
|
sort.Slice(list.Items, func(i, k int) bool {
|
|
|
|
return list.Items[i].Path < list.Items[k].Path
|
|
|
|
})
|
|
|
|
for i, item := range expected {
|
|
|
|
require.Equal(t, item.Path, list.Items[i].Path)
|
|
|
|
require.Equal(t, item.IsPrefix, list.Items[i].IsPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err = metainfo.ListObjects(ctx, "testbucket", storj.ListOptions{Recursive: false, Direction: storj.After})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected = []storj.Object{
|
|
|
|
{Path: "müsic"},
|
|
|
|
{Path: "müsic/", IsPrefix: true},
|
|
|
|
{Path: "sample.😶"},
|
|
|
|
{Path: "ビデオ/", IsPrefix: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, len(expected), len(list.Items))
|
|
|
|
sort.Slice(list.Items, func(i, k int) bool {
|
|
|
|
return list.Items[i].Path < list.Items[k].Path
|
|
|
|
})
|
|
|
|
for i, item := range expected {
|
2019-05-24 17:51:27 +01:00
|
|
|
t.Log(item.Path, list.Items[i].Path)
|
2019-03-30 11:21:49 +00:00
|
|
|
require.Equal(t, item.Path, list.Items[i].Path)
|
|
|
|
require.Equal(t, item.IsPrefix, list.Items[i].IsPrefix)
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 14:31:19 +01:00
|
|
|
|
|
|
|
func TestCommitSegment(t *testing.T) {
|
2019-04-22 10:07:50 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-05-24 17:51:27 +01:00
|
|
|
apiKey := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
|
2019-04-09 14:31:19 +01:00
|
|
|
|
2019-04-22 10:07:50 +01:00
|
|
|
metainfo, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey)
|
2019-04-09 14:31:19 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-25 16:36:23 +01:00
|
|
|
defer ctx.Check(metainfo.Close)
|
2019-04-09 14:31:19 +01:00
|
|
|
|
2019-04-22 10:07:50 +01:00
|
|
|
{
|
|
|
|
// error if pointer is nil
|
2019-07-01 16:54:11 +01:00
|
|
|
_, err = metainfo.CommitSegment(ctx, "bucket", "path", -1, nil, []*pb.OrderLimit{})
|
2019-04-22 10:07:50 +01:00
|
|
|
require.Error(t, err)
|
2019-04-09 14:31:19 +01:00
|
|
|
}
|
2019-04-22 10:07:50 +01:00
|
|
|
{
|
|
|
|
// error if number of remote pieces is lower then repair threshold
|
|
|
|
redundancy := &pb.RedundancyScheme{
|
|
|
|
MinReq: 1,
|
|
|
|
RepairThreshold: 2,
|
2019-06-21 19:15:58 +01:00
|
|
|
SuccessThreshold: 3,
|
|
|
|
Total: 4,
|
|
|
|
ErasureShareSize: 256,
|
2019-04-22 10:07:50 +01:00
|
|
|
}
|
2019-06-05 17:41:02 +01:00
|
|
|
expirationDate := time.Now()
|
|
|
|
addresedLimits, rootPieceID, err := metainfo.CreateSegment(ctx, "bucket", "path", -1, redundancy, 1000, expirationDate)
|
2019-04-22 10:07:50 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// create number of pieces below repair threshold
|
|
|
|
usedForPieces := addresedLimits[:redundancy.RepairThreshold-1]
|
|
|
|
pieces := make([]*pb.RemotePiece, len(usedForPieces))
|
|
|
|
for i, limit := range usedForPieces {
|
|
|
|
pieces[i] = &pb.RemotePiece{
|
|
|
|
PieceNum: int32(i),
|
|
|
|
NodeId: limit.Limit.StorageNodeId,
|
2019-07-03 17:14:37 +01:00
|
|
|
Hash: &pb.PieceHash{
|
|
|
|
PieceId: limit.Limit.PieceId,
|
|
|
|
PieceSize: 256,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
},
|
2019-04-22 10:07:50 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-05 17:41:02 +01:00
|
|
|
|
|
|
|
expirationDateProto, err := ptypes.TimestampProto(expirationDate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-22 10:07:50 +01:00
|
|
|
pointer := &pb.Pointer{
|
2019-07-03 17:14:37 +01:00
|
|
|
Type: pb.Pointer_REMOTE,
|
|
|
|
SegmentSize: 10,
|
2019-04-22 10:07:50 +01:00
|
|
|
Remote: &pb.RemoteSegment{
|
|
|
|
RootPieceId: rootPieceID,
|
|
|
|
Redundancy: redundancy,
|
|
|
|
RemotePieces: pieces,
|
|
|
|
},
|
2019-06-05 17:41:02 +01:00
|
|
|
ExpirationDate: expirationDateProto,
|
2019-04-22 10:07:50 +01:00
|
|
|
}
|
2019-04-09 14:31:19 +01:00
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
limits := make([]*pb.OrderLimit, len(addresedLimits))
|
2019-04-22 10:07:50 +01:00
|
|
|
for i, addresedLimit := range addresedLimits {
|
|
|
|
limits[i] = addresedLimit.Limit
|
|
|
|
}
|
|
|
|
_, err = metainfo.CommitSegment(ctx, "bucket", "path", -1, pointer, limits)
|
|
|
|
require.Error(t, err)
|
2019-06-19 21:13:11 +01:00
|
|
|
require.Contains(t, err.Error(), "less than or equal to the repair threshold")
|
2019-04-09 14:31:19 +01:00
|
|
|
}
|
2019-04-22 10:07:50 +01:00
|
|
|
})
|
2019-04-09 14:31:19 +01:00
|
|
|
}
|
2019-06-05 17:41:02 +01:00
|
|
|
|
2019-06-21 19:15:58 +01:00
|
|
|
func TestCreateSegment(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Metainfo.RS.Validate = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
apiKey := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
|
|
|
|
|
|
|
|
metainfo, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey)
|
|
|
|
require.NoError(t, err)
|
2019-06-25 16:36:23 +01:00
|
|
|
defer ctx.Check(metainfo.Close)
|
2019-06-21 19:15:58 +01:00
|
|
|
|
|
|
|
for _, r := range []struct {
|
|
|
|
rs *pb.RedundancyScheme
|
|
|
|
fail bool
|
|
|
|
}{
|
|
|
|
{ // error - ErasureShareSize <= 0
|
|
|
|
rs: &pb.RedundancyScheme{
|
|
|
|
MinReq: 1,
|
|
|
|
RepairThreshold: 2,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
Total: 4,
|
|
|
|
ErasureShareSize: -1,
|
|
|
|
},
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{ // error - any of the values are negative
|
|
|
|
rs: &pb.RedundancyScheme{
|
|
|
|
MinReq: 1,
|
|
|
|
RepairThreshold: -2,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
Total: -4,
|
|
|
|
ErasureShareSize: 10,
|
|
|
|
},
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{ // error - MinReq >= RepairThreshold
|
|
|
|
rs: &pb.RedundancyScheme{
|
|
|
|
MinReq: 10,
|
|
|
|
RepairThreshold: 2,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
Total: 4,
|
|
|
|
ErasureShareSize: 10,
|
|
|
|
},
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{ // error - MinReq >= RepairThreshold
|
|
|
|
rs: &pb.RedundancyScheme{
|
|
|
|
MinReq: 2,
|
|
|
|
RepairThreshold: 2,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
Total: 4,
|
|
|
|
ErasureShareSize: 10,
|
|
|
|
},
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{ // error - RepairThreshold >= SuccessThreshol
|
|
|
|
rs: &pb.RedundancyScheme{
|
|
|
|
MinReq: 1,
|
|
|
|
RepairThreshold: 3,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
Total: 4,
|
|
|
|
ErasureShareSize: 10,
|
|
|
|
},
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{ // error - SuccessThreshold >= Total
|
|
|
|
rs: &pb.RedundancyScheme{
|
|
|
|
MinReq: 1,
|
|
|
|
RepairThreshold: 2,
|
|
|
|
SuccessThreshold: 4,
|
|
|
|
Total: 4,
|
|
|
|
ErasureShareSize: 10,
|
|
|
|
},
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{ // ok - valid RS parameters
|
|
|
|
rs: &pb.RedundancyScheme{
|
|
|
|
MinReq: 1,
|
|
|
|
RepairThreshold: 2,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
Total: 4,
|
|
|
|
ErasureShareSize: 256,
|
|
|
|
},
|
|
|
|
fail: false,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
_, _, err := metainfo.CreateSegment(ctx, "bucket", "path", -1, r.rs, 1000, time.Now())
|
|
|
|
if r.fail {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-05 17:41:02 +01:00
|
|
|
func TestDoubleCommitSegment(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
apiKey := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
|
|
|
|
|
|
|
|
metainfo, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey)
|
|
|
|
require.NoError(t, err)
|
2019-06-25 16:36:23 +01:00
|
|
|
defer ctx.Check(metainfo.Close)
|
2019-06-05 17:41:02 +01:00
|
|
|
|
|
|
|
pointer, limits := runCreateSegment(ctx, t, metainfo)
|
|
|
|
|
2019-06-24 10:52:25 +01:00
|
|
|
_, err = metainfo.CommitSegment(ctx, "my-bucket-name", "file/path", -1, pointer, limits)
|
2019-06-05 17:41:02 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-24 10:52:25 +01:00
|
|
|
_, err = metainfo.CommitSegment(ctx, "my-bucket-name", "file/path", -1, pointer, limits)
|
2019-06-05 17:41:02 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "missing create request or request expired")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommitSegmentPointer(t *testing.T) {
|
|
|
|
// all tests needs to generate error
|
|
|
|
tests := []struct {
|
|
|
|
// defines how modify pointer before CommitSegment
|
|
|
|
Modify func(pointer *pb.Pointer)
|
|
|
|
ErrorMessage string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.ExpirationDate.Seconds += 100
|
|
|
|
},
|
|
|
|
ErrorMessage: "pointer expiration date does not match requested one",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.Redundancy.MinReq += 100
|
|
|
|
},
|
|
|
|
ErrorMessage: "pointer redundancy scheme date does not match requested one",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.Redundancy.RepairThreshold += 100
|
|
|
|
},
|
|
|
|
ErrorMessage: "pointer redundancy scheme date does not match requested one",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.Redundancy.SuccessThreshold += 100
|
|
|
|
},
|
|
|
|
ErrorMessage: "pointer redundancy scheme date does not match requested one",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.Redundancy.Total += 100
|
|
|
|
},
|
|
|
|
// this error is triggered earlier then Create/Commit RS comparison
|
|
|
|
ErrorMessage: "invalid no order limit for piece",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.Redundancy.ErasureShareSize += 100
|
|
|
|
},
|
|
|
|
ErrorMessage: "pointer redundancy scheme date does not match requested one",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.Redundancy.Type = 100
|
|
|
|
},
|
|
|
|
ErrorMessage: "pointer redundancy scheme date does not match requested one",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Type = pb.Pointer_INLINE
|
|
|
|
},
|
|
|
|
ErrorMessage: "pointer type is INLINE but remote segment is set",
|
|
|
|
},
|
2019-07-03 17:14:37 +01:00
|
|
|
{
|
|
|
|
// no piece hash removes piece from pointer, not enough pieces for successful upload
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.RemotePieces[0].Hash = nil
|
|
|
|
},
|
|
|
|
ErrorMessage: "Number of valid pieces (1) is less than or equal to the repair threshold (1)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// invalid timestamp removes piece from pointer, not enough pieces for successful upload
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.RemotePieces[0].Hash.Timestamp = time.Now().Add(-24 * time.Hour)
|
|
|
|
},
|
|
|
|
ErrorMessage: "Number of valid pieces (1) is less than or equal to the repair threshold (1)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// invalid hash PieceID removes piece from pointer, not enough pieces for successful upload
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.RemotePieces[0].Hash.PieceId = storj.PieceID{1}
|
|
|
|
},
|
|
|
|
ErrorMessage: "Number of valid pieces (1) is less than or equal to the repair threshold (1)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.Remote.RemotePieces[0].Hash.PieceSize = 1
|
|
|
|
},
|
|
|
|
ErrorMessage: "all pieces needs to have the same size",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Modify: func(pointer *pb.Pointer) {
|
|
|
|
pointer.SegmentSize = 100
|
|
|
|
},
|
|
|
|
ErrorMessage: "expected piece size is different from provided",
|
|
|
|
},
|
2019-06-05 17:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
apiKey := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
|
|
|
|
|
|
|
|
metainfo, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey)
|
|
|
|
require.NoError(t, err)
|
2019-06-25 16:36:23 +01:00
|
|
|
defer ctx.Check(metainfo.Close)
|
2019-06-05 17:41:02 +01:00
|
|
|
|
2019-07-03 17:14:37 +01:00
|
|
|
for i, test := range tests {
|
2019-06-05 17:41:02 +01:00
|
|
|
pointer, limits := runCreateSegment(ctx, t, metainfo)
|
|
|
|
test.Modify(pointer)
|
|
|
|
|
2019-06-24 10:52:25 +01:00
|
|
|
_, err = metainfo.CommitSegment(ctx, "my-bucket-name", "file/path", -1, pointer, limits)
|
2019-07-03 17:14:37 +01:00
|
|
|
require.Error(t, err, "Case #%v", i)
|
|
|
|
require.Contains(t, err.Error(), test.ErrorMessage, "Case #%v", i)
|
2019-06-05 17:41:02 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-19 13:02:37 +01:00
|
|
|
func TestSetAttribution(t *testing.T) {
|
2019-06-13 02:35:37 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
apiKey := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
|
|
|
|
uplink := planet.Uplinks[0]
|
2019-06-19 13:02:37 +01:00
|
|
|
|
2019-06-13 02:35:37 +01:00
|
|
|
config := uplink.GetConfig(planet.Satellites[0])
|
2019-07-02 11:08:02 +01:00
|
|
|
metainfo, _, cleanup, err := testplanet.DialMetainfo(ctx, uplink.Log.Named("metainfo"), config, uplink.Identity)
|
2019-06-13 02:35:37 +01:00
|
|
|
require.NoError(t, err)
|
2019-07-01 15:35:10 +01:00
|
|
|
defer ctx.Check(cleanup)
|
2019-06-19 13:02:37 +01:00
|
|
|
|
2019-07-03 19:07:44 +01:00
|
|
|
_, err = metainfo.CreateBucket(ctx, "alpha", &storj.Bucket{PathCipher: config.GetEncryptionParameters().CipherSuite})
|
2019-06-13 02:35:37 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
metainfoClient, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey)
|
|
|
|
require.NoError(t, err)
|
2019-06-25 16:36:23 +01:00
|
|
|
defer ctx.Check(metainfoClient.Close)
|
2019-06-13 02:35:37 +01:00
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
partnerID := testrand.UUID()
|
2019-06-13 02:35:37 +01:00
|
|
|
{
|
|
|
|
// bucket with no items
|
2019-06-26 11:38:51 +01:00
|
|
|
err = metainfoClient.SetAttribution(ctx, "alpha", partnerID)
|
2019-06-13 02:35:37 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// no bucket exists
|
2019-06-26 11:38:51 +01:00
|
|
|
err = metainfoClient.SetAttribution(ctx, "beta", partnerID)
|
2019-06-13 02:35:37 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
{
|
2019-06-26 17:22:01 +01:00
|
|
|
// already attributed bucket, adding files
|
2019-06-19 13:02:37 +01:00
|
|
|
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "alpha", "path", []byte{1, 2, 3})
|
2019-06-13 02:35:37 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// bucket with items
|
2019-06-26 11:38:51 +01:00
|
|
|
err = metainfoClient.SetAttribution(ctx, "alpha", partnerID)
|
2019-06-26 17:22:01 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
//non attributed bucket, and adding files
|
2019-07-03 19:56:17 +01:00
|
|
|
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "alpha-new", "path", []byte{1, 2, 3})
|
2019-06-26 17:22:01 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// bucket with items
|
2019-07-03 19:56:17 +01:00
|
|
|
err = metainfoClient.SetAttribution(ctx, "alpha-new", partnerID)
|
2019-06-13 02:35:37 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
func TestGetProjectInfo(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 2,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
apiKey0 := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
|
|
|
|
apiKey1 := planet.Uplinks[1].APIKey[planet.Satellites[0].ID()]
|
|
|
|
|
|
|
|
metainfo0, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
metainfo1, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
info0, err := metainfo0.GetProjectInfo(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, info0.ProjectSalt)
|
|
|
|
|
|
|
|
info1, err := metainfo1.GetProjectInfo(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, info1.ProjectSalt)
|
|
|
|
|
|
|
|
// Different projects should have different salts
|
|
|
|
require.NotEqual(t, info0.ProjectSalt, info1.ProjectSalt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
func runCreateSegment(ctx context.Context, t *testing.T, metainfo *metainfo.Client) (*pb.Pointer, []*pb.OrderLimit) {
|
2019-06-05 17:41:02 +01:00
|
|
|
pointer := createTestPointer(t)
|
|
|
|
expirationDate, err := ptypes.Timestamp(pointer.ExpirationDate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-24 10:52:25 +01:00
|
|
|
addressedLimits, rootPieceID, err := metainfo.CreateSegment(ctx, "my-bucket-name", "file/path", -1, pointer.Remote.Redundancy, memory.MiB.Int64(), expirationDate)
|
2019-06-05 17:41:02 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pointer.Remote.RootPieceId = rootPieceID
|
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
limits := make([]*pb.OrderLimit, len(addressedLimits))
|
2019-06-05 17:41:02 +01:00
|
|
|
for i, addressedLimit := range addressedLimits {
|
|
|
|
limits[i] = addressedLimit.Limit
|
2019-07-03 17:14:37 +01:00
|
|
|
|
|
|
|
if len(pointer.Remote.RemotePieces) > i {
|
|
|
|
pointer.Remote.RemotePieces[i].NodeId = addressedLimits[i].Limit.StorageNodeId
|
|
|
|
pointer.Remote.RemotePieces[i].Hash.PieceId = addressedLimits[i].Limit.PieceId
|
|
|
|
}
|
2019-06-05 17:41:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return pointer, limits
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestPointer(t *testing.T) *pb.Pointer {
|
|
|
|
rs := &pb.RedundancyScheme{
|
|
|
|
MinReq: 1,
|
|
|
|
RepairThreshold: 1,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
Total: 4,
|
|
|
|
ErasureShareSize: 1024,
|
|
|
|
Type: pb.RedundancyScheme_RS,
|
|
|
|
}
|
|
|
|
|
2019-07-03 17:14:37 +01:00
|
|
|
redundancy, err := eestream.NewRedundancyStrategyFromProto(rs)
|
|
|
|
require.NoError(t, err)
|
|
|
|
segmentSize := 4 * memory.KiB.Int64()
|
|
|
|
pieceSize := eestream.CalcPieceSize(segmentSize, redundancy)
|
|
|
|
timestamp := time.Now()
|
2019-06-05 17:41:02 +01:00
|
|
|
pointer := &pb.Pointer{
|
2019-07-03 17:14:37 +01:00
|
|
|
Type: pb.Pointer_REMOTE,
|
|
|
|
SegmentSize: segmentSize,
|
2019-06-05 17:41:02 +01:00
|
|
|
Remote: &pb.RemoteSegment{
|
|
|
|
Redundancy: rs,
|
|
|
|
RemotePieces: []*pb.RemotePiece{
|
2019-06-27 19:52:50 +01:00
|
|
|
{
|
2019-06-05 17:41:02 +01:00
|
|
|
PieceNum: 0,
|
2019-07-03 17:14:37 +01:00
|
|
|
Hash: &pb.PieceHash{
|
|
|
|
PieceSize: pieceSize,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
},
|
2019-06-05 17:41:02 +01:00
|
|
|
},
|
2019-06-27 19:52:50 +01:00
|
|
|
{
|
2019-06-05 17:41:02 +01:00
|
|
|
PieceNum: 1,
|
2019-07-03 17:14:37 +01:00
|
|
|
Hash: &pb.PieceHash{
|
|
|
|
PieceSize: pieceSize,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
},
|
2019-06-05 17:41:02 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ExpirationDate: ptypes.TimestampNow(),
|
|
|
|
}
|
|
|
|
return pointer
|
|
|
|
}
|
2019-06-24 10:52:25 +01:00
|
|
|
|
|
|
|
func TestBucketNameValidation(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
apiKey := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
|
|
|
|
|
|
|
|
metainfo, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey)
|
|
|
|
require.NoError(t, err)
|
2019-06-25 16:36:23 +01:00
|
|
|
defer ctx.Check(metainfo.Close)
|
2019-06-24 10:52:25 +01:00
|
|
|
|
|
|
|
rs := &pb.RedundancyScheme{
|
|
|
|
MinReq: 1,
|
|
|
|
RepairThreshold: 1,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
Total: 4,
|
|
|
|
ErasureShareSize: 1024,
|
|
|
|
Type: pb.RedundancyScheme_RS,
|
|
|
|
}
|
|
|
|
|
|
|
|
validNames := []string{
|
|
|
|
"tes", "testbucket",
|
|
|
|
"test-bucket", "testbucket9",
|
|
|
|
"9testbucket", "a.b",
|
|
|
|
"test.bucket", "test-one.bucket-one",
|
|
|
|
"test.bucket.one",
|
|
|
|
"testbucket-63-0123456789012345678901234567890123456789012345abc",
|
|
|
|
}
|
|
|
|
for _, name := range validNames {
|
|
|
|
_, _, err = metainfo.CreateSegment(ctx, name, "", -1, rs, 1, time.Now())
|
|
|
|
require.NoError(t, err, "bucket name: %v", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidNames := []string{
|
|
|
|
"", "t", "te", "-testbucket",
|
|
|
|
"testbucket-", "-testbucket-",
|
|
|
|
"a.b.", "test.bucket-.one",
|
|
|
|
"test.-bucket.one", "1.2.3.4",
|
|
|
|
"192.168.1.234", "testBUCKET",
|
|
|
|
"test/bucket",
|
|
|
|
"testbucket-64-0123456789012345678901234567890123456789012345abcd",
|
|
|
|
}
|
|
|
|
for _, name := range invalidNames {
|
|
|
|
_, _, err = metainfo.CreateSegment(ctx, name, "", -1, rs, 1, time.Now())
|
|
|
|
require.Error(t, err, "bucket name: %v", name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|