2018-12-05 14:03:23 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// 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-07 15:19:05 +00:00
|
|
|
//"github.com/gogo/protobuf/proto"
|
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-07 15:19:05 +00:00
|
|
|
func TestSameSerialNumberBandwidthAgreements(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-07 15:19:05 +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. */
|
|
|
|
satellitePubKey, satellitePrivKey, uplinkPrivKey := generateKeys(ctx, t)
|
|
|
|
server := bwagreement.NewServer(db.BandwidthAgreement(), zap.NewNop(), satellitePubKey)
|
|
|
|
|
2019-01-10 18:30:55 +00:00
|
|
|
pbaFile1, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, false)
|
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)
|
|
|
|
|
|
|
|
reply, err := server.BandwidthAgreements(ctx, rbaNode1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_OK, reply.Status)
|
|
|
|
|
|
|
|
reply, err = server.BandwidthAgreements(ctx, rbaNode2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_OK, reply.Status)
|
|
|
|
|
|
|
|
/* Storage node can submit a second bwagreement with a different sequence value.
|
|
|
|
Uplink downloads another file. New PayerBandwidthAllocation with a new sequence. */
|
2019-01-10 18:30:55 +00:00
|
|
|
pbaFile2, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, false)
|
2019-01-07 15:19:05 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rbaNode1, err = GenerateRenterBandwidthAllocation(pbaFile2, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
reply, err = server.BandwidthAgreements(ctx, rbaNode1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_OK, reply.Status)
|
|
|
|
|
|
|
|
/* Storage nodes can't submit a second bwagreement with the same sequence. */
|
|
|
|
rbaNode1, err = GenerateRenterBandwidthAllocation(pbaFile1, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
reply, err = server.BandwidthAgreements(ctx, rbaNode1)
|
2019-01-10 18:30:55 +00:00
|
|
|
assert.EqualError(t, err, "bwagreement error: SerialNumber already exists in the PayerBandwidthAllocation")
|
2019-01-07 15:19:05 +00:00
|
|
|
assert.Equal(t, pb.AgreementsSummary_FAIL, reply.Status)
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
reply, err = server.BandwidthAgreements(ctx, rbaNode2)
|
2019-01-10 18:30:55 +00:00
|
|
|
assert.EqualError(t, err, "bwagreement error: SerialNumber already exists in the PayerBandwidthAllocation")
|
2019-01-07 15:19:05 +00:00
|
|
|
assert.Equal(t, pb.AgreementsSummary_FAIL, reply.Status)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidBandwidthAgreements(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2018-12-07 12:11:35 +00:00
|
|
|
satellitePubKey, satellitePrivKey, uplinkPrivKey := generateKeys(ctx, t)
|
|
|
|
server := bwagreement.NewServer(db.BandwidthAgreement(), zap.NewNop(), satellitePubKey)
|
|
|
|
|
2019-01-10 18:30:55 +00:00
|
|
|
pba, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, false)
|
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-10 18:30:55 +00:00
|
|
|
// Make sure the bwagreement we are using as blueprint is valid and avoid false positives that way.
|
2019-01-07 15:19:05 +00:00
|
|
|
reply, err := server.BandwidthAgreements(ctx, rba)
|
2018-12-10 14:50:12 +00:00
|
|
|
assert.NoError(t, err)
|
2019-01-07 15:19:05 +00:00
|
|
|
assert.Equal(t, pb.AgreementsSummary_OK, reply.Status)
|
2019-01-10 18:30:55 +00:00
|
|
|
|
|
|
|
// storage nodes can't submit an expired bwagreement
|
|
|
|
expPBA, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey, uplinkPrivKey, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
rba, err = GenerateRenterBandwidthAllocation(expPBA, teststorj.NodeIDFromString("Storage node 1"), uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
reply, err = server.BandwidthAgreements(ctx, rba)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_FAIL, reply.Status)
|
|
|
|
|
|
|
|
/* Todo: Add more tests for bwagreement manipulations
|
2019-01-07 15:19:05 +00:00
|
|
|
|
|
|
|
/* copy and unmarshal pba and rba to manipulate it without overwriting it */
|
|
|
|
|
|
|
|
/* manipulate PayerBandwidthAllocation -> invalid signature */
|
|
|
|
|
|
|
|
/* self signed. Storage node sends a self signed bwagreement to get a higher payout */
|
|
|
|
|
|
|
|
/* malicious storage node would like to force a crash */
|
|
|
|
|
|
|
|
/* corrupted signature. Storage node sends an corrupted signuature to force a satellite crash */
|
|
|
|
|
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
|
|
|
}
|