2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-05 14:03:23 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-12-07 12:11:35 +00:00
|
|
|
"crypto/ecdsa"
|
2018-12-05 14:03:23 +00:00
|
|
|
"testing"
|
2019-01-15 04:44:56 +00:00
|
|
|
"time"
|
2018-12-05 14:03:23 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/gtank/cryptopasta"
|
2018-12-05 14:03:23 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-12-07 12:11:35 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-01-02 17:39:17 +00:00
|
|
|
"storj.io/storj/internal/testidentity"
|
2019-01-07 15:19:05 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-12-07 12:11:35 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2018-12-05 14:03:23 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2018-12-10 14:50:12 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2018-12-05 14:03:23 +00:00
|
|
|
)
|
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
func TestBandwidthAgreement(t *testing.T) {
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
2018-12-07 12:11:35 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
testDatabase(ctx, t, db.BandwidthAgreement())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDatabase(ctx context.Context, t *testing.T, bwdb bwagreement.DB) {
|
|
|
|
satellitePubKey, satellitePrivKey, uplinkPrivKey := generateKeys(ctx, t)
|
|
|
|
satellite := bwagreement.NewServer(bwdb, zap.NewNop(), satellitePubKey)
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
{ // TestSameSerialNumberBandwidthAgreements
|
|
|
|
pbaFile1, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, time.Hour)
|
2019-01-07 15:19:05 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rbaNode1, err := GenerateRenterBandwidthAllocation(pbaFile1, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rbaNode2, err := GenerateRenterBandwidthAllocation(pbaFile1, teststorj.NodeIDFromString("Storage node 2"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
/* More than one storage node can submit bwagreements with the same serial number.
|
|
|
|
Uplink would like to download a file from 2 storage nodes.
|
|
|
|
Uplink requests a PayerBandwidthAllocation from the satellite. One serial number for all storage nodes.
|
|
|
|
Uplink signes 2 RenterBandwidthAllocation for both storage node. */
|
|
|
|
{
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, rbaNode1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_OK, reply.Status)
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
reply, err = satellite.BandwidthAgreements(ctx, rbaNode2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_OK, reply.Status)
|
|
|
|
}
|
2019-01-07 15:19:05 +00:00
|
|
|
|
|
|
|
/* Storage node can submit a second bwagreement with a different sequence value.
|
|
|
|
Uplink downloads another file. New PayerBandwidthAllocation with a new sequence. */
|
2019-01-15 04:44:56 +00:00
|
|
|
{
|
|
|
|
pbaFile2, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, time.Hour)
|
|
|
|
assert.NoError(t, err)
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
rbaNode1, err := GenerateRenterBandwidthAllocation(pbaFile2, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, rbaNode1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_OK, reply.Status)
|
|
|
|
}
|
2019-01-07 15:19:05 +00:00
|
|
|
|
|
|
|
/* Storage nodes can't submit a second bwagreement with the same sequence. */
|
2019-01-15 04:44:56 +00:00
|
|
|
{
|
|
|
|
rbaNode1, err := GenerateRenterBandwidthAllocation(pbaFile1, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, rbaNode1)
|
|
|
|
assert.EqualError(t, err, "bwagreement error: SerialNumber already exists in the PayerBandwidthAllocation")
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
2019-01-07 15:19:05 +00:00
|
|
|
|
|
|
|
/* Storage nodes can't submit the same bwagreement twice.
|
|
|
|
This test is kind of duplicate cause it will most likely trigger the same sequence error.
|
|
|
|
For safety we will try it anyway to make sure nothing strange will happen */
|
2019-01-15 04:44:56 +00:00
|
|
|
{
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, rbaNode2)
|
|
|
|
assert.EqualError(t, err, "bwagreement error: SerialNumber already exists in the PayerBandwidthAllocation")
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // TestExpiredBandwidthAgreements
|
|
|
|
{ // storage nodes can submit a bwagreement that will expire in one second
|
|
|
|
pba, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, time.Second)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rba, err := GenerateRenterBandwidthAllocation(pba, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, rba)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_OK, reply.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // storage nodes can't submit a bwagreement that expires right now
|
|
|
|
pba, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, 0*time.Second)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rba, err := GenerateRenterBandwidthAllocation(pba, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, rba)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // storage nodes can't submit a bwagreement that expires yesterday
|
|
|
|
pba, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, -23*time.Hour-55*time.Second)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rba, err := GenerateRenterBandwidthAllocation(pba, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, rba)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // TestManipulatedBandwidthAgreements
|
|
|
|
pba, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, time.Hour)
|
2018-12-10 14:50:12 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-07 12:11:35 +00:00
|
|
|
|
2019-01-07 15:19:05 +00:00
|
|
|
rba, err := GenerateRenterBandwidthAllocation(pba, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
2018-12-10 14:50:12 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-07 12:11:35 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
// Unmarschal Renter and Payer bwagreements
|
|
|
|
rbaData := &pb.RenterBandwidthAllocation_Data{}
|
|
|
|
err = proto.Unmarshal(rba.GetData(), rbaData)
|
2018-12-10 14:50:12 +00:00
|
|
|
assert.NoError(t, err)
|
2019-01-10 18:30:55 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
pbaData := &pb.PayerBandwidthAllocation_Data{}
|
|
|
|
err = proto.Unmarshal(pba.GetData(), pbaData)
|
2019-01-10 18:30:55 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
// Storage node manipulates the bwagreement
|
|
|
|
rbaData.Total = 1337
|
|
|
|
|
|
|
|
// Marschal the manipulated bwagreement
|
|
|
|
maniprba, err := proto.Marshal(rbaData)
|
2019-01-10 18:30:55 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
// Generate a new keypair for self signing bwagreements
|
|
|
|
_, manipPrivKey, _ := generateKeys(ctx, t)
|
|
|
|
manipPubKey, err := getUplinkPubKey(manipPrivKey)
|
|
|
|
assert.NoError(t, err)
|
2019-01-10 18:30:55 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
/* Storage node can't manipulate the bwagreement size (or any other field)
|
|
|
|
Satellite will verify Renter's Signature */
|
|
|
|
{
|
|
|
|
// Using uplink signature
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, &pb.RenterBandwidthAllocation{
|
|
|
|
Signature: rba.GetSignature(),
|
|
|
|
Data: maniprba,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.EqualError(t, err, "bwagreement error: Failed to verify Renter's Signature")
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Storage node can't sign the manipulated bwagreement
|
|
|
|
Satellite will verify Renter's Signature */
|
|
|
|
{
|
|
|
|
manipSignature, err := cryptopasta.Sign(maniprba, manipPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Using self created signature
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, &pb.RenterBandwidthAllocation{
|
|
|
|
Signature: manipSignature,
|
|
|
|
Data: maniprba,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.EqualError(t, err, "bwagreement error: Failed to verify Renter's Signature")
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Storage node can't replace uplink PubKey
|
|
|
|
Satellite will verify Payer's Signature */
|
|
|
|
{
|
|
|
|
// Overwrite the uplink public key with our own keypair
|
|
|
|
pbaData.PubKey = manipPubKey
|
|
|
|
|
|
|
|
manippba, err := proto.Marshal(pbaData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rbaData.PayerAllocation = &pb.PayerBandwidthAllocation{
|
|
|
|
Signature: pba.GetSignature(),
|
|
|
|
Data: manippba,
|
|
|
|
}
|
|
|
|
|
|
|
|
maniprba, err := proto.Marshal(rbaData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
manipSignature, err := cryptopasta.Sign(maniprba, manipPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Using self created signature + public key
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, &pb.RenterBandwidthAllocation{
|
|
|
|
Signature: manipSignature,
|
|
|
|
Data: maniprba,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.EqualError(t, err, "bwagreement error: Failed to verify Payer's Signature")
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Storage node can't self sign the PayerBandwidthAllocation.
|
|
|
|
Satellite will verify the Payer's Signature with his own public key. */
|
|
|
|
{
|
|
|
|
// Overwrite the uplink public key with our own keypair
|
|
|
|
pbaData.PubKey = manipPubKey
|
|
|
|
|
|
|
|
manippba, err := proto.Marshal(pbaData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
manipSignature, err := cryptopasta.Sign(manippba, manipPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rbaData.PayerAllocation = &pb.PayerBandwidthAllocation{
|
|
|
|
Signature: manipSignature,
|
|
|
|
Data: manippba,
|
|
|
|
}
|
|
|
|
|
|
|
|
maniprba, err := proto.Marshal(rbaData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
manipSignature, err = cryptopasta.Sign(maniprba, manipPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Using self created Payer and Renter bwagreement signatures
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, &pb.RenterBandwidthAllocation{
|
|
|
|
Signature: manipSignature,
|
|
|
|
Data: maniprba,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.EqualError(t, err, "bwagreement error: Failed to verify Payer's Signature")
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{ //TestInvalidBandwidthAgreements
|
|
|
|
pba, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, time.Hour)
|
|
|
|
assert.NoError(t, err)
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
{ // Storage node sends an corrupted signuature to force a satellite crash
|
|
|
|
rba, err := GenerateRenterBandwidthAllocation(pba, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
rba.Signature = []byte("invalid")
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, rba)
|
|
|
|
assert.EqualError(t, err, "bwagreement error: Invalid Renter's Signature Length")
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
2019-01-11 03:31:47 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
{ // Storage node sends an corrupted uplink pubkey to force a crash
|
|
|
|
rba, err := GenerateRenterBandwidthAllocation(pba, teststorj.NodeIDFromString("Storage node 2"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
2019-01-11 03:31:47 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
rbaData := &pb.RenterBandwidthAllocation_Data{}
|
|
|
|
err = proto.Unmarshal(rba.GetData(), rbaData)
|
|
|
|
assert.NoError(t, err)
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
pbaData := &pb.PayerBandwidthAllocation_Data{}
|
|
|
|
err = proto.Unmarshal(pba.GetData(), pbaData)
|
|
|
|
assert.NoError(t, err)
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
pbaData.PubKey = nil
|
2019-01-07 15:19:05 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
invalidpba, err := proto.Marshal(pbaData)
|
|
|
|
assert.NoError(t, err)
|
2019-01-11 03:31:47 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
rbaData.PayerAllocation = &pb.PayerBandwidthAllocation{
|
|
|
|
Signature: pba.GetSignature(),
|
|
|
|
Data: invalidpba,
|
|
|
|
}
|
2019-01-11 03:31:47 +00:00
|
|
|
|
2019-01-15 04:44:56 +00:00
|
|
|
invalidrba, err := proto.Marshal(rbaData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
reply, err := satellite.BandwidthAgreements(ctx, &pb.RenterBandwidthAllocation{
|
|
|
|
Signature: rba.GetSignature(),
|
|
|
|
Data: invalidrba,
|
|
|
|
})
|
|
|
|
assert.EqualError(t, err, "bwagreement error: Failed to extract Public Key from RenterBandwidthAllocation: asn1: syntax error: sequence truncated")
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_REJECTED, reply.Status)
|
|
|
|
}
|
|
|
|
}
|
2018-12-07 12:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateKeys(ctx context.Context, t *testing.T) (satellitePubKey *ecdsa.PublicKey, satellitePrivKey *ecdsa.PrivateKey, uplinkPrivKey *ecdsa.PrivateKey) {
|
2019-01-02 17:39:17 +00:00
|
|
|
fiS, err := testidentity.NewTestIdentity(ctx)
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-12-07 12:11:35 +00:00
|
|
|
satellitePubKey, ok := fiS.Leaf.PublicKey.(*ecdsa.PublicKey)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
satellitePrivKey, ok = fiS.Key.(*ecdsa.PrivateKey)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
fiU, err := testidentity.NewTestIdentity(ctx)
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-07 12:11:35 +00:00
|
|
|
|
|
|
|
uplinkPrivKey, ok = fiU.Key.(*ecdsa.PrivateKey)
|
|
|
|
assert.True(t, ok)
|
|
|
|
return
|
2018-12-05 14:03:23 +00:00
|
|
|
}
|