2019-04-15 11:12:22 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package piecestore_test
|
|
|
|
|
|
|
|
import (
|
2020-04-18 06:41:20 +01:00
|
|
|
"bytes"
|
2019-04-15 11:12:22 +01:00
|
|
|
"context"
|
2019-10-11 22:57:59 +01:00
|
|
|
"io/ioutil"
|
2019-04-15 11:12:22 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-10-15 18:22:15 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-04-15 11:12:22 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/signing"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-04-15 11:12:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const oneWeek = 7 * 24 * time.Hour
|
|
|
|
|
|
|
|
func TestOrderLimitPutValidation(t *testing.T) {
|
2019-08-08 02:47:30 +01:00
|
|
|
for _, tt := range []struct {
|
|
|
|
testName string
|
2019-04-15 11:12:22 +01:00
|
|
|
useUnknownSatellite bool
|
|
|
|
pieceID storj.PieceID
|
|
|
|
action pb.PieceAction
|
|
|
|
serialNumber storj.SerialNumber
|
|
|
|
pieceExpiration time.Duration
|
|
|
|
orderExpiration time.Duration
|
|
|
|
limit int64
|
|
|
|
availableSpace int64
|
|
|
|
err string
|
|
|
|
}{
|
2019-08-08 02:47:30 +01:00
|
|
|
{
|
|
|
|
testName: "unapproved satellite id",
|
2019-04-15 11:12:22 +01:00
|
|
|
useUnknownSatellite: true,
|
|
|
|
pieceID: storj.PieceID{1},
|
|
|
|
action: pb.PieceAction_PUT,
|
|
|
|
serialNumber: storj.SerialNumber{1},
|
|
|
|
pieceExpiration: oneWeek,
|
|
|
|
orderExpiration: oneWeek,
|
|
|
|
limit: memory.KiB.Int64(),
|
|
|
|
err: " is untrusted",
|
|
|
|
},
|
2019-08-08 02:47:30 +01:00
|
|
|
{
|
|
|
|
testName: "approved satellite id",
|
2019-04-15 11:12:22 +01:00
|
|
|
pieceID: storj.PieceID{2},
|
|
|
|
action: pb.PieceAction_PUT,
|
|
|
|
serialNumber: storj.SerialNumber{2},
|
|
|
|
pieceExpiration: oneWeek,
|
|
|
|
orderExpiration: oneWeek,
|
|
|
|
limit: 10 * memory.KiB.Int64(),
|
|
|
|
},
|
2019-08-08 02:47:30 +01:00
|
|
|
{
|
|
|
|
testName: "wrong action type",
|
2019-04-15 11:12:22 +01:00
|
|
|
pieceID: storj.PieceID{3},
|
|
|
|
action: pb.PieceAction_GET,
|
|
|
|
serialNumber: storj.SerialNumber{3},
|
|
|
|
pieceExpiration: oneWeek,
|
|
|
|
orderExpiration: oneWeek,
|
|
|
|
limit: memory.KiB.Int64(),
|
|
|
|
err: "expected put or put repair action got GET",
|
|
|
|
},
|
2019-08-08 02:47:30 +01:00
|
|
|
{
|
|
|
|
testName: "piece expired",
|
2019-04-15 11:12:22 +01:00
|
|
|
pieceID: storj.PieceID{4},
|
|
|
|
action: pb.PieceAction_PUT,
|
|
|
|
serialNumber: storj.SerialNumber{4},
|
|
|
|
pieceExpiration: -4 * 24 * time.Hour,
|
|
|
|
orderExpiration: oneWeek,
|
|
|
|
limit: memory.KiB.Int64(),
|
|
|
|
err: "piece expired:",
|
|
|
|
},
|
2019-08-08 02:47:30 +01:00
|
|
|
{
|
|
|
|
testName: "limit is negative",
|
2019-04-15 11:12:22 +01:00
|
|
|
pieceID: storj.PieceID{5},
|
|
|
|
action: pb.PieceAction_PUT,
|
|
|
|
serialNumber: storj.SerialNumber{5},
|
|
|
|
pieceExpiration: oneWeek,
|
|
|
|
orderExpiration: oneWeek,
|
|
|
|
limit: -1,
|
|
|
|
err: "order limit is negative",
|
|
|
|
},
|
2019-08-08 02:47:30 +01:00
|
|
|
{
|
|
|
|
testName: "order limit expired",
|
2019-04-15 11:12:22 +01:00
|
|
|
pieceID: storj.PieceID{6},
|
|
|
|
action: pb.PieceAction_PUT,
|
|
|
|
serialNumber: storj.SerialNumber{6},
|
|
|
|
pieceExpiration: oneWeek,
|
|
|
|
orderExpiration: -4 * 24 * time.Hour,
|
|
|
|
limit: memory.KiB.Int64(),
|
|
|
|
err: "order expired:",
|
|
|
|
},
|
2019-08-08 02:47:30 +01:00
|
|
|
{
|
|
|
|
testName: "allocated space limit",
|
2019-04-15 11:12:22 +01:00
|
|
|
pieceID: storj.PieceID{8},
|
|
|
|
action: pb.PieceAction_PUT,
|
|
|
|
serialNumber: storj.SerialNumber{8},
|
|
|
|
pieceExpiration: oneWeek,
|
|
|
|
orderExpiration: oneWeek,
|
|
|
|
limit: 10 * memory.KiB.Int64(),
|
|
|
|
availableSpace: 5 * memory.KiB.Int64(),
|
2020-07-21 19:39:21 +01:00
|
|
|
err: "not enough available disk space",
|
2019-04-15 11:12:22 +01:00
|
|
|
},
|
|
|
|
} {
|
2019-08-08 02:47:30 +01:00
|
|
|
tt := tt
|
|
|
|
t.Run(tt.testName, func(t *testing.T) {
|
2019-12-06 18:03:22 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-04-15 11:12:22 +01:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
// set desirable space
|
|
|
|
setSpace(ctx, t, planet, tt.availableSpace)
|
|
|
|
|
|
|
|
client, err := planet.Uplinks[0].DialPiecestore(ctx, planet.StorageNodes[0])
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(client.Close)
|
|
|
|
|
|
|
|
signer := signing.SignerFromFullIdentity(planet.Satellites[0].Identity)
|
|
|
|
satellite := planet.Satellites[0].Identity
|
|
|
|
if tt.useUnknownSatellite {
|
|
|
|
unapprovedSatellite, err := planet.NewIdentity()
|
|
|
|
require.NoError(t, err)
|
|
|
|
signer = signing.SignerFromFullIdentity(unapprovedSatellite)
|
|
|
|
satellite = unapprovedSatellite
|
|
|
|
}
|
|
|
|
|
|
|
|
orderLimit, piecePrivateKey := GenerateOrderLimit(
|
|
|
|
t,
|
|
|
|
satellite.ID,
|
|
|
|
planet.StorageNodes[0].ID(),
|
|
|
|
tt.pieceID,
|
|
|
|
tt.action,
|
|
|
|
tt.serialNumber,
|
|
|
|
tt.pieceExpiration,
|
|
|
|
tt.orderExpiration,
|
|
|
|
tt.limit,
|
|
|
|
)
|
|
|
|
|
|
|
|
orderLimit, err = signing.SignOrderLimit(ctx, signer, orderLimit)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-18 06:41:20 +01:00
|
|
|
buffer := make([]byte, 10*memory.KiB)
|
|
|
|
testrand.Read(buffer)
|
2019-12-06 18:03:22 +00:00
|
|
|
|
2020-04-18 06:41:20 +01:00
|
|
|
_, err = client.UploadReader(ctx, orderLimit, piecePrivateKey, bytes.NewReader(buffer))
|
2019-12-06 18:03:22 +00:00
|
|
|
if tt.err != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tt.err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOrderLimitGetValidation(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
|
|
|
|
defaultPieceSize := 10 * memory.KiB
|
|
|
|
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
err := storageNode.DB.Bandwidth().Add(ctx, planet.Satellites[0].ID(), pb.PieceAction_GET, memory.TB.Int64()-(15*memory.KiB.Int64()), time.Now())
|
2019-08-08 02:47:30 +01:00
|
|
|
require.NoError(t, err)
|
2019-12-06 18:03:22 +00:00
|
|
|
}
|
2019-04-15 11:12:22 +01:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
{ // upload test piece
|
|
|
|
client, err := planet.Uplinks[0].DialPiecestore(ctx, planet.StorageNodes[0])
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(client.Close)
|
2019-04-15 11:12:22 +01:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
signer := signing.SignerFromFullIdentity(planet.Satellites[0].Identity)
|
|
|
|
satellite := planet.Satellites[0].Identity
|
2019-04-15 11:12:22 +01:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
orderLimit, piecePrivateKey := GenerateOrderLimit(
|
|
|
|
t,
|
|
|
|
satellite.ID,
|
|
|
|
planet.StorageNodes[0].ID(),
|
|
|
|
storj.PieceID{1},
|
|
|
|
pb.PieceAction_PUT,
|
|
|
|
storj.SerialNumber{0},
|
|
|
|
oneWeek,
|
|
|
|
oneWeek,
|
|
|
|
defaultPieceSize.Int64(),
|
|
|
|
)
|
|
|
|
|
|
|
|
orderLimit, err = signing.SignOrderLimit(ctx, signer, orderLimit)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-18 06:41:20 +01:00
|
|
|
_, err = client.UploadReader(ctx, orderLimit, piecePrivateKey, bytes.NewReader(testrand.Bytes(defaultPieceSize)))
|
2019-12-06 18:03:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for all requests to finish to ensure that the upload usage has been
|
|
|
|
// accounted for.
|
2020-03-05 10:09:10 +00:00
|
|
|
require.NoError(t, planet.WaitForStorageNodeEndpoints(ctx))
|
2019-12-06 18:03:22 +00:00
|
|
|
|
|
|
|
for _, tt := range []struct {
|
|
|
|
satellite *identity.FullIdentity
|
|
|
|
pieceID storj.PieceID
|
|
|
|
action pb.PieceAction
|
|
|
|
serialNumber storj.SerialNumber
|
|
|
|
pieceExpiration time.Duration
|
|
|
|
orderExpiration time.Duration
|
|
|
|
limit int64
|
|
|
|
err string
|
|
|
|
}{
|
2020-02-12 21:19:42 +00:00
|
|
|
{ // incorrect action - PUT rather than GET
|
2019-12-06 18:03:22 +00:00
|
|
|
pieceID: storj.PieceID{1},
|
2020-02-12 21:19:42 +00:00
|
|
|
action: pb.PieceAction_PUT,
|
2019-12-06 18:03:22 +00:00
|
|
|
serialNumber: storj.SerialNumber{1},
|
|
|
|
pieceExpiration: oneWeek,
|
|
|
|
orderExpiration: oneWeek,
|
|
|
|
limit: 10 * memory.KiB.Int64(),
|
2020-02-12 21:19:42 +00:00
|
|
|
err: "expected get or get repair or audit action got PUT",
|
2019-12-06 18:03:22 +00:00
|
|
|
},
|
|
|
|
} {
|
2020-11-10 11:56:30 +00:00
|
|
|
func() {
|
|
|
|
client, err := planet.Uplinks[0].DialPiecestore(ctx, planet.StorageNodes[0])
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(client.Close)
|
2019-04-15 11:12:22 +01:00
|
|
|
|
2020-11-10 11:56:30 +00:00
|
|
|
signer := signing.SignerFromFullIdentity(planet.Satellites[0].Identity)
|
|
|
|
satellite := planet.Satellites[0].Identity
|
|
|
|
if tt.satellite != nil {
|
|
|
|
signer = signing.SignerFromFullIdentity(tt.satellite)
|
|
|
|
satellite = tt.satellite
|
|
|
|
}
|
2019-08-08 02:47:30 +01:00
|
|
|
|
2020-11-10 11:56:30 +00:00
|
|
|
orderLimit, piecePrivateKey := GenerateOrderLimit(
|
|
|
|
t,
|
|
|
|
satellite.ID,
|
|
|
|
planet.StorageNodes[0].ID(),
|
|
|
|
tt.pieceID,
|
|
|
|
tt.action,
|
|
|
|
tt.serialNumber,
|
|
|
|
tt.pieceExpiration,
|
|
|
|
tt.orderExpiration,
|
|
|
|
tt.limit,
|
|
|
|
)
|
2019-04-15 11:12:22 +01:00
|
|
|
|
2020-11-10 11:56:30 +00:00
|
|
|
orderLimit, err = signing.SignOrderLimit(ctx, signer, orderLimit)
|
|
|
|
require.NoError(t, err)
|
2019-04-15 11:12:22 +01:00
|
|
|
|
2020-11-10 11:56:30 +00:00
|
|
|
downloader, err := client.Download(ctx, orderLimit, piecePrivateKey, 0, tt.limit)
|
2019-08-08 02:47:30 +01:00
|
|
|
require.NoError(t, err)
|
2020-11-10 11:56:30 +00:00
|
|
|
|
|
|
|
buffer, readErr := ioutil.ReadAll(downloader)
|
|
|
|
closeErr := downloader.Close()
|
|
|
|
err = errs.Combine(readErr, closeErr)
|
|
|
|
if tt.err != "" {
|
|
|
|
assert.Equal(t, 0, len(buffer))
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tt.err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}()
|
2019-04-15 11:12:22 +01:00
|
|
|
}
|
2019-12-06 18:03:22 +00:00
|
|
|
})
|
2019-04-15 11:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func setSpace(ctx context.Context, t *testing.T, planet *testplanet.Planet, space int64) {
|
|
|
|
if space == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, storageNode := range planet.StorageNodes {
|
|
|
|
availableSpace, err := storageNode.Storage2.Monitor.AvailableSpace(ctx)
|
|
|
|
require.NoError(t, err)
|
2019-08-12 22:43:05 +01:00
|
|
|
// add these bytes to the space used cache so that we can test what happens
|
|
|
|
// when we exceeded available space on the storagenode
|
2020-01-07 23:34:51 +00:00
|
|
|
err = storageNode.DB.PieceSpaceUsedDB().UpdatePieceTotals(ctx, availableSpace-space, availableSpace-space)
|
2019-08-12 22:43:05 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = storageNode.Storage2.CacheService.Init(ctx)
|
|
|
|
require.NoError(t, err)
|
2019-04-15 11:12:22 +01:00
|
|
|
}
|
|
|
|
}
|