2018-11-05 15:23:54 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package bwagreement
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
2018-11-08 13:20:23 +00:00
|
|
|
"flag"
|
2018-11-05 15:23:54 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2018-11-08 13:20:23 +00:00
|
|
|
"os"
|
2018-11-05 15:23:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
"github.com/gtank/cryptopasta"
|
2018-11-05 15:23:54 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-12 21:59:30 +00:00
|
|
|
"storj.io/storj/pkg/peertls"
|
2018-11-05 15:23:54 +00:00
|
|
|
"storj.io/storj/pkg/provider"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBandwidthAgreements(t *testing.T) {
|
|
|
|
TS := NewTestServer(t)
|
|
|
|
defer TS.Stop()
|
|
|
|
|
|
|
|
signature := []byte("iamthedummysignatureoftypebyteslice")
|
|
|
|
data := []byte("iamthedummydataoftypebyteslice")
|
|
|
|
|
|
|
|
msg := &pb.RenterBandwidthAllocation{
|
|
|
|
Signature: signature,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
s, err := cryptopasta.Sign(msg.Data, TS.k.(*ecdsa.PrivateKey))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
msg.Signature = s
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
/* emulate sending the bwagreement stream from piecestore node */
|
|
|
|
stream, err := TS.c.BandwidthAgreements(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = stream.Send(msg)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, _ = stream.CloseAndRecv()
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestServer struct {
|
|
|
|
s *Server
|
|
|
|
grpcs *grpc.Server
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
c pb.BandwidthClient
|
|
|
|
k crypto.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTestServer(t *testing.T) *TestServer {
|
|
|
|
check := func(e error) {
|
|
|
|
if !assert.NoError(t, e) {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
caS, err := provider.NewTestCA(context.Background())
|
|
|
|
check(err)
|
|
|
|
fiS, err := caS.NewIdentity()
|
|
|
|
check(err)
|
|
|
|
so, err := fiS.ServerOption()
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
caC, err := provider.NewTestCA(context.Background())
|
|
|
|
check(err)
|
|
|
|
fiC, err := caC.NewIdentity()
|
|
|
|
check(err)
|
2018-11-09 22:08:33 +00:00
|
|
|
co, err := fiC.DialOption("")
|
2018-11-05 15:23:54 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
s := newTestServerStruct(t)
|
|
|
|
grpcs := grpc.NewServer(so)
|
|
|
|
|
|
|
|
k, ok := fiC.Key.(*ecdsa.PrivateKey)
|
|
|
|
assert.True(t, ok)
|
|
|
|
ts := &TestServer{s: s, grpcs: grpcs, k: k}
|
|
|
|
addr := ts.start()
|
|
|
|
ts.c, ts.conn = connect(addr, co)
|
|
|
|
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:20:23 +00:00
|
|
|
const (
|
|
|
|
// this connstring is expected to work under the storj-test docker-compose instance
|
2018-11-15 18:36:57 +00:00
|
|
|
defaultPostgresConn = "postgres://storj:storj-pass@test-postgres/teststorj?sslmode=disable"
|
2018-11-08 13:20:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// for travis build support
|
2018-11-15 18:36:57 +00:00
|
|
|
testPostgres = flag.String("postgres-test-db", os.Getenv("STORJ_POSTGRES_TEST"), "PostgreSQL test database connection string")
|
2018-11-08 13:20:23 +00:00
|
|
|
)
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
func newTestServerStruct(t *testing.T) *Server {
|
2018-11-08 13:20:23 +00:00
|
|
|
if *testPostgres == "" {
|
|
|
|
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", defaultPostgresConn)
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
k, err := peertls.NewKey()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
p, _ := k.(*ecdsa.PrivateKey)
|
|
|
|
server, err := NewServer("postgres", *testPostgres, zap.NewNop(), &p.PublicKey)
|
2018-11-09 22:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return server
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (TS *TestServer) start() (addr string) {
|
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to listen: %v", err)
|
|
|
|
}
|
|
|
|
pb.RegisterBandwidthServer(TS.grpcs, TS.s)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := TS.grpcs.Serve(lis); err != nil {
|
|
|
|
log.Fatalf("failed to serve: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return lis.Addr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func connect(addr string, o ...grpc.DialOption) (pb.BandwidthClient, *grpc.ClientConn) {
|
|
|
|
conn, err := grpc.Dial(addr, o...)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("did not connect: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := pb.NewBandwidthClient(conn)
|
|
|
|
|
|
|
|
return c, conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (TS *TestServer) Stop() {
|
|
|
|
if err := TS.conn.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
TS.grpcs.Stop()
|
|
|
|
}
|