2018-07-19 23:57:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-08-24 05:01:03 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-07-19 15:48:08 +01:00
|
|
|
)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
var ctx = context.Background()
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
func TestLookup(t *testing.T) {
|
|
|
|
cases := []struct {
|
2018-09-18 05:39:06 +01:00
|
|
|
self pb.Node
|
|
|
|
to pb.Node
|
|
|
|
find pb.Node
|
2018-08-27 18:28:16 +01:00
|
|
|
expectedErr error
|
2018-07-19 15:48:08 +01:00
|
|
|
}{
|
|
|
|
{
|
2018-09-18 05:39:06 +01:00
|
|
|
self: pb.Node{Id: NewNodeID(t), Address: &pb.NodeAddress{Address: ":7070"}},
|
|
|
|
to: pb.Node{}, // filled after server has been started
|
|
|
|
find: pb.Node{Id: NewNodeID(t), Address: &pb.NodeAddress{Address: ":9090"}},
|
2018-07-19 15:48:08 +01:00
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2018-09-07 10:42:04 +01:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
v.to = pb.Node{Id: NewNodeID(t), Address: &pb.NodeAddress{Address: lis.Addr().String()}}
|
2018-09-07 10:42:04 +01:00
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
srv, mock, err := newTestServer(ctx)
|
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:13:25 +01:00
|
|
|
go func() { assert.NoError(t, srv.Serve(lis)) }()
|
2018-07-19 15:48:08 +01:00
|
|
|
defer srv.Stop()
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
nc, err := NewNodeClient(identity, v.self)
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
_, err = nc.Lookup(ctx, v.to, v.find)
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.Equal(t, v.expectedErr, err)
|
|
|
|
assert.Equal(t, 1, mock.queryCalled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
func newTestServer(ctx context.Context) (*grpc.Server, *mockNodeServer, error) {
|
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
identOpt, err := identity.ServerOption()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer(identOpt)
|
2018-07-19 15:48:08 +01:00
|
|
|
mn := &mockNodeServer{queryCalled: 0}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
pb.RegisterNodesServer(grpcServer, mn)
|
2018-07-19 15:48:08 +01:00
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
return grpcServer, mn, nil
|
2018-07-19 15:48:08 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockNodeServer struct {
|
|
|
|
queryCalled int
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
func (mn *mockNodeServer) Query(ctx context.Context, req *pb.QueryRequest) (*pb.QueryResponse, error) {
|
2018-07-19 15:48:08 +01:00
|
|
|
mn.queryCalled++
|
2018-09-18 05:39:06 +01:00
|
|
|
return &pb.QueryResponse{}, nil
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
2018-09-11 08:27:12 +01:00
|
|
|
|
|
|
|
// NewNodeID returns the string representation of a dht node ID
|
|
|
|
func NewNodeID(t *testing.T) string {
|
|
|
|
id, err := kademlia.NewID()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return id.String()
|
|
|
|
}
|