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"
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
"github.com/golang/mock/gomock"
|
2018-07-19 15:48:08 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/dht/mocks"
|
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-10-08 16:09:37 +01:00
|
|
|
self: pb.Node{Id: "hello", Address: &pb.NodeAddress{Address: ":7070"}},
|
|
|
|
to: pb.Node{Id: "hello", Address: &pb.NodeAddress{Address: ":8080"}},
|
|
|
|
find: pb.Node{Id: "hello", 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-10-08 16:09:37 +01:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
mdht := mock_dht.NewMockDHT(ctrl)
|
|
|
|
mrt := mock_dht.NewMockRoutingTable(ctrl)
|
|
|
|
|
|
|
|
mdht.EXPECT().GetRoutingTable(gomock.Any()).Return(mrt, nil)
|
|
|
|
mrt.EXPECT().ConnectionSuccess(gomock.Any()).Return(nil)
|
2018-07-19 15:48:08 +01:00
|
|
|
|
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)
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
nc, err := NewNodeClient(identity, v.self, mdht)
|
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 {
|
2018-10-11 22:41:58 +01:00
|
|
|
fid, err := NewFullIdentity(ctx, 12, 4)
|
|
|
|
id := ID(fid.ID)
|
2018-09-11 08:27:12 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return id.String()
|
|
|
|
}
|