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"
|
|
|
|
|
|
|
|
"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"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func TestBandwidthAgreements(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()
|
|
|
|
|
|
|
|
satellitePubKey, satellitePrivKey, uplinkPrivKey := generateKeys(ctx, t)
|
|
|
|
server := bwagreement.NewServer(db.BandwidthAgreement(), zap.NewNop(), satellitePubKey)
|
|
|
|
|
2018-12-10 14:50:12 +00:00
|
|
|
pba, err := GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, satellitePrivKey)
|
|
|
|
assert.NoError(t, err)
|
2018-12-07 12:11:35 +00:00
|
|
|
|
2018-12-10 14:50:12 +00:00
|
|
|
rba, err := GenerateRenterBandwidthAllocation(pba, uplinkPrivKey)
|
|
|
|
assert.NoError(t, err)
|
2018-12-07 12:11:35 +00:00
|
|
|
|
2018-12-10 14:50:12 +00:00
|
|
|
/* emulate sending the bwagreement stream from piecestore node */
|
|
|
|
replay, err := server.BandwidthAgreements(ctx, rba)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, pb.AgreementsSummary_OK, replay.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
|
|
|
}
|